First of all we need to install NewWave:
NewWave is a new package that assumes a Negative Binomial distributions for dimensionality reduction and batch effect removal. In order to reduce the memory consumption it uses a PSOCK cluster combined with the R package SharedObject that allow to share a matrix between different cores without memory duplication. Thanks to that we can massively parallelize the estimation process with huge benefit in terms of time consumption. We can reduce even more the time consumption using some minibatch approaches on the different steps of the optimization.
I am going to show how to use NewWave with example data generated with Splatter.
params <- newSplatParams()
N=500
set.seed(1234)
data <- splatSimulateGroups(params,batchCells=c(N/2,N/2),
group.prob = rep(0.1,10),
de.prob = 0.2,
verbose = FALSE)
Now we have a dataset with 500 cells and 10000 genes, I will use only the 500 most variable genes. NewWave takes as input raw data, not normalized.
set.seed(12359)
hvg <- rowVars(counts(data))
names(hvg) <- rownames(counts(data))
data <- data[names(sort(hvg,decreasing=TRUE))[1:500],]
As you can see there is a variable called batch in the colData section.
colData(data)
#> DataFrame with 500 rows and 4 columns
#> Cell Batch Group ExpLibSize
#> <character> <character> <factor> <numeric>
#> Cell1 Cell1 Batch1 Group1 81520.3
#> Cell2 Cell2 Batch1 Group8 53043.1
#> Cell3 Cell3 Batch1 Group3 48867.3
#> Cell4 Cell4 Batch1 Group3 63087.6
#> Cell5 Cell5 Batch1 Group4 64671.9
#> ... ... ... ... ...
#> Cell496 Cell496 Batch2 Group2 89687.0
#> Cell497 Cell497 Batch2 Group5 51284.5
#> Cell498 Cell498 Batch2 Group2 72541.6
#> Cell499 Cell499 Batch2 Group9 41821.2
#> Cell500 Cell500 Batch2 Group5 77733.7
IMPORTANT: For batch effecr removal the batch variable must be a factor
We also have a variable called Group that represent the cell type labels.
We can see the how the cells are distributed between group and batch
There is a clear batch effect between the cells.
Let’s try to correct it.
I am going to show different implementation and the suggested way to use them with the given hardware.
Some advise:
This is the way to insert the batch variable, in the same manner can be inserted other cell-related variable and if you need some gene related variable those can be inserted in V.
res <- newWave(data,X = "~Batch", K=10, verbose = TRUE)
#> Time of setup
#> user system elapsed
#> 0.016 0.000 0.263
#> Time of initialization
#> user system elapsed
#> 0.058 0.000 0.571
#> Iteration 1
#> penalized log-likelihood = -1294550.1842374
#> Time of dispersion optimization
#> user system elapsed
#> 0.610 0.000 0.611
#> after optimize dispersion = -1059666.99127371
#> Time of right optimization
#> user system elapsed
#> 0.001 0.000 9.309
#> after right optimization= -1058904.16365502
#> after orthogonalization = -1058904.11563068
#> Time of left optimization
#> user system elapsed
#> 0.003 0.000 11.370
#> after left optimization= -1058557.43647889
#> after orthogonalization = -1058557.43286215
#> Iteration 2
#> penalized log-likelihood = -1058557.43286215
#> Time of dispersion optimization
#> user system elapsed
#> 1.108 0.036 1.532
#> after optimize dispersion = -1058550.14320343
#> Time of right optimization
#> user system elapsed
#> 0.002 0.000 7.404
#> after right optimization= -1058514.97921845
#> after orthogonalization = -1058514.97818088
#> Time of left optimization
#> user system elapsed
#> 0.002 0.000 5.165
#> after left optimization= -1058502.22838262
#> after orthogonalization = -1058502.22824743
In order to make it faster you can increase the number of cores using “children” parameter:
res2 <- newWave(data,X = "~Batch", K=10, verbose = TRUE, children=2)
#> Time of setup
#> user system elapsed
#> 0.009 0.012 0.297
#> Time of initialization
#> user system elapsed
#> 0.055 0.004 0.378
#> Iteration 1
#> penalized log-likelihood = -1294550.18429425
#> Time of dispersion optimization
#> user system elapsed
#> 0.615 0.008 0.624
#> after optimize dispersion = -1059666.99220952
#> Time of right optimization
#> user system elapsed
#> 0.002 0.000 3.523
#> after right optimization= -1058904.16537297
#> after orthogonalization = -1058904.11735097
#> Time of left optimization
#> user system elapsed
#> 0.002 0.000 5.931
#> after left optimization= -1058557.44027082
#> after orthogonalization = -1058557.43666754
#> Iteration 2
#> penalized log-likelihood = -1058557.43666754
#> Time of dispersion optimization
#> user system elapsed
#> 1.202 0.052 1.796
#> after optimize dispersion = -1058550.146521
#> Time of right optimization
#> user system elapsed
#> 0.002 0.000 8.118
#> after right optimization= -1058515.02569488
#> after orthogonalization = -1058515.02466314
#> Time of left optimization
#> user system elapsed
#> 0.003 0.000 4.180
#> after left optimization= -1058502.256635
#> after orthogonalization = -1058502.25650187
If you do not have an high number of cores to run newWave this is the fastest way to run. The optimization process is done by three process itereated until convercence.
Each of these three steps can be accelerated using mini batch, the number of observation is settled with these parameters:
res3 <- newWave(data,X = "~Batch", verbose = TRUE,K=10, children=2,
n_gene_disp = 100, n_gene_par = 100, n_cell_par = 100)
#> Time of setup
#> user system elapsed
#> 0.020 0.000 3.303
#> Time of initialization
#> user system elapsed
#> 0.064 0.012 0.529
#> Iteration 1
#> penalized log-likelihood = -1294550.18441955
#> Time of dispersion optimization
#> user system elapsed
#> 0.945 0.028 0.973
#> after optimize dispersion = -1059666.99377095
#> Time of right optimization
#> user system elapsed
#> 0.002 0.000 6.543
#> after right optimization= -1058904.16118874
#> after orthogonalization = -1058904.11314804
#> Time of left optimization
#> user system elapsed
#> 0.002 0.000 7.077
#> after left optimization= -1058557.44079729
#> after orthogonalization = -1058557.43719188
#> Iteration 2
#> penalized log-likelihood = -1058557.43719188
#> Time of dispersion optimization
#> user system elapsed
#> 0.369 0.012 0.381
#> after optimize dispersion = -1058557.43719188
#> Time of right optimization
#> user system elapsed
#> 0.002 0.000 1.156
#> after right optimization= -1058550.29285931
#> after orthogonalization = -1058550.29223553
#> Time of left optimization
#> user system elapsed
#> 0.002 0.000 0.709
#> after left optimization= -1058549.87221396
#> after orthogonalization = -1058549.87216992
If you have a lot of core disposable or you want to estimate a genewise dispersion parameter this is the fastes configuration:
res3 <- newWave(data,X = "~Batch", verbose = TRUE,K=10, children=2,
n_gene_par = 100, n_cell_par = 100, commondispersion = FALSE)
#> Time of setup
#> user system elapsed
#> 0.012 0.008 0.272
#> Time of initialization
#> user system elapsed
#> 0.075 0.000 0.532
#> Iteration 1
#> penalized log-likelihood = -1294550.18439853
#> Time of dispersion optimization
#> user system elapsed
#> 1.006 0.016 1.023
#> after optimize dispersion = -1059666.9937942
#> Time of right optimization
#> user system elapsed
#> 0.002 0.000 3.820
#> after right optimization= -1058904.1607334
#> after orthogonalization = -1058904.11269187
#> Time of left optimization
#> user system elapsed
#> 0.000 0.002 3.196
#> after left optimization= -1058557.4389688
#> after orthogonalization = -1058557.43535423
#> Iteration 2
#> penalized log-likelihood = -1058557.43535423
#> Time of dispersion optimization
#> user system elapsed
#> 0.047 0.004 0.547
#> after optimize dispersion = -1054542.01510891
#> Time of right optimization
#> user system elapsed
#> 0.002 0.000 0.615
#> after right optimization= -1054536.20004674
#> after orthogonalization = -1054536.19986668
#> Time of left optimization
#> user system elapsed
#> 0.001 0.000 0.664
#> after left optimization= -1054497.13872604
#> after orthogonalization = -1054497.13807506
#> Iteration 3
#> penalized log-likelihood = -1054497.13807506
#> Time of dispersion optimization
#> user system elapsed
#> 0.062 0.000 0.257
#> after optimize dispersion = -1054497.15305862
#> Time of right optimization
#> user system elapsed
#> 0.001 0.000 0.564
#> after right optimization= -1054488.90509888
#> after orthogonalization = -1054488.89897922
#> Time of left optimization
#> user system elapsed
#> 0.001 0.000 0.615
#> after left optimization= -1054460.58500094
#> after orthogonalization = -1054460.58382107
NB: do not use n_gene_disp in this case, it will slower the computation.
Now I can use the latent dimension rapresentation for visualization purpose:
latent <- reducedDim(res)
tsne_latent <- data.frame(Rtsne(latent)$Y)
tsne_latent$batch <- data$Batch
tsne_latent$group <- data$Group
or for clustering:
sessionInfo()
#> R version 4.2.1 (2022-06-23)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 20.04.5 LTS
#>
#> Matrix products: default
#> BLAS: /home/biocbuild/bbs-3.16-bioc/R/lib/libRblas.so
#> LAPACK: /home/biocbuild/bbs-3.16-bioc/R/lib/libRlapack.so
#>
#> locale:
#> [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
#> [3] LC_TIME=en_GB LC_COLLATE=C
#> [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
#> [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
#> [9] LC_ADDRESS=C LC_TELEPHONE=C
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
#>
#> attached base packages:
#> [1] stats4 stats graphics grDevices utils datasets methods
#> [8] base
#>
#> other attached packages:
#> [1] NewWave_1.8.0 mclust_6.0.0
#> [3] ggplot2_3.3.6 Rtsne_0.16
#> [5] irlba_2.3.5.1 Matrix_1.5-1
#> [7] splatter_1.22.0 SingleCellExperiment_1.20.0
#> [9] SummarizedExperiment_1.28.0 Biobase_2.58.0
#> [11] GenomicRanges_1.50.0 GenomeInfoDb_1.34.0
#> [13] IRanges_2.32.0 S4Vectors_0.36.0
#> [15] BiocGenerics_0.44.0 MatrixGenerics_1.10.0
#> [17] matrixStats_0.62.0
#>
#> loaded via a namespace (and not attached):
#> [1] rsvd_1.0.5 Rcpp_1.0.9 locfit_1.5-9.6
#> [4] lattice_0.20-45 assertthat_0.2.1 digest_0.6.30
#> [7] utf8_1.2.2 R6_2.5.1 backports_1.4.1
#> [10] evaluate_0.17 highr_0.9 pillar_1.8.1
#> [13] zlibbioc_1.44.0 rlang_1.0.6 jquerylib_0.1.4
#> [16] checkmate_2.1.0 rmarkdown_2.17 labeling_0.4.2
#> [19] BiocParallel_1.32.0 stringr_1.4.1 SharedObject_1.12.0
#> [22] beachmat_2.14.0 RCurl_1.98-1.9 munsell_0.5.0
#> [25] DelayedArray_0.24.0 BiocSingular_1.14.0 compiler_4.2.1
#> [28] xfun_0.34 pkgconfig_2.0.3 htmltools_0.5.3
#> [31] tidyselect_1.2.0 tibble_3.1.8 GenomeInfoDbData_1.2.9
#> [34] codetools_0.2-18 fansi_1.0.3 dplyr_1.0.10
#> [37] withr_2.5.0 bitops_1.0-7 grid_4.2.1
#> [40] jsonlite_1.8.3 gtable_0.3.1 lifecycle_1.0.3
#> [43] DBI_1.1.3 magrittr_2.0.3 scales_1.2.1
#> [46] ScaledMatrix_1.6.0 cli_3.4.1 stringi_1.7.8
#> [49] cachem_1.0.6 farver_2.1.1 XVector_0.38.0
#> [52] bslib_0.4.0 vctrs_0.5.0 generics_0.1.3
#> [55] tools_4.2.1 glue_1.6.2 parallel_4.2.1
#> [58] fastmap_1.1.0 yaml_2.3.6 colorspace_2.0-3
#> [61] knitr_1.40 sass_0.4.2