API_retrieve.R 3.94 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
##################################################
## Script purpose: An example of MINERVA API call to retrieve elements with a 
##                 given uniprot id and their interactions
## Date: 17/03/2019
## Author: Marek Ostaszewski (marek.ostaszewski@uni.lu)
## MINERVA API version: 14.0
##################################################

### A convenience function to handle GET and POST requests to MINERVA API
library(httr)
library(jsonlite)

### Utility function using httr:GET to send queries to a given MINERVA Platform instance
ask_GET <- function(mnv_base, query) {
  resp <- httr::GET(url = paste0(mnv_base, query),
                    httr::add_headers('Content-Type' = "application/x-www-form-urlencoded"))
  if(httr::status_code(resp) == 200) {
    return(httr::content(resp, as = "text"))
  }
  return(NULL)
}

### Base URL of the map, we use Parkinson's disease map
base_url <- "https://pdmap.uni.lu/minerva/api/projects/pd_map_autumn_19/"

### Request identifiers of models for a given project (main map and submaps)
models <- ask_GET(base_url, "models/")
models <- fromJSON(models, flatten = F)

### For each model, retrieve elements with columns including references
model_elements <- lapply(models$idObject, 
                         function(x) fromJSON(ask_GET(paste0(base_url,"models/",x,"/"), 
                                                      "bioEntities/elements/?columns=id,name,type,references"), 
                                              flatten = F))
#Define UniProt identifiers to be matched in model elements
uniprot_examples <- c("O43521", "P63167")

### For each model, retrieve reactions
model_reactions <- lapply(models$idObject, 
                          function(x) fromJSON(ask_GET(paste0(base_url,"models/",x,"/"), 
                                                       "bioEntities/reactions/?columns=id,type,reactionId,products,reactants,modifiers"), 
                                               flatten = F))

### For results in each model, find element identifiers that have the selected uniprot id
### then find reactions having at least one of these elements, and append them to the output vector
reaction_identifiers <- c()

for (m in 1:length(model_elements)) {
  ### In "resources" of each element, find those which have requested UniProt identifiers 
  hits <- sapply(model_elements[[m]]$references,
                 function(x) any(uniprot_examples %in% x$resource[x$type == "UNIPROT"]))
  
  ### Identifiers of the elements matching the search criteria
  identifiers <- model_elements[[m]]$id[hits]
  
  ### Look for reaction identifiers only for non-zero results and models with existing reactions 
  if(length(identifiers) > 0 && length(model_reactions[[m]]) > 0) {
    
    ### Which reactions have at least one modifier, product and reactant matching the list
    hits <- which(sapply(model_reactions[[m]]$modifiers, 
                      function(x) ifelse(length(x) == 0, F, any(x$aliasId %in% identifiers))))
    hits <- c(hits, which(sapply(model_reactions[[m]]$products, 
                      function(x) ifelse(length(x) == 0, F, any(x$aliasId %in% identifiers)))))
    hits <- c(hits, which(sapply(model_reactions[[m]]$reactants, 
                      function(x) ifelse(length(x) == 0, F, any(x$aliasId %in% identifiers)))))
    
    ### Append the data frame by these reaction identifiers
    reaction_identifiers <- rbind(reaction_identifiers,
                                  data.frame(reaction_id = model_reactions[[m]]$reactionId[unique(hits)],
                                             model_id = models$idObject[m], stringsAsFactors = F))
  }
}

### Create a validation URL for the disease map these identifiers come from
visualize_reactions <- "https://pdmap.uni.lu/minerva/?search="

### Get searchable reaction identifiers from the search results
searchable_identifiers <- paste(paste0("reaction:", reaction_identifiers$reaction_id), collapse = ";")

### Create the final link
print(paste0(visualize_reactions, searchable_identifiers))