-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfgsea.R
166 lines (135 loc) · 4.82 KB
/
fgsea.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
# Script for pathway analysis of bulk RNAseq data
# Differential expression results required
# Run downloaded txt file through FilePreProcess.ipynb first to generate CSV
# Setwd
setwd('/Users/jamesboot/Documents/9.Genome Centre Files/GC-MI-10822/Pathway_Analysis/')
# Load packages
library(tidyverse)
library(ggplot2)
library(fgsea)
library(multienrichjam)
library(enrichplot)
# Read in DE results generated from the python script
inFiles <- list.files(path = '.',
pattern = '.csv')
# Contains 3 sets of results
resList <- lapply(inFiles, function(x) {
read.csv(x)
})
# Name the list elemments by comparison name
names(resList) <- c('CVID_v_CVIDAutoimmunity',
'CVID_v_NonCVID',
'CVID_v_Healthy')
# Change colnames of each list element
newColNames <- c(
'RowNum',
'GeneID',
'GeneName',
"Pvalue",
"FDR",
"Ratio",
"FoldChange",
"LSMeanGroup1",
"LSMeanGroup2"
)
resList <- lapply(resList, setNames, newColNames)
# Load pathways
pathwayFiles <- list.files(path = '.', pattern = '.gmt.txt')
pathways <- lapply(pathwayFiles, function(x) {
gmtPathways(x)
})
names(pathways) <- c('Curated', 'GO_BP', 'Hallmark')
# Create folder for results to go in
if (!dir.exists('Results')) {
dir.create('Results')
}
# Now loop through comparisons and perform analysis
for (x in names(resList)) {
# Log
message(paste('Starting analysis for comparison:', x))
# Use unfiltered results
# Sort result by fold change (desc)
# Get order
order <- order(resList[[x]]$FoldChange,
decreasing = T)
# Set order
resList[[x]] <- resList[[x]][order, ]
# Create list of ranks
ranks <- resList[[x]]$FoldChange
names(ranks) <- resList[[x]]$GeneName
ranks <- na.omit(ranks)
# Nested for loop to run GSEA on all comparisons and all pathway sets and save results
for (pathwaySet in names(pathways)) {
# Log
message(paste('Running analysis for pathway set:', pathwaySet))
# Run GSEA
fgseaRes <- fgsea(
pathways = pathways[[pathwaySet]],
stats = ranks,
minSize = 15,
maxSize = 500
)
# See top results: sort by pval
ResOrder <- fgseaRes[order(pval),]
# Make new column to unlist gene names into
ResOrder$LeadingEdge <- NA
# For loop to go through and unlist each element and add as new element in col
for (i in 1:nrow(ResOrder)) {
listElement <- ResOrder$leadingEdge[[i]]
test <- paste(listElement, collapse = ', ')
ResOrder$LeadingEdge[i] <- test
}
# Log and save
message(paste('Saving result for:', pathwaySet))
write.csv(ResOrder[, c(1:7, 9)],
file = paste0('Results/', x, '_', pathwaySet, '.csv'))
ResOrder$leadingEdge <- NULL
ResOrder <- as.data.frame(ResOrder)
# VISUALISATION
# Ensure order of pathway is determined by padj
ResOrder$pathway <- factor(ResOrder$pathway,
levels = ResOrder$pathway[order(ResOrder$padj, decreasing = TRUE)])
# Bar plot
p <- ggplot(data = ResOrder[c(1:20),], aes(x = pathway, y = size, fill = padj)) +
geom_bar(stat = "identity") +
theme(axis.text = element_text(size = 6)) +
scale_fill_continuous(low="blue", high="red") +
coord_flip()
ggsave(p,
filename = paste0('Results/', x, '_', pathwaySet, '_bar.tiff'),
height = 8,
width = 8,
dpi = 300,
units = 'in')
# Add list of pathway genes to res
# Add true pathway size, fgsea size is a filtered size
ResOrder$pathwayGenes <- NA
ResOrder$TrueSize <- NA
for (i in 1:length(ResOrder$pathway)){
ResOrder$pathwayGenes[i] <- paste(pathways[[pathwaySet]][[as.character(ResOrder$pathway[i])]], collapse = ',')
ResOrder$TrueSize[i] <- length(pathways[[pathwaySet]][[as.character(ResOrder$pathway[i])]])
}
# Convert dataframe to enrichment result
enrichRes <- enrichDF2enrichResult(ResOrder,
pvalueCutoff = 1,
pAdjustMethod = 'none',
keyColname = 'pathway',
pathGenes = 'TrueSize',
geneColname = 'pathwayGenes',
geneDelim = ',',
pvalueColname = 'padj')
# Create similarity matrix
enrichRes2 <- pairwise_termsim(enrichRes)
# Enrichment map
map1 <- emapplot(enrichRes2,
cex.params = list(category_label = 0.7))
ggsave(map1,
filename = paste0('Results/', x, '_', pathwaySet, '_map.tiff'),
height = 16,
width = 16,
dpi = 300,
units = 'in')
}
# Log
message(paste('Finished analysis for:', x))
}