-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcombin_gsea_function.R
More file actions
174 lines (144 loc) · 6.25 KB
/
combin_gsea_function.R
File metadata and controls
174 lines (144 loc) · 6.25 KB
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
suppressPackageStartupMessages({
library(qs)
library(Seurat)
library(presto)
library(msigdbr)
library(fgsea)
library(dplyr)
library(ggplot2)
library(tidyverse)
library(scran)
library(here)
library(tictoc)
library(purrr)
})
#### check/make the frozen GSEA database ####
# check if the file exists
ifelse (test = file.exists(here("data/GSEA_DB","static_snapshot.qs")),
yes = {
# load the file
genesets_db <- qread(file = here("data/GSEA_DB", "static_snapshot.qs"))
# print the date of the file
print("Loading the selected GSEA sets from January 2023")
},
no = {
# print that the file is empty
print("static_snapshot file is empty")
tictoc::tic()
# if the file does not exist, create the file
# GO Biological process
bio_pro <-
msigdbr(
species = "Homo sapiens",
category = "C5",
subcategory = "GO:BP"
)
bio_pro <- bio_pro %>% split(., x = .$gene_symbol, f = .$gs_name)
#reactome
react <-
msigdbr(
species = "Homo sapiens",
category = "C2",
subcategory = "REACTOME"
)
react <- react %>% split(., x = .$gene_symbol, f = .$gs_name)
#Transcription factor targets
tft <-
msigdbr(
species = "Homo sapiens",
category = "C3",
subcategory = "TFT:GTRD"
)
tft <- tft %>% split(., x = .$gene_symbol, f = .$gs_name)
#kegg
kegg <-
msigdbr(
species = "Homo sapiens",
category = "C2",
subcategory = "KEGG"
)
kegg <- kegg %>% split(., x = .$gene_symbol, f = .$gs_name)
# create a list of the genesets
genesets_db <-
list(kegg, bio_pro, react, tft) %>% set_names("kegg", "bio_pro", "react", "tft")
# genesets_db <- genesets_db %>% map( ~ split(., .x$gene_symbol, f = .x$gs_name))
# remove the genesets from the environment
rm(kegg); rm(bio_pro); rm(react); rm(tft)
# save the genesets to the file
qsave(x = genesets_db,
file = here("data/GSEA_DB", "static_snapshot.qs"))
# print that the file is saved
print(paste0(
"static_snapshot of GSEA DB is saved into",
here("data/GSEA_DB", "static_snapshot.qs")
))
# load the file
genesets_db <- qread(file = here("data/GSEA_DB", "static_snapshot.qs"))
# print the date of the file
print("and then loaded the selected GSEA sets from January 2023")
tictoc::toc()
})
#####
run_combin_GSEA <- function(paired_results, rank_how, genesets_db) {
set.seed(2206)
results <- list()
for (i in 1:length(paired_results)) {
if (rank_how == "log_fc" || rank_how == "logfc" || rank_how == "logFC" || rank_how == "log_FC" || rank_how == "log_Fc") {
ranked_genes <- paired_results[[i]] %>% dplyr::select(feature, logFC)
} else if (rank_how == "auc" || rank_how == "AuC" || rank_how == "AUC") {
ranked_genes <- paired_results[[i]] %>% dplyr::select(feature, auc)
ranked_genes$auc <- ranked_genes$auc-0.5 #centering AuC to Zero
} else {
stop("Non-defined ranking, sorry !")
}
ranked_genes <- deframe(ranked_genes)
print(ranked_genes)
fgsea_results <- lapply(genesets_db, function(y) {
fgseaRes <- fgsea(pathways = y,
stats = ranked_genes,
minSize = 15,
maxSize = 500,
gseaParam = 1
)
return(fgseaRes)
})
#
results[[i]] <- fgsea_results
# attaching ranked_genes for plotting purposes of fgsea
results[[i]]$ranks <- ranked_genes
results[[i]]$pair <- unique(paired_results[[i]]$pairs)
}
return(results)
}
#####
#####
#### Tidy clean up function for nested
#### list structure of fgsea result embedddings
library(corrr)
# res <- qs::qread("/Volumes/CRCT13/Ruchan/01_NK_project/01_human/8_clusters_nonproliferating_nk/Enrichment_results/detection_1_thresholded_all_combin_gsea_res.qs")
clean_up_gsea <- function(res, ranking_metric, thresholding) {
pair_names <- lapply(res, function(it)
it[["pair"]])
pair_names <- do.call(c, pair_names)
names(res) <- pair_names
res <- lapply(pair_names, function(pair_it) {
tmp_res <- res[[pair_it]]
tmp_res[["pair"]] <- NULL
new_names <- paste0(names(tmp_res), "-", pair_it)
names(tmp_res) <- new_names
return(tmp_res)
})
res <- do.call(c, res)
res <- enframe(res, "iteration", "results")
# Re-organizing an grouping per pairs
# in this task, *NES*: "Normalized Enrichment Score"
# is the interesting bit of comparison for us.
res %>%
separate(iteration, c("database", "pair"), sep = "-") %>%
dplyr::filter(database != "ranks") %>%
mutate(results = purrr::map(results, as.data.frame)) %>%
unnest(cols = c(results)) %>%
dplyr::select(database, pair, pathway, NES) %>%
mutate(ranking = ranking_metric,
threshold = thresholding)
}