-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzinbwave-seurat.rmd
248 lines (196 loc) · 11.5 KB
/
zinbwave-seurat.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
---
title: "zinbwave-seurat"
author: "JP Cartailler | Creative Data Solutions | Vanderbilt University"
date: "September 19, 2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## ZINB-Wave / Seurat working example
It turns out that the documentation for ZINB-WaVE is dependent on Seurat 2.3.0. Due to Seurat's rapid development lifecycle, most people are probably on newer versions and as such, things break when trying to use the ZINB-WaVE-derived Seurat object.
## How/What to install
1. Start with a fresh environment (or remove Seurat from your environment)
2. You’ll need Rtools installed (https://cran.r-project.org/bin/windows/Rtools/) – If you are on Windows and running R3.5.x and installed Rtools 3.5, there’s a bug. After you install Rtools, start an R console and execute “find_rtools()”. If that comes back TRUE, then it’s working. If FALSE, then apply the fix (find it here).
3. Start a R console or Rgui (not RStudio)
4. Install Seurat 2.3.0, from source:
```
R> library(devtools)
R> devtools::install_github("satijalab/seurat@v2.3.0")
```
5. Install ZINB-WaVE via the ‘biocLite("zinbwave")’ BUT do not let upgrade Seurat (it wants to), so allow it to do a partial and allow the non-Seurat packages to be upgraded.
## ZINB-WaVE example code
(lifted from their Vignette at http://bioconductor.org/packages/release/bioc/html/zinbwave.html)
```{r zinbwave}
# src: https://bioconductor.org/packages/release/bioc/vignettes/zinbwave/inst/doc/intro.R
#
#
## ---- eval=FALSE-----------------------------------------------------------
# source("https://bioconductor.org/biocLite.R")
# biocLite("zinbwave")
## ----options, include=FALSE, echo=FALSE------------------------------------
knitr::opts_chunk$set(warning=FALSE, error=FALSE, message=FALSE)
set.seed(1133)
## ----load_packs------------------------------------------------------------
library(zinbwave)
library(scRNAseq)
library(matrixStats)
library(magrittr)
library(ggplot2)
library(biomaRt)
# Register BiocParallel Serial Execution
BiocParallel::register(BiocParallel::SerialParam())
## ----pollen----------------------------------------------------------------
data("fluidigm")
fluidigm
table(colData(fluidigm)$Coverage_Type)
## ----filter----------------------------------------------------------------
filter <- rowSums(assay(fluidigm)>5)>5
table(filter)
fluidigm <- fluidigm[filter,]
## ----variance--------------------------------------------------------------
assay(fluidigm) %>% log1p %>% rowVars -> vars
names(vars) <- rownames(fluidigm)
vars <- sort(vars, decreasing = TRUE)
head(vars)
fluidigm <- fluidigm[names(vars)[1:100],]
## ----rename----------------------------------------------------------------
assayNames(fluidigm)[1] <- "counts"
## ----zinbwave--------------------------------------------------------------
fluidigm_zinb <- zinbwave(fluidigm, K = 2, epsilon=1000)
## ----zinb_plot-------------------------------------------------------------
W <- reducedDim(fluidigm_zinb)
data.frame(W, bio=colData(fluidigm)$Biological_Condition,
coverage=colData(fluidigm)$Coverage_Type) %>%
ggplot(aes(W1, W2, colour=bio, shape=coverage)) + geom_point() +
scale_color_brewer(type = "qual", palette = "Set1") + theme_classic()
## ----zinb_coverage---------------------------------------------------------
fluidigm_cov <- zinbwave(fluidigm, K=2, X="~Coverage_Type", epsilon=1000)
## ----zinb_plot2------------------------------------------------------------
W <- reducedDim(fluidigm_cov)
data.frame(W, bio=colData(fluidigm)$Biological_Condition,
coverage=colData(fluidigm)$Coverage_Type) %>%
ggplot(aes(W1, W2, colour=bio, shape=coverage)) + geom_point() +
scale_color_brewer(type = "qual", palette = "Set1") + theme_classic()
## ----tsne------------------------------------------------------------------
set.seed(93024)
library(Rtsne)
W <- reducedDim(fluidigm_cov)
tsne_data <- Rtsne(W, pca = FALSE, perplexity=10, max_iter=5000)
data.frame(Dim1=tsne_data$Y[,1], Dim2=tsne_data$Y[,2],
bio=colData(fluidigm)$Biological_Condition,
coverage=colData(fluidigm)$Coverage_Type) %>%
ggplot(aes(Dim1, Dim2, colour=bio, shape=coverage)) + geom_point() +
scale_color_brewer(type = "qual", palette = "Set1") + theme_classic()
## ----zinb------------------------------------------------------------------
zinb <- zinbFit(fluidigm, K=2, epsilon=1000)
## ----zinbwave2-------------------------------------------------------------
fluidigm_zinb <- zinbwave(fluidigm, fitted_model = zinb, K = 2, epsilon=1000)
```
## Seurat
Build the Seurat object
```{r seurat2}
library(Seurat)
seu <- CreateSeuratObject(raw.data = counts(fluidigm_zinb))
dim_jp <- reducedDim(fluidigm_zinb, "zinbwave")
seu <- SetDimReduction(object = seu, reduction.type = "zinbwave",
slot = "cell.embeddings",
new.data = dim_jp)
seu <- SetDimReduction(object = seu, reduction.type = "zinbwave", slot = "key",
new.data = "zinbwave")
seu <- FindClusters(object = seu, reduction.type = "zinbwave",
dims.use = 1:2, #this should match K
resolution = 0.6, print.output = 1, save.SNN = TRUE)
```
and do Seurat things with it:
```{r seurat}
# JP
jp <- seu
rownames(x = jp@data)
dim(jp@data)
mito.genes <- grep(pattern = "^N", x = rownames(x = jp@data), value = TRUE)
percent.mito <- Matrix::colSums(jp@raw.data[mito.genes, ])/Matrix::colSums(jp@raw.data)
jp <- AddMetaData(object = jp, metadata = percent.mito, col.name = "percent.mito")
VlnPlot(object = jp, features.plot = c("nGene", "nUMI", "percent.mito"), nCol = 3)
par(mfrow = c(1, 2))
GenePlot(object = jp, gene1 = "nUMI", gene2 = "percent.mito")
GenePlot(object = jp, gene1 = "nUMI", gene2 = "nGene")
# didn't filter b/c there aren't really any mito genes
#jp <- FilterCells(object = jp, subset.names = c("nGene", "percent.mito"),
# low.thresholds = c(200, -Inf), high.thresholds = c(2500, 0.05))
# normalize data
jp <- NormalizeData(object = jp, normalization.method = "LogNormalize",
scale.factor = 10000)
# scale data
jp <- ScaleData(object = jp, vars.to.regress = c("nUMI", "percent.mito"))
jp <- FindVariableGenes(object = jp, mean.function = ExpMean, dispersion.function = LogVMR)
jp <- RunPCA(object = jp, pc.genes = jp@var.genes, do.print = TRUE, pcs.print = 1:5,
genes.print = 5)
jp <- ProjectPCA(object = jp, do.print = FALSE)
VizPCA(object = jp, pcs.use = 1:2)
PCAPlot(object = jp, dim.1 = 1, dim.2 = 2)
```
## R Session
```
> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] parallel stats4 stats graphics grDevices utils datasets methods base
other attached packages:
[1] Seurat_2.3.0 Matrix_1.2-14 cowplot_0.9.3
[4] Rtsne_0.13 scRNAseq_1.6.0 biomaRt_2.36.1
[7] ggplot2_3.0.0 magrittr_1.5 zinbwave_1.2.0
[10] SingleCellExperiment_1.2.0 SummarizedExperiment_1.10.1 DelayedArray_0.6.6
[13] BiocParallel_1.14.2 matrixStats_0.54.0 Biobase_2.40.0
[16] GenomicRanges_1.32.6 GenomeInfoDb_1.16.0 IRanges_2.14.11
[19] S4Vectors_0.18.3 BiocGenerics_0.26.0
loaded via a namespace (and not attached):
[1] R.utils_2.7.0 tidyselect_0.2.4 RSQLite_2.1.1 AnnotationDbi_1.42.1
[5] htmlwidgets_1.2 ranger_0.10.1 grid_3.5.1 trimcluster_0.1-2.1
[9] munsell_0.5.0 ica_1.0-2 codetools_0.2-15 withr_2.1.2
[13] colorspace_1.3-2 knitr_1.20 rstudioapi_0.7 geometry_0.3-6
[17] pspline_1.0-18 ROCR_1.0-7 robustbase_0.93-2 dtw_1.20-1
[21] dimRed_0.1.0 gbRd_0.4-11 Rdpack_0.9-0 labeling_0.3
[25] lars_1.2 GenomeInfoDbData_1.1.0 bit64_0.9-7 ipred_0.9-7
[29] diptest_0.75-7 R6_2.2.2 VGAM_1.0-6 locfit_1.5-9.1
[33] flexmix_2.3-14 DRR_0.0.3 bitops_1.0-6 assertthat_0.2.0
[37] SDMTools_1.1-221 scales_1.0.0 nnet_7.3-12 gtable_0.2.0
[41] npsurv_0.4-0 ddalpha_1.3.4 timeDate_3043.102 rlang_0.2.2
[45] CVST_0.2-2 genefilter_1.62.0 scatterplot3d_0.3-41 RcppRoll_0.3.0
[49] splines_3.5.1 lazyeval_0.2.1 ModelMetrics_1.2.0 acepack_1.4.1
[53] broom_0.5.0 checkmate_1.8.5 reshape2_1.4.3 abind_1.4-5
[57] backports_1.1.2 Hmisc_4.1-1 caret_6.0-80 tools_3.5.1
[61] lava_1.6.3 gplots_3.0.1 RColorBrewer_1.1-2 proxy_0.4-22
[65] stabledist_0.7-1 ggridges_0.5.0 Rcpp_0.12.18 plyr_1.8.4
[69] base64enc_0.1-3 progress_1.2.0 zlibbioc_1.26.0 purrr_0.2.5
[73] RCurl_1.95-4.11 prettyunits_1.0.2 rpart_4.1-13 pbapply_1.3-4
[77] zoo_1.8-3 sfsmisc_1.1-2 cluster_2.0.7-1 data.table_1.11.4
[81] lmtest_0.9-36 RANN_2.6 mvtnorm_1.0-8 fitdistrplus_1.0-11
[85] gsl_1.9-10.3 lsei_1.2-0 hms_0.4.2 xtable_1.8-3
[89] XML_3.98-1.16 mclust_5.4.1 gridExtra_2.3 compiler_3.5.1
[93] tibble_1.4.2 KernSmooth_2.23-15 crayon_1.3.4 R.oo_1.22.0
[97] htmltools_0.3.6 segmented_0.5-3.0 pcaPP_1.9-73 Formula_1.2-3
[101] snow_0.4-3 tidyr_0.8.1 tclust_1.4-1 lubridate_1.7.4
[105] DBI_1.0.0 diffusionMap_1.1-0.1 magic_1.5-9 MASS_7.3-50
[109] fpc_2.1-11.1 R.methodsS3_1.7.1 gdata_2.18.0 metap_1.0
[113] bindr_0.1.1 gower_0.1.2 igraph_1.2.2 pkgconfig_2.0.2
[117] numDeriv_2016.8-1 foreign_0.8-70 recipes_0.1.3 foreach_1.4.4
[121] annotate_1.58.0 XVector_0.20.0 prodlim_2018.04.18 bibtex_0.4.2
[125] stringr_1.3.1 digest_0.6.17 tsne_0.1-3 pls_2.7-0
[129] copula_0.999-18 ADGofTest_0.3 softImpute_1.4 htmlTable_1.12
[133] edgeR_3.22.3 kernlab_0.9-27 gtools_3.8.1 modeltools_0.2-22
[137] nlme_3.1-137 bindrcpp_0.2.2 limma_3.36.3 pillar_1.3.0
[141] lattice_0.20-35 httr_1.3.1 DEoptimR_1.0-8 survival_2.42-3
[145] glue_1.3.0 FNN_1.1.2.1 png_0.1-7 prabclus_2.2-6
[149] iterators_1.0.10 glmnet_2.0-16 bit_1.1-14 mixtools_1.1.0
[153] class_7.3-14 stringi_1.1.7 blob_1.1.1 doSNOW_1.0.16
[157] latticeExtra_0.6-28 caTools_1.17.1.1 memoise_1.1.0 dplyr_0.7.6
[161] irlba_2.3.2 ape_5.1
```