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 Group7 52419.2
#> Cell2 Cell2 Batch1 Group4 52682.3
#> Cell3 Cell3 Batch1 Group9 47913.6
#> Cell4 Cell4 Batch1 Group9 53779.8
#> Cell5 Cell5 Batch1 Group3 65036.3
#> ... ... ... ... ...
#> Cell496 Cell496 Batch2 Group4 82067.6
#> Cell497 Cell497 Batch2 Group7 58320.4
#> Cell498 Cell498 Batch2 Group5 58710.2
#> Cell499 Cell499 Batch2 Group3 53200.1
#> Cell500 Cell500 Batch2 Group1 70719.2
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.015 0.004 0.301
#> Time of initialization
#> user system elapsed
#> 0.020 0.019 0.449
#> Iteration 1
#> penalized log-likelihood = -1282869.15170746
#> Time of dispersion optimization
#> user system elapsed
#> 0.474 0.052 0.470
#> after optimize dispersion = -1050481.67326747
#> Time of right optimization
#> user system elapsed
#> 0.001 0.000 4.327
#> after right optimization= -1049726.06219619
#> after orthogonalization = -1049726.03506746
#> Time of left optimization
#> user system elapsed
#> 0.016 0.116 4.074
#> after left optimization= -1049311.2455378
#> after orthogonalization = -1049311.23626327
#> Iteration 2
#> penalized log-likelihood = -1049311.23626327
#> Time of dispersion optimization
#> user system elapsed
#> 0.599 0.263 0.529
#> after optimize dispersion = -1049302.65751496
#> Time of right optimization
#> user system elapsed
#> 0.001 0.000 3.983
#> after right optimization= -1049253.44664212
#> after orthogonalization = -1049253.44398228
#> Time of left optimization
#> user system elapsed
#> 0.042 0.140 3.198
#> after left optimization= -1049235.56444358
#> after orthogonalization = -1049235.56409906
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.008 0.000 3.298
#> Time of initialization
#> user system elapsed
#> 0.033 0.006 0.386
#> Iteration 1
#> penalized log-likelihood = -1282869.15173973
#> Time of dispersion optimization
#> user system elapsed
#> 0.489 0.044 0.471
#> after optimize dispersion = -1050481.67364056
#> Time of right optimization
#> user system elapsed
#> 0.001 0.000 2.245
#> after right optimization= -1049726.06666877
#> after orthogonalization = -1049726.03954429
#> Time of left optimization
#> user system elapsed
#> 0.026 0.121 2.071
#> after left optimization= -1049311.23841073
#> after orthogonalization = -1049311.22910732
#> Iteration 2
#> penalized log-likelihood = -1049311.22910732
#> Time of dispersion optimization
#> user system elapsed
#> 0.572 0.291 0.531
#> after optimize dispersion = -1049302.65013903
#> Time of right optimization
#> user system elapsed
#> 0.001 0.000 2.048
#> after right optimization= -1049253.41772233
#> after orthogonalization = -1049253.41506501
#> Time of left optimization
#> user system elapsed
#> 0.025 0.134 1.741
#> after left optimization= -1049235.5652133
#> after orthogonalization = -1049235.56490742
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.004 0.004 0.301
#> Time of initialization
#> user system elapsed
#> 0.038 0.003 0.390
#> Iteration 1
#> penalized log-likelihood = -1282869.15180864
#> Time of dispersion optimization
#> user system elapsed
#> 0.494 0.039 0.474
#> after optimize dispersion = -1050481.67466744
#> Time of right optimization
#> user system elapsed
#> 0.001 0.001 2.248
#> after right optimization= -1049726.06361162
#> after orthogonalization = -1049726.03648205
#> Time of left optimization
#> user system elapsed
#> 0.017 0.134 2.076
#> after left optimization= -1049311.13068202
#> after orthogonalization = -1049311.12137823
#> Iteration 2
#> penalized log-likelihood = -1049311.12137823
#> Time of dispersion optimization
#> user system elapsed
#> 0.291 0.358 0.226
#> after optimize dispersion = -1049311.12137823
#> Time of right optimization
#> user system elapsed
#> 0.001 0.000 0.421
#> after right optimization= -1049302.24156228
#> after orthogonalization = -1049302.24056728
#> Time of left optimization
#> user system elapsed
#> 0.009 0.143 0.292
#> after left optimization= -1049301.80831252
#> after orthogonalization = -1049301.80822108
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.008 0.000 0.301
#> Time of initialization
#> user system elapsed
#> 0.038 0.005 0.398
#> Iteration 1
#> penalized log-likelihood = -1282869.15174234
#> Time of dispersion optimization
#> user system elapsed
#> 0.484 0.057 0.478
#> after optimize dispersion = -1050481.67282683
#> Time of right optimization
#> user system elapsed
#> 0.000 0.000 2.256
#> after right optimization= -1049726.08080333
#> after orthogonalization = -1049726.05369706
#> Time of left optimization
#> user system elapsed
#> 0.026 0.119 2.089
#> after left optimization= -1049311.15883928
#> after orthogonalization = -1049311.14952851
#> Iteration 2
#> penalized log-likelihood = -1049311.14952851
#> Time of dispersion optimization
#> user system elapsed
#> 0.143 0.220 0.421
#> after optimize dispersion = -1044507.1571129
#> Time of right optimization
#> user system elapsed
#> 0.000 0.001 0.397
#> after right optimization= -1044499.6757738
#> after orthogonalization = -1044499.67481886
#> Time of left optimization
#> user system elapsed
#> 0.024 0.111 0.453
#> after left optimization= -1044455.55888558
#> after orthogonalization = -1044455.55738691
#> Iteration 3
#> penalized log-likelihood = -1044455.55738691
#> Time of dispersion optimization
#> user system elapsed
#> 0.145 0.210 0.211
#> after optimize dispersion = -1044455.57335176
#> Time of right optimization
#> user system elapsed
#> 0.000 0.001 0.390
#> after right optimization= -1044447.97011761
#> after orthogonalization = -1044447.96958162
#> Time of left optimization
#> user system elapsed
#> 0.033 0.117 0.422
#> after left optimization= -1044415.7458558
#> after orthogonalization = -1044415.74508037
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.4.2 (2024-10-31)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 24.04.1 LTS
#>
#> Matrix products: default
#> BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
#> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so; LAPACK version 3.12.0
#>
#> locale:
#> [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
#> [3] LC_TIME=en_US.UTF-8 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
#>
#> time zone: Etc/UTC
#> tzcode source: system (glibc)
#>
#> attached base packages:
#> [1] stats4 stats graphics grDevices utils datasets methods
#> [8] base
#>
#> other attached packages:
#> [1] NewWave_1.17.0 mclust_6.1.1
#> [3] ggplot2_3.5.1 Rtsne_0.17
#> [5] irlba_2.3.5.1 Matrix_1.7-1
#> [7] splatter_1.31.0 SingleCellExperiment_1.29.1
#> [9] SummarizedExperiment_1.37.0 Biobase_2.67.0
#> [11] GenomicRanges_1.59.1 GenomeInfoDb_1.43.2
#> [13] IRanges_2.41.2 S4Vectors_0.45.2
#> [15] BiocGenerics_0.53.3 generics_0.1.3
#> [17] MatrixGenerics_1.19.0 matrixStats_1.4.1
#> [19] rmarkdown_2.29
#>
#> loaded via a namespace (and not attached):
#> [1] gtable_0.3.6 xfun_0.49 bslib_0.8.0
#> [4] lattice_0.22-6 vctrs_0.6.5 tools_4.4.2
#> [7] parallel_4.4.2 tibble_3.2.1 pkgconfig_2.0.3
#> [10] SharedObject_1.21.0 checkmate_2.3.2 lifecycle_1.0.4
#> [13] GenomeInfoDbData_1.2.13 farver_2.1.2 compiler_4.4.2
#> [16] munsell_0.5.1 codetools_0.2-20 htmltools_0.5.8.1
#> [19] sys_3.4.3 buildtools_1.0.0 sass_0.4.9
#> [22] yaml_2.3.10 pillar_1.10.0 crayon_1.5.3
#> [25] jquerylib_0.1.4 BiocParallel_1.41.0 DelayedArray_0.33.3
#> [28] cachem_1.1.0 abind_1.4-8 rsvd_1.0.5
#> [31] locfit_1.5-9.10 digest_0.6.37 BiocSingular_1.23.0
#> [34] labeling_0.4.3 maketools_1.3.1 fastmap_1.2.0
#> [37] grid_4.4.2 colorspace_2.1-1 cli_3.6.3
#> [40] SparseArray_1.7.2 magrittr_2.0.3 S4Arrays_1.7.1
#> [43] withr_3.0.2 UCSC.utils_1.3.0 scales_1.3.0
#> [46] backports_1.5.0 XVector_0.47.0 httr_1.4.7
#> [49] beachmat_2.23.5 ScaledMatrix_1.15.0 evaluate_1.0.1
#> [52] knitr_1.49 rlang_1.1.4 Rcpp_1.0.13-1
#> [55] glue_1.8.0 jsonlite_1.8.9 R6_2.5.1
#> [58] zlibbioc_1.52.0