Commit 719d4873 authored by Marek Ostaszewski's avatar Marek Ostaszewski
Browse files

Upload API retrieve example

parent f10f49a6
##################################################
## 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))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment