Available at RPubs
I will try to connect to the FAIRDOM SEEK with the collection of their APIs.
The API description with examples is available here.
My final goal is to enable upload/download from pISA-tree to FAIRDOMHub. This feature will be a part of the R package pISAR that will enable use of pISA structure and metafile information for reproducible statistical analyses.
RCurlMy first attempt will be the RCurl package avaialbe at CRAN. Additional description is a part of Omegahat project.
library(RCurl)
## Loading required package: bitops
First check if the base URL exists
(FAIRDOMExists = url.exists("https://www.fairdomhub.org/"))
## [1] TRUE
For this I will use the GET \people\{id} from documentation and my ID {808}.
person <- getURL("https://www.fairdomhub.org/people/808")
str(person)
## chr "<!doctype html>\n\n<html lang=\"en\">\n<head>\n <meta http-equiv=\"content-type\" content=\"text/html;charset=UTF-8\"/>\n <me"| __truncated__
It returns the HTML content and might not be the optimal way.
httrThe use is described in LockeData blog.
This solution uses packages httr and jsonlite and is based on libcurl (the same is used by RCurl). Package httr has useful vignettes that were a good guide for the example below.
library(httr)
library(jsonlite)
r <- GET("https://www.fairdomhub.org/people/808",
add_headers(Accept="application/json"))
names(r)
## [1] "url" "status_code" "headers" "all_headers" "cookies"
## [6] "content" "date" "times" "request" "handle"
r
## Response [https://www.fairdomhub.org/people/808]
## Date: 2018-06-11 18:17
## Status: 200
## Content-Type: application/vnd.api+json; charset=utf-8
## Size: 1.36 kB
## <BINARY BODY>
The response is structured and can be investigated with the helper functions. The response has three important parts: status, headers, and body.
Check status
http_status(r)
## $category
## [1] "Success"
##
## $reason
## [1] "OK"
##
## $message
## [1] "Success: (200) OK"
or just a status code
r$status_code
## [1] 200
Body can be accessed with the function content
cont <- content(r,"text")
cont
## [1] "{\"data\":{\"id\":\"808\",\"type\":\"people\",\"attributes\":{\"avatar\":\"/people/808/avatars/473\",\"title\":\"Andrej Blejec\",\"description\":null,\"first_name\":\"Andrej\",\"last_name\":\"Blejec\",\"web_page\":\"http://ablejec.nib.si\",\"orcid\":\"http://orcid.org/0000-0001-7484-6031\",\"mbox_sha1sum\":\"e37350ada8b1af0c7fe7a6cb61a6a5d35d20f8bd\",\"phone\":\"+38659232789\",\"skype_name\":\"ablejec\",\"expertise\":[\"Bioinformatics\",\"Computational Statistics\",\"Mathematical and statistical modeling\",\"Programming\",\"R\",\"Statistics\",\"Visualization\"],\"tools\":[\"Computational Systems Biology\",\"Data Management\",\"Data integration\",\"R\",\"Statistical tools\",\"Statistics\"],\"project_positions\":[{\"project_id\":\"57\",\"position_id\":\"3\",\"position_name\":\"Project Coordinator\"}]},\"relationships\":{\"projects\":{\"data\":[{\"id\":\"56\",\"type\":\"projects\"},{\"id\":\"57\",\"type\":\"projects\"}]},\"institutions\":{\"data\":[{\"id\":\"189\",\"type\":\"institutions\"}]},\"investigations\":{\"data\":[]},\"studies\":{\"data\":[]},\"assays\":{\"data\":[]},\"data_files\":{\"data\":[]},\"models\":{\"data\":[]},\"sops\":{\"data\":[]},\"publications\":{\"data\":[]},\"presentations\":{\"data\":[]},\"events\":{\"data\":[]},\"documents\":{\"data\":[]}},\"links\":{\"self\":\"/people/808\"},\"meta\":{\"created\":\"2016-11-07T22:17:49.000Z\",\"modified\":\"2017-08-01T08:28:42.000Z\",\"api_version\":\"0.1\",\"uuid\":\"f1427a00-8765-0134-769f-549f350973c0\",\"base_url\":\"https://fairdomhub.org\"}},\"jsonapi\":{\"version\":\"1.0\"}}"
length(cont)
## [1] 1
This gives us a text version of the JSON object. We can use built-in parsers to get other forms of data. We can parse the content into the R list object
cont <- content(r,"parsed",type="application/json")
cont
## $data
## $data$id
## [1] "808"
##
## $data$type
## [1] "people"
##
## $data$attributes
## $data$attributes$avatar
## [1] "/people/808/avatars/473"
##
## $data$attributes$title
## [1] "Andrej Blejec"
##
## $data$attributes$description
## NULL
##
## $data$attributes$first_name
## [1] "Andrej"
##
## $data$attributes$last_name
## [1] "Blejec"
##
## $data$attributes$web_page
## [1] "http://ablejec.nib.si"
##
## $data$attributes$orcid
## [1] "http://orcid.org/0000-0001-7484-6031"
##
## $data$attributes$mbox_sha1sum
## [1] "e37350ada8b1af0c7fe7a6cb61a6a5d35d20f8bd"
##
## $data$attributes$phone
## [1] "+38659232789"
##
## $data$attributes$skype_name
## [1] "ablejec"
##
## $data$attributes$expertise
## $data$attributes$expertise[[1]]
## [1] "Bioinformatics"
##
## $data$attributes$expertise[[2]]
## [1] "Computational Statistics"
##
## $data$attributes$expertise[[3]]
## [1] "Mathematical and statistical modeling"
##
## $data$attributes$expertise[[4]]
## [1] "Programming"
##
## $data$attributes$expertise[[5]]
## [1] "R"
##
## $data$attributes$expertise[[6]]
## [1] "Statistics"
##
## $data$attributes$expertise[[7]]
## [1] "Visualization"
##
##
## $data$attributes$tools
## $data$attributes$tools[[1]]
## [1] "Computational Systems Biology"
##
## $data$attributes$tools[[2]]
## [1] "Data Management"
##
## $data$attributes$tools[[3]]
## [1] "Data integration"
##
## $data$attributes$tools[[4]]
## [1] "R"
##
## $data$attributes$tools[[5]]
## [1] "Statistical tools"
##
## $data$attributes$tools[[6]]
## [1] "Statistics"
##
##
## $data$attributes$project_positions
## $data$attributes$project_positions[[1]]
## $data$attributes$project_positions[[1]]$project_id
## [1] "57"
##
## $data$attributes$project_positions[[1]]$position_id
## [1] "3"
##
## $data$attributes$project_positions[[1]]$position_name
## [1] "Project Coordinator"
##
##
##
##
## $data$relationships
## $data$relationships$projects
## $data$relationships$projects$data
## $data$relationships$projects$data[[1]]
## $data$relationships$projects$data[[1]]$id
## [1] "56"
##
## $data$relationships$projects$data[[1]]$type
## [1] "projects"
##
##
## $data$relationships$projects$data[[2]]
## $data$relationships$projects$data[[2]]$id
## [1] "57"
##
## $data$relationships$projects$data[[2]]$type
## [1] "projects"
##
##
##
##
## $data$relationships$institutions
## $data$relationships$institutions$data
## $data$relationships$institutions$data[[1]]
## $data$relationships$institutions$data[[1]]$id
## [1] "189"
##
## $data$relationships$institutions$data[[1]]$type
## [1] "institutions"
##
##
##
##
## $data$relationships$investigations
## $data$relationships$investigations$data
## list()
##
##
## $data$relationships$studies
## $data$relationships$studies$data
## list()
##
##
## $data$relationships$assays
## $data$relationships$assays$data
## list()
##
##
## $data$relationships$data_files
## $data$relationships$data_files$data
## list()
##
##
## $data$relationships$models
## $data$relationships$models$data
## list()
##
##
## $data$relationships$sops
## $data$relationships$sops$data
## list()
##
##
## $data$relationships$publications
## $data$relationships$publications$data
## list()
##
##
## $data$relationships$presentations
## $data$relationships$presentations$data
## list()
##
##
## $data$relationships$events
## $data$relationships$events$data
## list()
##
##
## $data$relationships$documents
## $data$relationships$documents$data
## list()
##
##
##
## $data$links
## $data$links$self
## [1] "/people/808"
##
##
## $data$meta
## $data$meta$created
## [1] "2016-11-07T22:17:49.000Z"
##
## $data$meta$modified
## [1] "2017-08-01T08:28:42.000Z"
##
## $data$meta$api_version
## [1] "0.1"
##
## $data$meta$uuid
## [1] "f1427a00-8765-0134-769f-549f350973c0"
##
## $data$meta$base_url
## [1] "https://fairdomhub.org"
##
##
##
## $jsonapi
## $jsonapi$version
## [1] "1.0"
names(cont)
## [1] "data" "jsonapi"
cont$jsonapi
## $version
## [1] "1.0"
names(cont$data)
## [1] "id" "type" "attributes" "relationships"
## [5] "links" "meta"
Response elements
cont$data$id
## [1] "808"
List the response content
d <- cont$data
for (nm in names(d)) {
cat("\n",nm,"--------------\n\n")
print(d[[nm]])
}
##
## id --------------
##
## [1] "808"
##
## type --------------
##
## [1] "people"
##
## attributes --------------
##
## $avatar
## [1] "/people/808/avatars/473"
##
## $title
## [1] "Andrej Blejec"
##
## $description
## NULL
##
## $first_name
## [1] "Andrej"
##
## $last_name
## [1] "Blejec"
##
## $web_page
## [1] "http://ablejec.nib.si"
##
## $orcid
## [1] "http://orcid.org/0000-0001-7484-6031"
##
## $mbox_sha1sum
## [1] "e37350ada8b1af0c7fe7a6cb61a6a5d35d20f8bd"
##
## $phone
## [1] "+38659232789"
##
## $skype_name
## [1] "ablejec"
##
## $expertise
## $expertise[[1]]
## [1] "Bioinformatics"
##
## $expertise[[2]]
## [1] "Computational Statistics"
##
## $expertise[[3]]
## [1] "Mathematical and statistical modeling"
##
## $expertise[[4]]
## [1] "Programming"
##
## $expertise[[5]]
## [1] "R"
##
## $expertise[[6]]
## [1] "Statistics"
##
## $expertise[[7]]
## [1] "Visualization"
##
##
## $tools
## $tools[[1]]
## [1] "Computational Systems Biology"
##
## $tools[[2]]
## [1] "Data Management"
##
## $tools[[3]]
## [1] "Data integration"
##
## $tools[[4]]
## [1] "R"
##
## $tools[[5]]
## [1] "Statistical tools"
##
## $tools[[6]]
## [1] "Statistics"
##
##
## $project_positions
## $project_positions[[1]]
## $project_positions[[1]]$project_id
## [1] "57"
##
## $project_positions[[1]]$position_id
## [1] "3"
##
## $project_positions[[1]]$position_name
## [1] "Project Coordinator"
##
##
##
##
## relationships --------------
##
## $projects
## $projects$data
## $projects$data[[1]]
## $projects$data[[1]]$id
## [1] "56"
##
## $projects$data[[1]]$type
## [1] "projects"
##
##
## $projects$data[[2]]
## $projects$data[[2]]$id
## [1] "57"
##
## $projects$data[[2]]$type
## [1] "projects"
##
##
##
##
## $institutions
## $institutions$data
## $institutions$data[[1]]
## $institutions$data[[1]]$id
## [1] "189"
##
## $institutions$data[[1]]$type
## [1] "institutions"
##
##
##
##
## $investigations
## $investigations$data
## list()
##
##
## $studies
## $studies$data
## list()
##
##
## $assays
## $assays$data
## list()
##
##
## $data_files
## $data_files$data
## list()
##
##
## $models
## $models$data
## list()
##
##
## $sops
## $sops$data
## list()
##
##
## $publications
## $publications$data
## list()
##
##
## $presentations
## $presentations$data
## list()
##
##
## $events
## $events$data
## list()
##
##
## $documents
## $documents$data
## list()
##
##
##
## links --------------
##
## $self
## [1] "/people/808"
##
##
## meta --------------
##
## $created
## [1] "2016-11-07T22:17:49.000Z"
##
## $modified
## [1] "2017-08-01T08:28:42.000Z"
##
## $api_version
## [1] "0.1"
##
## $uuid
## [1] "f1427a00-8765-0134-769f-549f350973c0"
##
## $base_url
## [1] "https://fairdomhub.org"
Person data are in the attributes part
Here is the first name of the person with id 808:
cont$data$attributes$first_name
## [1] "Andrej"
The rudimetary R read connection with FAIRDOMHub can be established using the package httr. From here it should be fairly possible to write the wrapper functions for easier usage.
GET and content# this function is equivaalent to a call:
# curl -X GET "https://www.fairdomhub.org/what/id" -H "accept: application/json"
#
myGET <- function(what, id,
uri="https://www.fairdomhub.org", ... ){
if(!missing(what)) uri <- paste0(uri,"/",what)
if(!missing(id)) uri <- paste0(uri,"/",id)
r <- GET(uri,
add_headers(Accept="application/json"))
cat("Status code:",r$status_code,"\n")
invisible(r)
}
myData <- function(r,
part="attributes",
type="application/json", ...){
invisible(content(r,"parsed",type=type)$data[[part]])
}
Get data for person 808
# https://www.fairdomhub.org/people/808
r <- myGET("people","808")
## Status code: 200
r$status_code
## [1] 200
Get the person 808 last name and tools
myData(r)$last_name
## [1] "Blejec"
myData(r)$tools
## [[1]]
## [1] "Computational Systems Biology"
##
## [[2]]
## [1] "Data Management"
##
## [[3]]
## [1] "Data integration"
##
## [[4]]
## [1] "R"
##
## [[5]]
## [1] "Statistical tools"
##
## [[6]]
## [1] "Statistics"
Get person 808 institution id
myData(r,"relationships")$institutions
## $data
## $data[[1]]
## $data[[1]]$id
## [1] "189"
##
## $data[[1]]$type
## [1] "institutions"
instID <- myData(r, "relationships")$institutions$data[[1]]$id
instID
## [1] "189"
Get institution instID` name. The details are in the institutions part.
# https://www.fairdomhub.org/institutions/189
rinst <- myGET("institutions",instID)
## Status code: 200
myData(rinst)$title
## [1] "National Institute of Biology"
Full institution description
catln <- function(...) cat(...,"\n")
d <- sapply(myData(rinst),catln)
## /institutions/189/avatars/568
## National Institute of Biology
## SI
##
## Ljubljana
## Vecna pot 111
## SI-1000 Ljubljana
## Slovenia
## http://www.nib.si