resolve_aliases.R 4.57 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: Translate raw CellDesigner SIF to Entrez identifiers using MINERVA 
## Date: 05.06.2020
## Author: Marek Ostaszewski
##################################################

library(httr)
library(jsonlite)

### A convenience function to handle API queries
12
13
14
15
16
17
ask_GET <- function(fask_url, verbose = F) {
  if(verbose) {
    message(URLencode(fask_url))
  }
  resp <- httr::GET(url = URLencode(fask_url),
                    httr::add_headers('Content-Type' = "application/x-www-form-urlencoded"))
Marek Ostaszewski's avatar
Marek Ostaszewski committed
18
19
20
21
22
23
  if(httr::status_code(resp) == 200) {
    return(httr::content(resp, as = "text"))
  }
  return(NULL)
}

24
25
### Define the source diagram name
diag_name <- "Endoplasmatic Reticulum stress"
Marek Ostaszewski's avatar
Marek Ostaszewski committed
26
27

### Read in the raw SIF version (here straight from the github of Aurelien)
28
raw_sif <- read.table(url("https://git-r3lab.uni.lu/covid/models/-/raw/master/Executable%20Modules/SBML_qual_build/sif/ER_Stress_stable_raw.sif"),
Marek Ostaszewski's avatar
Marek Ostaszewski committed
29
30
31
32
33
34
                      sep = " ", header = F, stringsAsFactors = F)

### Get MINERVA elements
### The address of the COVID-19 Disease Map in MINERVA
map <- "https://covid19map.elixir-luxembourg.org/minerva/api/"
### Get configuration of the COVID-19 Disease Map, to obtain the latest (default) version
35
cfg <- fromJSON(ask_GET(paste0(map, "configuration/")))
Marek Ostaszewski's avatar
Marek Ostaszewski committed
36
37
38
39
40
41
42
project_id <- cfg$options[cfg$options$type == "DEFAULT_MAP","value"]
### The address of the latest (default) build 
mnv_base <- paste0(map,"projects/",project_id,"/")

message(paste0("Asking for diagrams in: ", mnv_base, "models/"))

### Get diagrams
43
models <- ask_GET(paste0(mnv_base, "models/"))
Marek Ostaszewski's avatar
Marek Ostaszewski committed
44
45
models <- fromJSON(models, flatten = F)

46
this_refs <- models[models$name == diag_name,]
Marek Ostaszewski's avatar
Marek Ostaszewski committed
47
### Get elements of the chosen diagram
48
49
model_elements <- fromJSON(ask_GET(paste0(mnv_base,"models/",models$idObject[models$name == diag_name],"/", 
                                   "bioEntities/elements/?columns=id,name,type,references,elementId,complexId,bounds,hypothetical")),
Marek Ostaszewski's avatar
Marek Ostaszewski committed
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
                           flatten = F)

message("Fetching entrez ids...")
### Get information about Entrez identifiers from MINERVA elements
entrez <- sapply(model_elements$references, function(x) ifelse(length(x) == 0, NA, x[x$type == "ENTREZ", "resource"]))
names(entrez) <- model_elements$elementId

### An utility function to retrieve Entrez based on the species id
### if the id is a complex, the function goes recursively and fetches the ids of elements in this complex
group_elements <- function(feid, felements, fentrez) {
  pos <- which(felements$elementId == feid)
  ### Any elements that may be nested in the 'feid' (CellDesigner alias)
  incs <- felements$elementId[felements$complexId %in% felements$id[pos]]
  if(length(incs) > 0) {
    ### If nested elements found, run the function recursively for the contained elements
    return(paste(unlist(sapply(incs, group_elements, felements, fentrez)), collapse = ";"))
  } else {
    ### If no nested elements, return Entrez
    rid <- fentrez[[feid]]
    if(is.na(rid)) {
      ### If Entrez not available, return name
      rid <- felements$name[pos]
    }
    return(rid)
  }
}

77
78
79
80
81
82
83
84
85
### A function to get information about hypothetical complexes
get_role <- function(feid, felements) {
  fpos <- which(felements$elementId == feid)
  if(length(fpos) == 0) return(NA)
  
  role <- ifelse(felements[fpos, "type"] == "Complex",
                 ifelse(felements[fpos, "hypothetical"], "group", "complex"),
                 "node")
  return(role)
Marek Ostaszewski's avatar
Marek Ostaszewski committed
86
87
}

Marek Ostaszewski's avatar
Marek Ostaszewski committed
88
89
90
message("Translating...")
### Create a copy
translated_sif <- raw_sif
Marek Ostaszewski's avatar
Marek Ostaszewski committed
91
92
### Retrieve Entrez and type for the entire columns of sources and targets
s.entrez <- sapply(raw_sif[,1], group_elements, model_elements, entrez)
93
s.type <- sapply(raw_sif[,1], get_role, model_elements)
Marek Ostaszewski's avatar
Marek Ostaszewski committed
94
t.entrez <- sapply(raw_sif[,3], group_elements, model_elements, entrez)
95
t.type <- sapply(raw_sif[,3], get_role, model_elements)
Marek Ostaszewski's avatar
Marek Ostaszewski committed
96
97
98
99
100
### Collect x.y information
s.xy <- t(sapply(raw_sif[,1], function(x) unlist(model_elements$bounds[model_elements$elementId == x, c(3,4)])))
colnames(s.xy) <- c("source.x", "source.y")
t.xy <- t(sapply(raw_sif[,3], function(x) unlist(model_elements[model_elements$elementId == x,1][,c(3,4)])))
colnames(t.xy) <- c("targets.x", "targets.y")
Marek Ostaszewski's avatar
Marek Ostaszewski committed
101

Marek Ostaszewski's avatar
Marek Ostaszewski committed
102
### Combine into a single data frame
Marek Ostaszewski's avatar
Marek Ostaszewski committed
103
104
105
106
translated_sif <- data.frame(source = s.entrez, source.type = s.type,
                             sign = raw_sif[,2], 
                             target = t.entrez, target.type = t.type,
                             s.xy, t.xy)
Marek Ostaszewski's avatar
Marek Ostaszewski committed
107

Marek Ostaszewski's avatar
Marek Ostaszewski committed
108
write.table(translated_sif, file = "translated_sif.txt",
Marek Ostaszewski's avatar
Marek Ostaszewski committed
109
            sep = "\t", quote = F, col.names = T, row.names = F)
Marek Ostaszewski's avatar
Marek Ostaszewski committed
110
message("Done.")