-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3_Exercise3.Rmd
282 lines (227 loc) · 9.3 KB
/
3_Exercise3.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
---
title: "Exercise 1"
output:
html_document:
number_sections: true
toc: true
---
# Exercise 3
# Goals
- Try different packages to "integrate" (= combine and mitigate batch effect for) multiple datasets
# Introduction
In the third exercise, you will apply what you learnt in exercise 2 to annotate a breast cancer dataset.
```{r, message=FALSE, warning=F, results=FALSE}
# If you are asked:
# These packages have more recent versions available.
# It is recommended to update all of them.
# Which would you like to update?
# Enter 1 and press enter
# Load the environment and libraries
# remotes::install_github("carmonalab/scIntegrationMetrics")
install.packages("devtools")
devtools::install_github('satijalab/seurat-data')
library(Seurat)
library(SeuratData)
library(scGate)
library(SignatuR)
library(STACAS)
library(scIntegrationMetrics)
library(ggplot2)
library(dplyr)
library(patchwork)
library(parallel)
library(harmony)
```
# Set paths
```{r}
my_seed <- 321
set.seed(my_seed)
root <- getwd()
path_data <- file.path(root, "data/Exercise3")
dir.create(file.path(path_data))
path_output <- file.path(root, "output/Exercise3")
dir.create(file.path(path_output))
path_plots <- file.path(root, "plots/Exercise3")
dir.create(file.path(path_plots))
```
# Load the data
```{r, fig.height=10, fig.width=10}
InstallData("panc8")
data("panc8")
panc8 <- UpdateSeuratObject(object = panc8)
ndim <- 30
panc8 <- panc8 |> NormalizeData() |> FindVariableFeatures() |> ScaleData() |> RunPCA(npcs=ndim) |> RunUMAP(reduction = "pca", dims = 1:ndim, seed.use=my_seed)
p.list.Tcell <- list()
p.list.Tcell[[1]] <- DimPlot(panc8, label = T, repel = T, group.by = 'celltype')
p.list.Tcell[[2]] <- DimPlot(panc8, label = T, repel = T, group.by = 'tech')
p.list.Tcell[[3]] <- DimPlot(panc8, label = T, repel = T, group.by = 'dataset')
for (p in p.list.Tcell) {
print(p)
}
wrap_plots(p.list.Tcell, ncol = 2) & theme(aspect.ratio = 1) & NoLegend()
```
- What do you notice in the UMAP by celltype?
- Have a look at the UMAP by sequencing technology. Can you explain why the same celltype forms multiple clusters?
- Looking at the UMAP by dataset, what can you see when looking at the different datasets acquired with the inDrop technology? (the datasets called indrop1, indrop2, indrop3, indrop4 are acquired with the same technology)
# Apply Harmony batch correction
```{r}
panc8 <- RunHarmony(panc8, "tech")
panc8 <- RunUMAP(panc8, reduction = "harmony", dims = 1:ndim, seed.use=my_seed)
```
# Investigate the effect of batch correction
```{r, fig.height=10, fig.width=10}
p.list.Tcell <- list()
p.list.Tcell[[1]] <- DimPlot(panc8, label = T, repel = T, group.by = 'celltype')
p.list.Tcell[[2]] <- DimPlot(panc8, label = T, repel = T, group.by = 'tech')
p.list.Tcell[[3]] <- DimPlot(panc8, label = T, repel = T, group.by = 'dataset')
for (p in p.list.Tcell) {
print(p)
}
wrap_plots(p.list.Tcell, ncol = 2) & theme(aspect.ratio = 1) & NoLegend()
```
# Apply STACAS batch correction
```{r}
panc8.list <- SplitObject(panc8, split.by = "tech")
panc8.stacas <- Run.STACAS(panc8.list)
panc8.stacas <- RunUMAP(panc8.stacas, reduction = "pca", dims = 1:ndim, seed.use=my_seed)
rm(panc8.list)
gc()
```
# Investigate the effect of batch correction
```{r, fig.height=10, fig.width=10}
p.list.Tcell <- list()
p.list.Tcell[[1]] <- DimPlot(panc8.stacas, label = T, repel = T, group.by = 'celltype')
p.list.Tcell[[2]] <- DimPlot(panc8.stacas, label = T, repel = T, group.by = 'tech')
p.list.Tcell[[3]] <- DimPlot(panc8.stacas, label = T, repel = T, group.by = 'dataset')
for (p in p.list.Tcell) {
print(p)
}
wrap_plots(p.list.Tcell, ncol = 2) & theme(aspect.ratio = 1) & NoLegend()
```
# ℹ️ Exercise
- Looking at the alpha cell cluster, what do you notice, regarding celltype and tech?
- Are batches well mixed?
- Is biological information retained well? (If we mix everything, of course batches are well mixed but celltypes should not be mixed!)
- Do you think this is a good integration result?
## Calculate the silhouette width before and after integration with Harmony
- Silhouette width for each celltype
- Compare mean silhouette width of all celltypes before and after
```{r}
Idents(panc8) <- "celltype"
dist.matrix <- dist(Embeddings(object = panc8[["pca"]])[, 1:ndim])
clusters <- panc8$celltype
sil <- cluster::silhouette(as.numeric(as.factor(clusters)), dist = dist.matrix)
panc8$sil <- sil[, "sil_width"]
celltype_means_before <- c()
dataset_means_before <- c()
for (tech in unique(panc8$tech)) {
a <- c()
for (celltype in unique(panc8$celltype)) {
b <- mean(panc8$sil[panc8$tech == tech & panc8$celltype == celltype])
a <- append(a, b)
celltype_means_before <- append(celltype_means_before, b)
}
dataset_means_before <- append(dataset_means_before, mean(a))
print(mean(a))
}
mean(dataset_means_before)
mean(celltype_means_before)
Idents(panc8) <- "celltype"
dist.matrix <- dist(Embeddings(object = panc8[["harmony"]])[, 1:ndim])
clusters <- panc8$celltype
sil <- cluster::silhouette(as.numeric(as.factor(clusters)), dist = dist.matrix)
panc8$sil <- sil[, "sil_width"]
celltype_means_after <- c()
dataset_means_after <- c()
for (tech in unique(panc8$tech)) {
a <- c()
for (celltype in unique(panc8$celltype)) {
b <- mean(panc8$sil[panc8$tech == tech & panc8$celltype == celltype])
a <- append(a, b)
celltype_means_after <- append(celltype_means_after, b)
}
dataset_means_after <- append(dataset_means_after, mean(a))
print(mean(a))
}
mean(dataset_means_after)
mean(celltype_means_after)
```
```{r}
Idents(panc8.stacas) <- "celltype"
dist.matrix <- dist(Embeddings(object = panc8[["pca"]])[, 1:ndim])
clusters <- panc8$celltype
sil <- cluster::silhouette(as.numeric(as.factor(clusters)), dist = dist.matrix)
panc8$sil <- sil[, "sil_width"]
celltype_means_before <- c()
dataset_means_before <- c()
for (tech in unique(panc8$tech)) {
a <- c()
for (celltype in unique(panc8$celltype)) {
b <- mean(panc8$sil[panc8$tech == tech & panc8$celltype == celltype])
a <- append(a, b)
celltype_means_before <- append(celltype_means_before, b)
}
dataset_means_before <- append(dataset_means_before, mean(a))
print(mean(a))
}
mean(dataset_means_before)
mean(celltype_means_before)
Idents(panc8.stacas) <- "celltype"
dist.matrix <- dist(Embeddings(object = panc8.stacas[["pca"]])[, 1:ndim])
clusters <- panc8.stacas$celltype
sil <- cluster::silhouette(as.numeric(as.factor(clusters)), dist = dist.matrix)
panc8.stacas$sil <- sil[, "sil_width"]
celltype_means_after <- c()
dataset_means_after <- c()
for (tech in unique(panc8.stacas$tech)) {
a <- c()
for (celltype in unique(panc8.stacas$celltype)) {
b <- mean(panc8.stacas$sil[panc8.stacas$tech == tech & panc8.stacas$celltype == celltype])
a <- append(a, b)
celltype_means_after <- append(celltype_means_after, b)
}
dataset_means_after <- append(dataset_means_after, mean(a))
print(mean(a))
}
mean(dataset_means_after)
mean(celltype_means_after)
```
# Re-calculate integration metrics after batch correction
```{r}
metrics <- getIntegrationMetrics(panc8, method.reduction = "pca", meta.label = "celltype",
meta.batch = "tech",
iLISI_perplexity = 20)
unlist(metrics)
metrics <- getIntegrationMetrics(panc8, method.reduction = "harmony", meta.label = "celltype",
meta.batch = "tech",
iLISI_perplexity = 20)
unlist(metrics)
metrics <- getIntegrationMetrics(panc8.stacas, method.reduction = "pca", meta.label = "celltype",
meta.batch = "tech",
iLISI_perplexity = 20)
unlist(metrics)
```
# ℹ️ Exercise
- Looking at the alpha cell cluster, what do you notice, regarding celltype and tech?
- Are batches well mixed?
- Is biological information retained well? (If we mix everything, of course batches are well mixed but celltypes should not be mixed!)
- Do you think this is a good integration result?
## Cluster purity evaluation
- Cluster the cells in both batch corrected objects (make sure that all cells close to the alpha cluster are assigned to the one same cluster. Do not care about the other clusters for the sake of simplicity)
- Calculate the fraction of alpha cells vs non-alpha-cells in the alpha cluster for both objects
- Calculate the fraction of alpha cells in the alpha cluster vs the sum of all cells annotated as alpha cells (celltype in the metadata)
```{r}
panc8 <- FindNeighbors(panc8, reduction = "harmony", dims = 1:ndim)
panc8 <- FindClusters(panc8, resolution = 0.05)
DimPlot(panc8, label = T, repel = T, group.by = 'seurat_clusters')
panc8.stacas <- FindNeighbors(panc8.stacas, dims = 1:ndim)
panc8.stacas <- FindClusters(panc8.stacas, resolution = 0.05)
DimPlot(panc8.stacas, label = T, repel = T, group.by = 'seurat_clusters')
# Example true labels and predicted clusters (replace these with your own data)
alpha_cluster.harmony <- table(panc8$celltype[panc8$RNA_snn_res.0.05 == 0])
alpha_cluster.stacas <- table(panc8.stacas$celltype[panc8.stacas$integrated_snn_res.0.05 == 0])
alpha_cluster.harmony["alpha"] / sum(alpha_cluster.harmony)
alpha_cluster.stacas["alpha"] / sum(alpha_cluster.stacas)
alpha_cluster.harmony["alpha"] / length(panc8$celltype[panc8$celltype == "alpha"])
alpha_cluster.stacas["alpha"] / length(panc8.stacas$celltype[panc8.stacas$celltype == "alpha"])
```