library(StabMap)
library(magrittr)
library(scater)
library(scran)
library(SingleCellMultiModal)
library(gridExtra)
StabMap is a technique for performing mosaic single cell data integration. Mosaic data integration presents the challenge of integration of data where only some features or cells are shared across datasets. For example, these challenges arise when integrating single-cell datasets that measure different molecular profiles, such as chromatin accessibility or RNA expression assays. Integrative analysis of such data may provide a more in-depth profile of each cell, facilitating downstream analysis. To read more about StabMap please see our paper on Nature Biotechnology.
In this vignette we will elaborate on how mosaic single cell data
integration is implemented in the StabMap
package. We
address a few key goals:
Mosaic Data integration for 2 datasets
Demonstrating cell imputation following integration
Indirect mosaic data integration for 3 datasets, including 2 non-overlapping datasets
In this tutorial we will work with a multi-assay single cell dataset, consisting of ATAC and gene expression data for 10,032 cells.
Perform some exploration of this data.
## A MultiAssayExperiment object of 2 listed
## experiments with user-defined names and respective classes.
## Containing an ExperimentList class object of length 2:
## [1] atac: SingleCellExperiment with 108344 rows and 10032 columns
## [2] rna: SingleCellExperiment with 36549 rows and 10032 columns
## Functionality:
## experiments() - obtain the ExperimentList instance
## colData() - the primary/phenotype DataFrame
## sampleMap() - the sample coordination DataFrame
## `$`, `[`, `[[` - extract colData columns, subset, or experiment
## *Format() - convert into a long or wide DataFrame
## assays() - convert ExperimentList to a SimpleList of matrices
## exportClass() - save data to flat files
## DataFrame with 6 rows and 6 columns
## nCount_RNA nFeature_RNA nCount_ATAC nFeature_ATAC
## <integer> <integer> <integer> <integer>
## AAACAGCCAAGGAATC 8380 3308 55582 13878
## AAACAGCCAATCCCTT 3771 1896 20495 7253
## AAACAGCCAATGCGCT 6876 2904 16674 6528
## AAACAGCCAGTAGGTG 7614 3061 39454 11633
## AAACAGCCAGTTTACG 3633 1691 20523 7245
## AAACAGCCATCCAGGT 7782 3028 22412 8602
## celltype broad_celltype
## <character> <character>
## AAACAGCCAAGGAATC naive CD4 T cells Lymphoid
## AAACAGCCAATCCCTT memory CD4 T cells Lymphoid
## AAACAGCCAATGCGCT naive CD4 T cells Lymphoid
## AAACAGCCAGTAGGTG naive CD4 T cells Lymphoid
## AAACAGCCAGTTTACG memory CD4 T cells Lymphoid
## AAACAGCCATCCAGGT non-classical monocy.. Myeloid
## [1] 36549 10032
## [1] "atac" "rna"
Keep the first 2,000 cells only. Normalise and select variable features for the RNA modality.
sce.rna <- experiments(mae)[["rna"]]
# Normalisation
sce.rna <- logNormCounts(sce.rna)
# Feature selection
decomp <- modelGeneVar(sce.rna)
hvgs <- rownames(decomp)[decomp$mean > 0.01 & decomp$p.value <= 0.05]
length(hvgs)
## [1] 952
Keep the first 2,000 cells only. Normalise and select variable features for the ATAC modality.
## [1] 108344 10032
sce.atac <- experiments(mae)[["atac"]]
# Normalise
sce.atac <- logNormCounts(sce.atac)
# Feature selection using highly variable peaks
# And adding matching peaks to genes
decomp <- modelGeneVar(sce.atac)
hvgs <- rownames(decomp)[decomp$mean > 0.25 &
decomp$p.value <= 0.05]
length(hvgs)
## [1] 788
Create a composite full data matrix by concatenating.
## [1] 1740 10032
assayType <- ifelse(rownames(logcounts_all) %in% rownames(sce.rna),
"rna", "atac"
)
table(assayType)
## assayType
## atac rna
## 788 952
We will simulate a situation where half of the cells correspond to the Multiome (RNA + ATAC features) modality, and half of the cells correspond to the RNA modality. Our goal is to then integrate both datasets by generating a joint embedding of the cells using all data, and to impute the missing ATAC cell values from the RNA modality cells.
dataType <- setNames(
sample(c("RNA", "Multiome"), ncol(logcounts_all),
prob = c(0.5, 0.5), replace = TRUE
),
colnames(logcounts_all)
)
table(dataType)
## dataType
## Multiome RNA
## 5025 5007
assay_list <- list(
RNA = logcounts_all[assayType %in% c("rna"), dataType %in% c("RNA")],
Multiome = logcounts_all[
assayType %in% c("rna", "atac"), dataType %in% c("Multiome")
]
)
lapply(assay_list, dim)
## $RNA
## [1] 952 5007
##
## $Multiome
## [1] 1740 5025
## $RNA
## [1] "dgCMatrix"
## attr(,"package")
## [1] "Matrix"
##
## $Multiome
## [1] "dgCMatrix"
## attr(,"package")
## [1] "Matrix"
Examine the shared features between the two datasets using
mosaicDataUpSet()
.
From this we note that there are shared features between the RNA and Multiome datasets, but there are many features that are observed only in the Multiome dataset and not the RNA - as we had constructed.
We can understand the mosaicDataTopology()
of these
datasets, which generates an igraph
object, which can be
inspected and plotted. The mosaicDataTopology()
is a
weighted network where nodes represent each dataset, and edges connect
nodes with at least one overlapping feature.
## IGRAPH 2ba29df UN-- 2 1 --
## + attr: name (v/c), frame.color (v/c), color (v/c), label.color (v/c),
## | label.family (v/c)
## + edge from 2ba29df (vertex names):
## [1] RNA--Multiome
From this we note that the datasets RNA and Multiome share at least some features. StabMap requires that the mosaic data topology network be connected, that is, that there should be a path between every pair of nodes in the network.
We now aim to integrate the data from the RNA and Multiome modality
by generating a common joint embedding for these data using
stabMap()
. The stabMap()
integration approach
aims to stabilize integration of single-cell data by exploting the
non-overlapping features, so that cells with similar biological profiles
will cluster. Stabilisation using non-overlapping features may be
important when there are limited overlapping features or when the
informative features are unknown.
stabMap
doing?
stabMap
generates a joint embedding using 3 steps:
Identify the mosaicDataTopology()
Embed the reference dataset into a lower dimensional space
Project cells from non-reference datasets onto the reference
dataset embedding by using a model to traverse shortest paths in the
mosaicDataTopology()
Since the Multiome data contains all features, we treat this as the
reference dataset. Since we already examined the mosaic data topology,
we set plot = FALSE
.
## treating "Multiome" as reference
## generating embedding for path with reference "Multiome": "Multiome"
## generating embedding for path with reference "Multiome": "RNA" -> "Multiome"
## [1] 10032 50
## Multiome_PC1 Multiome_PC2 Multiome_PC3 Multiome_PC4
## AAACAGCCAATCCCTT 12.885344 -3.075968 -1.723863 -0.3561525
## AAACAGCCAGTTTACG 11.314093 -2.344855 2.608507 1.2228681
## AAACATGCAAGGTCCT 13.821325 -3.100703 4.755135 -0.6836924
## AAACATGCACCGGCTA 6.287519 -2.080285 -24.802926 -0.6373922
## AAACATGCAGCAAGTG 12.500354 -3.058831 5.358400 -2.6757611
## Multiome_PC5
## AAACAGCCAATCCCTT -4.6468061
## AAACAGCCAGTTTACG -8.5576292
## AAACATGCAAGGTCCT 6.0538837
## AAACATGCACCGGCTA 7.1583625
## AAACATGCAGCAAGTG -0.1806992
We can reduce the dimension further using non-linear approaches such as UMAP.
## [1] 10032 2
Here we see that the RNA and Multiome cells are fairly well-mixed.
Given the joint embedding, we can predict the missing ATAC cell
values using imputeEmbedding()
. We use
imputeEmbedding()
for demonstration purposes as for our
data both modalities have sufficient sample sizes (cells) and thus
cellular imputation isn’t needed.
To imputeEmbedding()
we provide the data list, and the
joint embedding as output from stabMap()
. We set the
Multiome cells as reference and the RNA cells as query. This is useful
for downstream visualisation or further interpretation.
imp <- imputeEmbedding(
assay_list,
stab,
reference = colnames(assay_list[["Multiome"]]),
query = colnames(assay_list[["RNA"]])
)
class(imp)
## [1] "list"
## [1] "Multiome"
## $Multiome
## [1] 1740 5007
## $RNA
## [1] 952 5007
##
## $Multiome
## [1] 1740 5025
## 5 x 5 sparse Matrix of class "dgCMatrix"
## AAACAGCCAAGGAATC AAACAGCCAATGCGCT AAACAGCCAGTAGGTG AAACAGCCATCCAGGT
## CA6 1.299581 1.338925 1.075695 .
## CNR2 . . . .
## IFNLR1 . . . .
## RCAN3 1.414502 1.553737 1.656583 .
## ZNF683 . . . .
## AAACATGCACTTGTTC
## CA6 .
## CNR2 .
## IFNLR1 .
## RCAN3 .
## ZNF683 .
We can also leverage this joint embedding to annotate the query data. We will use a k-nearest neighbors (KNN) based algorithm to transfer cell type labels from the reference to the query dataset. For our demonstration we will treat the Multiome dataset as the reference and the RNA dataset as the query.
The column data of the single cell experiments objects contained in
mae
contain cell type annotations for each cell in the
celltype
column. We first extract cell type annotations for
our reference dataset (Multiome).
annotation <- "celltype"
referenceLabels <- colData(
experiments(mae)[["rna"]]
)[colnames(assay_list$Multiome), annotation]
names(referenceLabels) <- colnames(assay_list$Multiome)
table(referenceLabels)
## referenceLabels
## CD56 (bright) NK cells CD56 (dim) NK cells MAIT T cells
## 189 217 49
## classical monocytes effector CD8 T cells intermediate monocytes
## 987 205 344
## memory B cells memory CD4 T cells myeloid DC
## 207 792 105
## naive B cells naive CD4 T cells naive CD8 T cells
## 152 745 783
## non-classical monocytes plasmacytoid DC
## 199 51
To classify query cells based on a reference dataset we can use the
classifyEmbedding()
function. We provide the joint
embedding generated by stabMap()
and cell type labels for
the reference dataset to the classifyEmbedding()
function.
classifyEmbedding()
returns a dataframe with predicted
labels in the predicted_labels
column.
As we have simulated out datasets we have the true label annotations for the RNA (query) cells. We can evaluate how well our predicted annotations match the true annotations use a measure such as accuracy.
# Extract query labels
queryLabels <- colData(
experiments(mae)[["rna"]]
)[colnames(assay_list$RNA), annotation]
names(queryLabels) <- colnames(assay_list$RNA)
acc <- mean(queryLabels == knn_out[names(queryLabels), "predicted_labels"])
acc
## [1] 0.9203116
Since both the reference and query cells are embedded in the same low dimensional space we can also visualise their cells together. Here we present a UMAP visualisation colour coded by their cell types.
# Extract reference and query cells from UMAP embedding
stab_umap_ref <- stab_umap[colnames(assay_list$Multiome), ]
stab_umap_query <- stab_umap[colnames(assay_list$RNA), ]
# Create UMAP for reference cells
df_umap_ref <- data.frame(
x = stab_umap_ref[, 1],
y = stab_umap_ref[, 2],
cell_type = referenceLabels[rownames(stab_umap_ref)]
)
p_ref <- df_umap_ref %>%
ggplot() +
aes(x = x, y = y, colour = cell_type) +
geom_point(size = 1) +
ggtitle("Reference cell type annotation")
# Create UMAP for query cells
df_umap_query <- data.frame(
x = stab_umap_query[, 1],
y = stab_umap_query[, 2],
cell_type = queryLabels[rownames(stab_umap_query)]
)
p_query <- df_umap_query %>%
ggplot() +
aes(x = x, y = y, colour = cell_type) +
geom_point(size = 1) +
ggtitle("Query predicted cell types")
grid.arrange(p_ref, p_query, ncol = 2)
StabMap is a flexible framework for mosaic data integration, and can still integrate data even when there are pairs of datasets that share no features at all. So long as there is a path connecting the datasets along the mosaic data topology (and the underlying assumption that the shared features along these paths contain information), then we can extract meaningful joint embeddings. To demonstrate this, we will simulate three data sources.
dataTypeIndirect <- setNames(
sample(c("RNA", "Multiome", "ATAC"), ncol(logcounts_all),
prob = c(0.3, 0.3, 0.3), replace = TRUE
),
colnames(logcounts_all)
)
table(dataTypeIndirect)
## dataTypeIndirect
## ATAC Multiome RNA
## 3428 3355 3249
assay_list_indirect <- list(
RNA = logcounts_all[assayType %in% c("rna"), dataTypeIndirect %in% c("RNA")],
Multiome = logcounts_all[
assayType %in% c("rna", "atac"), dataTypeIndirect %in% c("Multiome")
],
ATAC = logcounts_all[
assayType %in% c("atac"), dataTypeIndirect %in% c("ATAC")
]
)
lapply(assay_list_indirect, dim)
## $RNA
## [1] 952 3249
##
## $Multiome
## [1] 1740 3355
##
## $ATAC
## [1] 788 3428
## $RNA
## [1] "dgCMatrix"
## attr(,"package")
## [1] "Matrix"
##
## $Multiome
## [1] "dgCMatrix"
## attr(,"package")
## [1] "Matrix"
##
## $ATAC
## [1] "dgCMatrix"
## attr(,"package")
## [1] "Matrix"
Using mosaicDataUpSet()
, we note that there are no
shared features between the ATAC and RNA datasets. For their integration
we might be able to match features by extracting genomic positions and
making the “central dogma assumption”, that is, that the peaks
associated with a genomic position overlapping a gene should correspond
to positive gene expression for that gene. However, using
stabMap()
we need not make this assumption for the data
integration to be performed.
We can understand the mosaicDataTopology()
of these
datasets, which generates an igraph
object, which can be
inspected and plotted.
## IGRAPH db2217f UN-- 3 2 --
## + attr: name (v/c), frame.color (v/c), color (v/c), label.color (v/c),
## | label.family (v/c)
## + edges from db2217f (vertex names):
## [1] RNA --Multiome Multiome--ATAC
StabMap only requires that the mosaic data topology network be connected, that is, that there should be a path between every pair of nodes in the network. While ATAC and RNA have no overlapping features, since there is a path between RNA and ATAC (via Multiome), we can proceed.
We now generate a common joint embedding for these data using
stabMap()
. Since the Multiome data contains all features,
we again treat this as the reference dataset. Since we already examined
the mosaic data topology, we set plot = FALSE
.
## treating "Multiome" as reference
## generating embedding for path with reference "Multiome": "Multiome"
## generating embedding for path with reference "Multiome": "RNA" -> "Multiome"
## generating embedding for path with reference "Multiome": "ATAC" -> "Multiome"
## [1] 10032 50
## Multiome_PC1 Multiome_PC2 Multiome_PC3 Multiome_PC4
## AAACAGCCAAGGAATC -16.51582 -3.279365 7.152313 -0.567712443
## AAACAGCCAATCCCTT -12.81491 -2.834585 -1.430482 -0.194437009
## AAACAGCCAATGCGCT -12.82778 -1.411441 6.301149 -0.884834392
## AAACAGCCAGTAGGTG -17.42144 -3.279140 4.925968 0.007653547
## AAACATGCAAGGTCCT -13.70480 -2.844677 4.874172 -0.975532677
## Multiome_PC5
## AAACAGCCAAGGAATC 4.347495
## AAACAGCCAATCCCTT -4.652206
## AAACAGCCAATGCGCT 2.547419
## AAACAGCCAGTAGGTG 4.219489
## AAACATGCAAGGTCCT 6.247341
We can reduce the dimension further using non-linear approaches such as UMAP.
## [1] 10032 2
plot(stab_indirect_umap,
pch = 16, cex = 0.3,
col = factor(dataTypeIndirect[rownames(stab_indirect)])
)
Here we see that the RNA, ATAC and Multiome cells are fairly well-mixed.
Colouring the cells by their original cell type, we can also see that the mosaic data integration is meaningful.
cellType <- setNames(mae$celltype, colnames(mae[[1]]))
plot(stab_indirect_umap,
pch = 16, cex = 0.3,
col = factor(cellType[rownames(stab_indirect)])
)
## 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] HDF5Array_1.35.2 rhdf5_2.51.1
## [3] DelayedArray_0.33.3 SparseArray_1.7.2
## [5] S4Arrays_1.7.1 abind_1.4-8
## [7] Matrix_1.7-1 gridExtra_2.3
## [9] SingleCellMultiModal_1.18.0 MultiAssayExperiment_1.33.1
## [11] scran_1.35.0 scater_1.35.0
## [13] ggplot2_3.5.1 scuttle_1.17.0
## [15] SingleCellExperiment_1.29.1 SummarizedExperiment_1.37.0
## [17] Biobase_2.67.0 GenomicRanges_1.59.1
## [19] GenomeInfoDb_1.43.2 IRanges_2.41.2
## [21] S4Vectors_0.45.2 BiocGenerics_0.53.3
## [23] generics_0.1.3 MatrixGenerics_1.19.0
## [25] matrixStats_1.4.1 magrittr_2.0.3
## [27] StabMap_1.1.0 BiocStyle_2.35.0
##
## loaded via a namespace (and not attached):
## [1] sys_3.4.3 jsonlite_1.8.9 ggbeeswarm_0.7.2
## [4] magick_2.8.5 farver_2.1.2 rmarkdown_2.29
## [7] zlibbioc_1.52.0 vctrs_0.6.5 memoise_2.0.1
## [10] htmltools_0.5.8.1 BiocBaseUtils_1.9.0 AnnotationHub_3.15.0
## [13] curl_6.0.1 BiocNeighbors_2.1.2 Rhdf5lib_1.29.0
## [16] sass_0.4.9 bslib_0.8.0 plyr_1.8.9
## [19] cachem_1.1.0 buildtools_1.0.0 igraph_2.1.2
## [22] mime_0.12 lifecycle_1.0.4 pkgconfig_2.0.3
## [25] rsvd_1.0.5 R6_2.5.1 fastmap_1.2.0
## [28] GenomeInfoDbData_1.2.13 digest_0.6.37 colorspace_2.1-1
## [31] AnnotationDbi_1.69.0 dqrng_0.4.1 irlba_2.3.5.1
## [34] ExperimentHub_2.15.0 RSQLite_2.3.9 beachmat_2.23.4
## [37] filelock_1.0.3 labeling_0.4.3 fansi_1.0.6
## [40] httr_1.4.7 compiler_4.4.2 bit64_4.5.2
## [43] withr_3.0.2 BiocParallel_1.41.0 viridis_0.6.5
## [46] DBI_1.2.3 UpSetR_1.4.0 rappdirs_0.3.3
## [49] rjson_0.2.23 bluster_1.17.0 tools_4.4.2
## [52] vipor_0.4.7 beeswarm_0.4.0 glue_1.8.0
## [55] rhdf5filters_1.19.0 grid_4.4.2 cluster_2.1.8
## [58] gtable_0.3.6 BiocSingular_1.23.0 ScaledMatrix_1.15.0
## [61] metapod_1.15.0 utf8_1.2.4 XVector_0.47.0
## [64] RcppAnnoy_0.0.22 ggrepel_0.9.6 BiocVersion_3.21.1
## [67] pillar_1.9.0 limma_3.63.2 dplyr_1.1.4
## [70] BiocFileCache_2.15.0 lattice_0.22-6 bit_4.5.0.1
## [73] tidyselect_1.2.1 locfit_1.5-9.10 maketools_1.3.1
## [76] Biostrings_2.75.2 knitr_1.49 edgeR_4.5.1
## [79] xfun_0.49 statmod_1.5.0 UCSC.utils_1.3.0
## [82] yaml_2.3.10 evaluate_1.0.1 codetools_0.2-20
## [85] tibble_3.2.1 BiocManager_1.30.25 cli_3.6.3
## [88] uwot_0.2.2 munsell_0.5.1 jquerylib_0.1.4
## [91] Rcpp_1.0.13-1 dbplyr_2.5.0 png_0.1-8
## [94] parallel_4.4.2 blob_1.2.4 sparseMatrixStats_1.19.0
## [97] SpatialExperiment_1.17.0 slam_0.1-55 viridisLite_0.4.2
## [100] scales_1.3.0 purrr_1.0.2 crayon_1.5.3
## [103] rlang_1.1.4 KEGGREST_1.47.0