-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscRNAseqTutorial_SampleIntegrationD2.Rmd
More file actions
342 lines (220 loc) · 7.56 KB
/
Copy pathscRNAseqTutorial_SampleIntegrationD2.Rmd
File metadata and controls
342 lines (220 loc) · 7.56 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
---
title: "Single-Cell RNA-seq Analysis of Human PBMCs with Bioconductor- sample integration"
author: "Adrien Jolly"
---
---
# Load Required Packages
```{r setup, message=FALSE, warning=FALSE}
library(DropletUtils)
library(AnnotationHub)
library(SingleCellExperiment)
library(scater)
library(scran)
library(bluster)
library(batchelor)
library(ggplot2)
library(patchwork)
library(SingleR)
library(celldex)
library(DESeq2)
library(edgeR)
library(scDblFinder)
```
---
# Load 10x Data
3 samples
```{r}
# loading the samples
sce1 <- readRDS("~/Nextcloud2/TutorialSingleCellRNAseq/tutorial2602/sce1.rds")
sce2 <- readRDS("~/Nextcloud2/TutorialSingleCellRNAseq/tutorial2602/sce2.rds")
sce3 <- readRDS("~/Nextcloud2/TutorialSingleCellRNAseq/tutorial2602/sce3.rds")
saveRDS(sce,"scecombined2702.rds")
sce=readRDS("scecombined2702.rds")
```
# now we combine the datasets
```{r}
ctsce1= sce1$celltype
ctsce2= sce2$celltype
ctsce3= sce3$celltype
colData(sce1)=colData(sce1)[1:2]
colData(sce2)=colData(sce2)[1:2]
colData(sce3)=colData(sce3)[1:2]
sce1$celltype = ctsce1
sce2$celltype = ctsce2
sce3$celltype = ctsce3
# keep only common genes
common_genes <- Reduce(intersect,list(rownames(sce1),rownames(sce2),rownames(sce3)))
sce1 <- sce1[common_genes, ]
sce2 <- sce2[common_genes, ]
sce3 <- sce3[common_genes, ]
#if necessary, we can remove some columns which do not match between samples
#colData(sce1)=colData(sce1)[1:2]
#colData(sce2)=colData(sce2)[1:2]
#colData(sce3)=colData(sce3)[1:2]
# exclude dimensional reduction which we will generate again
reducedDim(sce1,"PCA") <- NULL
reducedDim(sce2,"PCA") <- NULL
reducedDim(sce3,"PCA") <- NULL
reducedDim(sce1,"UMAP") <- NULL
reducedDim(sce2,"UMAP") <- NULL
reducedDim(sce3,"UMAP") <- NULL
#rescale everything
sce.list <- multiBatchNorm(sce1, sce2, sce3)
#add information for each donor
sce.list[[1]]$donor <- "D1"
sce.list[[2]]$donor <- "D2"
sce.list[[3]]$donor <- "D3"
#check that cell annotation columns are the same
setdiff(colnames(colData(sce1)),colnames(colData(sce2)))
sce <- do.call(cbind, sce.list)
table(sce$donor)
```
# model variance fo combined dataset
```{r}
dec <- modelGeneVar(sce)
fit.default <- metadata(dec)
plot(dec$mean,
dec$total,
pch=16,
cex=0.5,
xlab="Mean expression",
ylab="Total variance",
main="Mean-Variance Plot")
curve(fit.default$trend(x), col="dodgerblue", add=TRUE, lwd=2)
```
# get top Highly variable Genes
```{r}
hvg <- getTopHVGs(dec, n=2000)
head(hvg)
```
# PCA
Dimensionality reduction on selected features.
```{r}
sce <- runPCA(sce, subset_row=hvg)
```
For further analysis, we want to select a number of components which are informative and discard non informative PCs
```{r}
var_explained <- attr(reducedDim(sce, "PCA"), "percentVar")
plot(var_explained,
type="b",
xlab="PC",
ylab="Percent variance explained",
main="Elbow Plot")
```
#run UMAP
```{r}
set.seed(1001010) # initialize UMAP (for reproducibility)
sce <- runUMAP(sce, dimred="PCA", n_dimred=7)
```
#visualize based on manual annotation of single samples
```{r}
plotUMAP(sce, colour_by="donor",point_size=0.3)
```
#marker genes per cluster identification
```{r}
markers <- findMarkers(sce, sce$clustersCorrected)
FDRclust6=markers[[6]]$FDR
length(which(FDRclust6<0.05))
markersClust2=markers[[2]]
#markers for a given cluster will have high summary log2FC
topClust2=markersClust2[order(markersClust2$summary.logFC,decreasing=TRUE),]
rownames(topClust2)[1:30]
```
# batch correction:
in order to perform a single coherent clustering we may need to create a corrected dimensional reduction which eliminate the sample effect.
```{r}
#we batch correct using fastMNN
sce.mnn <- fastMNN(sce.list,d = 6)
reducedDim(sce.mnn, "corrected")
reducedDim(sce, "MNN") <- reducedDim(sce.mnn, "corrected")
# we perform a UMAP using the corrected dimensionality reduction and check whether the donor effect is corrected.
sce <- runUMAP(
sce,
dimred = "MNN"
)
plotUMAP(sce, colour_by = "donor",point_size=0.5)
```
# Clustering and marker detection
we now recluster based on MNN corrected factors
```{r}
clustersCorrected <- clusterRows(reducedDim(sce, "MNN")[,1:6], NNGraphParam(cluster.fun="louvain",cluster.args=list(resolution=0.2)))
sce$clustersCorrected <- factor(clustersCorrected)
```
#Visualize corrected clusters
```{r}
plotUMAP(sce, colour_by="clustersCorrected")
```
#check the proportion of cells of each patient per cluster
```{r}
# table of cells per patient, and per cluster
CLusterPerDonor <- table(sce$clustersCorrected,sce$donor)
# % of cells of each patient per cluster
props <- prop.table(CLusterPerDonor, margin = 1)
#plotting the proportion with a heatmap
library(pheatmap)
pheatmap(props,cluster_rows = FALSE, cluster_cols = FALSE,ylim=c(0,1))
```
# Create Pseudobulk Counts
cells are not biological replicates, statistical tools to study differential gene expression between populations
```{r}
table(sce$donor)
```
#Aggregate by donor and new clusters (clustersCorrected) (generated from combined dataset) to create a single cell experiment with nrow= n genes and ncol = (n donors * m clusters).
```{r}
#The function sums raw cell counts within groups specified by the user
pseudo <- aggregateAcrossCells(
sce,
ids=DataFrame(donor=sce$donor,
cluster=sce$clustersCorrected)
)
```
---
#. Differential Expression with DESeq2
#We perform differential gene expression between clusters using DESeq. A tool using generalized linear models to test whether a specific factor (like cluster, batch effect..) affect the expression of given genes.
```{r}
# we create the DESeq2 object with the raw count matrix (DESeq handles normalization internally), and colData (annotations at the level of the pseudobulk). the "design" option specifies the linear model, in this case we set the set out that gene expression is only affected by the cluster (clustersCorrected).
dds <- DESeqDataSetFromMatrix(
countData=assay(pseudo, "counts"),
colData=colData(pseudo),
design=~clustersCorrected
)
#keep exclusively column of interest (since the column were generated from the column of the single cell experiments most of them are now meaningless at bulk level)
colData(dds) <- colData(dds)[6]
dds <- DESeq(dds)
#compare expression in cluster 1 vs cluster 2.
res <- results(dds,contrast=c("clustersCorrected","1","2"))
```
generate volcano plot:
```{r}
DifExpr <- as.matrix(res[order(res$padj),])
DifExpr <- DifExpr[which(DifExpr[,"padj"]<0.05),]
markers <- rownames(DifExpr[which(abs(DifExpr[,"log2FoldChange"])>=5&DifExpr[,"padj"]<=0.0001),])
p <- EnhancedVolcano(res,
rownames(res),
x ="log2FoldChange",
y ="padj",
selectLab=markers,
max.overlaps = Inf,
xlab = bquote(~Log[2]~ 'fold change'),
pCutoff = 0.05,
FCcutoff = 0.5,
pointSize = 1.0,
labSize = 4.0,
labCol = 'black',
boxedLabels = TRUE,
colAlpha = 4/5,
legendPosition = 'right',
legendLabSize = 14,
legendIconSize = 4.0,
drawConnectors = TRUE,
widthConnectors = 0.75,
colConnectors = 'black',
title="cluster x vs cluster y - comp",
subtitle="FDR<=0.05, Absolute FC>=0.5",
gridlines.major = FALSE,
gridlines.minor = FALSE,
xlim=c(-3,5)
)
p
ggsave("EnhancedVolcano.pdf", plot = p, width = 8, height = 6)
```