API_find_drug_targets.R 3.03 KB
Newer Older
Marek Ostaszewski's avatar
Marek Ostaszewski committed
1
2
##################################################
## Script purpose: An example of MINERVA API call to retrieve 
3
4
##                 HGNC symbols of elements targetted by a given drug,
##                 and to retrieve drugs targetting elements with a given UniProt
5
## Date: 17/03/2020
Marek Ostaszewski's avatar
Marek Ostaszewski committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
## 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
Marek Ostaszewski's avatar
new map    
Marek Ostaszewski committed
25
base_url <- "https://pdmap.uni.lu/minerva/api/projects/pd_map_winter_22/"
Marek Ostaszewski's avatar
Marek Ostaszewski committed
26
27
28
29
30
31
32
33
34
35
36

### Query for the drug targets, our drug of choice, 'aspirin', 
### is provided directly in the query; this is a one-target example
aspirin_targets <- fromJSON(ask_GET(base_url,"drugs:search?query=aspirin"))

### Omit results with zero map elements, these are drug targets not in the map
map_hits <- sapply(aspirin_targets$targets[[1]]$targetElements, nrow) > 0

### Extract HGNC symbols from the 'targetParticipants' part of the results
aspirin_map_hgncs <- sapply(aspirin_targets$targets[[1]]$targetParticipants[map_hits], 
                            function(x) x[,"resource"])
37
38
39
40
41
42


### Query for the drugs targetting given
### Uniprot identifiers

#Define UniProt identifiers to be matched in model elements
Marek Ostaszewski's avatar
new map    
Marek Ostaszewski committed
43
uniprot_examples <- c("P14416", "P07101")
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

### 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))

### For results in each model, find element identifiers that have the selected uniprot id
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 <- c(identifiers, unlist(model_elements[[m]]$id[hits]))
}
### For each identifier, request fo its drug targets, select 'name' from results 
drugs <- sapply(identifiers, 
                function(x) fromJSON(ask_GET(base_url,paste0("drugs:search?target=ALIAS:", x)))$name)
### Display unique results
Marek Ostaszewski's avatar
new map    
Marek Ostaszewski committed
70
unique(unlist(drugs))