-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQC.R
184 lines (136 loc) · 6.7 KB
/
QC.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
# ____________________________________________________________________________
# Script information ####
# title: Determine thresholds for QC metrics and filter barcodes
# author: Jose Alquicira Hernandez
# date: 2021-10-28
# description: Determines and plots thresholds for QC metrics across by pool
# ____________________________________________________________________________
# HPC details ####
# screen -S qc_thr
# qrsh -N qc_thr -l mem_requested=100G -pe smp 1 -q short.q
# conda activate r-4.1.1
# ____________________________________________________________________________
# Import libraries ####
# Primary
library("stringr")
library("here")
library("dsLib")
library("data.table")
library("purrr")
library("ggplot2")
library("ggrepel")
library("patchwork")
library("colorspace")
# ____________________________________________________________________________
# Set output ####
output <- set_output("2021-10-28", "qc_filter_barcodes")
# ____________________________________________________________________________
# Import data ####
md <- readRDS("results/2021-10-26_pre-qc_metadata_aggregation/metadata.RDS")
md[, pool_number := str_remove(pool, "pool_")]
md[, pool_number := factor(pool_number, sort(unique(as.integer(pool_number))))]
# 1,550,233 cells
# ____________________________________________________________________________
# Extract singlets ####
path <- file.path("../onek1k_joseah/data", "singlets", "barcodes_assigned_to_ppl.txt")
singlets <- fread(file = here(path))
singlets[, id := paste(str_remove(BARCODE, "-.*$"), str_remove(batch, "sample"), sep = "-")]
# Keep singlets only
md_s <- md[barcode %chin% singlets$id]
# Add individual information to metadata
singlets <- singlets[id %chin% md_s$barcode]
singlets <- singlets[,.(barcode = id, individual = PERSON),]
md_s <- merge(md_s, singlets, by = "barcode")
# 1,306,442 cells
# ____________________________________________________________________________
# Get thresholds for QC metrics ####
# Aggregate data by pool
md_s_pool <- md_s[, .(data = list(.SD)), by = .(pool, pool_number)]
# Create function to get the cutoffs based on Z-scores
get_thrs <- function(x, metric, mad_lower = 3, mad_upper = 2){
## Retrieve metric if interest across all pools
value <- x[[metric]]
## Get cutoffs depending on SDs
x_median <- median(value)
x_mad <- mad(value)
higher <- x_median + mad_upper*x_mad
lower <- x_median - mad_lower*x_mad
# Return lower and upper thresholds
list(lower = lower, higher = higher)
}
# Get thresholds
md_s_pool[, nCount_RNA_thr := lapply(data, get_thrs, metric = "nCount_RNA", mad_lower = 2, mad_upper = Inf)]
md_s_pool[, nFeature_RNA_thr := lapply(data, get_thrs, metric = "nFeature_RNA", mad_lower = 2, mad_upper = Inf)]
md_s_pool[, percent.mt_thr := lapply(data, get_thrs, metric = "percent.mt", mad_lower = Inf, mad_upper = 3)]
# Tidy threshold info for plotting
nCount_RNA_thr <- md_s_pool[ ,.(nCount_RNA = map_dbl(nCount_RNA_thr, "lower"), pool_number)]
nFeature_RNA_thr <- md_s_pool[ ,.(nFeature_RNA = map_dbl(nFeature_RNA_thr, "lower"), pool_number)]
percent.mt_thr <- md_s_pool[ ,.(percent.mt = map_dbl(percent.mt_thr, "higher"), pool_number)]
# ____________________________________________________________________________
# Plot distributions + thresholds ####
p1 <- ggplot(md_s, aes(pool_number, nCount_RNA)) +
geom_boxplot(outlier.size = 0.15, lwd = 0.35) +
geom_line(aes(group = 1), data = nCount_RNA_thr, color = "red") +
xlab("") +
ylab("Number of UMIs") +
theme_classic() +
rotate_x()
p2 <- ggplot(md_s, aes(pool_number, nFeature_RNA)) +
geom_boxplot(outlier.size = 0.15, lwd = 0.35) +
geom_line(aes(group = 1), data = nFeature_RNA_thr, color = "red") +
xlab("") +
ylab("Number of genes") +
theme_classic() +
rotate_x()
p3 <- ggplot(md_s, aes(pool_number, percent.mt)) +
geom_boxplot(outlier.size = 0.15, lwd = 0.35) +
geom_line(aes(group = 1), data = percent.mt_thr, color = "red") +
xlab("Pool") +
ylab("% mitochondrial expression") +
theme_classic() +
rotate_x()
p <- p1 / p2 / p3
ggsave(here(output, "qc_metrics.png"), width = 8, height = 9, dpi = "print")
ggsave(here(output, "qc_metrics.pdf"), width = 8, height = 9, dpi = "print")
# ____________________________________________________________________________
# Filter data ####
# Get logical vectors per metric
md_s_pool[, nCount_RNA_pass := mapply(function(x, thr){
x$nCount_RNA %between% thr
}, data, nCount_RNA_thr, SIMPLIFY = FALSE)]
md_s_pool[, nFeature_RNA_pass := mapply(function(x, thr){
x$nFeature_RNA %between% thr
}, data, nFeature_RNA_thr, SIMPLIFY = FALSE)]
md_s_pool[, percent.mt_pass := mapply(function(x, thr){
x$percent.mt %between% thr
}, data, percent.mt_thr, SIMPLIFY = FALSE)]
# Get consensus
md_s_pool[, index := mapply(function(x, y, z){
x & y & z
}, nCount_RNA_pass, nFeature_RNA_pass, percent.mt_pass, SIMPLIFY = FALSE)]
# Filter data based on consensus
md_s_pool[, data_filter := mapply(function(x, i) x[i, ], data, index, SIMPLIFY = FALSE)]
# Get barcodes after QC
barcodes <- unlist(sapply(md_s_pool$data_filter, function(x) x$barcode))
# Subset metadata
md_qc <- md_s[barcode %chin% barcodes]
# Compare cell type proportions before and after QC
summary <- list(pre = md_s[, .(perc_pre = .N / nrow(md) * 100), predicted.celltype.l2], post = md_qc[, .(perc_post = .N / nrow(md) * 100), predicted.celltype.l2])
all_summ <- reduce(summary, merge, by = "predicted.celltype.l2")
all_summ[, ratio := perc_pre / perc_post]
p_ratio <- ggplot(all_summ, aes(perc_pre, perc_post, color = ratio)) +
geom_point() +
xlab(expression("log"[10]*"(% of cells "*italic(before)*" QC)")) +
ylab(expression("log"[10]*"(% of cells "*italic(after)*" QC)")) +
scale_x_log10() +
scale_y_log10() +
geom_abline(slope = 1, intercept = 0) +
geom_text_repel(aes(label = predicted.celltype.l2), size = 2) +
scale_color_continuous_diverging(palette = "Berlin", name = "Ratio\n(pre/post QC)") +
theme_classic()
ggsave(here(output, "cell_proportions.png"), width = 6, height = 4, dpi = "print")
# Save data ---------------------------------------------------------------
saveRDS(md_qc, file = here(output, "QC.RDS"))
# 1,249,037 cells
# Session info ------------------------------------------------------------
print_session(here(output))