forked from hemberg-lab/scRNA.seq.course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
07-exprs-qc-reads.Rmd
258 lines (222 loc) · 6.71 KB
/
07-exprs-qc-reads.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
---
output: html_document
---
# Expression QC (Reads)
This chapter contains the summary plots and tables for the QC exercise based on the reads for the Bischak data discussed in the previous chapter.
```{r, message=FALSE, warning=FALSE}
library(scater, quietly = TRUE)
library(knitr)
options(stringsAsFactors = FALSE)
```
```{r, echo=FALSE}
opts_chunk$set(out.width='90%', fig.align = 'center')
```
```{r}
reads <- read.table("blischak/reads.txt", sep = "\t")
anno <- read.table("blischak/annotation.txt", sep = "\t", header = TRUE)
```
```{r}
knitr::kable(
head(reads[ , 1:3]), booktabs = TRUE,
caption = 'A table of the first 6 rows and 3 columns of the molecules table.'
)
knitr::kable(
head(anno), booktabs = TRUE,
caption = 'A table of the first 6 rows of the anno table.'
)
```
```{r}
pheno_data <- new("AnnotatedDataFrame", anno)
rownames(pheno_data) <- pheno_data$sample_id
reads <- scater::newSCESet(
countData = reads,
phenoData = pheno_data
)
```
```{r}
keep_feature <- rowSums(counts(reads) > 0) > 0
reads <- reads[keep_feature, ]
```
```{r}
ercc <- featureNames(reads)[grepl("ERCC-", featureNames(reads))]
mt <- c("ENSG00000198899", "ENSG00000198727", "ENSG00000198888",
"ENSG00000198886", "ENSG00000212907", "ENSG00000198786",
"ENSG00000198695", "ENSG00000198712", "ENSG00000198804",
"ENSG00000198763", "ENSG00000228253", "ENSG00000198938",
"ENSG00000198840")
```
```{r}
reads <- scater::calculateQCMetrics(
reads,
feature_controls = list(ERCC = ercc, MT = mt)
)
```
```{r total-counts-hist-reads, fig.cap = "Histogram of library sizes for all cells"}
hist(
reads$total_counts,
breaks = 100
)
abline(v = 1.3e6, col = "red")
```
```{r}
filter_by_total_counts <- (reads$total_counts > 1.3e6)
```
```{r}
knitr::kable(
as.data.frame(table(filter_by_total_counts)),
booktabs = TRUE,
row.names = FALSE,
caption = 'The number of cells removed by total counts filter (FALSE)'
)
```
```{r total-features-hist-reads, fig.cap = "Histogram of the number of detected genes in all cells"}
hist(
reads$total_features,
breaks = 100
)
abline(v = 7000, col = "red")
```
```{r}
filter_by_expr_features <- (reads$total_features > 7000)
```
```{r}
knitr::kable(
as.data.frame(table(filter_by_expr_features)),
booktabs = TRUE,
row.names = FALSE,
caption = 'The number of cells removed by total features filter (FALSE)'
)
```
```{r mt-vs-counts-reads, fig.cap = "Percentage of counts in MT genes"}
scater::plotPhenoData(
reads,
aes_string(x = "total_features",
y = "pct_counts_feature_controls_MT",
colour = "batch")
)
```
```{r ercc-vs-counts-reads, fig.cap = "Percentage of counts in ERCCs"}
scater::plotPhenoData(
reads,
aes_string(x = "total_features",
y = "pct_counts_feature_controls_ERCC",
colour = "batch")
)
```
```{r}
filter_by_ERCC <- reads$batch != "NA19098.r2" &
reads$pct_counts_feature_controls_ERCC < 25
```
```{r}
knitr::kable(
as.data.frame(table(filter_by_ERCC)),
booktabs = TRUE,
row.names = FALSE,
caption = 'The number of cells removed by ERCC filter (FALSE)'
)
```
```{r}
filter_by_MT <- reads$pct_counts_feature_controls_MT < 30
```
```{r}
knitr::kable(
as.data.frame(table(filter_by_MT)),
booktabs = TRUE,
row.names = FALSE,
caption = 'The number of cells removed by MT filter (FALSE)'
)
```
```{r}
reads$use <- (
# sufficient features (genes)
filter_by_expr_features &
# sufficient molecules counted
filter_by_total_counts &
# sufficient endogenous RNA
filter_by_ERCC &
# remove cells with unusual number of reads in MT genes
filter_by_MT
)
```
```{r}
knitr::kable(
as.data.frame(table(reads$use)),
booktabs = TRUE,
row.names = FALSE,
caption = 'The number of cells removed by manual filter (FALSE)'
)
```
```{r}
reads$use_default <- (
# remove cells with unusual numbers of genes
!reads$filter_on_total_features &
# sufficient molecules counted
!reads$filter_on_total_counts &
# sufficient endogenous RNA
!reads$filter_on_pct_counts_feature_controls_ERCC &
# remove cells with unusual number of reads in MT genes
!reads$filter_on_pct_counts_feature_controls_MT &
# controls shouldn't be used in downstream analysis
!reads$is_cell_control
)
```
```{r}
knitr::kable(
as.data.frame(table(reads$use_default)),
booktabs = TRUE,
row.names = FALSE,
caption = 'The number of cells removed by default filter (FALSE)'
)
```
```{r auto-cell-filt-reads, fig.align='center', fig.cap="PCA plot used for automatic detection of cell outliers", message=FALSE, warning=FALSE, out.width='90%'}
reads <-
scater::plotPCA(reads,
size_by = "total_features",
shape_by = "use",
pca_data_input = "pdata",
detect_outliers = TRUE,
return_SCESet = TRUE)
```
```{r}
knitr::kable(
as.data.frame(table(reads$outlier)),
booktabs = TRUE,
row.names = FALSE,
caption = 'The number of cells removed by automatic filter (FALSE)'
)
```
```{r cell-filt-comp-reads, fig.cap = "Comparison of the default, automatic and manual cell filters"}
def <- colnames(reads)[!reads$use_default]
auto <- colnames(reads)[reads$outlier]
man <- colnames(reads)[!reads$use]
venn.diag <- limma::vennCounts(cbind(colnames(reads) %in% def,
colnames(reads) %in% auto,
colnames(reads) %in% man))
limma::vennDiagram(venn.diag,
names = c("Default", "Automatic", "Manual"),
circle.col = c("magenta", "blue", "green"))
```
```{r top50-gene-expr-reads, fig.cap = "Number of total counts consumed by the top 50 expressed genes", fig.asp = 1}
scater::plotQC(reads, type = "highest-expression")
```
```{r}
filter_genes <- apply(counts(reads[, pData(reads)$use]), 1,
function(x) length(x[x > 1]) >= 2)
fData(reads)$use <- filter_genes
```
```{r}
knitr::kable(
as.data.frame(table(filter_genes)),
booktabs = TRUE,
row.names = FALSE,
caption = 'The number of genes removed by gene filter (FALSE)'
)
```
```{r}
dim(reads[fData(reads)$use, pData(reads)$use])
```
```{r}
saveRDS(reads, file = "blischak/reads.rds")
```
If you want to further check yourself you can download our [`reads`](http://hemberg-lab.github.io/scRNA.seq.course/blischak/reads.rds) object. If you followed the steps above it should be exactly the same as yours.
By comparing Figure \@ref(fig:cell-filt-comp) and Figure \@ref(fig:cell-filt-comp-reads), it is clear that the reads based filtering removed 49 more cells than the UMI based analysis. If you go back and compare the results you should be able to conclude that the ERCC and MT filters are more strict for the reads-based analysis.