-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkshop.rmd
633 lines (434 loc) · 14.9 KB
/
workshop.rmd
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
---
title: "ISCB 2022 Gene set enrichment workshop"
author: "January Weiner"
date: "`r Sys.Date()`"
output:
html_document:
code_folding: show
toc: no
bibliography: bibliography.bib
link-citations: true
---
```{r,echo=FALSE}
## Set default options for the knitr RMD processing
knitr::opts_chunk$set(warning=FALSE,message=FALSE,fig.width=5,fig.height=5,cache=TRUE,autodep=TRUE, results="hide")
```
```{r libraries,cache=FALSE}
library(tidyverse)
library(colorDF)
library(ggplot2)
library(ggbeeswarm)
library(tmod)
library(colorDF)
library(cowplot)
theme_set(theme_minimal())
```
# A simple data set
## Basic analysis
This is a data set that I used in my poster. It is available from GEO and
contains RNA-Seq counts for healthy individuals and COVID-19 patients, described by @mick2020upper.
The data set has been already processed and downloaded; however, below you
will find the code to replicate this procedure (if you want).
```{r pheno_data_download,eval=FALSE}
#| class.source: fold-hide
## this sometimes fails. The RDS of the object is provided
Sys.setenv(VROOM_CONNECTION_SIZE=8*131072)
## with getGEO, we can only get the phenoData
geo <- getGEO("GSE156063")[[1]]
saveRDS(geo, file="GSE156063.geo")
```
```{r pheno_data_cleanup,eval=FALSE}
#| class.source: fold-hide
geo <- readRDS("GSE156063.geo")
covar <- as(phenoData(geo), "data.frame")
## remove columns with only one value
boring <- map_lgl(covar, ~ length(unique(.x)) == 1)
covar <- covar[ , !boring ]
## clean up
covar <- covar %>%
dplyr::rename(gender = "gender:ch1") %>%
mutate(disease = gsub(" .*", "", .data[["disease state:ch1"]])) %>%
mutate(label = description) %>%
mutate(group = disease) %>%
arrange(description) %>%
dplyr::select(all_of(c("title", "label", "gender", "disease", "group")))
```
```{r featuredata_download,eval=FALSE}
#| class.source: fold-hide
## the counts must be downloaded from GEO separately.
if(!file.exists("GSE156063_swab_gene_counts.csv.gz")) {
download.file("https://ftp.ncbi.nlm.nih.gov/geo/series/GSE156nnn/GSE156063/suppl/GSE156063_swab_gene_counts.csv.gz",
"GSE156063_swab_gene_counts.csv.gz")
}
counts <- read_csv("GSE156063_swab_gene_counts.csv.gz")
.tmp <- counts[[1]]
counts <- as.matrix(counts[,-1])
rownames(counts) <- .tmp
#counts <- counts[ , covar$description ]
counts <- counts[ , covar$label ]
lcpm <- edgeR::cpm(counts, log=TRUE)
#stopifnot(all(colnames(counts) == covar$description))
stopifnot(all(colnames(counts) == covar$label))
annot <- data.frame(ENSEMBL = rownames(counts))
.tmp <- mapIds(org.Hs.eg.db, annot$ENSEMBL, column=c("ENTREZID"), keytype="ENSEMBL")
annot$ENTREZID <- .tmp[ match(annot$ENSEMBL, names(.tmp)) ]
.tmp <- mapIds(org.Hs.eg.db, annot$ENSEMBL, column=c("SYMBOL"), keytype="ENSEMBL")
annot$SYMBOL <- .tmp[ match(annot$ENSEMBL, names(.tmp)) ]
.tmp <- mapIds(org.Hs.eg.db, annot$ENSEMBL, column=c("GENENAME"), keytype="ENSEMBL")
annot$GENENAME <- .tmp[ match(annot$ENSEMBL, names(.tmp)) ]
```
```{r Preparation_of_the_covariates,cache=FALSE,eval=FALSE}
#| class.source: fold-hide
sel <- covar$group %in% c("no", "SC2")
counts <- counts[ , sel ]
covar <- covar[ sel, ]
covar$group <- as.character(covar$group)
covar$group.disease <- paste0(covar$group, '_', covar$disease)
rownames(covar) <- covar$label
saveRDS(covar, file="covar.rds")
saveRDS(counts, file="counts.rds")
```
```{r DESeq2,cache=FALSE,eval=FALSE}
#| class.source: fold-hide
## DESeq2 calculations
## manual cache, since the operation takes a long time
sel <- !is.na(covar$group)
ds2 <- DESeqDataSetFromMatrix(counts[,sel], colData=covar[sel, ], design=~ disease)
ds2_file <- "ds2_cached.rds"
if(!file.exists(ds2_file)) {
message("Running DESeq2")
ds2 <- DESeq(ds2)
saveRDS(ds2, file=ds2_file)
} else {
message("Reading ds2 from manual cache")
ds2 <- readRDS(ds2_file)
}
```
First, take a look at the structure of the data:
```{r results="markdown"}
covar <- readRDS("data/covar.rds") # covariates
summary_colorDF(covar)
```
The results table is stored under "data/disease_SC2_vs_no.tsv". In
`data/annotation.tsv`, there is the annotation of the genes in this data
set.
```{r}
dsres <- read_table("data/disease_SC2_vs_no.tsv")
annot <- read_table("data/annotation.tsv")
dsres <- merge(annot, dsres, by="ENSEMBL")
```
Basic CERNO analysis:
```{r basic_tmod,results="markdown"}
#| fig.width=6,
#| fig.height=12
gl <- dsres$SYMBOL[ order(dsres$pvalue) ]
tres <- tmodCERNOtest(gl)
print(tres)
```
Which we can quickly visualize:
```{r basic_panelplot}
#| fig.width=6,
#| fig.height=12
ggPanelplot(tres)
```
We can also add information about whether the genes go up or down. Note
that we need to put `tres` inside a list, so `ggPanelplot` can match tmod
results with the `sgenes` object.
```{r basic_panelplot2}
#| fig.width=6,
#| fig.height=12
sgenes <- tmodDecideTests(dsres$SYMBOL, lfc = dsres$log2FoldChange,
pval=dsres$pvalue)
names(sgenes) <- "SC2_vs_no"
tres <- list("SC2_vs_no"=tres)
ggPanelplot(tres, sgenes=sgenes)
```
## Using another gene sets
Another built-in gene set are the cell surface markers.
```{r cellsignatures}
#| fig.width=6,
#| fig.height=12
data(cell_signatures)
tres2 <- tmodCERNOtest(gl, mset=cell_signatures)
tres2 <- list("SC2_vs_no"=tres2)
sgenes2 <- tmodDecideTests(dsres$SYMBOL, lfc = dsres$log2FoldChange,
pval=dsres$pvalue, mset=cell_signatures)
names(sgenes2) <- "SC2_vs_no"
ggPanelplot(tres2, sgenes=sgenes2, mset=cell_signatures)
```
The mset parameter is used in most of the functions to use another gene
sets.
## Inspecting individual enrichments
Let us consider the gene set "LI.M68" – RIG-1 like receptor signalling.
First, show the genes in this gene set:
```{r results="markdown"}
data(tmod) # make the built-in gene sets visible
getModuleMembers("LI.M68", mset=tmod)
```
Evidence plot – a ROC curve:
```{r evidence plots}
#| fig.width=12,
#| fig.height=12
par(mfrow=c(2,2))
evidencePlot(gl, m = "DC.M1.2", gene.labels=TRUE)
evidencePlot(gl, m = "LI.M68", gene.labels=TRUE)
evidencePlot(gl, m = "LI.M165")
evidencePlot(gl, m = "LI.M4.0")
```
## Eigengenes
The basic idea is as follos: run PCA only on a selected group of genes, use
PC1 instead of average gene expression of that gene set (mind the signs!).
Below, `counts.rds` contains the raw RNA-seq counts, which I first convert
to log~2~ counts per million.
```{r eigengenes_manual}
data(tmod)
counts <- readRDS("data/counts.rds")
lcpm <- edgeR::cpm(counts, log=TRUE)
mm <- getModuleMembers("LI.M68", mset=tmod)[[1]]
sel <- annot$SYMBOL %in% mm
covar$eig <- prcomp(t(lcpm[ sel, ]), scale.=TRUE)$x[,1]
```
We can use eigengenes to nicely plot the results:
```{r eigengenes}
ggplot(covar, aes(x=disease, y=eig)) + geom_boxplot(outlier.shape = NA) +
geom_beeswarm(cex=3)
```
(However, mind the sign!)
In tmod, eigengenes can be computed directly. Tmod makes sure that the
eigengene correlates positively with the majority of the genes in the gene
set and reverses the sign when necessary:
```{r eigengenes4}
#| fig.width=10,
#| fig.height=10
selm <- tres[[1]]$ID[1:4]
eig <- eigengene(lcpm, annot$SYMBOL)
eig <- as.data.frame(t(eig)) %>%
select(all_of(selm)) %>%
rownames_to_column("label") %>%
inner_join(covar, by="label")
plots <- map(selm, ~ ggplot(eig, aes_string(x="disease", y=.x)) +
geom_boxplot(outlier.shape=NA) +
geom_beeswarm(cex=3) +
ggtitle(tres[[1]]$Title[ match(.x, tres[[1]]$ID) ]))
plot_grid(plotlist=plots)
```
# Combining GSE with other tools
## Combining gene set enrichments with PCA
Using a method like tmodCERNOtest, tmodUtest or fsgsea makes it possible to
apply gene set enrichment to any single ranked list of genes.
(I have selected the components based on correlation with `covar$disease`)
```{r pca1}
#| fig.width=6,
#| fig.height=6
pca <- prcomp(t(lcpm), scale.=TRUE)
pca_df <- pca$x %>% as.data.frame %>%
rownames_to_column("label") %>% merge(covar)
ggplot(pca_df, aes(x=PC5, y=PC6, color=disease)) + geom_point()
```
We can sort all the genes by their decreasing absolute loadings in the pca object.
```{r}
#| fig.width=12,
#| fig.height=8
pcares <- map(1:10, ~ {
gl <- annot$SYMBOL[ order(-abs(pca$rotation[,.x])) ]
tmodCERNOtest(gl)
})
names(pcares) <- colnames(pca$rotation)[1:10]
sgenes <- tmodDecideTests(annot$SYMBOL,
lfc=pca$rotation[, 1:10],
pval=NULL,
lfc.thr = 0)
ggPanelplot(pcares, sgenes=sgenes, filter_row_auc = .85)
```
```{r}
ggplot(pca_df, aes(x=PC2, y=PC3, color=disease)) + geom_point()
```
Check the function `tmod_pca` for more details.
## Machine learning
```{r}
library(randomForest)
vars <- apply(lcpm, 1, var)
sel <- vars > quantile(vars, .75)
rf <- randomForest(t(lcpm[sel, ]), factor(covar$disease), importance=TRUE)
print(rf)
```
```{r}
if(require(myfuncs)) {
rocplot(rf2pr(rf), confmat = F)
}
```
We can run gene set enrichment analysis on the genes ordered by variable
importance.
```{r}
rf$importance %>% as.data.frame() %>%
rownames_to_column("ENSEMBL") %>%
merge(annot) %>% arrange(-MeanDecreaseAccuracy) %>%
pull(SYMBOL) %>% tmodCERNOtest()
```
## Correlation analysis
Let us consider the gene ANKRD22. Based on coexpression, what function is
it related to? We will first remove the disease effect so as we are
clustering only by variations in expression within the groups, and not by
changes between the groups.
```{r results="markdown"}
lcpm_c <- lm(t(lcpm) ~ disease, data=covar)$residuals
lcpm_c <- t(lcpm_c)
ankrd22_i <- which(annot$SYMBOL == "ANKRD22")
cor_ank <- cor(lcpm_c[ankrd22_i, ], t(lcpm_c)) %>% t() %>%
as.data.frame() %>% rownames_to_column("ENSEMBL") %>%
merge(annot)
tcor <- cor_ank %>% arrange(-abs(V1)) %>% pull(SYMBOL) %>%
tmodCERNOtest() %>% filter(N1 < 30 & adj.P.Val < 1e-3) %>%
arrange(-AUC)
print(tcor)
```
# Using gene sets from msigdbr
Converting the data frame from the msigdbr package for use with tmod is straightforward:
```{r}
library(msigdbr)
msig_df <- msigdbr("Homo sapiens")
goset <- msig_df %>%
filter(gs_subcat == "GO:BP") %>%
makeTmodFromDataFrame(module_col="gs_name",
title_col="gs_name",
feature_col="gene_symbol")
```
```{r results="markdown"}
library(DT)
tmodCERNOtest(gl, mset=goset, qval = 1e-3) %>%
filter(N1 > 5) %>%
datatable() %>%
formatSignif(c("adj.P.Val", "P.Value")) %>%
formatRound(c("cES", "AUC"))
```
# Disentangling results
## Upset plots
```{r}
#| fig.width=14,
#| fig.height=8
tres[[1]] %>% filter(N1 < 20) %>% pull(ID) %>% upset()
```
## Overlaps between gene sets
```{r results="markdown"}
#| fig.width=12,
#| fig.height=8
foo <- tres[[1]] %>%
slice(1:20) %>%
pull(ID) %>%
modCorPlot(stat = "overlap")
foo
```
## "Leading edge" analysis
Which genes are driving the enrichment?
```{r}
evidencePlot(gl, m = "LI.M165")
```
```{r}
evidencePlot(gl, m = "LI.M165", style="gsea")
```
Get the leading edge:
```{r results="markdown"}
lea <- tmodLEA(gl, "LI.M165")
lea
```
```{r}
clr <- ifelse(gl %in% lea[[1]], "red", "grey")
names(clr) <- gl
evidencePlot(gl, m = "LI.M165",
gene.labels=TRUE,
gene.colors = clr)
```
And GSEA style:
```{r}
clr <- ifelse(gl %in% lea[[1]], "red", "grey")
names(clr) <- gl
evidencePlot(gl, m = "LI.M165",
gene.labels=TRUE,
gene.colors = clr,
style="gsea")
abline(v=attr(lea[[1]], "LEA"), col="red", lwd=2)
```
# The vaccination example
This example data set has been published by @weiner2019characterization.
RNA has been collected daily from individuals vaccinated with one of seven
vaccines over the course of several days. The data set has been collected
using microarrays. The pre-calculated results include two influenza
vaccines, one containing an adjuvant and the other not. For details how to
get this table, see the online tmod manual
[here](https://january3.github.io/tmod/articles/user_manual/tmod_user_manual.html),
section "Transcriptional responses to vaccination".
```{r vaccine_read_data}
tt <- readRDS("data/vaccination_toptable.rds")
```
The subset contains results for 5 time points for two vaccines (10
contrasts in total). The two vaccines are denoted with "A" (no adjuvant)
and "F" (adjuvant). We will run gene set enrichment on each of these time
points using `tmodCERNOtest`.
```{r vaccine_tmod}
contrasts_v <- paste0(rep(c("F", "A"), each=5), "_", 1:5)
sort_cols <- paste0("qval.", contrasts_v)
res <- map(sort_cols, ~ {
tmodCERNOtest(tt$GENE_SYMBOL[ order(tt[[.x]]) ])
})
names(res) <- contrasts_v
```
The figure below shows the results.
```{r vaccine_panelplot0,fig.width=12,fig.height=8}
ggPanelplot(res, filter_row_q = 1e-3, filter_row_auc = .85, q_thr=0.01)
```
Do these gene sets contain genes that go up or down? For this, we need to
determine how many genes in each gene set are significantly going up or
down.
```{r vaccine_sgenes}
pval <- tt %>% select(starts_with("qval"))
lfc <- tt %>% select(starts_with("logFC"))
sgenes <- tmodDecideTests(tt$GENE_SYMBOL, lfc=lfc, pval=pval)
names(sgenes) <- contrasts_v
```
```{r vaccine_panelplot,fig.width=12,fig.height=8}
ggPanelplot(res, sgenes=sgenes, filter_row_q = 1e-3, filter_row_auc = .85, q_thr=0.01)
```
## Concordance / discordance plots
Are responses on day 1 in Fluad similar to responses on day 1 in Agrippal?
* test for interaction, or
* use disco plots
$$d_s = \log_2 FC_A \cdot \log_2 FC_B \cdot |\log_{10}p_A + \log_{10}p_B|$$
```{r}
disco <- function(lfc1, lfc2, pval1, pval2) {
lfc1 * lfc2 * abs(log10(pval1) + log10(pval2))
}
tt <- tt %>% mutate(disco=disco(logFC.F_1, logFC.A_1, qval.F_1, qval.A_1)) %>%
mutate(disco=ifelse(abs(disco) > .5, sign(disco), disco)) %>%
arrange(abs(disco))
ggplot(tt, aes(x=logFC.F_1, y=logFC.A_1, color=disco)) +
scale_color_gradient2(low="blue", mid="grey", high = "red") +
geom_point(alpha=.1) +
geom_abline(slope = 1, intercept = 0, color="grey")
```
How about F day 1 and F day 5?
```{r}
tt <- tt %>% mutate(disco=disco(logFC.F_1, logFC.F_5, qval.F_1, qval.F_5)) %>%
mutate(disco=ifelse(abs(disco) > .5, sign(disco), disco)) %>%
arrange(abs(disco))
ggplot(tt, aes(x=logFC.F_1, y=logFC.F_5, color=disco)) +
scale_color_gradient2(low="blue", mid="grey", high = "red") +
geom_point(alpha=.1) +
geom_abline(slope = 1, intercept = 0, color="grey")
```
We can now make gene set enrichment of the *discordant* and *concordant*
gene sets.
```{r}
#| fig.width=6,
#| fig.height=12
tdisco <- list()
tdisco$discordant <- tt %>% arrange(disco) %>% pull(GENE_SYMBOL) %>%
tmodCERNOtest() %>% filter(AUC > .55)
tdisco$concordant <- tt %>% arrange(-disco) %>% pull(GENE_SYMBOL) %>%
tmodCERNOtest() %>% filter(AUC > .55)
sgenes <- tmodDecideTests(tt$GENE_SYMBOL, lfc=tt$disco, lfc.thr = 0)
sgenes <- list(discordant=sgenes[[1]], concordant=sgenes[[1]])
ggPanelplot(tdisco, sgenes=sgenes)
```
Note: there is more to the gene set enrichment analysis with disco. There
will be more on that in both the tmod package and the tmod manual.
# Bibliography