forked from hemberg-lab/scRNA.seq.course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
15-exprs-qc-reads.Rmd
203 lines (172 loc) · 4.26 KB
/
15-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
---
output: html_document
---
## Expression QC (Reads)
```{r, echo=FALSE}
library(knitr)
opts_chunk$set(out.width='90%', fig.align = 'center')
```
```{r, message=FALSE, warning=FALSE}
library(SingleCellExperiment)
library(scater)
options(stringsAsFactors = FALSE)
```
```{r}
reads <- read.table("tung/reads.txt", sep = "\t")
anno <- read.table("tung/annotation.txt", sep = "\t", header = TRUE)
```
```{r}
head(reads[ , 1:3])
head(anno)
```
```{r}
reads <- SingleCellExperiment(
assays = list(counts = as.matrix(reads)),
colData = anno
)
```
```{r}
keep_feature <- rowSums(counts(reads) > 0) > 0
reads <- reads[keep_feature, ]
```
```{r}
isSpike(reads, "ERCC") <- grepl("^ERCC-", rownames(reads))
isSpike(reads, "MT") <- rownames(reads) %in%
c("ENSG00000198899", "ENSG00000198727", "ENSG00000198888",
"ENSG00000198886", "ENSG00000212907", "ENSG00000198786",
"ENSG00000198695", "ENSG00000198712", "ENSG00000198804",
"ENSG00000198763", "ENSG00000228253", "ENSG00000198938",
"ENSG00000198840")
```
```{r}
reads <- calculateQCMetrics(
reads,
feature_controls = list(
ERCC = isSpike(reads, "ERCC"),
MT = isSpike(reads, "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}
table(filter_by_total_counts)
```
```{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}
table(filter_by_expr_features)
```
```{r mt-vs-counts-reads, fig.cap = "Percentage of counts in MT genes"}
plotPhenoData(
reads,
aes_string(
x = "total_features",
y = "pct_counts_MT",
colour = "batch"
)
)
```
```{r ercc-vs-counts-reads, fig.cap = "Percentage of counts in ERCCs"}
plotPhenoData(
reads,
aes_string(
x = "total_features",
y = "pct_counts_ERCC",
colour = "batch"
)
)
```
```{r}
filter_by_ERCC <-
reads$batch != "NA19098.r2" & reads$pct_counts_ERCC < 25
table(filter_by_ERCC)
filter_by_MT <- reads$pct_counts_MT < 30
table(filter_by_MT)
```
```{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}
table(reads$use)
```
```{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 <- plotPCA(
reads,
size_by = "total_features",
shape_by = "use",
pca_data_input = "pdata",
detect_outliers = TRUE,
return_SCE = TRUE
)
```
```{r}
table(reads$outlier)
```
```{r cell-filt-comp-reads, fig.cap = "Comparison of the default, automatic and manual cell filters"}
library(limma)
auto <- colnames(reads)[reads$outlier]
man <- colnames(reads)[!reads$use]
venn.diag <- vennCounts(
cbind(colnames(reads) %in% auto,
colnames(reads) %in% man)
)
vennDiagram(
venn.diag,
names = c("Automatic", "Manual"),
circle.col = c("blue", "green")
)
```
```{r top50-gene-expr-reads, fig.cap = "Number of total counts consumed by the top 50 expressed genes", fig.asp = 1}
plotQC(reads, type = "highest-expression")
```
```{r}
filter_genes <- apply(
counts(reads[, colData(reads)$use]),
1,
function(x) length(x[x > 1]) >= 2
)
rowData(reads)$use <- filter_genes
```
```{r}
table(filter_genes)
```
```{r}
dim(reads[rowData(reads)$use, colData(reads)$use])
```
```{r}
assay(reads, "logcounts_raw") <- log2(counts(reads) + 1)
reducedDim(reads) <- NULL
```
```{r}
saveRDS(reads, file = "tung/reads.rds")
```
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 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.
```{r}
sessionInfo()
```