-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpostprocess_GSEA.R
341 lines (326 loc) · 11.9 KB
/
postprocess_GSEA.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# Script to postprocess GSEA results to keep only those gene sets below a given
# FDR q cutoff and to make the output compatible with Brainarray Entrez Gene
# probeset mapping (instead of Affymetrix probeset mapping)
#
# Adam Gower
arglist <- commandArgs(trailing=TRUE)
if (length(arglist) < 3) {
stop(
"Usage: postprocess_GSEA.R --args ",
"[run path] [description of comparison] [output filename] [FDR q cutoff]"
)
} else {
run.path <- arglist[1]
comparison <- arglist[2]
output.filename <- arglist[3]
q.cutoff <- as.numeric(arglist[4])
}
# If an FDR q cutoff is not provided, use 0.25
if (is.na(q.cutoff)) q.cutoff <- 0.25
cat("Working on run folder:", run.path)
cat("\n")
setwd(run.path)
# Set phenotype labels based on comparison
phenotypes <- list(
pos = paste("upregulated with respect to", comparison),
neg = paste("downregulated with respect to", comparison)
)
# Rename the "na_pos"/"na_neg" fields in the index.html file
cat("Editing index.html.\n")
report <- readLines("index.html")
for (direction in c("pos", "neg")) {
report <- sub(
paste0(
"na</b></h4><ul><li>",
"([0-9]+ / [0-9]+|None of the) gene sets are ",
"(upregulated|enriched) in phenotype <b>",
"na_", direction
),
paste0(
phenotypes[[direction]],
"</b></h4><ul><li>\\1 gene sets are <b>",
phenotypes[[direction]]
),
report
)
}
report <- sub(
"na_pos</b><i> versus </i><b>na_neg",
paste0(phenotypes$pos, "</b><i> versus </i><b>", phenotypes$neg),
report
)
write(report, "index.html")
for (direction in c("pos", "neg")) {
# Get name of '.xls' (TSV) file containing output for given direction
report_filename <- list.files(
pattern=paste("gsea_report_for_na", direction, "[0-9]+.xls", sep="_")
)
# Read TSV file into a data frame
report <- read.delim(
report_filename, stringsAsFactors=FALSE, check.names=FALSE, row.names=1
)
# Identify the rows (gene sets) that pass the FDR q cutoff
gene_sets_pass <- report[["FDR q-val"]] < q.cutoff
# Iterate over each gene set passing FDR q cutoff
for (gene.set in rownames(report)[gene_sets_pass]) {
# Edit Details .html file
filename <- paste0(gene.set, ".html")
cat("Editing file:", filename)
cat("\n")
html <- readLines(filename)
# Remove Phenotype row and rename "na_pos"/"na_neg" phenotype labels
html <- sub(
"<tr><td>Phenotype</td><td>NoPhenotypeAvailable</td></tr>", "", html
)
html <- sub(paste0("na_", direction), phenotypes[[direction]], html)
# Replace incorrect Affymetrix link with Entrez Gene link
# and remove unneeded 'Entrez' and 'Source' links
html <- gsub(
"https\\://www.affymetrix.com/LinkServlet\\?probeset=([0-9]+)",
"http://www.ncbi.nlm.nih.gov/gene?term=\\1[uid]",
html
)
html <- gsub(
"<br><a href='[^']+'>Entrez</a>,  <a href='[^']+'>Source</a>",
"",
html
)
# Remove " [Source:...]" suffix from end of GENE_TITLE column
html <- gsub(" \\[Source:[^]]+\\]", "", html)
# Reconstitute HTML and write back to file
write(paste(html, collapse="\n"), filename)
# Edit .xls (TSV) file containing GSEA details table
filename <- paste0(gene.set, ".xls")
cat("Editing file:", filename)
cat("\n")
xls <- readLines(filename)
# Remove " [Source:...]" suffix from end of GENE_TITLE column
xls <- gsub(" \\[Source: [^\\]]+\\]", "", xls)
# Write table back to file
write(paste(xls, collapse="\n"), filename)
}
# Remove the .html, .xls, and .png files that correspond to the gene sets
# that fail the FDR q cutoff
for (gene.set in rownames(report)[!gene_sets_pass]) {
cat("Removing Details files for gene set:", gene.set)
cat("\n")
unlink(paste0(gene.set, ".html"), force=TRUE)
unlink(paste0(gene.set, ".xls"), force=TRUE)
filename <- list.files(
pattern=paste("enplot", gene.set, "[0-9]+.png", sep="_")
)
if (length(filename) == 1) {
id <- as.integer(sub("^enplot_.+_([0-9]+).png$", "\\1", filename))
unlink(paste0("enplot_", gene.set, "_", id, ".png"), force=TRUE)
unlink(paste0("gset_rnd_es_dist_", id+1, ".png"), force=TRUE)
}
}
# Lightly parse the "snapshot" .html file to extract header, table elements,
# and footer, removing row delimiters
filename <- paste(direction, "snapshot.html", sep="_")
cat("Editing:", filename)
cat("\n")
snapshot <- readLines(filename)
snapshot[[2]] <- gsub("</?tr>", "", snapshot[[2]])
snapshot[2] <- strsplit(snapshot[2], "</?td>(<td>)?")
# Keep only table elements corresponding to gene sets that passed FDR q cutoff
i <- which(gene_sets_pass)
n <- sum(gene_sets_pass)
snapshot[[2]] <- snapshot[[2]][c(1, i+1, length(snapshot[[2]]))]
# Fix the title of the HTML file
snapshot[[2]][1] <-
sub(
"Snapshot of [0-9]+ enrichment plots",
paste("Snapshot of", n, "enrichment plots"),
snapshot[[2]][1]
)
# Reconstitute HTML and write back to file
snapshot[[2]][1+(1:n)] <- paste0("<td>", snapshot[[2]][1+(1:n)], "</td>")
snapshot[[2]] <- paste(
c(
snapshot[[2]][1],
na.omit(
c(
rbind(
"<tr>",
matrix(
c(snapshot[[2]][(1:n)+1], rep(NA, 3*ceiling(n/3)-n)),
nrow=3, ncol=ceiling(n/3)
),
"</tr>"
)
)
),
snapshot[[2]][n+2]
),
collapse=""
)
snapshot <- paste(snapshot, collapse="\n")
write(snapshot, filename)
# Lightly parse report .html file to extract header, table rows, and footer
filename <- list.files(
pattern=paste("gsea_report_for_na", direction, "[0-9]+.html", sep="_")
)
cat("Editing:", filename)
cat("\n")
html <- readLines(filename)
html[2] <- strsplit(html[2], "</?tr>(<tr>)?")
# Fix the title and caption of the HTML file
html[[2]][1] <- sub(
"Report for na_pos [0-9]+",
paste("Report for", phenotypes[[direction]]),
html[[2]][1]
)
html[[2]][length(html[[2]])] <- sub(
"phenotype <b>na<b>",
paste0("phenotype <b>", phenotypes[[direction]], "<b>"),
html[[2]][length(html[[2]])]
)
# Remove hyperlinks from table rows that failed FDR q cutoff
html[[2]][which(report[["FDR q-val"]] >= q.cutoff)+1] <- sub(
pattern = paste0(
"<td><a href='[^']+'>([^<]+)</a></td><td><a href='[^']+'>",
"Details ...",
"</a></td>"
),
replacement = "<td>\\1</td><td></td>",
x = html[[2]][which(report[["FDR q-val"]] >= q.cutoff)+1]
)
# Reconstitute HTML and write back to file
html[[2]][2:(length(html[[2]])-1)] <- paste0(
"<tr>", html[[2]][2:(length(html[[2]])-1)], "</tr>"
)
html[[2]] <- paste(html[[2]], collapse="")
html <- paste(html, collapse="\n")
write(html, filename)
}
# Combine '.xls' (TSV) output files into single TSV file for import to Excel
output <- NULL
for (direction in c("neg", "pos")) {
filename <- list.files(
pattern=paste("gsea_report_for_na", direction, "[0-9]+.xls", sep="_")
)
output <- rbind(
output, read.delim(filename, check.names=FALSE, stringsAsFactors=FALSE)
)
}
# Rename/remove columns
colnames(output)[colnames(output) == "NAME"] <- "Gene Set Name"
output[["GS<br> follow link to MSigDB"]] <- NULL
output[["GS DETAILS"]] <- NULL
colnames(output)[colnames(output) == "SIZE"] <- "Gene Set Size"
colnames(output)[colnames(output) == "ES"] <- "Enrichment Score (ES)"
colnames(output)[colnames(output) == "NES"] <-
"Normalized Enrichment Score (NES)"
colnames(output)[colnames(output) == "NOM p-val"] <- "Nominal p value"
colnames(output)[colnames(output) == "FDR q-val"] <- "FDR q value"
output[["FWER p-val"]] <- NULL
output[["RANK AT MAX"]] <- NULL
output[["LEADING EDGE"]] <- NULL
# Remove column with empty column name
output[colnames(output) == ""] <- list(NULL)
# Extract the list of gmt filenames from the .rpt file
rpt.filename <- list.files(pattern=".+.GseaPreranked.[0-9]+.rpt")
rpt <- readLines(rpt.filename)
params <- strsplit(rpt[grep("^param", rpt)], "\t")
params <- structure(sapply(params, "[", 3), names=sapply(params, "[", 2))
gmt.filenames <- strsplit(params["gmx"], ",")[[1]]
# Define human-readable labels for the various MSigDB collections
group.labels <- c(
"h.all" = "H Hallmark",
"c1.all" = "C1 Cytogenetic bands",
"c2.all" = "C2 Curated gene sets",
"c2.cgp" = "C2 Chemical/genetic perturbations",
"c2.cp" = "C2 Canonical pathways",
"c2.cp.biocarta" = "C2 BioCarta pathway",
"c2.cp.kegg" = "C2 KEGG pathway",
"c2.cp.kegg_legacy" = "C2 KEGG legacy pathway",
"c2.cp.kegg_medicus" = "C2 KEGG MEDICUS pathway",
"c2.cp.pid" = "C2 PID pathway",
"c2.cp.reactome" = "C2 Reactome pathway",
"c2.cp.wikipathways" = "C2 WikiPathways pathway",
"c3.all" = "C3 Regulatory targets",
"c3.mir" = "C3 microRNA motifs",
"c3.mir.mir_legacy" = "C3 Legacy microRNA motif",
"c3.mir.mirdb" = "C3 miRDB microRNA motif",
"c3.tft" = "C3 TF motifs",
"c3.tft.tft_legacy" = "C3 Legacy TF motif",
"c3.tft.gtrd" = "C3 GTRD TF motif",
"c4.all" = "C4 Computational gene sets",
"c4.3ca" = "C4 Curated Cancer Cell Atlas",
"c4.cgn" = "C4 Cancer gene neighborhoods",
"c4.cm" = "C4 Cancer modules",
"c5.all" = "C5 Ontology gene sets",
"c5.go" = "C5 Gene Ontology",
"c5.bp" = "C5 GO Biological Process",
"c5.go.bp" = "C5 GO Biological Process",
"c5.cc" = "C5 GO Cellular Component",
"c5.go.cc" = "C5 GO Cellular Component",
"c5.mf" = "C5 GO Molecular Function",
"c5.go.mf" = "C5 GO Molecular Function",
"c5.hpo" = "C5 Human Phenotype Ontology",
"c6.all" = "C6 Oncogenic signatures",
"c7.all" = "C7 Immunologic signatures",
"c7.immunesigdb" = "C7 ImmuneSigDB",
"c7.vax" = "C7 HIPC vaccine response",
"c8.all" = "C8 Cell type signatures",
"mh.all" = "MH Hallmark",
"m1.all" = "M1 Cytogenetic bands",
"m2.all" = "M2 Curated gene sets",
"m2.cgp" = "M2 Chemical/genetic perturbations",
"m2.cp" = "M2 Canonical pathways",
"m2.cp.biocarta" = "M2 BioCarta pathway",
"m2.cp.reactome" = "M2 Reactome pathway",
"m2.cp.wikipathways" = "M2 WikiPathways pathway",
"m3.all" = "M3 Regulatory targets",
"m3.mirdb" = "M3 miRDB microRNA motif",
"m3.gtrd" = "M3 GTRD TF motif",
"m5.all" = "M5 Ontology gene sets",
"m5.go" = "M5 Gene Ontology",
"m5.go.bp" = "M5 GO Biological Process",
"m5.go.cc" = "M5 GO Cellular Component",
"m5.go.mf" = "M5 GO Molecular Function",
"m5.mpt" = "M5 Mouse Phenotype Ontology MP Tumor",
"m8.all" = "M8 Cell type signatures"
)
# Add 'Group' column (MSigDB collection name) to beginning of output data frame
group.table <- NULL
for (filename in gmt.filenames) {
gene.set.names <- sapply(strsplit(readLines(filename), "\t"), "[", 1)
group.label <- group.labels[
sub("\\.v.+\\.entrez\\.gmt$", "", basename(filename))
]
group.table <- rbind(
group.table,
data.frame(
Group=group.label, Name=gene.set.names,
row.names=NULL, stringsAsFactors=FALSE
)
)
}
output <- cbind(
Group=group.table$Group[match(output[["Gene Set Name"]], group.table$Name)],
output
)
# Convert the 'Gene Set Name' column to an Excel HYPERLINK formula
output[["Gene Set Name"]] <- paste0(
"=HYPERLINK(",
dQuote(
file.path(
"http://www.broadinstitute.org/gsea/msigdb/cards",
paste0(output[["Gene Set Name"]], ".html")
)
),
",",
dQuote(output[["Gene Set Name"]]),
")"
)
# Reorder by NES (in descending order by default)
output <- output[
order(output[["Normalized Enrichment Score (NES)"]], decreasing=TRUE),
]
# Write output to a tab-delimited file
write.table(
output, output.filename,
quote=FALSE, sep="\t", row.names=FALSE, col.names=TRUE
)