-
Notifications
You must be signed in to change notification settings - Fork 7
/
de_othermethods.Rmd
361 lines (317 loc) · 9.27 KB
/
de_othermethods.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
---
title: "DE for CD4+ Tcells - clustering on PCA"
author: "Fanny Perraudeau"
date: "`r Sys.Date()`"
output:
html_document:
fig_height: 7
fig_width: 7
toc: yes
code_folding: hide
toc_float: yes
---
```{r options, echo=FALSE, results="hide",mesasge=FALSE, error=FALSE, include=FALSE, autodep=TRUE}
knitr::opts_chunk$set(fig.align="center", cache=TRUE, error=FALSE, message=FALSE, warning=TRUE)
library(zinbwave)
library(edgeR)
library(RColorBrewer)
library(ggplot2)
```
Using clustering on PCA, we want to compare the different DE methods and perform gene set enrichment analysis.
# Data
```{r loadobject}
load("../data/core.rda")
core = core[,colData(core)$seurat %in% 1:2]
core = core[rowSums(assay(core)) > 0, ]
colData(core)$seurat = factor(colData(core)$seurat)
core
```
# Compute ZINB-WaVE observational weights
```{r,eval=FALSE}
#previous epsilon=1e8 but then looked at mocks
library(doParallel)
library(BiocParallel)
NCORES = 2
registerDoParallel(NCORES)
register(DoparParam())
print(system.time(zinb <- zinbFit(core, X = '~ seurat',
epsilon = 1e12)))
save(zinb, file = 'zinb.rda')
```
```{r weightszinb}
load('zinb.rda')
counts = assay(core)
weights_zinbwave = computeObservationalWeights(zinb, counts)
```
```{r weightszinbZeros}
hist(weights_zinbwave[assay(core) == 0], main = 'zinbwave',
xlab = 'Weights')
```
```{r}
design = model.matrix(~ colData(core)$seurat)
```
# Methods
## edgeR
```{r fitedger}
fit_edgeR <- function(counts, design, filter = NULL){
library(edgeR)
d = DGEList(counts)
d = suppressWarnings(calcNormFactors(d))
d = estimateDisp(d, design)
fit = glmFit(d, design)
glm = glmLRT(fit)
tab = glm$table
tab$padj = p.adjust(tab$PValue, "BH")
tab$gene = rownames(tab)
de <- as.data.frame(tab, stringsAsFactors = FALSE)
de = de[, c('gene', 'PValue', 'padj', 'logFC')]
colnames(de) = c('gene', 'pval', 'padj', 'logfc')
de
}
```
```{r edgeR}
edgeR <- fit_edgeR(counts, design)
edgeR$method <- 'edgeR'
```
## zinbwave-weighted edgeR
```{r fitedgeRzi}
fit_edgeR_zi <- function(counts, design, weights,
filter = NULL){
library(edgeR)
d = DGEList(counts)
d = suppressWarnings(calcNormFactors(d))
d$weights <- weights
d = estimateDisp(d, design)
fit = glmFit(d,design)
glm = glmWeightedF(fit, filter = filter)
tab = glm$table
tab$gene = rownames(tab)
de <- data.frame(tab, stringsAsFactors = FALSE)
de = de[, c('gene', 'PValue', 'padjFilter', 'logFC')]
colnames(de) = c('gene', 'pval', 'padj', 'logfc')
de
}
```
```{r edgeRzi}
nf <- edgeR::calcNormFactors(counts)
baseMean = unname(rowMeans(sweep(counts,2,nf,FUN="*")))
zinbwave_edgeR <- fit_edgeR_zi(counts, design,
weights = weights_zinbwave,
filter = baseMean)
zinbwave_edgeR$method <- 'zinbwave_edgeR'
```
## limma-voom
```{r runlimmavoom}
runLimmavoom <- function(counts, design) {
library(limma)
library(edgeR)
dgel <- DGEList(counts)
dgel <- edgeR::calcNormFactors(dgel)
v <- voom(dgel,design,plot=FALSE)
fit <- lmFit(v,design)
fit <- eBayes(fit)
tt <- topTable(fit,coef=2,n=nrow(dgel),sort.by="none")
pvals <- tt$P.Value
padj <- p.adjust(pvals,method="BH")
padj[is.na(padj)] <- 1
data.frame(gene = rownames(tt), pval=pvals, padj=padj, logfc=tt$logFC)
}
```
```{r limmavoom}
voom <- runLimmavoom(counts, design)
voom$method <- 'limmavoom'
```
# Seurat
```{r seurat}
seurat = read.csv('tcellmarkers_seurat.csv')
seurat$method = 'seurat'
seurat = seurat[, c('gene', 'p_val', 'p_val_adj',
'avg_logFC', 'method')]
colnames(seurat) = colnames(edgeR)
```
## Run MAST
```{r runMAST}
runMAST <- function(counts, design) {
library(MAST)
tpm <- counts*1e6/colSums(counts)
tpm <- log2(tpm+1)
sca <- FromMatrix(tpm,cData=data.frame(group=factor(design[,2])))
# here, we keep all genes so that we can fairly compare MAST and the other methods. So, no adaptive thresholding or filtering by gene expression
assays(sca) <- list(tpm=assay(sca))
ngeneson <- apply(counts,2,function(x) mean(x>0))
CD <- colData(sca)
CD$ngeneson <- ngeneson
CD$cngeneson <- CD$ngeneson-mean(ngeneson)
colData(sca) <- CD
## differential expression
fit <- zlm(~ cngeneson + group, sca = sca,
method = "bayesglm", ebayes = TRUE)
summaryDt = summary(fit, doLRT='group1')
summaryDt = summaryDt$datatable
fcHurdle <- merge(summaryDt[contrast=='group1'&component=='H',.(primerid, `Pr(>Chisq)`)],
summaryDt[contrast=='group1' & component=='logFC', .(primerid, coef, ci.hi, ci.lo)], by='primerid')
fcHurdle[,padj:=p.adjust(`Pr(>Chisq)`, 'fdr')]
df = data.frame(gene = fcHurdle$primerid,
pval=fcHurdle[,'Pr(>Chisq)'], padj=fcHurdle$padj,
logfc=fcHurdle$coef)
colnames(df)[2] = 'pval'
df
}
```
```{r mast}
mast <- runMAST(counts, design)
mast$method <- 'MAST'
```
# Results
Let's compare the different methods.
```{r res}
de = rbind(edgeR, seurat, voom, zinbwave_edgeR,mast)
write.csv(de, file = 'deGenes.csv', row.names = FALSE)
```
```{r}
de = read.csv('deGenes.csv',stringsAsFactors = FALSE)
head(de,2)
```
## GSEA pre ranked
We use gene set enrichment analysis and xCell gene sets.
```{r}
library(xCell) # for db
library(fgsea)
library(GSEABase)
## extract genesets from xcell
nagenes = unique(de[is.na(de$logfc), 'gene'])
de = de[!de$gene %in% nagenes, ]
genesets <- unlist(geneIds(xCell.data$signatures))
celltypes <- sapply(strsplit(names(genesets), "%"), function(x) x[1])
names(genesets) <- NULL
gs <- tapply(genesets, celltypes, c)
set.seed(6372)
gsea_res = lapply(unique(de$method), function(x){
print(x)
temp = de[de$method == x, ]
pval = temp$pval
zscores = qnorm(1 - (pval/2))
zscores[is.infinite(zscores)] = max(zscores[!is.infinite(zscores)])
logfc = temp$logfc
zscores[logfc<0] = -zscores[logfc<0]
names(zscores) = temp$gene
if (x == 'seurat') zscores = -zscores
gsea = fgsea(gs, zscores, nperm = 10000, minSize = 5)
gsea$method = x
gsea[order(-abs(gsea$NES)), ]
})
lapply(gsea_res, head)
```
```{r}
gseaDf = as.data.frame(do.call(rbind, gsea_res))
gseaDf = gseaDf[gseaDf$size > 100, ]
gseaDf = gseaDf[, c('method', 'pathway', 'NES')]
#gseaDf$method = factor(gseaDf$method, levels = c('edgeR', 'seurat', 'MAST', 'zinbwave_DESeq2', 'limmavoom', 'zinbwave_edgeR'))
sortedPwy = gseaDf[gseaDf$method == 'zinbwave_edgeR', ]
sortedPwy = sortedPwy[order(sortedPwy$NES), 'pathway']
gseaDf$pathway = factor(gseaDf$pathway, levels = sortedPwy)
```
```{r}
write.csv(gseaDf, file = 'gsea.csv')
```
```{r tenxcaseNESallPCA}
ggplot(gseaDf, aes(method, pathway)) +
geom_tile(aes(fill = NES)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_fill_gradient2(low = "blue", high = "red",
mid = "white", midpoint = 0,
space = "Lab",
name="Normalized\nEnrichment\nScore") +
ylab('Cell Type') + xlab('Method')
```
```{r tenxcaseNEScd4PCA}
size=20
chosen = c('CD4+ memory T-cells','CD4+ Tem','CD4+ Tcm','CD4+ naive T-cells')
sub = gseaDf[gseaDf$pathway %in% chosen, ]
nes = sub$NES
limit = max(abs(min(nes)), max(nes))
sub$pathway = factor(sub$pathway, levels = chosen)
ggplot(sub, aes(method, pathway)) +
geom_tile(aes(fill = NES)) +
scale_fill_gradient2(low = "blue", high = "red", mid = "white",
midpoint = 0, space = "Lab",
name="Normalized\nEnrichment\nScore",
limit = c(-limit, limit)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
ylab('Cell Type') + xlab('Method') +
theme(text = element_text(size = size))
```
```{r}
lapply(gsea_res, function(x){
x[x$pathway %in% chosen, ]
})
```
## Histogram of pvalues
We are expecting uniformity of the pvalue with a pick close to zero corresponding to DE genes.
```{r tenxcaseHistPvalPCA}
par(mfrow= c(3,2))
for (x in unique(de$method)){
hist(de[de$method == x, 'pval'], main = x, ylim = c(0, 3000),
xlab = 'pvalue')
}
par(mfrow= c(1,1))
```
## Number of DE genes
```{r}
for (x in unique(de$method)){
print(x)
print(sum(de[de$method ==x, 'padj'] < 0.05, na.rm = TRUE))
}
```
## Concordance between DE genes
### Venn diagram
```{r}
thr = 0.05
```
DE genes are genes with an adjusted pvalue lower than `r thr`.
```{r venn}
library(dplyr)
ve = de %>% group_by(method, gene) %>%
summarize(pval = min(pval, na.rm=TRUE)) %>%
ungroup() %>% as.data.frame()
ve$de = ve$pval < thr
ve = reshape2::acast(ve[, c(1,2,4)], gene ~ method, sum)
aa <- vennCounts(ve)
vennDiagram(aa, main = 'DE gene, adj pvalue < 0.05')
```
## Most DE genes per method in terms of pvalue
```{r}
ranks = lapply(unique(de$method), function(x){
temp = de[de$method == x, ]
temp$rank = rank(temp$pval)
temp
})
de = do.call(rbind, ranks)
ranks = lapply(unique(de$method), function(x){
temp = de[de$method == x, ]
temp = temp[temp$rank < 10, ]
temp[order(temp$rank), ]
})
names(ranks) = unique(de$method)
ranks
```
## Most DE genes per method in terms of fold change
```{r}
ranks = lapply(unique(de$method), function(x){
temp = de[de$method == x, ]
temp$rank = rank(-abs(temp$logfc))
temp
})
de = do.call(rbind, ranks)
ranks = lapply(unique(de$method), function(x){
temp = de[de$method == x, ]
temp = temp[temp$rank < 10, ]
temp[order(temp$rank), ]
})
names(ranks) = unique(de$method)
ranks
```
#sessionInfo
```{r}
sessionInfo()
```