-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_functionalAnnotation_fgsea2.R
545 lines (493 loc) · 20.5 KB
/
04_functionalAnnotation_fgsea2.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
library(fgsea)
library(BiocParallel)
library(fastmatch)
library(stats)
library(ggplot2)
library(gridExtra)
library(grid)
library(data.table)
#' list matching
#'
#' returns a vector of the positions of all matches of a list
#' it is used by fgsea2
#' @param x the values to be matched
#' @param list a list or vector to be matched
#' @import fastmatch
#' @examples
#' listmatch(c("x", "z"), letters)
#' listmatch(c("x", "z"), list(letters[1:10], letters))
listmatch <- function(x, list) {
which(sapply(list, function(xx) any(xx %fin% x)))
}
#' Runs preranked gene set enrichment analysis on variables linked to multiple IDs.
#'
#' The function is modified from function \code{fgsea} in package "fgsea",
#' but a new argument "statsAnnot" is
#' added to support running GSEA on variables linked to multiple IDs, such as proteomics or
#' PTM data.
#' @param pathways List of gene sets to check.
#' @param stats Named vector of gene-level stats. Names should be the same as in 'pathways'
#' @param nperm Number of permutations to do. Minimial possible nominal p-value is about 1/nperm
#' @param statsAnnot a list has the same length as stats. If this argument is given the IDs in this list
#' will be mapped to pathways, so one gene name in pathways could be mapped to multiple
#' variables in stats.
#' @param minSize Minimal size of a gene set to test. All pathways below the threshold are excluded.
#' @param maxSize Maximal size of a gene set to test. All pathways above the threshold are excluded.
#' @param nproc If not equal to zero sets BPPARAM to use nproc workers (default = 0).
#' @param gseaParam GSEA parameter value, all gene-level statis are raised to the power of `gseaParam`
#' before calculation of GSEA enrichment scores.
#' @param BPPARAM Parallelization parameter used in bplapply.
#' Can be used to specify cluster to run. If not initialized explicitly or
#' by setting `nproc` default value `bpparam()` is used.
#' @return A table with GSEA results. Each row corresponds to a tested pathway.
#' The columns are the following:
#' \itemize{
#' \item pathway -- name of the pathway as in `names(pathway)`;
#' \item pval -- an enrichment p-value;
#' \item padj -- a BH-adjusted p-value;
#' \item ES -- enrichment score, same as in Broad GSEA implementation;
#' \item NES -- enrichment score normalized to mean enrichment of random samples of the same size;
#' \item nMoreExtreme` -- a number of times a random gene set had a more
#' extreme enrichment score value;
#' \item size -- size of the pathway after removing genes not present in `names(stats)`.
#' \item leadingEdge -- vector with indexes of leading edge genes that drive the enrichment, see \url{http://software.broadinstitute.org/gsea/doc/GSEAUserGuideTEXT.htm#_Running_a_Leading}.
#' }
#'
#' @export
#' @import data.table
#' @import BiocParallel
#' @import fastmatch
#' @import stats
#' @import fgsea
#' @examples
#' ############ example in fgsea package ###############
#' data(examplePathways)
#' data(exampleRanks)
#'
#' fgseaRes2 <- fgsea2(examplePathways[1:10],
#' exampleRanks,
#' statsAnnot = names(exampleRanks),
#' nperm=1000, maxSize=500)
#'
#' plotEnrichment(examplePathways[["5991130_Programmed_Cell_Death"]], exampleRanks) +
#' labs(title="Programmed Cell Death")
#'
#' plotEnrichment2(examplePathways[["5991130_Programmed_Cell_Death"]], exampleRanks,
#' statsAnnot = names(exampleRanks)) +
#' labs(title="Programmed Cell Death")
#'
#' plotGseaTable2(pathways = examplePathways[1:10], stats = exampleRanks,
#' statsAnnot=names(exampleRanks), fgseaRes = fgseaRes2)
#'
#'
#'
#' ############ example simulated data ###############
#' stats <- 1:20 + 0.5
#' names(stats) <- letters[1:20]
#' stannot <- strsplit(names(stats), "")
#' stannot[1:5] <- lapply(stannot[1:5], c, "a")
#' stannot[16:20] <- lapply(stannot[16:20], c, "t")
#' gs <- list(top = c("a"), # positive control - ES > 0
#' bottom = c("t"), # positive control - ES < 0
#' r1 = "q", # negative control
#' r2 = "h" # negative control
#' )
#' res <- fgsea2(gs, stats, statsAnnot = stannot, nperm=10000, minSize = 1)
#'
#' plotEnrichment2(pathway = gs[["top"]], stats = stats, statsAnnot = stannot)
#' plotEnrichment2(pathway = gs[["bottom"]], stats = stats, statsAnnot = stannot)
#' plotEnrichment2(pathway = gs[["r1"]], stats = stats, statsAnnot = stannot)
#' plotEnrichment2(pathway = gs[["r2"]], stats = stats, statsAnnot = stannot)
#'
#' plotGseaTable2(pathways = gs, stats = stats, statsAnnot=stannot, fgseaRes = res)
fgsea2 <- function(pathways, stats, nperm,
statsAnnot=NULL,
minSize=1, maxSize=Inf,
nproc=0,
gseaParam=1,
BPPARAM=NULL) {
# check and stop
if (is.null(names(stats)))
stop("stats has to be a named vector")
i <- is.na(stats)
if (any(i)) {
message(paste(sum(i), "NA values were excluded from stats!"))
stats <- stats[!i]
statsAnnot <- statsAnnot[!i]
}
# Warning message for ties in stats
ties <- sum(duplicated(stats[stats != 0]))
if (ties != 0) {
warning("There are ties in the preranked stats (",
paste(round(ties * 100 / length(stats), digits = 2)),
"% of the list).\n",
"The order of those tied genes will be arbitrary, which may produce unexpected results.")
}
# Warning message for duplicate gene names
if (any(duplicated(names(stats)))) {
warning("There are duplicate gene names, fgsea may produce unexpected results")
}
granularity <- 1000
permPerProc <- rep(granularity, floor(nperm / granularity))
if (nperm - sum(permPerProc) > 0) {
permPerProc <- c(permPerProc, nperm - sum(permPerProc))
}
seeds <- sample.int(10^9, length(permPerProc))
if (is.null(BPPARAM)) {
if (nproc != 0) {
if (.Platform$OS.type == "windows") {
# windows doesn't support multicore, using snow instead
BPPARAM <- SnowParam(workers = nproc)
} else {
BPPARAM <- MulticoreParam(workers = nproc)
}
} else {
BPPARAM <- bpparam()
}
}
minSize <- max(minSize, 1)
### ===============================================
if (is.null(statsAnnot))
statsAnnot <- names(stats)
# stats <- sort(stats, decreasing = TRUE)
# stats <- abs(stats)^gseaParam
sord <- order(stats, decreasing = TRUE)
stats <- stats[sord]
stats <- abs(stats)^gseaParam
statsAnnot <- statsAnnot[sord]
# pathwaysFiltered <- lapply(pathways, function(p) {
# as.vector(na.omit(fmatch(p, names(stats))))
# })
pathwaysFiltered <- lapply(pathways, function(p) {
as.vector(listmatch(p, statsAnnot))
})
### ===============================================
pathwaysSizes <- sapply(pathwaysFiltered, length)
toKeep <- which(minSize <= pathwaysSizes & pathwaysSizes <= maxSize)
m <- length(toKeep)
if (m == 0) {
return(data.table(pathway=character(),
pval=numeric(),
padj=numeric(),
ES=numeric(),
NES=numeric(),
nMoreExtreme=numeric(),
size=integer(),
leadingEdge=list()))
}
pathwaysFiltered <- pathwaysFiltered[toKeep]
pathwaysSizes <- pathwaysSizes[toKeep]
K <- max(pathwaysSizes)
gseaStatRes <- do.call(rbind,
lapply(pathwaysFiltered, calcGseaStat,
stats=stats,
returnLeadingEdge=TRUE))
leadingEdges <- mapply("[", list(names(stats)), gseaStatRes[, "leadingEdge"], SIMPLIFY = FALSE)
pathwayScores <- unlist(gseaStatRes[, "res"])
universe <- seq_along(stats)
counts <- bplapply(seq_along(permPerProc), function(i) {
nperm1 <- permPerProc[i]
leEs <- rep(0, m)
geEs <- rep(0, m)
leZero <- rep(0, m)
geZero <- rep(0, m)
leZeroSum <- rep(0, m)
geZeroSum <- rep(0, m)
if (m == 1) {
for (i in seq_len(nperm1)) {
randSample <- sample.int(length(universe), K)
randEsP <- calcGseaStat(
stats = stats,
selectedStats = randSample,
gseaParam = 1)
leEs <- leEs + (randEsP <= pathwayScores)
geEs <- geEs + (randEsP >= pathwayScores)
leZero <- leZero + (randEsP <= 0)
geZero <- geZero + (randEsP >= 0)
leZeroSum <- leZeroSum + pmin(randEsP, 0)
geZeroSum <- geZeroSum + pmax(randEsP, 0)
}
} else {
aux <- fgsea:::calcGseaStatCumulativeBatch(
stats = stats,
gseaParam = 1,
pathwayScores = pathwayScores,
pathwaysSizes = pathwaysSizes,
iterations = nperm1,
seed = seeds[i])
leEs = get("leEs", aux)
geEs = get("geEs", aux)
leZero = get("leZero", aux)
geZero = get("geZero", aux)
leZeroSum = get("leZeroSum", aux)
geZeroSum = get("geZeroSum", aux)
}
data.table(pathway=seq_len(m),
leEs=leEs, geEs=geEs,
leZero=leZero, geZero=geZero,
leZeroSum=leZeroSum, geZeroSum=geZeroSum
)
}, BPPARAM=BPPARAM)
counts <- rbindlist(counts)
# Getting rid of check NOTEs
leEs=leZero=geEs=geZero=leZeroSum=geZeroSum=NULL
pathway=padj=pval=ES=NES=geZeroMean=leZeroMean=NULL
nMoreExtreme=nGeEs=nLeEs=size=NULL
leadingEdge=NULL
.="damn notes"
pvals <- counts[,
list(pval=min((1+sum(leEs)) / (1 + sum(leZero)),
(1+sum(geEs)) / (1 + sum(geZero))),
leZeroMean = sum(leZeroSum) / sum(leZero),
geZeroMean = sum(geZeroSum) / sum(geZero),
nLeEs=sum(leEs),
nGeEs=sum(geEs)
)
,
by=.(pathway)]
pvals[, padj := p.adjust(pval, method="BH")]
pvals[, ES := pathwayScores[pathway]]
pvals[, NES := ES / ifelse(ES > 0, geZeroMean, abs(leZeroMean))]
pvals[, leZeroMean := NULL]
pvals[, geZeroMean := NULL]
pvals[, nMoreExtreme := ifelse(ES > 0, nGeEs, nLeEs)]
pvals[, nLeEs := NULL]
pvals[, nGeEs := NULL]
pvals[, size := pathwaysSizes[pathway]]
pvals[, pathway := names(pathwaysFiltered)[pathway]]
pvals[, leadingEdge := .(leadingEdges)]
# Makes pvals object printable immediatly
pvals <- pvals[]
pvals$leadingEdge <- sapply(pvals$leadingEdge, paste, collapse = ";")
pvals
}
#' Plots GSEA enrichment plot for function fgsea2.
#' @param pathway Gene set to plot.
#' @param stats Gene-level statistics.
#' @param statsAnnot a list has the same length as stats. If this argument is given the IDs in this list
#' will be mapped to pathways, so one gene name in pathways could be mapped to multiple
#' variables in stats.
#' @param gseaParam GSEA parameter.
#' @param ticksSize width of vertical line corresponding to a gene (default: 0.2)
#' @return ggplot object with the enrichment plot.
#' @export
#' @examples
#' ############ example in fgsea package ###############
#' data(examplePathways)
#' data(exampleRanks)
#'
#' fgseaRes2 <- fgsea2(examplePathways[1:10],
#' exampleRanks,
#' statsAnnot = names(exampleRanks),
#' nperm=1000, maxSize=500)
#'
#' plotEnrichment(examplePathways[["5991130_Programmed_Cell_Death"]], exampleRanks) +
#' labs(title="Programmed Cell Death")
#'
#' plotEnrichment2(examplePathways[["5991130_Programmed_Cell_Death"]], exampleRanks,
#' statsAnnot = names(exampleRanks)) +
#' labs(title="Programmed Cell Death")
#'
#' plotGseaTable2(pathways = examplePathways[1:10], stats = exampleRanks,
#' statsAnnot=names(exampleRanks), fgseaRes = fgseaRes2)
#'
#'
#'
#' ############ example simulated data ###############
#' stats <- 1:20 + 0.5
#' names(stats) <- letters[1:20]
#' stannot <- strsplit(names(stats), "")
#' stannot[1:5] <- lapply(stannot[1:5], c, "a")
#' stannot[16:20] <- lapply(stannot[16:20], c, "t")
#' gs <- list(top = c("a"), # positive control - ES > 0
#' bottom = c("t"), # positive control - ES < 0
#' r1 = "q", # negative control
#' r2 = "h" # negative control
#' )
#' res <- fgsea2(gs, stats, statsAnnot = stannot, nperm=10000, minSize = 1)
#'
#' plotEnrichment2(pathway = gs[["top"]], stats = stats, statsAnnot = stannot)
#' plotEnrichment2(pathway = gs[["bottom"]], stats = stats, statsAnnot = stannot)
#' plotEnrichment2(pathway = gs[["r1"]], stats = stats, statsAnnot = stannot)
#' plotEnrichment2(pathway = gs[["r2"]], stats = stats, statsAnnot = stannot)
#'
#' plotGseaTable2(pathways = gs, stats = stats, statsAnnot=stannot, fgseaRes = res)
plotEnrichment2 <- function(pathway, stats, statsAnnot = NULL,
gseaParam=1,
ticksSize=0.2) {
rnk <- rank(-stats)
ord <- order(rnk)
statsAdj <- stats[ord]
statsAdj <- sign(statsAdj) * (abs(statsAdj) ^ gseaParam)
statsAdj <- statsAdj / max(abs(statsAdj))
### ===============================================
# pathway <- unname(as.vector(na.omit(match(pathway, names(statsAdj)))))
if (is.null(statsAnnot))
statsAnnot <- names(stats)
statsAnnot <- statsAnnot[ord]
pathway <- listmatch(pathway, statsAnnot)
### ===============================================
pathway <- sort(pathway)
gseaRes <- calcGseaStat(statsAdj, selectedStats = pathway,
returnAllExtremes = TRUE)
bottoms <- gseaRes$bottoms
tops <- gseaRes$tops
n <- length(statsAdj)
xs <- as.vector(rbind(pathway - 1, pathway))
ys <- as.vector(rbind(bottoms, tops))
toPlot <- data.frame(x=c(0, xs, n + 1), y=c(0, ys, 0))
diff <- (max(tops) - min(bottoms)) / 8
# Getting rid of NOTEs
x=y=NULL
g <- ggplot(toPlot, aes(x=x, y=y)) +
geom_point(color="green", size=0.1) +
geom_hline(yintercept=max(tops), colour="red", linetype="dashed") +
geom_hline(yintercept=min(bottoms), colour="red", linetype="dashed") +
geom_hline(yintercept=0, colour="black") +
geom_line(color="green") + theme_bw() +
geom_segment(data=data.frame(x=pathway),
mapping=aes(x=x, y=-diff/2,
xend=x, yend=diff/2),
size=ticksSize) +
theme(panel.border=element_blank(),
panel.grid.minor=element_blank()) +
labs(x="rank", y="enrichment score")
g
}
#' Plots table of enrichment graphs using ggplot and gridExtra for fgsea2.
#' @param pathways Pathways to plot table, as in `fgsea` function.
#' @param stats Gene-level stats, as in `fgsea` function.
#' @param fgseaRes Table with fgsea results.
#' @param statsAnnot a list has the same length as stats. If this argument is given the IDs in this list
#' will be mapped to pathways, so one gene name in pathways could be mapped to multiple
#' variables in stats.
#' @param gseaParam GSEA-like parameter. Adjusts displayed statistic values,
#' values closer to 0 flatten plots. Default = 1, value of 0.5 is a good
#' choice too.
#' @param colwidths Vector of five elements corresponding to column width for
#' grid.arrange. If column width is set to zero, the column is not drawn.
#' @return TableGrob object returned by grid.arrange.
#' @import ggplot2
#' @import gridExtra
#' @import grid
#' @export
#' @examples
#' @examples
#' ############ example in fgsea package ###############
#' data(examplePathways)
#' data(exampleRanks)
#'
#' fgseaRes2 <- fgsea2(examplePathways[1:10],
#' exampleRanks,
#' statsAnnot = names(exampleRanks),
#' nperm=1000, maxSize=500)
#'
#' plotEnrichment(examplePathways[["5991130_Programmed_Cell_Death"]], exampleRanks) +
#' labs(title="Programmed Cell Death")
#'
#' plotEnrichment2(examplePathways[["5991130_Programmed_Cell_Death"]], exampleRanks,
#' statsAnnot = names(exampleRanks)) +
#' labs(title="Programmed Cell Death")
#'
#' plotGseaTable2(pathways = examplePathways[1:10], stats = exampleRanks,
#' statsAnnot=names(exampleRanks), fgseaRes = fgseaRes2)
#'
#'
#'
#' ############ example simulated data ###############
#' stats <- 1:20 + 0.5
#' names(stats) <- letters[1:20]
#' stannot <- strsplit(names(stats), "")
#' stannot[1:5] <- lapply(stannot[1:5], c, "a")
#' stannot[16:20] <- lapply(stannot[16:20], c, "t")
#' gs <- list(top = c("a"), # positive control - ES > 0
#' bottom = c("t"), # positive control - ES < 0
#' r1 = "q", # negative control
#' r2 = "h" # negative control
#' )
#' res <- fgsea2(gs, stats, statsAnnot = stannot, nperm=10000, minSize = 1)
#'
#' plotEnrichment2(pathway = gs[["top"]], stats = stats, statsAnnot = stannot)
#' plotEnrichment2(pathway = gs[["bottom"]], stats = stats, statsAnnot = stannot)
#' plotEnrichment2(pathway = gs[["r1"]], stats = stats, statsAnnot = stannot)
#' plotEnrichment2(pathway = gs[["r2"]], stats = stats, statsAnnot = stannot)
#'
#' plotGseaTable2(pathways = gs, stats = stats, statsAnnot=stannot, fgseaRes = res)
plotGseaTable2 <- function(pathways, stats, fgseaRes,statsAnnot=NULL,
gseaParam=1,
colwidths=c(5, 3, 0.8, 1.2, 1.2)) {
rnk <- rank(-stats)
ord <- order(rnk)
statsAdj <- stats[ord]
statsAdj <- sign(statsAdj) * (abs(statsAdj) ^ gseaParam)
statsAdj <- statsAdj / max(abs(statsAdj))
### ===============================================
# pathways <- lapply(pathways, function(p) {
# unname(as.vector(na.omit(match(p, names(statsAdj)))))
# })
if (is.null(statsAnnot))
statsAnnot <- names(stats)
statsAnnot <- statsAnnot[ord]
pathways <- lapply(pathways, function(p) {
listmatch(p, statsAnnot)
})
### ===============================================
ps <- lapply(names(pathways), function(pn) {
p <- pathways[[pn]]
annotation <- fgseaRes[match(pn, fgseaRes$pathway), ]
list(
textGrob(pn, just="right", x=unit(0.95, "npc")),
ggplot() +
geom_segment(aes(x=p, xend=p,
y=0, yend=statsAdj[p]),
size=0.2) +
scale_x_continuous(limits=c(0, length(statsAdj)),
expand=c(0, 0)) +
scale_y_continuous(limits=c(-1, 1),
expand=c(0, 0)) +
xlab(NULL) + ylab(NULL) +
theme(panel.background = element_blank(),
axis.line=element_blank(),
axis.text=element_blank(),
axis.ticks=element_blank(),
panel.grid = element_blank(),
axis.title=element_blank(),
plot.margin = rep(unit(0,"null"),4),
panel.spacing = rep(unit(0,"null"),4)
),
textGrob(sprintf("%.2f", annotation$NES)),
textGrob(sprintf("%.1e", annotation$pval)),
textGrob(sprintf("%.1e", annotation$padj))
)
})
rankPlot <-
ggplot() +
geom_blank() +
scale_x_continuous(limits=c(0, length(statsAdj)),
expand=c(0, 0)) +
scale_y_continuous(limits=c(-1, 1),
expand=c(0, 0)) +
xlab(NULL) + ylab(NULL) +
theme(panel.background = element_blank(),
axis.line=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
panel.grid = element_blank(),
axis.title=element_blank(),
plot.margin = unit(c(0,0,0.5,0), "npc"),
panel.spacing = unit(c(0,0,0,0), "npc")
)
grobs <- c(
lapply(c("Pathway", "Gene ranks", "NES", "pval", "padj"), textGrob),
unlist(ps, recursive = FALSE),
list(nullGrob(),
rankPlot,
nullGrob(),
nullGrob(),
nullGrob()))
# not drawing column if corresponding colwidth is set to zero
grobsToDraw <- rep(colwidths != 0, length(grobs)/length(colwidths))
grid.arrange(grobs=grobs[grobsToDraw],
ncol=sum(colwidths != 0),
widths=colwidths[colwidths != 0])
}