minerva_access_functions.R 3.61 KB
Newer Older
Marek Ostaszewski's avatar
Marek Ostaszewski committed
1
2
3
4
5
6
7
8
9
10
11
##################################################
## Project: COVID-19 Disease Map
## Script purpose: Convenience functions for accessing the MINERVA Platform
## Date: 24.12.2020
## Author: Marek Ostaszewski
##################################################

library(httr)
library(jsonlite)

### A convenience function to handle API queries
Marek Ostaszewski's avatar
Marek Ostaszewski committed
12
ask_GET <- function(fask_url, verbose = F, encoding = "UTF-8") {
Marek Ostaszewski's avatar
Marek Ostaszewski committed
13
14
15
16
17
18
  if(verbose) {
    message(URLencode(fask_url))
  }
  resp <- httr::GET(url = URLencode(fask_url),
                    httr::add_headers('Content-Type' = "application/x-www-form-urlencoded"))
  if(httr::status_code(resp) == 200) {
Marek Ostaszewski's avatar
Marek Ostaszewski committed
19
20
21
22
23
24
25
26
27
28
29
30
31
    ### when the content is sent as a zip file, it needs to be handled differently,
    ### i.e. saved to a tmp file and unzipped
    if(headers(resp)$`content-type` == "application/zip") {
      tmp <- tempfile()
      tmpc <- file(tmp, "wb")
      writeBin(httr::content(resp, as = "raw"), con = tmpc)
      close(tmpc)
      unzipped <- unzip(tmp)
      file.remove(tmp)
      return(unzipped)
    } else {
      return(httr::content(resp, as = "text", encoding = encoding)) 
    }
Marek Ostaszewski's avatar
Marek Ostaszewski committed
32
33
34
35
  }
  return(NULL)
}

Marek Ostaszewski's avatar
Marek Ostaszewski committed
36
37
38
39
40
41
### Get the id of the default project for this MINERVA instance
get_default_project <- function(map_api) {
  cfg <- fromJSON(ask_GET(paste0(map_api, "configuration/")))
  return(cfg$options[cfg$options$type == "DEFAULT_MAP","value"])
}

Marek Ostaszewski's avatar
Marek Ostaszewski committed
42
### Get the components of a given map/project on the MINERVA Platform
43
44
45
get_map_components <- function(map_api, project_id = NULL, 
                               e_columns = "",
                               r_columns = "") {
Marek Ostaszewski's avatar
Marek Ostaszewski committed
46
47
  if(is.null(project_id)) {
    ### If project id not given, get configuration of the map, to obtain the latest (default) version
48
    project_id <- get_default_project(map_api)
Marek Ostaszewski's avatar
Marek Ostaszewski committed
49
50
51
52
53
54
55
56
57
  }
  ### The address of the latest (default) build 
  mnv_base <- paste0(map_api, "projects/",project_id,"/")
  
  message(paste0("Asking for diagrams in: ", mnv_base, "models/"))
  
  ### Get diagrams
  models <- fromJSON(ask_GET(paste0(mnv_base, "models/")))
  
58
59
60
  if(e_columns != "") { e_columns <- paste0("?columns=", e_columns) }
  if(r_columns != "") { r_columns <- paste0("?columns=", r_columns) }
  
Marek Ostaszewski's avatar
Marek Ostaszewski committed
61
62
63
  ### Get elements of the chosen diagram
  model_elements <- lapply(models$idObject, 
                           function(x)
64
                             fromJSON(ask_GET(paste0(mnv_base,"models/",x,"/","bioEntities/elements/",e_columns)),
Marek Ostaszewski's avatar
Marek Ostaszewski committed
65
66
67
68
69
                                      flatten = F))
  
  ### Request for reactions that have at least one top 10 element as participant
  model_reactions <- lapply(models$idObject,
                            function(x)
70
                              fromJSON(ask_GET(paste0(mnv_base,"models/",x,"/","bioEntities/elements/",r_columns)),
Marek Ostaszewski's avatar
Marek Ostaszewski committed
71
72
73
74
75
76
                                       flatten = F))
  ### Pack all into a list and return
  return(list(models = models, map_elements = model_elements, map_reactions = model_reactions))
}

### Get annotation of a given type, from element/reaction references
Marek Ostaszewski's avatar
Marek Ostaszewski committed
77
78
79
get_annotation <- function(references, selected_type) {
  if(length(references) == 0) { return(NA) }
  with(data.frame(references), resource[type == selected_type])
Marek Ostaszewski's avatar
Marek Ostaszewski committed
80
}
81
82

### A function retrieving element identifiers from a given MINERVA element set
83
get_components_annotations <- function(components, annotation_type) {
84
85
86
  ### Get MINERVA references as a named list
  message(paste0("Retrieving ", annotation_type, "identifiers for this MINERVA collection"))
  ### Apply get_annotation function
87
88
  mnv_refs <- sapply(components$map_elements, function(x) sapply(x$references, get_annotation, annotation_type) )
  names(mnv_refs) <- components$models$name
89
90
  return(mnv_refs)
}