-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_GSEA_Rho-as-Rank.R
279 lines (221 loc) · 9.95 KB
/
03_GSEA_Rho-as-Rank.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
library(ComplexHeatmap)
library(corrplot)
library(circlize)
library(fgsea)
library(data.table)
#library(clusterProfiler)
library(ggplot2)
library(fastDummies)
library(tidyr)
library(dplyr)
options(stringsAsFactors = F)
# ------------------- Function Block ------------------- #
read.gmt <- function(fname){
res <- list(genes=list(),
desc=list())
gmt <- file(fname)
gmt.lines <- readLines(gmt)
close(gmt)
gmt.list <- lapply(gmt.lines,
function(x) unlist(strsplit(x, split="\t")))
gmt.names <- sapply(gmt.list, '[', 1)
gmt.desc <- lapply(gmt.list, '[', 2)
gmt.genes <- lapply(gmt.list,
function(x){x[3:length(x)]})
names(gmt.desc) <- names(gmt.genes) <- gmt.names
return(gmt.genes)
}
ssGSEA <- function(gmtfile=gmtfile,fileranks=fileranks,Ptype=Ptype,pval_cutoff=pval_cutoff){
gmt <- read.gmt(gmtfile)
ranks_df <- read.delim(fileranks)
names <- colnames(ranks_df)
ranks_df$geneID <- row.names(ranks_df)
ranks_df <- ranks_df %>% select(geneID, all_of(names))
ranks_df <- ranks_df[complete.cases(ranks_df), ]
ranks_ch <- colnames(ranks_df)[-1]
ranks_ch <- setNames(ranks_ch, ranks_ch)
#run fastGSEA
tmp_ranks <- lapply(ranks_ch, function(rankname){
tmpdf <- ranks_df[,c('geneID', rankname)]
tmpdf <- tmpdf[complete.cases(tmpdf),]
tmpranks <- tmpdf[[rankname]]
names(tmpranks) <- tmpdf$geneID
fgseaRes <- fgsea(pathways = gmt, stats = tmpranks,
minSize = 15, maxSize = 2000, nperm = 1000)
fgseaRes <- as.data.frame(fgseaRes)
fgseaRes <- fgseaRes[,c('pathway', 'pval', 'padj', 'NES','size','leadingEdge')]
fgseaRes
})
# Remove ranks without enrichment
tmp_ranks <- Filter(function(x) nrow(x) > 1, tmp_ranks)
# Write output NES
rm(df)
for(name_rank in names(tmp_ranks)){
rank_out <- tmp_ranks[[name_rank]]
xxx <- rank_out[,"NES"]
names(xxx) <- rank_out[,"pathway"]
df <- cbind(df,xxx)
colnames(df)[ncol(df)] <- name_rank
}
df <- df[,-1]
rank_nameout <- paste0('NES_', fileranks,sep="")
write.table(df, file = rank_nameout, sep = '\t', quote = F, row.names = T,col.names=NA)
# Write output AdjP
rm(df)
for(name_rank in names(tmp_ranks)){
rank_out <- tmp_ranks[[name_rank]]
xxx <- rank_out[,"padj"]
names(xxx) <- rank_out[,"pathway"]
df <- cbind(df,xxx)
colnames(df)[ncol(df)] <- name_rank
}
df <- df[,-1]
rank_nameout <- paste0('padj_', fileranks,sep="")
write.table(df, file = rank_nameout, sep = '\t', quote = F, row.names = T,col.names=NA)
# Write output pval
rm(df)
for(name_rank in names(tmp_ranks)){
rank_out <- tmp_ranks[[name_rank]]
xxx <- rank_out[,"pval"]
names(xxx) <- rank_out[,"pathway"]
df <- cbind(df,xxx)
colnames(df)[ncol(df)] <- name_rank
}
df <- df[,-1]
rank_nameout <- paste0('pval_', fileranks,sep="")
write.table(df, file = rank_nameout, sep = '\t', quote = F, row.names = T,col.names=NA)
# Write output Leading Edge genes
rm(df)
for(name_rank in names(tmp_ranks)){
rank_out <- tmp_ranks[[name_rank]]
xxx <- rank_out[,"leadingEdge"]
names(xxx) <- rank_out[,"pathway"]
df <- cbind(df,xxx)
colnames(df)[ncol(df)] <- name_rank
}
df <- df[,-1]
rank_nameout <- paste0('LE_', fileranks,sep="")
write.table(df, file = rank_nameout, sep = '\t', quote = F, row.names = T,col.names=NA)
nes <- data.table::fread(paste0('NES_', fileranks,sep=""))
colnames(nes)[1] <- "pathway"
pval <- data.table::fread(paste0(Ptype,'_', fileranks,sep=""))
colnames(pval)[1] <- "pathway"
nes_melt <- nes %>%
gather(sample, nes, -pathway)
pval_melt <- pval %>%
gather(sample, pval, -pathway)
result <- full_join(nes_melt, pval_melt, by=c("pathway", "sample")) %>%
filter(pval <= pval_cutoff) %>%
select(pathway, sample, nes) %>%
spread(sample, nes)
rank_nameout <- paste0('NES_',Ptype,pval_cutoff,"_", fileranks,sep="")
write.table(result, file = rank_nameout, sep = '\t', quote = F, row.names = T,col.names=NA)
}
# ------------------- End of function block ------------------- #
# ----------- Prepare input for GSEA ------------------- #
system("mkdir data/GSEA")
# Preparing Rho table to be used as a rank
CortableB <- read.delim("data/correlation/spearman_table_rlogViralLoadB.tsv")
CortableB$mean <- apply(X =CortableB[,1:2], 1, mean )
CortableB <- CortableB %>% arrange(mean)
CortableB <- CortableB %>% select(b_v2_logeidml.Spearman_R, b_v7_logeidml.Spearman_R)
write.table(CortableB, "data/GSEA/B_Rho_RlogViralLoad.tsv", sep = "\t", quote = F)
CortableH1 <- read.delim("data/correlation/spearman_table_rlogViralLoadH1.tsv")
CortableH1$mean <- apply(X =CortableH1[,1:2], 1, mean )
CortableH1 <- CortableH1 %>% arrange(mean)
CortableH1 <- CortableH1 %>% select(h1_v2_logeidml.Spearman_R, h1_v7_logeidml.Spearman_R)
write.table(CortableH1, "data/GSEA/H1_Rho_RlogViralLoad.tsv",sep = "\t", quote = F)
CortableH3 <- read.delim("data/correlation/spearman_table_rlogViralLoadH3.tsv")
CortableH3$mean <- apply(X =CortableH3[,1:2], 1, mean )
CortableH3 <- CortableH3 %>% arrange(mean)
CortableH3 <- CortableH3 %>% select(h3_v2_logeidml.Spearman_R, h3_v7_logeidml.Spearman_R)
write.table(CortableH3, "data/GSEA/H3_Rho_RlogViralLoad.tsv",sep = "\t", quote = F)
setwd("data/GSEA/")
system("ln -s ../raw/ReactomePathwaysLevel3.gmt")
# ----------- Reading rank data - Reapeat the the GSEA function for each rank ------------------- #
gmtfile <- "ReactomePathwaysLevel3.gmt"
Ptype <- "padj"
pval_cutoff <- 0.25
toremove <- setdiff(ls(), c("ssGSEA", "read.gmt", "gmtfile", "Ptype", "pval_cutoff"))
rm(list = toremove)
fileranks <- "H3_Rho_RlogViralLoad.tsv"
#Run ssGSEA
ssGSEA(gmtfile = gmtfile,
fileranks = fileranks,
Ptype = Ptype,
pval_cutoff = pval_cutoff)
toremove <- setdiff(ls(), c("ssGSEA", "read.gmt", "gmtfile", "Ptype", "pval_cutoff"))
rm(list = toremove)
fileranks <- "B_Rho_RlogViralLoad.tsv"
ssGSEA(gmtfile = gmtfile,
fileranks = fileranks,
Ptype = Ptype,
pval_cutoff = pval_cutoff)
toremove <- setdiff(ls(), c("ssGSEA", "read.gmt", "gmtfile", "Ptype", "pval_cutoff"))
rm(list = toremove)
fileranks <- "H1_Rho_RlogViralLoad.tsv"
ssGSEA(gmtfile = gmtfile,
fileranks = fileranks,
Ptype = Ptype,
pval_cutoff = pval_cutoff)
# output files were inspected manually and pathways selected to produced file is "Pathways_to_display.tsv"
# The file Pathways_to_display.tsv is included in "data/raw"
setwd("../../")
rm(list = ls())
# ----------- create Corplots using GSEA curated output ------------------- #
selPathways <- read.delim("data/raw/Pathways_to_display.tsv")
selPathways <- selPathways$Reactome.pathway
#selPathways <- c("Nucleotide-binding domain, leucine rich repeat containing receptor NLR signaling pathways", selPathways)
# GSEA p values and NES has changes since last version - using 2020 tables to recreate exactly values from 2020
B_NESpval <- read.delim("data/GSEA/padj_B_Rho_RlogViralLoad.tsv")
B_NES <- read.delim("data/GSEA/NES_B_Rho_RlogViralLoad.tsv")
H1_NESpval <- read.delim("data/GSEA/padj_H1_Rho_RlogViralLoad.tsv")
H1_NES <- read.delim("data/GSEA/NES_H1_Rho_RlogViralLoad.tsv")
H3_NESpval <- read.delim("data/GSEA/padj_H3_Rho_RlogViralLoad.tsv")
H3_NES <- read.delim("data/GSEA/NES_H3_Rho_RlogViralLoad.tsv")
NES <- full_join(H1_NES, H3_NES, by="X" )
NES <- full_join(NES, B_NES, by="X" )
row.names(NES) <- NES$X
Padj <- full_join(H1_NESpval, H3_NESpval, by="X" )
Padj <- full_join(Padj, B_NESpval, by="X" )
row.names(Padj) <- Padj$X
all(row.names(Padj) %in% row.names(NES))
all(row.names(Padj) == row.names(NES) )
library(reshape2)
NES_melted <- melt(NES, id.vars = "X")
NES_melted$ID <- paste0(NES_melted$X, "_", NES_melted$variable)
Padj_melted <- melt(Padj, id.vars = "X")
Padj_melted$ID <- paste0(Padj_melted$X, "_", Padj_melted$variable)
colnames(Padj_melted)[3] <- "padj"
combined <- full_join(NES_melted,Padj_melted[,c(3,4)], by="ID")
combined <- combined %>% filter(X %in% selPathways )
colnames(combined)
# Checking the max and min P adjusted values to set the size scale
min(-log10(combined$padj))
max(-log10(combined$padj))
head(combined)
# Plot figure 2A
pdf("data/GSEA/dotplot_GSEA_RhoRank_rlogViralLoad_selectedPathways.pdf")
ggplot(data = combined, aes(x = variable, y = X, color = value, size = -log10(padj))) +
geom_point() +
theme_bw() +
theme(text = element_text(size = 8)) +
scale_color_gradient2(midpoint=0, low="blue3", mid="white", high="red3",breaks=c(-2,0,2)) +
scale_size_continuous(range = c(1, 10), breaks = c(0.2,1,1.8))
dev.off()
# Leading edge genes for each pathway and strain are save as "LE_STRAIN_Rho_RlogViralLoad.tsv"
# LE tables has has unwanted line breaks in few cases before "(" "," and "Double quotes" which need to be fixed before proceed.
system("./Processing_LE-tables.sh")
# ----------- get LE_genes from selected pathways ------------------- #
LEtable_h1 <- read.delim("data/GSEA/LE_H1_Rho_RlogViralLoad.tsv")
LEtable_h1 <- LEtable_h1 %>% filter(X %in% selPathways ) %>% select(X, h1_v2_logeidml.Spearman_R, h1_v7_logeidml.Spearman_R)
write.table(LEtable_h1, "data/GSEA/LE_H1_Rho_RlogViralLoad_selectedPathways.tsv", sep = "\t", quote = F, row.names = F)
LEtable_h3 <- read.delim("data/GSEA/LE_H3_Rho_RlogViralLoad.tsv")
LEtable_h3 <- LEtable_h3 %>% filter(X %in% selPathways ) %>% select(X, h3_v2_logeidml.Spearman_R, h3_v7_logeidml.Spearman_R)
write.table(LEtable_h3, "data/GSEA/LE_H3_Rho_RlogViralLoad_selectedPathways.tsv", sep = "\t", quote = F, row.names = F)
LEtable_b <- read.delim("data/GSEA/LE_B_Rho_RlogViralLoad.tsv")
LEtable_b <- LEtable_b %>% filter(X %in% selPathways ) %>% select(X, b_v2_logeidml.Spearman_R, b_v7_logeidml.Spearman_R)
write.table(LEtable_b, "data/GSEA/LE_B_Rho_RlogViralLoad_selectedPathways.tsv", sep = "\t", quote = F, row.names = F)
LEtable <- full_join(LEtable_h1, LEtable_h3, by="X")
LEtable <- full_join(LEtable, LEtable_b, by="X")
write.table(LEtable, "data/GSEA/LE_AllStrains_Rho_RlogViralLoad_selectedPathways.tsv", sep = "\t", quote = F, row.names = F)