First of all we need to install NewWave:
if(!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("NewWave")
suppressPackageStartupMessages(
{library(SingleCellExperiment)
library(splatter)
library(irlba)
library(Rtsne)
library(ggplot2)
library(mclust)
library(NewWave)}
)
#> Warning: multiple methods tables found for 'union'
#> Warning: multiple methods tables found for 'intersect'
#> Warning: multiple methods tables found for 'setdiff'
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 62034.1
#> Cell2 Cell2 Batch1 Group8 74118.6
#> Cell3 Cell3 Batch1 Group7 48435.5
#> Cell4 Cell4 Batch1 Group3 52106.5
#> Cell5 Cell5 Batch1 Group4 35777.3
#> ... ... ... ... ...
#> Cell496 Cell496 Batch2 Group7 54830.5
#> Cell497 Cell497 Batch2 Group1 57871.8
#> Cell498 Cell498 Batch2 Group10 65977.0
#> Cell499 Cell499 Batch2 Group4 70555.5
#> Cell500 Cell500 Batch2 Group1 60146.1
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.003 0.004 0.294
#> Time of initialization
#> user system elapsed
#> 0.022 0.015 0.445
#> Iteration 1
#> penalized log-likelihood = -1289024.36469799
#> Time of dispersion optimization
#> user system elapsed
#> 0.507 0.124 0.478
#> after optimize dispersion = -1053591.60057709
#> Time of right optimization
#> user system elapsed
#> 0.000 0.000 4.284
#> after right optimization= -1052857.99422888
#> after orthogonalization = -1052857.97949629
#> Time of left optimization
#> user system elapsed
#> 0.000 0.173 4.029
#> after left optimization= -1052523.26550243
#> after orthogonalization = -1052523.26242462
#> Iteration 2
#> penalized log-likelihood = -1052523.26242462
#> Time of dispersion optimization
#> user system elapsed
#> 0.607 0.283 0.540
#> after optimize dispersion = -1052516.20351531
#> Time of right optimization
#> user system elapsed
#> 0.00 0.00 3.87
#> after right optimization= -1052483.17682088
#> after orthogonalization = -1052483.17480895
#> Time of left optimization
#> user system elapsed
#> 0.038 0.140 3.279
#> after left optimization= -1052470.99837755
#> after orthogonalization = -1052470.99826481
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.007 0.000 0.296
#> Time of initialization
#> user system elapsed
#> 0.032 0.011 0.387
#> Iteration 1
#> penalized log-likelihood = -1289024.36475811
#> Time of dispersion optimization
#> user system elapsed
#> 0.499 0.037 0.470
#> after optimize dispersion = -1053591.60164864
#> Time of right optimization
#> user system elapsed
#> 0.001 0.000 2.219
#> after right optimization= -1052857.98959864
#> after orthogonalization = -1052857.97484963
#> Time of left optimization
#> user system elapsed
#> 0.022 0.129 2.050
#> after left optimization= -1052523.26878603
#> after orthogonalization = -1052523.26571154
#> Iteration 2
#> penalized log-likelihood = -1052523.26571154
#> Time of dispersion optimization
#> user system elapsed
#> 0.569 0.301 0.534
#> after optimize dispersion = -1052516.20662173
#> Time of right optimization
#> user system elapsed
#> 0.001 0.000 1.997
#> after right optimization= -1052483.2135225
#> after orthogonalization = -1052483.21151474
#> Time of left optimization
#> user system elapsed
#> 0.020 0.123 1.732
#> after left optimization= -1052471.04412959
#> after orthogonalization = -1052471.04400208
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.006 0.001 0.299
#> Time of initialization
#> user system elapsed
#> 0.036 0.003 0.398
#> Iteration 1
#> penalized log-likelihood = -1289024.36473235
#> Time of dispersion optimization
#> user system elapsed
#> 0.477 0.056 0.474
#> after optimize dispersion = -1053591.60246208
#> Time of right optimization
#> user system elapsed
#> 0.001 0.000 2.222
#> after right optimization= -1052857.99085958
#> after orthogonalization = -1052857.97610896
#> Time of left optimization
#> user system elapsed
#> 0.031 0.111 2.040
#> after left optimization= -1052523.25759939
#> after orthogonalization = -1052523.25452303
#> Iteration 2
#> penalized log-likelihood = -1052523.25452303
#> Time of dispersion optimization
#> user system elapsed
#> 0.269 0.259 0.195
#> after optimize dispersion = -1052523.25452303
#> Time of right optimization
#> user system elapsed
#> 0.001 0.000 0.395
#> after right optimization= -1052515.04972296
#> after orthogonalization = -1052515.03924835
#> Time of left optimization
#> user system elapsed
#> 0.014 0.131 0.283
#> after left optimization= -1052514.58670019
#> after orthogonalization = -1052514.58652924
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.303
#> Time of initialization
#> user system elapsed
#> 0.032 0.007 0.394
#> Iteration 1
#> penalized log-likelihood = -1289024.36490782
#> Time of dispersion optimization
#> user system elapsed
#> 0.472 0.060 0.471
#> after optimize dispersion = -1053591.60400483
#> Time of right optimization
#> user system elapsed
#> 0.001 0.000 2.245
#> after right optimization= -1052857.97869222
#> after orthogonalization = -1052857.96393463
#> Time of left optimization
#> user system elapsed
#> 0.026 0.119 2.078
#> after left optimization= -1052523.25108942
#> after orthogonalization = -1052523.24801321
#> Iteration 2
#> penalized log-likelihood = -1052523.24801321
#> Time of dispersion optimization
#> user system elapsed
#> 0.102 0.253 0.408
#> after optimize dispersion = -1048339.18780778
#> Time of right optimization
#> user system elapsed
#> 0.001 0.000 0.400
#> after right optimization= -1048334.31238115
#> after orthogonalization = -1048334.31193955
#> Time of left optimization
#> user system elapsed
#> 0.023 0.126 0.469
#> after left optimization= -1048292.6166913
#> after orthogonalization = -1048292.6158559
#> Iteration 3
#> penalized log-likelihood = -1048292.6158559
#> Time of dispersion optimization
#> user system elapsed
#> 0.106 0.249 0.203
#> after optimize dispersion = -1048292.63048365
#> Time of right optimization
#> user system elapsed
#> 0.001 0.000 0.407
#> after right optimization= -1048286.89398167
#> after orthogonalization = -1048286.89361094
#> Time of left optimization
#> user system elapsed
#> 0.027 0.123 0.409
#> after left optimization= -1048255.46674521
#> after orthogonalization = -1048255.46611719
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.0 GenomeInfoDb_1.43.1
#> [13] IRanges_2.41.1 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 fansi_1.0.6
#> [10] pkgconfig_2.0.3 SharedObject_1.21.0 checkmate_2.3.2
#> [13] lifecycle_1.0.4 GenomeInfoDbData_1.2.13 farver_2.1.2
#> [16] compiler_4.4.2 munsell_0.5.1 codetools_0.2-20
#> [19] htmltools_0.5.8.1 sys_3.4.3 buildtools_1.0.0
#> [22] sass_0.4.9 yaml_2.3.10 pillar_1.9.0
#> [25] crayon_1.5.3 jquerylib_0.1.4 BiocParallel_1.41.0
#> [28] DelayedArray_0.33.2 cachem_1.1.0 abind_1.4-8
#> [31] rsvd_1.0.5 locfit_1.5-9.10 digest_0.6.37
#> [34] BiocSingular_1.23.0 labeling_0.4.3 maketools_1.3.1
#> [37] fastmap_1.2.0 grid_4.4.2 colorspace_2.1-1
#> [40] cli_3.6.3 SparseArray_1.7.2 magrittr_2.0.3
#> [43] S4Arrays_1.7.1 utf8_1.2.4 withr_3.0.2
#> [46] UCSC.utils_1.3.0 scales_1.3.0 backports_1.5.0
#> [49] XVector_0.47.0 httr_1.4.7 beachmat_2.23.1
#> [52] ScaledMatrix_1.15.0 evaluate_1.0.1 knitr_1.49
#> [55] rlang_1.1.4 Rcpp_1.0.13-1 glue_1.8.0
#> [58] jsonlite_1.8.9 R6_2.5.1 zlibbioc_1.52.0