Skip to content

Commit e89cde5

Browse files
committed
translate decouplrnival output
1 parent 3c01d3b commit e89cde5

File tree

4 files changed

+140
-6
lines changed

4 files changed

+140
-6
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export(preprocess_COSMOS_signaling_to_metabolism)
1717
export(reduce_solution_network)
1818
export(run_COSMOS_metabolism_to_signaling)
1919
export(run_COSMOS_signaling_to_metabolism)
20+
export(translate_res)
2021
import(decoupleR)
2122
import(dplyr)
2223
importFrom(dplyr,"%>%")

R/decoupleRnival.R

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#' @param n_layers The number of layers that will be propagated upstream.
1010
#' @param n_perm The number of permutations to use in decoupleR's algorithm.
1111
#' @param downstream_cutoff If downstream measurments should be included above a given threshold
12+
#' @param statistic the decoupleR stat to consider: "wmean", "norm_wmean", or "ulm"
1213
#'
1314
#' @return A data frame containing the score of the nodes upstream of the
1415
#' downstream input based on the iterative propagation
@@ -30,16 +31,26 @@
3031
#'
3132
#' # View the results
3233
#' print(result)
33-
decoupleRnival <- function(upstream_input = NULL, downstream_input, meta_network, n_layers, n_perm = 1000, downstream_cutoff = 0){
34+
decoupleRnival <- function(upstream_input = NULL, downstream_input, meta_network, n_layers, n_perm = 1000, downstream_cutoff = 0, statistic = "norm_wmean"){
3435

3536

3637
regulons <- meta_network
3738

3839
names(regulons)[names(regulons) == "sign" | names(regulons) == "interaction"] <- "mor"
3940
regulons <- regulons[!(regulons$source %in% names(downstream_input)),]
4041

41-
n_plus_one <- run_wmean(mat = as.matrix(data.frame(downstream_input)), network = regulons, times = n_perm, minsize = 1)
42-
n_plus_one <- n_plus_one[n_plus_one$statistic == "norm_wmean",c(2,4)]
42+
switch(statistic,
43+
"norm_wmean" = {
44+
n_plus_one <- run_wmean(mat = as.matrix(data.frame(downstream_input)), network = regulons, times = n_perm, minsize = 1)
45+
},
46+
"wmean" = {
47+
n_plus_one <- run_wmean(mat = as.matrix(data.frame(downstream_input)), network = regulons, times = 2, minsize = 1)
48+
},
49+
"ulm" = {
50+
n_plus_one <- run_ulm(mat = as.matrix(data.frame(downstream_input)), network = regulons, minsize = 1)
51+
})
52+
53+
n_plus_one <- n_plus_one[n_plus_one$statistic == statistic,c(2,4)]
4354
# regulons <- regulons[!(regulons$source %in% n_plus_one$source),]
4455

4556
res_list <- list()
@@ -52,8 +63,17 @@ decoupleRnival <- function(upstream_input = NULL, downstream_input, meta_network
5263
previous_n_plu_one <- res_list[[i - 1]]
5364
row.names(previous_n_plu_one) <- previous_n_plu_one$source
5465
previous_n_plu_one <- previous_n_plu_one[,-1,drop = F]
55-
n_plus_one <- run_wmean(mat = as.matrix(previous_n_plu_one), network = regulons, times = n_perm, minsize = 1)
56-
n_plus_one <- n_plus_one[n_plus_one$statistic == "norm_wmean",c(2,4)]
66+
switch(statistic,
67+
"norm_wmean" = {
68+
n_plus_one <- run_wmean(mat = as.matrix(previous_n_plu_one), network = regulons, times = n_perm, minsize = 1)
69+
},
70+
"wmean" = {
71+
n_plus_one <- run_wmean(mat = as.matrix(previous_n_plu_one), network = regulons, times = 2, minsize = 1)
72+
},
73+
"ulm" = {
74+
n_plus_one <- run_ulm(mat = as.matrix(previous_n_plu_one), network = regulons, minsize = 1)
75+
})
76+
n_plus_one <- n_plus_one[n_plus_one$statistic == statistic,c(2,4)]
5777
regulons <- regulons[!(regulons$source %in% n_plus_one$source),]
5878
res_list[[i]] <- as.data.frame(n_plus_one)
5979
i <- i +1
@@ -268,3 +288,71 @@ meta_network_cleanup <- function(meta_network)
268288
meta_network <- meta_network[meta_network$interaction %in% c(1,-1),]
269289
return(meta_network)
270290
}
291+
292+
#' translate_res
293+
#'
294+
#' formats the network with readable names
295+
#'
296+
#' @param SIF result SIF of decoupleRnival pipeline
297+
#' @param ATT result ATT of decoupleRnival pipeline
298+
#' @param metab_mapping a named vector with HMDB Ids as names and desired metabolite names as values.
299+
#' @return list with network and attribute tables.
300+
#' @importFrom stringr str_extract
301+
#' @export
302+
#'
303+
#' @examples
304+
#' # Create a meta network data frame
305+
#' example_SIF <- data.frame(
306+
#' source = c("GPX1", "Gene863__GPX1"),
307+
#' target = c("Gene863__GPX1", "Metab__HMDB0003337_c"),
308+
#' sign = c(1, 1)
309+
#' )
310+
#'
311+
#' example_ATT <- data.frame(
312+
#' Nodes = c("GPX1", "Gene863__GPX1","Metab__HMDB0003337_c"),
313+
#' sign = c(1, 1, 1)
314+
#' )
315+
#'
316+
#' example_SIF
317+
#'
318+
#' data("HMDB_mapper_vec")
319+
#'
320+
#' translated_res <- translate_res(example_SIF,example_ATT,HMDB_mapper_vec)
321+
#'
322+
#' translated_res$SIF
323+
translate_res <- function(SIF,ATT,HMDB_mapper_vec = NULL)
324+
{
325+
if (is.null(HMDB_mapper_vec)) {
326+
data("HMDB_mapper_vec", package = "cosmosR", envir = environment())
327+
}
328+
colnames(ATT)[1] <- "Nodes"
329+
for (i in c(1, 2)) {
330+
SIF[, i] <- sapply(SIF[, i], function(x, HMDB_mapper_vec) {
331+
x <- gsub("Metab__", "", x)
332+
x <- gsub("^Gene", "Enzyme", x)
333+
suffixe <- stringr::str_extract(x, "_[a-z]$")
334+
x <- gsub("_[a-z]$", "", x)
335+
if (x %in% names(HMDB_mapper_vec)) {
336+
x <- HMDB_mapper_vec[x]
337+
x <- paste("Metab__", paste(x, suffixe, sep = ""),
338+
sep = "")
339+
}
340+
return(x)
341+
}, HMDB_mapper_vec = HMDB_mapper_vec)
342+
}
343+
ATT[, 1] <- sapply(ATT[, 1], function(x, HMDB_mapper_vec) {
344+
x <- gsub("Metab__", "", x)
345+
x <- gsub("^Gene", "Enzyme", x)
346+
suffixe <- stringr::str_extract(x, "_[a-z]$")
347+
x <- gsub("_[a-z]$", "", x)
348+
if (x %in% names(HMDB_mapper_vec)) {
349+
x <- HMDB_mapper_vec[x]
350+
x <- paste("Metab__", x, sep = "")
351+
}
352+
if (!is.na(suffixe)) {
353+
x <- paste(x, suffixe, sep = "")
354+
}
355+
return(x)
356+
}, HMDB_mapper_vec = HMDB_mapper_vec)
357+
return(list("SIF" = SIF, "ATT" = ATT))
358+
}

man/decoupleRnival.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/translate_res.Rd

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)