-
Notifications
You must be signed in to change notification settings - Fork 0
/
05a_clustering_main.R
403 lines (288 loc) · 11.4 KB
/
05a_clustering_main.R
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
###############################################################
# LC snRNA-seq analyses: clustering and supervised thresholding
# Lukas Weber, Oct 2022
###############################################################
library(here)
library(SingleCellExperiment)
library(scater)
library(scran)
library(bluster)
library(ggplot2)
library(viridisLite)
library(ggVennDiagram)
library(dplyr)
library(tidyr)
dir_plots <- here("plots", "singleNucleus", "05a_clustering_main")
# ---------------
# Load SCE object
# ---------------
# load SCE object from previous script
fn <- here("processed_data", "SCE", "sce_filtered")
sce <- readRDS(paste0(fn, ".rds"))
dim(sce)
table(colData(sce)$Sample)
# ----------
# Clustering
# ----------
# clustering algorithm and parameters from OSCA
# two-stage clustering algorithm using high-resolution k-means and graph-based clustering
set.seed(121)
clus <- clusterCells(
sce,
use.dimred = "PCA",
BLUSPARAM = TwoStepParam(
first = KmeansParam(centers = 2000),
second = NNGraphParam(k = 10)
)
)
colLabels(sce) <- clus
# number of nuclei per cluster and sample
table(colLabels(sce))
table(colLabels(sce), colData(sce)$Sample)
# expression of key markers for NE, 5-HT, and cholinergic neuron populations
ix <- c(
DBH = which(rowData(sce)$gene_name == "DBH"),
TH = which(rowData(sce)$gene_name == "TH"),
SLC6A2 = which(rowData(sce)$gene_name == "SLC6A2"),
TPH2 = which(rowData(sce)$gene_name == "TPH2"),
SLC6A4 = which(rowData(sce)$gene_name == "SLC6A4"),
SLC5A7 = which(rowData(sce)$gene_name == "SLC5A7")
)
n_clus <- length(table(colLabels(sce)))
res_list <- list()
for (k in seq_len(n_clus)) {
res_list[[k]] <- rowMeans(logcounts(sce)[ix, colLabels(sce) == k])
}
res_mat <- do.call("rbind", res_list)
rownames(res_mat) <- seq_len(n_clus)
colnames(res_mat) <- names(ix)
cbind(
n = table(colLabels(sce)),
table(colLabels(sce), colData(sce)$Sample),
res_mat
)
# -----------------------
# Supervised thresholding
# -----------------------
# identify NE neuron nuclei based on positive expression of DBH and TH
# check DBH, TH, and SLC6A2
ix_DBH <- which(rowData(sce)$gene_name == "DBH")
ix_TH <- which(rowData(sce)$gene_name == "TH")
ix_SLC6A2 <- which(rowData(sce)$gene_name == "SLC6A2")
# number of nuclei
rbind(
DBHpos = table(counts(sce)[ix_DBH, ] > 0),
THpos = table(counts(sce)[ix_TH, ] > 0),
SLC6A2pos = table(counts(sce)[ix_SLC6A2, ] > 0),
DBHposTHpos = table(counts(sce)[ix_DBH, ] > 0 & counts(sce)[ix_TH, ] > 0),
DBHposTHposSLC6A2pos = table(counts(sce)[ix_DBH, ] > 0 & counts(sce)[ix_TH, ] > 0 & counts(sce)[ix_SLC6A2, ] > 0)
)
# select nuclei based on DBH and TH
supervised_NE <- counts(sce)[ix_DBH, ] > 0 & counts(sce)[ix_TH, ] > 0
table(supervised_NE)
stopifnot(length(supervised_NE) == ncol(sce))
colData(sce)$supervised_NE <- supervised_NE
# check expression of key markers
res <- rowMeans(logcounts(sce)[ix, colData(sce)$supervised_NE])
names(res) <- names(ix)
res
# identify cholinergic inhibitory neurons based on marker genes
ix_SLC5A7 <- which(rowData(sce)$gene_name == "SLC5A7")
ix_CHAT <- which(rowData(sce)$gene_name == "CHAT")
ix_ACHE <- which(rowData(sce)$gene_name == "ACHE")
ix_BCHE <- which(rowData(sce)$gene_name == "BCHE")
ix_SLC18A3 <- which(rowData(sce)$gene_name == "SLC18A3")
ix_PRIMA1 <- which(rowData(sce)$gene_name == "PRIMA1")
table(counts(sce)[ix_SLC5A7, ] > 0)
table(counts(sce)[ix_SLC5A7, ] > 0 &
counts(sce)[ix_CHAT, ] > 0 &
counts(sce)[ix_ACHE, ] > 0)
table(counts(sce)[ix_SLC5A7, ] > 0 &
counts(sce)[ix_CHAT, ] > 0 &
counts(sce)[ix_ACHE, ] > 0 &
counts(sce)[ix_BCHE, ] > 0 &
counts(sce)[ix_SLC18A3, ] > 0 &
counts(sce)[ix_PRIMA1, ] > 0)
# -----------------
# summarize results
# -----------------
# number of nuclei per cluster and sample
# unsupervised clustering
table(colLabels(sce))
table(colLabels(sce), colData(sce)$Sample)
# NE neuron cluster and 5-HT neuron cluster identified from marker genes above
clus_NE <- 6
clus_5HT <- 21
sum(colLabels(sce) == clus_NE)
sum(colLabels(sce) == clus_5HT)
tbl <- rbind(
NE = table(colLabels(sce) == clus_NE, colData(sce)$Sample)[2, ],
`5HT` = table(colLabels(sce) == clus_5HT, colData(sce)$Sample)[2, ]
)
tbl
rowSums(tbl)
# supervised thresholding
table(colData(sce)$supervised_NE)
table(colData(sce)$supervised_NE, colData(sce)$Sample)[2, ]
# comparison between unsupervised clustering and supervised thresholding
table(
unsupervised = colLabels(sce) == clus_NE,
supervised = colData(sce)$supervised_NE
)
# mitochondrial percentages in NE neuron clusters
# unsupervised
summary(colData(sce)$subsets_Mito_percent[colLabels(sce) == clus_NE])
# supervised
summary(colData(sce)$subsets_Mito_percent[colData(sce)$supervised_NE])
# for plotting
sce_plot <- sce
rownames(sce_plot) <- rowData(sce_plot)$gene_name
# unsupervised
sce_clusNE <- sce_plot[, colLabels(sce_plot) == clus_NE]
sce_clus5HT <- sce_plot[, colLabels(sce_plot) == clus_5HT]
# supervised
sce_supNE <- sce_plot[, colData(sce_plot)$supervised_NE]
genes_NE <- c("DBH", "TH", "SLC6A2")
genes_5HT <- c("TPH2", "SLC6A4")
# --------------------------------------------------
# Venn diagram comparing unsupervised and supervised
# --------------------------------------------------
colData(sce)$Key <- paste(colData(sce)$Sample, colData(sce)$Barcode, sep = "_")
# NE neurons: unsupervised clustering vs. supervised thresholding
x <- list(
`clustering NE` = colData(sce)$Key[colLabels(sce) == clus_NE],
`supervised NE` = colData(sce)$Key[colData(sce)$supervised_NE]
)
ggVennDiagram(x) +
scale_fill_gradient(low = "#F4FAFE", high = "#4981BF") +
scale_color_manual(values = c("black", "black")) +
theme_void() +
theme(plot.background = element_rect(fill = "white", color = "white"))
fn <- file.path(dir_plots, "vennDiagram_clusteringVsSupervised_NE")
ggsave(paste0(fn, ".pdf"), width = 5.5, height = 3.5)
ggsave(paste0(fn, ".png"), width = 5.5, height = 3.5)
# ------------------------------------
# plot QC metrics in NE neuron cluster
# ------------------------------------
# plot QC metrics
p <- gridExtra::grid.arrange(
plotColData(sce_clusNE, x = "Sample", y = "sum", colour_by = "subsets_Mito_percent") +
scale_y_log10() + ggtitle("Total count") + scale_color_viridis_c(name = "mito"),
plotColData(sce_clusNE, x = "Sample", y = "detected", colour_by = "subsets_Mito_percent") +
scale_y_log10() + ggtitle("Detected genes") + scale_color_viridis_c(name = "mito"),
plotColData(sce_clusNE, x = "Sample", y = "subsets_Mito_percent", colour_by = "subsets_Mito_percent") +
ggtitle("Mito percent") + scale_color_viridis_c(name = "mito"),
ncol = 3
)
p
fn <- file.path(dir_plots, "QC_metrics_NEcluster")
ggsave(paste0(fn, ".pdf"), plot = p, width = 10, height = 3.5)
ggsave(paste0(fn, ".png"), plot = p, width = 10, height = 3.5)
# plot histogram of mitochondrial proportion
df <- as.data.frame(colData(sce_clusNE)) %>%
select(c("Barcode", "subsets_Mito_percent"))
ggplot(df, aes(x = subsets_Mito_percent)) +
geom_histogram(bins = 20, fill = "navy") +
labs(x = "mitochondrial percentage") +
ggtitle("NE neuron cluster nuclei") +
theme_bw()
fn <- here(dir_plots, paste0("histogram_mito_NEcluster"))
ggsave(paste0(fn, ".pdf"), width = 4.5, height = 4)
ggsave(paste0(fn, ".png"), width = 4.5, height = 4)
# -------------------------------
# plot expression of marker genes
# -------------------------------
# unsupervised clustering
# plot expression of NE neuron marker genes
p <- gridExtra::grid.arrange(
plotExpression(sce_clusNE, genes_NE, colour_by = "sum") +
ggtitle("NE neuron cluster"),
plotExpression(sce_clusNE, genes_NE, colour_by = "detected") +
ggtitle("NE neuron cluster"),
plotExpression(sce_clusNE, genes_NE, colour_by = "subsets_Mito_percent") +
ggtitle("NE neuron cluster") + scale_color_viridis_c(name = "mito"),
ncol = 3
)
p
fn <- file.path(dir_plots, "markerExpression_NEcluster")
ggsave(paste0(fn, ".pdf"), plot = p, width = 9, height = 3.5)
ggsave(paste0(fn, ".png"), plot = p, width = 9, height = 3.5)
# plot expression of 5-HT neuron marker genes
p <- gridExtra::grid.arrange(
plotExpression(sce_clus5HT, genes_5HT, colour_by = "sum") +
ggtitle("5-HT neuron cluster"),
plotExpression(sce_clus5HT, genes_5HT, colour_by = "detected") +
ggtitle("5-HT neuron cluster"),
plotExpression(sce_clus5HT, genes_5HT, colour_by = "subsets_Mito_percent") +
ggtitle("5-HT neuron cluster") + scale_color_viridis_c(name = "mito"),
ncol = 3
)
p
fn <- file.path(dir_plots, "markerExpression_5HTcluster")
ggsave(paste0(fn, ".pdf"), plot = p, width = 8, height = 3.5)
ggsave(paste0(fn, ".png"), plot = p, width = 8, height = 3.5)
# supervised thresholding
# plot expression of NE neuron marker genes
p <- gridExtra::grid.arrange(
plotExpression(sce_supNE, genes_NE, colour_by = "sum") +
ggtitle("Supervised NE"),
plotExpression(sce_supNE, genes_NE, colour_by = "detected") +
ggtitle("Supervised NE"),
plotExpression(sce_supNE, genes_NE, colour_by = "subsets_Mito_percent") +
ggtitle("Supervised NE") + scale_color_viridis_c(name = "mito"),
ncol = 3
)
p
fn <- file.path(dir_plots, "markerExpression_supervisedNE")
ggsave(paste0(fn, ".pdf"), plot = p, width = 9, height = 3.5)
ggsave(paste0(fn, ".png"), plot = p, width = 9, height = 3.5)
# ----------
# plot UMAPs
# ----------
# identify populations of interest
colData(sce)$unsupervised_NE <- colLabels(sce) == clus_NE
colData(sce)$unsupervised_5HT <- colLabels(sce) == clus_5HT
# unsupervised clustering
# NE neurons
plotReducedDim(sce, dimred = "UMAP", colour_by = "unsupervised_NE") +
scale_color_manual(values = c("navy", "red"), name = "NE cluster") +
ggtitle("Unsupervised clustering")
fn <- file.path(dir_plots, "UMAP_NEcluster")
ggsave(paste0(fn, ".pdf"), width = 5.5, height = 5)
ggsave(paste0(fn, ".png"), width = 5.5, height = 5)
# 5-HT neurons
plotReducedDim(sce, dimred = "UMAP", colour_by = "unsupervised_5HT") +
scale_color_manual(values = c("navy", "red"), name = "5-HT cluster") +
ggtitle("Unsupervised clustering")
fn <- file.path(dir_plots, "UMAP_5HTcluster")
ggsave(paste0(fn, ".pdf"), width = 5.5, height = 5)
ggsave(paste0(fn, ".png"), width = 5.5, height = 5)
# all clusters
pal <- unname(palette.colors(36, "Polychrome 36"))
plotReducedDim(sce, dimred = "UMAP", colour_by = "label") +
scale_color_manual(values = pal, name = "cluster") +
ggtitle("Unsupervised clustering")
fn <- file.path(dir_plots, "UMAP_clustering")
ggsave(paste0(fn, ".pdf"), width = 6, height = 4.75)
ggsave(paste0(fn, ".png"), width = 6, height = 4.75)
# supervised thresholding
# NE neurons
plotReducedDim(sce, dimred = "UMAP", colour_by = "supervised_NE") +
scale_color_manual(values = c("navy", "red"), name = "supervised NE") +
ggtitle("Supervised thresholding")
fn <- file.path(dir_plots, "UMAP_supervisedNE")
ggsave(paste0(fn, ".pdf"), width = 5.5, height = 5)
ggsave(paste0(fn, ".png"), width = 5.5, height = 5)
# sample IDs
# note: batch integration was not included due to our interest in rare populations (LC-NE neurons)
# sample IDs
plotReducedDim(sce, dimred = "UMAP", colour_by = "Sample") +
ggtitle("Sample IDs")
fn <- file.path(dir_plots, "UMAP_sampleIDs")
ggsave(paste0(fn, ".pdf"), width = 5.5, height = 5)
ggsave(paste0(fn, ".png"), width = 5.5, height = 5)
# -----------
# Save object
# -----------
fn_out <- here("processed_data", "SCE", "sce_clustering_main")
saveRDS(sce, paste0(fn_out, ".rds"))