1 Introduction

Data from different experimental platforms and/or batches exhibit systematic variation – i.e., batch effects. Therefore, when conducting joint analysis of data from different batches, a key first step is to align the datasets.

corralm is a multi-table adaptation of correspondence analysis designed for single-cell data, which applies multi-dimensional optimized scaling and matrix factorization to compute integrated embeddings across the datasets. These embeddings can then be used in downstream analyses, such as clustering, cell type classification, trajectory analysis, etc.

See the vignette for corral for dimensionality reduction of a single matrix of single-cell data.

2 Loading packages and data

We will use the SCMixology datasets from the CellBench package (Tian et al. 2019).

library(corral)
library(SingleCellExperiment)
library(ggplot2)
library(CellBench)
library(MultiAssayExperiment)

scmix_dat <- load_all_data()[1:3]

These datasets include a mixture of three lung cancer cell lines:

  • H2228
  • H1975
  • HCC827

which was sequenced using three platforms:

  • 10X
  • CELseq2
  • Dropseq
scmix_dat
## $sc_10x
## class: SingleCellExperiment 
## dim: 16468 902 
## metadata(3): scPipe Biomart log.exprs.offset
## assays(2): counts logcounts
## rownames(16468): ENSG00000272758 ENSG00000154678 ... ENSG00000054219
##   ENSG00000137691
## rowData names(0):
## colnames(902): CELL_000001 CELL_000002 ... CELL_000955 CELL_000965
## colData names(14): unaligned aligned_unmapped ... cell_line sizeFactor
## reducedDimNames(0):
## mainExpName: NULL
## altExpNames(0):
## 
## $sc_celseq
## class: SingleCellExperiment 
## dim: 28204 274 
## metadata(3): scPipe Biomart log.exprs.offset
## assays(2): counts logcounts
## rownames(28204): ENSG00000281131 ENSG00000227456 ... ENSG00000148143
##   ENSG00000226887
## rowData names(0):
## colnames(274): A1 A10 ... P8 P9
## colData names(15): unaligned aligned_unmapped ... cell_line sizeFactor
## reducedDimNames(0):
## mainExpName: NULL
## altExpNames(0):
## 
## $sc_dropseq
## class: SingleCellExperiment 
## dim: 15127 225 
## metadata(3): scPipe Biomart log.exprs.offset
## assays(2): counts logcounts
## rownames(15127): ENSG00000223849 ENSG00000225355 ... ENSG00000133789
##   ENSG00000146674
## rowData names(0):
## colnames(225): CELL_000001 CELL_000002 ... CELL_000249 CELL_000302
## colData names(14): unaligned aligned_unmapped ... cell_line sizeFactor
## reducedDimNames(0):
## mainExpName: NULL
## altExpNames(0):

Each sequencing platform captures a different set of genes. In order to apply this method, the matrices need to be matched by features (i.e., genes). We’ll find the intersect of the three datasets, then subset for that as we proceed.

First, we will prepare the data by: 1. adding to the colData the sequencing platform (Method in colData for each SCE), and 2. subsetting by the intersect of the genes.

platforms <- c('10X','CELseq2','Dropseq')
for(i in seq_along(scmix_dat)) {
  colData(scmix_dat[[i]])$Method<- rep(platforms[i], ncol(scmix_dat[[i]]))
}

scmix_mae <- as(scmix_dat,'MultiAssayExperiment')
scmix_dat <- as.list(MultiAssayExperiment::experiments(MultiAssayExperiment::intersectRows(scmix_mae)))

corralm can be applied to the following types of objects:

  • a single SingleCellExperiment requires specifying splitby (also see documentation of corralm_matlist for additional optional arguments that can be passed), which is a character string for the attribute in colData that is tracking the batches. In our case, this would be the “Method” attribute we just added. The output from this type of input is the same SingleCellExperiment, with the result added to the reducedDim slot under corralm.
  • a list of SingleCellExperiment does not require any specific arguments. The output is a list of the input SingleCellExperiments, with the result added to the reducedDim slot under corralm.
  • a list of matrices (or other matrix-like objects: matrix, Matrix, tibble, data.frame, etc.) also does not require specific arguments. The output will be a concatenated list of SVD output matrices (u,d,v) where v contains a concatenated vector of the embeddings for the cells
  • a list of SummarizedExperiments does not require specific arguments. The output is the same as for a list of matrices.
  • MultiAssayExperiment or ExperimentList does not require any specific arguments. corralm will identify the intersect of the rows, and use these to match the matrices. The output will be the same as for a list of matrices.

For purposes of illustration, we will walk through using corralm with a single SCE, and with a list of matrices.

3 corralm on a single SingleCellExperiment

First, setting up the data to demonstrate this:

colData(scmix_dat[[2]])$non_ERCC_percent <- NULL
# need to remove this column so the objects can be concatenated

scmix_sce <- SingleCellExperiment::cbind(scmix_dat[[1]],
                                         scmix_dat[[2]],
                                         scmix_dat[[3]])

