-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmultisubjectDSanalysis_wrapper_code.R
More file actions
415 lines (290 loc) · 15.6 KB
/
multisubjectDSanalysis_wrapper_code.R
File metadata and controls
415 lines (290 loc) · 15.6 KB
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
# MAST_RE gives an error for lowly expressed genes, which might cause the program to fail. This filters them out.
RunMAST <- function(raw.data,normalized.data,group,individual=NULL)
{
library(MAST)
nonzero_genes <- Matrix::rowSums(raw.data)!=0
raw.data <- raw.data[nonzero_genes,]
normalized.data <- normalized.data[nonzero_genes,]
if (!is.null(individual))
{
i <- apply(raw.data,1,function(x) sum(x!=0)) > length(table(individual))
raw.data <- raw.data[i,]
normalized.data <- normalized.data[i,]
}
colData <- data.frame(group=factor(group))
fData <- data.frame(primerid=rownames(normalized.data))
sca <- suppressMessages(MAST::FromMatrix(exprsArray=as.matrix(normalized.data), cData=colData, fData=fData))
cdr2 <- colSums(SummarizedExperiment::assay(sca)>0)
SummarizedExperiment::colData(sca)$ngeneson <- scale(cdr2)
SummarizedExperiment::colData(sca)$group <- factor(plyr::mapvalues(x = SummarizedExperiment::colData(sca)$group,
from = levels(SummarizedExperiment::colData(sca)$group),
to = c("Control","Case")))
if (is.null(individual))
{
zlmCond <- zlm(~group + ngeneson, sca)
} else {
SummarizedExperiment::colData(sca)$DonorID <- factor(individual)
zlmCond <- zlm(~group + ngeneson + (1 | DonorID), sca, method='glmer',ebayes = F,
strictConvergence = FALSE)
}
summaryCond <- suppressMessages(MAST::summary(zlmCond,
doLRT='groupCase'))
summaryDt <- summaryCond$datatable
summaryDt_H <- summaryDt[summaryDt$component=='H',] # H=combined using hurdle method
summaryDt_H$FDR <- p.adjust(summaryDt_H$`Pr(>Chisq)`,method = "fdr")
return(summaryDt_H)
}
# MAST_RE gives an error for lowly expressed genes, which might cause the program to fail. This does not filter them out.
RunMAST_no_filtering <- function(raw.data,normalized.data,group,individual=NULL)
{
library(MAST)
nonzero_genes <- Matrix::rowSums(raw.data)!=0
raw.data <- raw.data[nonzero_genes,]
normalized.data <- normalized.data[nonzero_genes,]
# if (!is.null(individual))
# {
# i <- apply(raw.data,1,function(x) sum(x!=0)) > length(table(individual))
# raw.data <- raw.data[i,]
# normalized.data <- normalized.data[i,]
# }
colData <- data.frame(group=factor(group))
fData <- data.frame(primerid=rownames(normalized.data))
sca <- suppressMessages(MAST::FromMatrix(exprsArray=as.matrix(normalized.data), cData=colData, fData=fData))
cdr2 <- colSums(SummarizedExperiment::assay(sca)>0)
SummarizedExperiment::colData(sca)$ngeneson <- scale(cdr2)
SummarizedExperiment::colData(sca)$group <- factor(plyr::mapvalues(x = SummarizedExperiment::colData(sca)$group,
from = levels(SummarizedExperiment::colData(sca)$group),
to = c("Control","Case")))
if (is.null(individual))
{
zlmCond <- zlm(~group + ngeneson, sca)
} else {
SummarizedExperiment::colData(sca)$DonorID <- factor(individual)
zlmCond <- zlm(~group + ngeneson + (1 | DonorID), sca, method='glmer',ebayes = F,
strictConvergence = FALSE)
}
summaryCond <- suppressMessages(MAST::summary(zlmCond,
doLRT='groupCase'))
summaryDt <- summaryCond$datatable
summaryDt_H <- summaryDt[summaryDt$component=='H',] # H=combined using hurdle method
summaryDt_H$FDR <- p.adjust(summaryDt_H$`Pr(>Chisq)`,method = "fdr")
return(summaryDt_H)
}
RunSeuratDE <- function(raw.data,normalized.data,individual,group,test="wilcox",use.individidual.latent.var=FALSE)
{
library(Seurat)
group <- factor(plyr::mapvalues(x = group,from = names(table(group)),to = c("Control","Case")))
seurat.object <- CreateSeuratObject(counts = raw.data,min.cells = 1, min.features=0)
seurat.object[["group"]] <- group
seurat.object[["individual"]] <- individual
rm(raw.data)
gc()
seurat.object <- NormalizeData(seurat.object)
#Replace normalized data with normalized.data object
seurat.object@assays$RNA@data <- normalized.data[rownames(seurat.object),]
rm(normalized.data)
gc()
Idents(seurat.object) <- "group"
# test.use
# Denotes which test to use. Available options are:
# "wilcox" : Identifies differentially expressed genes between two groups of cells using a Wilcoxon Rank Sum test (default)
# "bimod" : Likelihood-ratio test for single cell gene expression, (McDavid et al., Bioinformatics, 2013)
# "roc" : Identifies 'markers' of gene expression using ROC analysis. For each gene, evaluates (using AUC) a classifier built on that gene alone, to classify between two groups of cells. An AUC value of 1 means that expression values for this gene alone can perfectly classify the two groupings (i.e. Each of the cells in cells.1 exhibit a higher level than each of the cells in cells.2). An AUC value of 0 also means there is perfect classification, but in the other direction. A value of 0.5 implies that the gene has no predictive power to classify the two groups. Returns a 'predictive power' (abs(AUC-0.5) * 2) ranked matrix of putative differentially expressed genes.
# "t" : Identify differentially expressed genes between two groups of cells using the Student's t-test.
# "negbinom" : Identifies differentially expressed genes between two groups of cells using a negative binomial generalized linear model. Use only for UMI-based datasets
# "poisson" : Identifies differentially expressed genes between two groups of cells using a poisson generalized linear model. Use only for UMI-based datasets
#"LR" : Uses a logistic regression framework to determine differentially expressed genes. Constructs a logistic regression model predicting group membership based on each feature individually and compares this to a null model with a likelihood ratio test.
#"MAST" : Identifies differentially expressed genes between two groups of cells using a hurdle model tailored to scRNA-seq data. Utilizes the MAST package to run the DE testing.
#"DESeq2" : Identifies differentially expressed genes between two groups of cells based on a model using DESeq2 which uses a negative binomial distribution (Love et al, Genome Biology, 2014).This test does not support pre-filtering of genes based on average difference (or percent detection rate) between cell groups. However, genes may be pre-filtered based on their minimum detection rate (min.pct) across both cell groups. To use this method, please install DESeq2, using the instructions at https://bioconductor.org/packages/release/bioc/html/DESeq2.html
if (use.individidual.latent.var)
{
markers <- FindMarkers(seurat.object,ident.1="Case",ident.2="Control",logfc.threshold = 0,min.pct = 0,min.cells.feature=1,test.use=test,latent.vars = "individual")
} else {
markers <- FindMarkers(seurat.object,ident.1="Case",ident.2="Control",logfc.threshold = 0,min.pct = 0,test.use=test)
}
return(markers)
}
RunMuscat_MM <- function(raw.data,normalized.data,individual,group)
{
library(SingleCellExperiment)
sce <- SingleCellExperiment(assays=list(counts=raw.data,logcounts=normalized.data),colData=DataFrame(sample_id=factor(individual),group_id=factor(group),cluster_id=factor(rep("cluster",length(group)))))
library(muscat)
#Using the mixed model from muscat
print("Running mixed model")
mm <- mmDS(sce,min_cells=1)
df <- mm$cluster
df <- df[,c("logFC","p_val","p_adj.loc","gene")]
colnames(df) <- c("logFC","pvalue","padj","gene")
return(df)
}
CreatePseudoBulkData <- function(raw.data,normalized.data,sample.id,fun="sum")
{
# fun can be "sum" or "mean"
library(muscat)
library(SingleCellExperiment)
if (fun=="sum")
{
sce <- SingleCellExperiment(assays=list(counts=raw.data),colData=DataFrame(sample_id=sample.id))
} else if (fun=="mean") {
sce <- SingleCellExperiment(assays=list(counts=normalized.data),colData=DataFrame(sample_id=sample.id))
}
pb <- aggregateData(sce, assay = "counts", fun=fun, by=c("sample_id"))
return(pb)
}
RunPseudobulkMethod <- function(raw.data,normalized.data,individual,group,test="ROTS",sum.or.mean="sum")
{
pb <- CreatePseudoBulkData(raw.data = raw.data,normalized.data = normalized.data,
sample.id = individual,fun = sum.or.mean)
if (test=="ROTS")
{
group <- apply(as.matrix(table(individual,group)),1,function(x) x[1]==0)
group_names <- names(group)
group <- as.numeric(group)
names(group) <- group_names
group <- group[colnames(pb)]
library(SingleCellExperiment)
library(edgeR)
library(ROTS)
if (sum.or.mean=="sum")
{
y <- DGEList(assay(pb), remove.zeros = T)
y <- calcNormFactors(y)
logcpm <- edgeR::cpm(y, normalized.lib.sizes=T, prior.count=1, log=T)
resrots <- ROTS(data = logcpm, groups = group, seed = 1234)
}
else if (sum.or.mean == "mean")
{
y <- assay(pb)
print(dim(y))
y <- y[apply(y,1,sum)!=0,]
print(dim(y))
resrots <- ROTS(data = y, groups = group, seed = 1234)
}
rotsdf <- as.data.frame(resrots$logfc)
rotsdf$gene <- rownames(rotsdf)
rotsdf <- cbind(rotsdf, resrots$pvalue)
rotsdf <- cbind(rotsdf, resrots$FDR)
rownames(rotsdf) <- 1:nrow(rotsdf)
colnames(rotsdf) <- c("logFC", "gene", "pvalue", "FDR")
rotsdf <- rotsdf[,c("logFC", "pvalue", "FDR","gene")]
colnames(rotsdf) <- c("logFC","pvalue","padj","gene")
return(rotsdf)
}
else if (test=="Limma")
{
library(edgeR)
library(limma)
group <- apply(as.matrix(table(individual,group)),1,function(x) x[1]==0)
group_names <- names(group)
group <- as.numeric(group)
names(group) <- group_names
group <- group[colnames(pb)]
group <- plyr::mapvalues(group,from = c(0,1),to = c("A","B"))
#Make the model and contrast for statistical testing
mm_highcells <- model.matrix(~0 + factor(as.character(group)))
dimnames(mm_highcells) <- list(names(group), levels(factor(as.character(group))))
mm_highcells <- mm_highcells[colnames(pb),]
contrast_highcells <- makeContrasts("B-A", levels = mm_highcells)
if (sum.or.mean == "sum")
{
dge <- DGEList(counts = assay(pb), remove.zeros = T)
dge <- calcNormFactors(dge)
v <- voom(dge, design = mm_highcells, plot = F)
fit <- lmFit(v, design = mm_highcells)
fit2 <- contrasts.fit(fit, contrasts = contrast_highcells)
fit2 <- eBayes(fit2)
limma_out <- topTable(fit2, number = nrow(dge))
} else {
v <- assay(pb)
v <- v[apply(v,1,sum)!=0,]
fit <- lmFit(v, design = mm_highcells)
fit2 <- contrasts.fit(fit, contrasts = contrast_highcells)
fit2 <- eBayes(fit2)
limma_out <- topTable(fit2, number = nrow(v))
}
df <- limma_out[,c("logFC","P.Value","adj.P.Val")]
df$gene <- rownames(limma_out)
colnames(df) <- c("logFC","pvalue","padj","gene")
return(df)
}
else if (test=="edgeR")
{
library(edgeR)
library(limma)
group <- apply(as.matrix(table(individual,group)),1,function(x) x[1]==0)
group_names <- names(group)
group <- as.numeric(group)
names(group) <- group_names
group <- group[colnames(pb)]
group <- plyr::mapvalues(group,from = c(0,1),to = c("A","B"))
#Make the model and contrast for statistical testing
mm_highcells <- model.matrix(~0 + factor(as.character(group)))
dimnames(mm_highcells) <- list(names(group), levels(factor(as.character(group))))
mm_highcells <- mm_highcells[colnames(pb),]
contrast_highcells <- makeContrasts("B-A", levels = mm_highcells)
dge <- DGEList(counts = assay(pb), group = group, remove.zeros = T)
dge <- calcNormFactors(dge)
dge <- estimateDisp(dge, design = mm_highcells)
fit <- glmQLFit(dge, design = mm_highcells)
fit2 <- glmQLFTest(fit, contrast = contrast_highcells)
tt <- topTags(fit2, n = nrow(dge))
edger_out <- tt$table
df <- edger_out[,c("logFC","PValue","FDR")]
df$gene <- rownames(edger_out)
colnames(df) <- c("logFC","pvalue","padj","gene")
return(df)
}
else if (test=="DESeq2")
{
library(limma)
library(edgeR)
library(DESeq2)
group <- apply(as.matrix(table(individual,group)),1,function(x) x[1]==0)
group_names <- names(group)
group <- as.numeric(group)
names(group) <- group_names
group <- group[colnames(pb)]
group <- plyr::mapvalues(group,from = c(0,1),to = c("A","B"))
#Make the model and contrast for statistical testing
mm_highcells <- model.matrix(~0 + factor(as.character(group)))
dimnames(mm_highcells) <- list(names(group), levels(factor(as.character(group))))
mm_highcells <- mm_highcells[colnames(pb),]
contrast_highcells <- makeContrasts("B-A", levels = mm_highcells)
pb_counts <- assay(pb)
mode(pb_counts) <- "integer"
dge <- DESeqDataSetFromMatrix(pb_counts, colData = colData(pb), design = mm_highcells)
dds <- DESeq(dge)
res <- results(dds, contrast = contrast_highcells)
deseq_out <- as.data.frame(res@listData, row.names = res@rownames)
df <- deseq_out[,c("log2FoldChange","pvalue","padj")]
df$gene <- rownames(deseq_out)
colnames(df) <- c("logFC","pvalue","padj","gene")
return(df)
}
}
# NEBULA R code throws an error if the cells are already grouped by clusters and samples and the user tries to do it again.
RunNEBULA <- function(raw.data,normalized.data,individual,group,method="LN",grouped=FALSE)
{
library(nebula, quietly = TRUE)
group <- as.factor(group)
group <- plyr::mapvalues(group,levels(group),c("case","control"))
pred <- as.data.frame(group)
colnames(pred) <- "cc"
df = model.matrix(~cc, data=pred)
library_sizes <- Matrix::colSums(raw.data)
if (!grouped)
{
data_g = group_cell(count=raw.data,id=as.character(individual),pred=df)
re = nebula(data_g$count,data_g$id,pred=data_g$pred,method=method,offset=library_sizes)
} else {
re = nebula(raw.data,as.character(individual),pred=df,method=method,offset=library_sizes)
}
pvals <- re$summary$p_cccontrol
genes <- re$summary$gene
logfc <- re$summary$logFC_cccontrol
padj <- p.adjust(pvals,method = "BH")
df <- data.frame(cbind(logfc,pvals,padj))
df$gene <- genes
colnames(df) <- c("logFC","pvalue","padj","gene")
return(df)
}