-
Notifications
You must be signed in to change notification settings - Fork 0
/
03a_quality_control.R
169 lines (116 loc) · 4.84 KB
/
03a_quality_control.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
########################################
# LC snRNA-seq analyses: quality control
# Lukas Weber, Oct 2022
########################################
library(here)
library(SingleCellExperiment)
library(scater)
library(scran)
library(ggplot2)
library(dplyr)
library(tidyr)
dir_plots <- here("plots", "singleNucleus", "03_quality_control")
# ---------------
# Load SCE object
# ---------------
# load SCE object from previous script
fn <- here("processed_data", "SCE", "sce_doubletsRemoved")
sce <- readRDS(paste0(fn, ".rds"))
table(colData(sce)$Sample)
# --------------------
# Quality control (QC)
# --------------------
# perform QC on sum UMI counts and number of detected genes
# note: not using mitochondrial proportion (due to biological reasons in LC-NE neurons)
# store QC metrics
sce <- addPerCellQC(sce, subsets = list(Mito = which(seqnames(sce) == "chrM")))
# check distributions
range(colData(sce)$sum)
range(colData(sce)$detected)
quantile(colData(sce)$sum, seq(0, 1, by = 0.1))
quantile(colData(sce)$detected, seq(0, 1, by = 0.1))
# check 3 median absolute deviations (MADs)
reasons <- perCellQCFilters(sce)
attr(reasons$low_lib_size, "thresholds")
attr(reasons$low_n_features, "thresholds")
# note: 3 MADs is outside range of values (minimum and maximum) for both sum sum
# UMIs and detected genes, so we keep all cells
colData(sce)$discard <- FALSE
# note high mitochondrial percentages
range(colData(sce)$subsets_Mito_percent)
quantile(colData(sce)$subsets_Mito_percent, seq(0, 1, by = 0.1))
quantile(colData(sce)$subsets_Mito_percent, seq(0.9, 1, by = 0.01))
mean(colData(sce)$subsets_Mito_percent > 10)
mean(colData(sce)$subsets_Mito_percent > 20)
# for easier plot legends
sce_plot <- sce
colData(sce_plot)$mito <- colData(sce)$subsets_Mito_percent
# plot QC metrics
p <- gridExtra::grid.arrange(
plotColData(sce_plot, x = "Sample", y = "sum", colour_by = "mito") +
scale_y_log10() + ggtitle("Total count"),
plotColData(sce_plot, x = "Sample", y = "detected", colour_by = "mito") +
scale_y_log10() + ggtitle("Detected genes"),
plotColData(sce_plot, x = "Sample", y = "mito", colour_by = "mito") +
ggtitle("Mitochondrial percent"),
ncol = 3
)
p
fn <- file.path(dir_plots, "QC_metrics")
ggsave(paste0(fn, ".pdf"), plot = p, width = 11, height = 3.5)
ggsave(paste0(fn, ".png"), plot = p, width = 11, height = 3.5)
# ------------------------------------
# Investigate mitochondrial proportion
# ------------------------------------
# investigate mitochondrial proportion in nuclei with expression of DBH and TH
# (i.e. supervised approximate identification of NE neuron nuclei)
ix_DBH <- which(rowData(sce)$gene_name == "DBH")
ix_TH <- which(rowData(sce)$gene_name == "TH")
ix_supervised <- counts(sce)[ix_DBH, ] > 0 & counts(sce)[ix_TH, ] > 0
# number of nuclei
table(ix_supervised)
# mitochondrial proportion in these nuclei
summary(colData(sce)$subsets_Mito_percent[ix_supervised])
quantile(colData(sce)$subsets_Mito_percent[ix_supervised], seq(0, 1, by = 0.1))
# number of nuclei with high proportion of mitochondrial reads
table(colData(sce)$subsets_Mito_percent[ix_supervised] > 20)
mean(colData(sce)$subsets_Mito_percent[ix_supervised] > 20)
sce_supervised <- sce[, ix_supervised]
# for easier plot legends
colData(sce_supervised)$mito <- colData(sce_supervised)$subsets_Mito_percent
# plot QC metrics
p <- gridExtra::grid.arrange(
plotColData(sce_supervised, x = "Sample", y = "sum", colour_by = "mito") +
scale_y_log10() + ggtitle("Total count"),
plotColData(sce_supervised, x = "Sample", y = "detected", colour_by = "mito") +
scale_y_log10() + ggtitle("Detected genes"),
plotColData(sce_supervised, x = "Sample", y = "mito", colour_by = "mito") +
ggtitle("Mitochondrial percent"),
ncol = 3
)
p
fn <- file.path(dir_plots, "QC_metrics_NEsupervised")
ggsave(paste0(fn, ".pdf"), plot = p, width = 11, height = 3.5)
ggsave(paste0(fn, ".png"), plot = p, width = 11, height = 3.5)
# plot QC metrics: mitochondrial only
plotColData(sce_supervised, x = "Sample", y = "mito", colour_by = "mito") +
ggtitle("Mitochondrial percent")
fn <- file.path(dir_plots, "QC_metrics_mito_NEsupervised")
ggsave(paste0(fn, ".pdf"), width = 4, height = 3.5)
ggsave(paste0(fn, ".png"), width = 4, height = 3.5)
# plot histogram of mitochondrial proportion
df <- as.data.frame(colData(sce_supervised)) %>%
select(c("Barcode", "subsets_Mito_percent"))
ggplot(df, aes(x = subsets_Mito_percent)) +
geom_histogram(bins = 20, fill = "navy") +
labs(x = "mitochondrial percentage") +
ggtitle("Supervised NE neuron nuclei") +
theme_bw()
fn <- here(dir_plots, paste0("histogram_mito_NEsupervised"))
ggsave(paste0(fn, ".pdf"), width = 4.5, height = 4)
ggsave(paste0(fn, ".png"), width = 4.5, height = 4)
# -----------
# Save object
# -----------
fn_out <- here("processed_data", "SCE", "sce_qualityControlled")
saveRDS(sce, paste0(fn_out, ".rds"))