Running corralm, and specifying the splitby argument: (Note that the default is for the counts matrix to be used. To change this default, use the whichmat argument.)

scmix_sce <- corralm(scmix_sce, splitby = 'Method')

Visualizing the results:

plot_embedding_sce(sce = scmix_sce, 
                   which_embedding = 'corralm', 
                   color_attr = 'Method', 
                   color_title = 'platform', 
                   ellipse_attr = 'cell_line', 
                   plot_title = 'corralm on scmix', 
                   saveplot = FALSE)
## Warning: The following aesthetics were dropped during statistical transformation: colour
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?

4 corralm on a list of matrices

Again, preparing the data to be in this input format:

scmix_matlist <- sce2matlist(sce = scmix_sce, 
                             splitby = 'Method', 
                             whichmat = 'counts')

# for plotting purposes later, while we're here
platforms <- colData(scmix_sce)$Method
cell_lines <- colData(scmix_sce)$cell_line

Running corralm and visualizing output… (the embeddings are in the v matrix because these data are matched by genes in the rows and have cells in the columns; if this were reversed, with cells in the rows and genes/features in the column, then the cell embeddings would instead be in the u matrix.)

scmix_corralm <- corralm(scmix_matlist)
scmix_corralm
## corralm output summary==========================================
##   Output "list" includes SVD output (u, d, v) & a table of the
##   dimensions of the input matrices (batch_sizes)
## Variance explained----------------------------------------------
##                           PC1  PC2  PC3  PC4  PC5  PC6  PC7  PC8
## percent.Var.explained    0.03 0.02 0.01 0.01 0.01 0.01 0.01 0.01
## cumulative.Var.explained 0.03 0.05 0.05 0.06 0.07 0.07 0.08 0.08
## 
## Dimensions of output elements-----------------------------------
##   Singular values (d) :: 30
##   Left singular vectors (u) :: 13575 30
##   Right singular vectors (v) :: 1401 30
##   See corralm help for details on each output element.
## 
## Original batches & sizes (in order)-----------------------------
##   10X :: 902
##   CELseq2 :: 274
##   Dropseq :: 225
## 
##   Use plot_embedding to visualize; see docs for details.
## ================================================================
plot_embedding(embedding = scmix_corralm$v, 
               plot_title = 'corralm on scmix', 
               color_vec = platforms, 
               color_title = 'platform', 
               ellipse_vec = cell_lines, 
               saveplot = FALSE)
## Warning: The following aesthetics were dropped during statistical transformation: colour
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?

As expected, we get the same results as above. (Note that in performing SVD, the direction of the axes doesn’t matter and they may be flipped between runs, as corral and corralm use irlba to perform fast approximation.)

5 Scaled variance plots to evaluate integration

Scaled variance plots provide a simple and fast visual summary of the integration of embeddings. It can be called using the scal_var function, and works on both corralm objects and custom embeddings (with a vector indicating batch).

When integrating embedding representations across batches, measures for cluster evaluation are effective for assessing group compactness and recovery of cell populations via clustering. However, they do not directly assess how well dataset embeddings are integrated across batches. To focus specifically on batch integration, we developed and applied a heuristic scaled variance metric, which captures the relative dispersion of each batch with respect to the entire dataset. The scaled variance of component dimension \(d^*\) for the subset of observations in batch \(b^*\), \(SV_{b^*,d}\), is computed with: \[SV_{b^*,d} = \frac{\mathrm{Var}(\mathbf{E_{b=b^*,d=d^*}})}{\mathrm{Var}(\mathbf{E_{d=d^*}})}\] where \(\mathbf{E}\) is the matrix of embeddings, and \(b\) indexes the rows (observations by batch) while \(d\) indexes the columns to indicate which component dimension to evaluate.

scal_var(scmix_corralm)

When the datasets are well integrated, SV values for each batch are close to 1, indicating that each batch’s subset has similar dispersion as compared to the entire embedding. In contrast, if there is poorer integration, the scaled variance values will be more extreme away from 1 because the variance within batches will differ more from the variance overall. This metric is appropriate when the types of cells represented in different datasets are expected to be similar, but cannot account for situations where the expected distribution of cell types (and therefore, embeddings) is fundamentally different between batches.

6 Session information

