-
Notifications
You must be signed in to change notification settings - Fork 0
/
PBMC3_filtering.qmd
236 lines (172 loc) · 4.38 KB
/
PBMC3_filtering.qmd
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
---
title: Filtering of PBMC3 using COTAN
---
Library import
```{r}
library(dplyr)
library(COTAN)
library(Seurat)
library(tibble)
library(ggplot2)
library(zeallot)
library(DropletUtils)
```
Settings
```{r}
datasetName = 'PBMC3'
datasetFolder = './data/'
inDir = paste(datasetFolder, datasetName, '/raw/10X/', sep='')
outDir = paste(datasetFolder, datasetName, '/filtered/', sep='')
dir10X = paste(outDir, '10X/', sep='')
if (!dir.exists(outDir)) {
dir.create(outDir, recursive = TRUE, showWarnings = FALSE)
}
setLoggingLevel(2)
setLoggingFile(paste(outDir, "logfile.log", sep=""))
options(parallelly.fork.enable = TRUE)
```
Data loading
```{r}
dataset = Read10X(data.dir = inDir, strip.suffix = TRUE)
dataset = dataset[[1]]
sampleCond <- datasetName
PBMC3 <- COTAN(raw = dataset)
PBMC3 <- initializeMetaDataset(
PBMC3,
GEO = paste("10X ", datasetName, sep=""),
sequencingMethod = "10X",
sampleCond = sampleCond
)
```
Inspect cells’ sizes
```{r}
cellSizePlot(PBMC3)
```
Drop cells with too many reads as they are probably doublets
```{r}
cellsSizeThr <- 20000
PBMC3 <- addElementToMetaDataset(PBMC3, "Cells size threshold", cellsSizeThr)
cellsToRem <- getCells(PBMC3)[getCellsSize(PBMC3) > cellsSizeThr]
PBMC3 <- dropGenesCells(PBMC3, cells = cellsToRem)
cellSizePlot(PBMC3, splitPattern = "-", numCol = 2)
```
Inspect the number of expressed genes per cell
```{r}
genesSizePlot(PBMC3, splitPattern = "-", numCol = 2)
```
Drop cells with too high genes expression as they are probably doublets
```{r}
geneSizeThr <- 3500
PBMC3 <- addElementToMetaDataset(PBMC3, "Num genes threshold", geneSizeThr)
numExprGenes <- getNumExpressedGenes(PBMC3)
cellsToRem <- names(numExprGenes)[numExprGenes > geneSizeThr]
PBMC3 <- dropGenesCells(PBMC3, cells = cellsToRem)
genesSizePlot(PBMC3, splitPattern = "-", numCol = 2)
```
Check number of mithocondrial genes expressed in each cell
```{r}
mitGenesPattern <- "^[Mm][Tt]-"
getGenes(PBMC3)[grep(mitGenesPattern, getGenes(PBMC3))]
```
```{r}
c(mitPlot, mitSizes) %<-%
mitochondrialPercentagePlot(PBMC3, genePrefix = mitGenesPattern,
splitPattern = "-", numCol = 2)
plot(mitPlot)
```
We drop cells with a too high percentage of mitocondrial genes (are likely dead)
```{r}
mitPercThr <- 10
PBMC3 <- addElementToMetaDataset(PBMC3, "Mitoc. perc. threshold", mitPercThr)
cellsToRem <- rownames(mitSizes)[mitSizes[["mit.percentage"]] > mitPercThr]
PBMC3 <- dropGenesCells(PBMC3, cells = cellsToRem)
c(mitPlot, mitSizes) %<-%
mitochondrialPercentagePlot(PBMC3, genePrefix = mitGenesPattern,
splitPattern = "-", numCol = 2)
plot(mitPlot)
```
Check number of ribosomial genes expressed in each cell
```{r}
ribGenesPattern <- "^RP[SL]\\d+"
getGenes(PBMC3)[grep(ribGenesPattern, getGenes(PBMC3))]
```
```{r}
c(ribPlot, ribSizes) %<-%
mitochondrialPercentagePlot(PBMC3, genePrefix = ribGenesPattern,
splitPattern = "-", numCol = 2)
plot(ribPlot)
```
Check no further outliers after all the culling
```{r}
cellSizePlot(PBMC3, splitPattern = "-", numCol = 2)
```
```{r}
genesSizePlot(PBMC3, splitPattern = "-", numCol = 2)
```
Cleaning, round 1
```{r}
PBMC3 <- clean(PBMC3)
c(pcaCellsPlot, pcaCellsData, genesPlot, UDEPlot, nuPlot, zoomedNuPlot) %<-% cleanPlots(PBMC3)
plot(pcaCellsPlot)
```
```{r}
plot(genesPlot)
```
```{r}
PBMC3 <- addElementToMetaDataset(PBMC3, "Num drop B group", 0)
```
```{r}
plot(UDEPlot)
```
```{r}
plot(nuPlot)
```
```{r}
plot(zoomedNuPlot)
```
```{r}
yset=0.16
nuDf <- data.frame("nu" = sort(getNu(PBMC3)), "n" = seq_along(getNu(PBMC3)))
PBMC3 <- addElementToMetaDataset(PBMC3, "Threshold low UDE cells:", yset)
cellsToRem <-rownames(nuDf)[nuDf[["nu"]] < yset]
PBMC3 <- dropGenesCells(PBMC3, cells = cellsToRem)
```
Cleaning, round 2
```{r}
PBMC3 <- clean(PBMC3)
c(pcaCellsPlot, pcaCellsData, genesPlot, UDEPlot, nuPlot, zoomedNuPlot) %<-% cleanPlots(PBMC3)
plot(pcaCellsPlot)
```
```{r}
plot(pcaCellsData)
```
```{r}
plot(genesPlot)
```
```{r}
plot(UDEPlot)
```
```{r}
plot(nuPlot)
```
```{r}
plot(zoomedNuPlot)
```
```{r}
plot(cellSizePlot(PBMC3, splitPattern = "-", numCol = 2))
```
```{r}
plot(genesSizePlot(PBMC3, splitPattern = "-", numCol = 2))
```
Save the filtered dataset
```{r}
if (!dir.exists(dir10X)) {
write10xCounts(dir10X, getRawData(PBMC3))
}
```
```{r}
saveRDS(PBMC3, file = paste0(outDir, sampleCond, ".cotan.RDS"))
```
```{r}
sessionInfo()
```