minerva_access_functions.R 2.67 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
    return(httr::content(resp, as = "text", encoding = encoding))
Marek Ostaszewski's avatar
Marek Ostaszewski committed
20
21
22
23
  }
  return(NULL)
}

Marek Ostaszewski's avatar
Marek Ostaszewski committed
24
25
26
27
28
29
### 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
30
31
32
33
### Get the components of a given map/project on the MINERVA Platform
get_map_components <- function(map_api, project_id = NULL) {
  if(is.null(project_id)) {
    ### If project id not given, get configuration of the map, to obtain the latest (default) version
34
    project_id <- get_default_project(map_api)
Marek Ostaszewski's avatar
Marek Ostaszewski committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  }
  ### 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/")))
  
  ### Get elements of the chosen diagram
  model_elements <- lapply(models$idObject, 
                           function(x)
                             fromJSON(ask_GET(paste0(mnv_base,"models/",x,"/",
                                                     "bioEntities/elements/?columns=id,name,type,references,elementId,complexId")),
                                      flatten = F))
  
  ### Request for reactions that have at least one top 10 element as participant
  model_reactions <- lapply(models$idObject,
                            function(x)
                              fromJSON(ask_GET(paste0(mnv_base,"models/",x,"/",
                                                      "bioEntities/reactions/?columns=modifiers,products,reactants")),
                                       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
get_annotation <- function(freferences, ftype) {
  sapply(freferences, 
         function(x) {
           ifelse(any(x$type == ftype), x$resource[x$type == ftype], NA)
         })
Marek Ostaszewski's avatar
Marek Ostaszewski committed
67
}