sessionInfo()
## R version 4.3.1 (2023-06-16)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 22.04.3 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.18-bioc/R/lib/libRblas.so 
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
## 
## 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       
## 
## time zone: America/New_York
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
##  [1] MultiAssayExperiment_1.28.0 CellBench_1.18.0           
##  [3] tibble_3.2.1                magrittr_2.0.3             
##  [5] scater_1.30.0               scuttle_1.12.0             
##  [7] DuoClustering2018_1.19.0    ggplot2_3.4.4              
##  [9] SingleCellExperiment_1.24.0 SummarizedExperiment_1.32.0
## [11] Biobase_2.62.0              GenomicRanges_1.54.0       
## [13] GenomeInfoDb_1.38.0         IRanges_2.36.0             
## [15] S4Vectors_0.40.0            BiocGenerics_0.48.0        
## [17] MatrixGenerics_1.14.0       matrixStats_1.0.0          
## [19] corral_1.12.0               gridExtra_2.3              
## [21] BiocStyle_2.30.0           
## 
## loaded via a namespace (and not attached):
##   [1] jsonlite_1.8.7                ggbeeswarm_0.7.2             
##   [3] magick_2.8.1                  farver_2.1.1                 
##   [5] rmarkdown_2.25                zlibbioc_1.48.0              
##   [7] vctrs_0.6.4                   memoise_2.0.1                
##   [9] DelayedMatrixStats_1.24.0     RCurl_1.98-1.12              
##  [11] BiocBaseUtils_1.4.0           htmltools_0.5.6.1            
##  [13] S4Arrays_1.2.0                AnnotationHub_3.10.0         
##  [15] curl_5.1.0                    BiocNeighbors_1.20.0         
##  [17] SparseArray_1.2.0             sass_0.4.7                   
##  [19] bslib_0.5.1                   plyr_1.8.9                   
##  [21] lubridate_1.9.3               cachem_1.0.8                 
##  [23] mime_0.12                     lifecycle_1.0.3              
##  [25] pkgconfig_2.0.3               rsvd_1.0.5                   
##  [27] Matrix_1.6-1.1                R6_2.5.1                     
##  [29] fastmap_1.1.1                 GenomeInfoDbData_1.2.11      
##  [31] shiny_1.7.5.1                 digest_0.6.33                
##  [33] colorspace_2.1-0              AnnotationDbi_1.64.0         
##  [35] irlba_2.3.5.1                 ExperimentHub_2.10.0         
##  [37] RSQLite_2.3.1                 beachmat_2.18.0              
##  [39] filelock_1.0.2                labeling_0.4.3               
##  [41] timechange_0.2.0              fansi_1.0.5                  
##  [43] httr_1.4.7                    abind_1.4-5                  
##  [45] compiler_4.3.1                bit64_4.0.5                  
##  [47] withr_2.5.1                   BiocParallel_1.36.0          
##  [49] viridis_0.6.4                 DBI_1.1.3                    
##  [51] maps_3.4.1                    rappdirs_0.3.3               
##  [53] DelayedArray_0.28.0           tools_4.3.1                  
##  [55] vipor_0.4.5                   beeswarm_0.4.0               
##  [57] interactiveDisplayBase_1.40.0 httpuv_1.6.12                
##  [59] glue_1.6.2                    promises_1.2.1               
##  [61] grid_4.3.1                    Rtsne_0.16                   
##  [63] reshape2_1.4.4                generics_0.1.3               
##  [65] gtable_0.3.4                  tidyr_1.3.0                  
##  [67] data.table_1.14.8             BiocSingular_1.18.0          
##  [69] ScaledMatrix_1.10.0           utf8_1.2.4                   
##  [71] XVector_0.42.0                ggrepel_0.9.4                
##  [73] BiocVersion_3.18.0            pillar_1.9.0                 
##  [75] stringr_1.5.0                 pals_1.8                     
##  [77] later_1.3.1                   dplyr_1.1.3                  
##  [79] BiocFileCache_2.10.0          lattice_0.22-5               
##  [81] FNN_1.1.3.2                   bit_4.0.5                    
##  [83] tidyselect_1.2.0              Biostrings_2.70.0            
##  [85] transport_0.14-6              knitr_1.44                   
##  [87] bookdown_0.36                 xfun_0.40                    
##  [89] stringi_1.7.12                yaml_2.3.7                   
##  [91] evaluate_0.22                 codetools_0.2-19             
##  [93] BiocManager_1.30.22           cli_3.6.1                    
##  [95] uwot_0.1.16                   xtable_1.8-4                 
##  [97] munsell_0.5.0                 jquerylib_0.1.4              
##  [99] dichromat_2.0-0.1             Rcpp_1.0.11                  
## [101] mapproj_1.2.11                dbplyr_2.3.4                 
## [103] png_0.1-8                     parallel_4.3.1               
## [105] ellipsis_0.3.2                assertthat_0.2.1             
## [107] blob_1.2.4                    mclust_6.0.0                 
## [109] sparseMatrixStats_1.14.0      bitops_1.0-7                 
## [111] viridisLite_0.4.2             ggthemes_4.2.4               
## [113] scales_1.2.1                  purrr_1.0.2                  
## [115] crayon_1.5.2                  rlang_1.1.1                  
## [117] KEGGREST_1.42.0

References

Tian, Luyi, Xueyi Dong, Saskia Freytag, Kim-Anh Lê Cao, Shian Su, Abolfazl JalalAbadi, Daniela Amann-Zalcenstein, et al. 2019. “Benchmarking Single Cell RNA-Sequencing Analysis Pipelines Using Mixture Control Experiments.” Nature Methods 16 (6): 479–87. https://doi.org/10.1038/s41592-019-0425-8.