-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_cosmos_results_new.R
189 lines (148 loc) · 6.18 KB
/
format_cosmos_results_new.R
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
cleanup_protein_node <- function(node)
{
prefixe <- str_extract(node, "Gene[0-9]+__")
prefixe <- gsub("Gene","Enzyme",prefixe)
suffixe_direction <- str_extract(node, "_reverse")
suffixe_exchange <- str_extract(node, "EXCHANGE[0-9]+")
node <- gsub("Gene[0-9]+__","",node)
node <- gsub("_reverse","",node)
node <- gsub("EXCHANGE[0-9]+","",node)
node <- str_split(node,'_')[[1]]
return(list("prefixe" = prefixe, "node" = node,"suffixe_direction" = suffixe_direction, "suffixe_exchange" = suffixe_exchange))
}
cleanup_metab_node <- function(node)
{
prefixe <- 'Metab__'
suffixe <- str_extract(node,'___[a-z]____')
node <- gsub('Metab__','',node)
node <- gsub('___[a-z]____','',node)
return(list("prefixe" = prefixe, "node" = node,"suffixe_compartment" = suffixe))
}
translate_column <- function(sif_column,
metab_mapping,
gene_mapping)
{
sif_column <- sapply(sif_column, function(x,
metab_mapping,
gene_mapping)
{
node <- x
type_node <- ifelse(grepl("Metab",node),'metabolite','protein')
if(type_node == 'protein')
{
node <- cleanup_protein_node(node)
node$node <- unlist(sapply(node$node, function(x,
gene_mapping)
{
return(ifelse(x %in% names(gene_mapping),
gene_mapping[x],
x))
},simplify = T,
gene_mapping = gene_mapping, USE.NAMES = F))
node <- node[!is.na(node)]
node$node <- paste(node$node, collapse = '_')
node <- paste(node, collapse = '')
} else
{
node <- cleanup_metab_node(node)
node$node <- unlist(sapply(node$node, function(x,
metab_mapping)
{
return(ifelse(x %in% names(metab_mapping),
metab_mapping[x],
x))
},simplify = T,
metab_mapping = metab_mapping, USE.NAMES = F))
node <- node[!is.na(node)]
node <- paste(node, collapse = '')
}
}, simplify = T,
metab_mapping = metab_mapping,
gene_mapping = gene_mapping, USE.NAMES = F)
return(sif_column)
}
translate_sif <- function(sif,
metab_mapping,
gene_mapping)
{
sif$source <- translate_column(sif$source,
metab_mapping,
gene_mapping)
sif$target <- translate_column(sif$target,
metab_mapping,
gene_mapping)
return(sif)
}
#' format_COSMOS_res
#'
#' formats the network with readable names
#'
#' @param cosmos_res results of CARNIVAL run
#' @param metab_mapping a named vector with pubchem id as names and desired metabolite names as values.
#' @param gene_mapping by default, use the 'org.Hs.eg.db' to map gene names. Can also be a named vector with entrez gene id as names and desired gene names as values.
#' @param measured_nodes vector of nodes that are measured or inputs
#' @param omnipath_ptm ptms database from OmnipathR
#' @importFrom org.Hs.eg.db org.Hs.eg.db
#' @import dorothea
#' @importFrom AnnotationDbi mapIds
format_COSMOS_res <- function(cosmos_res,
metab_mapping,
gene_mapping = 'org.Hs.eg.db',
measured_nodes,
omnipath_ptm)
{
sif <- as.data.frame(cosmos_res$aggregated_network)
sif$source <- gsub("^X","",sif$source)
sif$target <- gsub("^X","",sif$target)
att <- as.data.frame(cosmos_res$aggregated_network_node_attributes )#[,c(1,2)]
names(att)[1] <- "Nodes"
att$measured <- ifelse(att$Nodes %in% measured_nodes, 1, 0)
att$Nodes <- gsub("^X","",att$Nodes)
att$type <- ifelse(grepl("Metab",att$Nodes), "metabolite","protein")
att <- att[abs(as.numeric(att$AvgAct)) > 0,]
########################
prots <- unique(att$Nodes)
prots <- prots[!(grepl("Metab",prots))]
prots <- gsub("Gene[0-9]+__","",prots)
prots <- gsub("_reverse","",prots)
prots <- gsub("EXCHANGE.*","",prots)
prots <- unique(prots)
prots <- unlist(sapply(prots, function(x){unlist(strsplit(x,split = "_"))}))
if(is.null(names(gene_mapping)))
{
entrezid <- prots
gene_mapping <- AnnotationDbi::mapIds(org.Hs.eg.db::org.Hs.eg.db, entrezid, 'SYMBOL', 'ENTREZID')
gene_mapping <- unlist(gene_mapping)
gene_mapping <- gene_mapping[!is.na(gene_mapping)]
} else
{
if(sum(names(gene_mapping) %in% prots) == 0)
{
print('Error : none of gene mapping identifiers are not found in the network solution. Please make sure you inputed a properlly named vector. Else use the default value for this argument : "org.Hs.eg.db"')
return('Bad identifer mapping')
}
}
sif <- translate_sif(sif,
metab_mapping,
gene_mapping)
att$Nodes <- translate_column(att$Nodes,
metab_mapping,
gene_mapping)
########################
omnipath_ptm <- omnipath_ptm[omnipath_ptm$modification %in% c("dephosphorylation","phosphorylation"),]
KSN <- omnipath_ptm[,c(4,3)]
KSN$substrate_genesymbol <- paste(KSN$substrate_genesymbol,omnipath_ptm$residue_type, sep ="_")
KSN$substrate_genesymbol <- paste(KSN$substrate_genesymbol,omnipath_ptm$residue_offset, sep = "")
KSN$sign <- ifelse(omnipath_ptm$modification == "phosphorylation", 1, -1)
att$type <- ifelse(att$type == 'protein', 'metab_enzyme', att$type)
att$type <- ifelse(att$Nodes %in% KSN$enzyme_genesymbol, "Kinase",att$type)
dorothea <- as.data.frame(dorothea::dorothea_hs[dorothea::dorothea_hs$confidence %in% c("A","B","C","D","E"),c(3,1,4)])
names(dorothea) <- c("target_genesymbol","source_genesymbol","sign")
att$type <- ifelse(att$Nodes %in% dorothea$source_genesymbol, "TF",att$type)
att$Activity <- sign(as.numeric(as.character(att$AvgAct)))
sif <- sif[sif$source != sif$target,]
sif <- sif[sif$source %in% att$Nodes & sif$target %in% att$Nodes,]
measured <- att[att$measured == 1,"Nodes"]
att$measured <- ifelse(att$Nodes %in% measured, 1, 0)
return(list(sif,att))
}