-
Notifications
You must be signed in to change notification settings - Fork 3
/
08-reduccion_dimensiones.R
256 lines (195 loc) · 6.95 KB
/
08-reduccion_dimensiones.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
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
## ---- warning=FALSE, message=FALSE----------------------
library(scRNAseq)
sce.zeisel <- ZeiselBrainData(ensembl = TRUE)
# Estos datos contienen tipos celulares previamente anotados
table(sce.zeisel$level1class)
## ---- warning=FALSE, message=FALSE----------------------
# Quality control
# Descartar celulas con alto contenido mitocondrial o con alto porcentaje de spike-ins
library(scater)
is.mito <- which(rowData(sce.zeisel)$featureType == "mito")
stats <- perCellQCMetrics(sce.zeisel,
subsets = list(Mt = is.mito)
)
qc <- quickPerCellQC(stats,
percent_subsets = c("altexps_ERCC_percent", "subsets_Mt_percent")
)
sce.zeisel <- sce.zeisel[, !qc$discard]
## ---- warning=FALSE, message=FALSE----------------------
# normalization
# encontramos unos clusters rápidos para las células y usamos esa información para calcular los factores de tamaño
library(scran)
set.seed(1000)
clusters <- quickCluster(sce.zeisel)
sce.zeisel <- computeSumFactors(sce.zeisel,
cluster = clusters
)
sce.zeisel <- logNormCounts(sce.zeisel)
## ---- warning=FALSE, message=FALSE----------------------
# variance-modelling
dec.zeisel <- modelGeneVarWithSpikes(
sce.zeisel,
"ERCC"
)
top.zeisel <- getTopHVGs(dec.zeisel, n = 2000)
## ---- warning=FALSE, message=FALSE----------------------
library(BiocFileCache)
bfc <- BiocFileCache()
raw.path <- bfcrpath(bfc, file.path(
"http://cf.10xgenomics.com/samples",
"cell-exp/2.1.0/pbmc4k/pbmc4k_raw_gene_bc_matrices.tar.gz"
))
untar(raw.path, exdir = file.path(tempdir(), "pbmc4k"))
## ---- warning=FALSE, message=FALSE----------------------
library(DropletUtils)
library(Matrix)
fname <- file.path(tempdir(), "pbmc4k/raw_gene_bc_matrices/GRCh38")
sce.pbmc <- read10xCounts(fname, col.names = TRUE)
## ---- warning=FALSE, message=FALSE----------------------
# gene-annotation
library(scater)
rownames(sce.pbmc) <- uniquifyFeatureNames(
rowData(sce.pbmc)$ID, rowData(sce.pbmc)$Symbol
)
library(EnsDb.Hsapiens.v86)
location <- mapIds(EnsDb.Hsapiens.v86,
keys = rowData(sce.pbmc)$ID,
column = "SEQNAME", keytype = "GENEID"
)
# cell-detection
set.seed(100)
e.out <- emptyDrops(counts(sce.pbmc))
sce.pbmc <- sce.pbmc[, which(e.out$FDR <= 0.001)]
## ---- warning=FALSE, message=FALSE----------------------
# quality-control
stats <- perCellQCMetrics(sce.pbmc,
subsets = list(Mito = which(location == "MT"))
)
high.mito <- isOutlier(stats$subsets_Mito_percent,
type = "higher"
)
sce.pbmc <- sce.pbmc[, !high.mito]
# normalization
library(scran)
set.seed(1000)
clusters <- quickCluster(sce.pbmc)
sce.pbmc <- computeSumFactors(sce.pbmc, cluster = clusters)
sce.pbmc <- logNormCounts(sce.pbmc)
## -------------------------------------------------------
# variance modelling
set.seed(1001)
dec.pbmc <- modelGeneVarByPoisson(sce.pbmc)
top.pbmc <- getTopHVGs(dec.pbmc, prop = 0.1)
## ---- warning=FALSE, message=FALSE----------------------
library(scran)
library(scater)
set.seed(100)
sce.zeisel <- runPCA(sce.zeisel,
subset_row = top.zeisel
)
## ---- warning=FALSE, message=FALSE, fig.dim = c(5, 4)----
library(PCAtools)
percent.var <- attr(reducedDim(sce.zeisel), "percentVar")
chosen.elbow <- PCAtools::findElbowPoint(percent.var)
plot(percent.var, xlab = "PC", ylab = "Variance explained (%)")
abline(v = chosen.elbow, col = "red")
## -------------------------------------------------------
choices <- getClusteredPCs(reducedDim(sce.zeisel))
chosen.clusters <- metadata(choices)$chosen
plot(choices$n.pcs, choices$n.clusters,
xlab = "Number of PCs", ylab = "Number of clusters"
)
abline(a = 1, b = 1, col = "red")
abline(v = chosen.clusters, col = "grey80", lty = 2)
## -------------------------------------------------------
set.seed(100)
# Compute and store the 'full' set of PCs
sce.zeisel <- runPCA(sce.zeisel, subset_row = top.zeisel)
# Can also select d and store the reduced set of PCs
# e.g., using the elbow point
reducedDim(sce.zeisel, "PCA_elbow") <- reducedDim(
sce.zeisel, "PCA"
)[, 1:chosen.elbow]
# e.g., based on population structure
reducedDim(sce.zeisel, "PCA_clusters") <- reducedDim(
sce.zeisel, "PCA"
)[, 1:chosen.clusters]
## ---- warning=FALSE, message=FALSE----------------------
library(scran)
set.seed(111001001)
denoised.pbmc <- denoisePCA(sce.pbmc,
technical = dec.pbmc, subset.row = top.pbmc
)
## -------------------------------------------------------
dim(reducedDim(denoised.pbmc, "PCA"))
## -------------------------------------------------------
dec.pbmc2 <- modelGeneVar(sce.pbmc)
denoised.pbmc2 <- denoisePCA(sce.pbmc,
technical = dec.pbmc2, subset.row = top.pbmc
)
dim(reducedDim(denoised.pbmc2))
## -------------------------------------------------------
set.seed(001001001)
denoised.zeisel <- denoisePCA(sce.zeisel,
technical = dec.zeisel, subset.row = top.zeisel
)
dim(reducedDim(denoised.zeisel))
## ---- fig.dim = c(6, 4)---------------------------------
plotReducedDim(sce.zeisel, dimred = "PCA")
## ---- fig.dim = c(6, 4)---------------------------------
plotReducedDim(sce.zeisel,
dimred = "PCA",
colour_by = "level1class"
)
## ---- fig.dim = c(6, 4)---------------------------------
plotReducedDim(sce.zeisel,
dimred = "PCA",
ncomponents = 4, colour_by = "level1class"
)
## ---- fig.dim = c(5, 4)---------------------------------
set.seed(00101001101)
sce.zeisel <- runTSNE(sce.zeisel, dimred = "PCA")
plotReducedDim(sce.zeisel, dimred = "TSNE", colour_by = "level1class")
## ---- fig.dim = c(6, 4)---------------------------------
set.seed(100)
sce.zeisel <- runTSNE(sce.zeisel,
dimred = "PCA",
perplexity = 30
)
plotReducedDim(sce.zeisel,
dimred = "TSNE",
colour_by = "level1class"
)
## ---- fig.width = 21------------------------------------
set.seed(100)
sce.zeisel <- runTSNE(sce.zeisel, dimred = "PCA", perplexity = 5)
p1 <- plotReducedDim(sce.zeisel, dimred = "TSNE", colour_by = "level1class")
sce.zeisel <- runTSNE(sce.zeisel, dimred = "PCA", perplexity = 20)
p2 <- plotReducedDim(sce.zeisel, dimred = "TSNE", colour_by = "level1class")
sce.zeisel <- runTSNE(sce.zeisel, dimred = "PCA", perplexity = 80)
p3 <- plotReducedDim(sce.zeisel, dimred = "TSNE", colour_by = "level1class")
library("patchwork")
p1 + p2 + p3
## ---- fig.dim = c(6, 4)---------------------------------
set.seed(1100101001)
sce.zeisel <- runUMAP(sce.zeisel, dimred = "PCA")
plotReducedDim(sce.zeisel,
dimred = "UMAP",
colour_by = "level1class"
)
## ---- fig.dim = c(6, 4)---------------------------------
set.seed(100)
sce.zeisel <- runUMAP(sce.zeisel,
dimred = "PCA",
n_neighbors = 15
)
plotReducedDim(sce.zeisel,
dimred = "UMAP",
colour_by = "level1class"
)
## -------------------------------------------------------
## Información de la sesión de R
Sys.time()
proc.time()
options(width = 120)
sessioninfo::session_info()