all_times <- list() # store the time for each chunk knitr::knit_hooks$set(time_it = local({ now <- NULL function(before, options) { if (before) { now <<- Sys.time() } else { res <- difftime(Sys.time(), now) all_times[[options$label]] <<- res } } })) knitr::opts_chunk$set( message = FALSE, warning = FALSE, time_it = TRUE, error = TRUE )
This vignette introduces the process of mapping query datasets to annotated references in Seurat. In this example, we map one of the first scRNA-seq datasets released by 10X Genomics of 2,700 PBMC to our recently described CITE-seq reference of 162,000 PBMC measured with 228 antibodies. We chose this example to demonstrate how supervised analysis guided by a reference dataset can help to enumerate cell states that would be challenging to find with unsupervised analysis. In a second example, we demonstrate how to serially map Human Cell Atlas datasets of human BMNC profiled from different individuals onto a consistent reference.
We have previously demonstrated how to use reference-mapping approach to annotate cell labels in a query dataset . In Seurat v4, we have substantially improved the speed and memory requirements for integrative tasks including reference mapping, and also include new functionality to project query cells onto a previously computed UMAP visualization.
In this vignette, we demonstrate how to use a previously established reference to interpret an scRNA-seq query:
To run this vignette please install Seurat v4, available on CRAN. Additionally, you will need to install the SeuratDisk
package.
install.packages("Seurat") remotes::install_github("mojaveazure/seurat-disk")
library(Seurat) library(SeuratDisk) library(ggplot2) library(patchwork)
options(SeuratData.repo.use = "http://satijalab04.nygenome.org")
We load the reference (download here) from our recent paper, and visualize the pre-computed UMAP. This reference is stored as an h5Seurat file, a format that enables on-disk storage of multimodal Seurat objects (more details on h5Seurat and SeuratDisk
can be found here).
reference <- readRDS("../data/pbmc_multimodal_2023.rds")
DimPlot(object = reference, reduction = "wnn.umap", group.by = "celltype.l2", label = TRUE, label.size = 3, repel = TRUE) + NoLegend()
To demonstrate mapping to this multimodal reference, we will use a dataset of 2,700 PBMCs generated by 10x Genomics and available via SeuratData
.
library(SeuratData) pbmc3k <- LoadData('pbmc3k') pbmc3k[['RNA']] <- as(pbmc3k[['RNA']], Class = 'Assay5')
The reference was normalized using SCTransform()
, so we use the same approach to normalize the query here.
pbmc3k <- SCTransform(pbmc3k, verbose = FALSE)
We then find anchors between reference and query. As described in the manuscript, we used a precomputed supervised PCA (spca) transformation for this example. We recommend the use of supervised PCA for CITE-seq datasets, and demonstrate how to compute this transformation on the next tab of this vignette. However, you can also use a standard PCA transformation.
anchors <- FindTransferAnchors( reference = reference, query = pbmc3k, normalization.method = "SCT", reference.reduction = "spca", dims = 1:50 )
We then transfer cell type labels and protein data from the reference to the query. Additionally, we project the query data onto the UMAP structure of the reference.
pbmc3k <- MapQuery( anchorset = anchors, query = pbmc3k, reference = reference, refdata = list( celltype.l1 = "celltype.l1", celltype.l2 = "celltype.l2", predicted_ADT = "ADT" ), reference.reduction = "spca", reduction.model = "wnn.umap" )
What is
MapQuery
doing?
MapQuery()
is a wrapper around three functions: TransferData()
, IntegrateEmbeddings()
, and ProjectUMAP()
. TransferData()
is used to transfer cell type labels and impute the ADT values. IntegrateEmbeddings()
and ProjectUMAP()
are used to project the query data onto the UMAP structure of the reference. The equivalent code for doing this with the intermediate functions is below:
pbmc3k <- TransferData( anchorset = anchors, reference = reference, query = pbmc3k, refdata = list( celltype.l1 = "celltype.l1", celltype.l2 = "celltype.l2", predicted_ADT = "ADT") ) pbmc3k <- IntegrateEmbeddings( anchorset = anchors, reference = reference, query = pbmc3k, new.reduction.name = "ref.spca" ) pbmc3k <- ProjectUMAP( query = pbmc3k, query.reduction = "ref.spca", reference = reference, reference.reduction = "spca", reduction.model = "wnn.umap" )
We can now visualize the 2,700 query cells. They have been projected into a UMAP visualization defined by the reference, and each has received annotations at two levels of granularity (level 1, and level 2).
p1 = DimPlot(pbmc3k, reduction = "ref.umap", group.by = "predicted.celltype.l1", label = TRUE, label.size = 3, repel = TRUE) + NoLegend() p2 = DimPlot(pbmc3k, reduction = "ref.umap", group.by = "predicted.celltype.l2", label = TRUE, label.size = 3 ,repel = TRUE) + NoLegend() p1 + p2
The reference-mapped dataset helps us identify cell types that were previously blended in an unsupervised analysis of the query dataset. Just a few examples include plasmacytoid dendritic cells (pDC), hematopoietic stem and progenitor cells (HSPC), regulatory T cells (Treg), CD8 Naive T cells, cells, CD56+ NK cells, memory, and naive B cells, and plasmablasts.
Each prediction is assigned a score between 0 and 1.
FeaturePlot(pbmc3k, features = c("pDC", "CD16 Mono", "Treg"), reduction = "ref.umap", cols = c("lightgrey", "darkred"), ncol = 3) & theme(plot.title = element_text(size = 10))
library(ggplot2) plot <- FeaturePlot(pbmc3k, features = "CD16 Mono", reduction = "ref.umap", cols = c("lightgrey", "darkred")) + ggtitle("CD16 Mono") + theme(plot.title = element_text(hjust = 0.5, size = 30)) + labs(color = "Prediction Score") + xlab("UMAP 1") + ylab("UMAP 2") + theme(axis.title = element_text(size = 18), legend.text = element_text(size = 18), legend.title = element_text(size = 25)) ggsave(filename = "../output/images/multimodal_reference_mapping.jpg", height = 7, width = 12, plot = plot, quality = 50)
We can verify our predictions by exploring the expression of canonical marker genes. For example, CLEC4C and LIRA4 have been reported as markers of pDC identity, consistent with our predictions. Similarly, if we perform differential expression to identify markers of Tregs, we identify a set of canonical markers including RTKN2, CTLA4, FOXP3, and IL2RA.
Idents(pbmc3k) <- 'predicted.celltype.l2' VlnPlot(pbmc3k, features = c("CLEC4C", "LILRA4"), sort = TRUE) + NoLegend() treg_markers <- FindMarkers(pbmc3k, ident.1 = "Treg", only.pos = TRUE, logfc.threshold = 0.1) print(head(treg_markers))
Finally, we can visualize the imputed levels of surface protein, which were inferred based on the CITE-seq reference.
DefaultAssay(pbmc3k) <- 'predicted_ADT' # see a list of proteins: rownames(pbmc3k) FeaturePlot(pbmc3k, features = c("CD3-1", "CD45RA", "IgD"), reduction = "ref.umap", cols = c("lightgrey", "darkgreen"), ncol = 3)
In the previous examples, we visualize the query cells after mapping to the reference-derived UMAP. Keeping a consistent visualization can assist with the interpretation of new datasets. However, if there are cell states that are present in the query dataset that are not represented in the reference, they will project to the most similar cell in the reference. This is the expected behavior and functionality as established by the UMAP package, but can potentially mask the presence of new cell types in the query which may be of interest.
In our manuscript, we map a query dataset containing developing and differentiated neutrophils, which are not included in our reference. We find that computing a new UMAP ('de novo visualization') after merging the reference and query can help to identify these populations, as demonstrated in Supplementary Figure 8. In the 'de novo' visualization, unique cell states in the query remain separated. In this example, the 2,700 PBMC does not contain unique cell states, but we demonstrate how to compute this visualization below.
We emphasize that if users are attempting to map datasets where the underlying samples are not PBMC, or contain cell types that are not present in the reference, computing a 'de novo' visualization is an important step in interpreting their dataset.
reference <- DietSeurat(reference, counts = FALSE, dimreducs = "spca") pbmc3k <- DietSeurat(pbmc3k, counts = FALSE, dimreducs = "ref.spca")
#merge reference and query reference$id <- 'reference' pbmc3k$id <- 'query' refquery <- merge(reference, pbmc3k) refquery[["spca"]] <- merge(reference[["spca"]], pbmc3k[["ref.spca"]]) refquery <- RunUMAP(refquery, reduction = 'spca', dims = 1:50) DimPlot(refquery, group.by = 'id', shuffle = TRUE)
As a second example, we map a dataset of human bone marrow mononuclear (BMNC) cells from eight individual donors, produced by the Human Cell Atlas. As a reference, we use the CITE-seq reference of human BMNC that we analyzed using weighted-nearest neighbor analysis (WNN).
This vignette exhibits the same reference-mapping functionality as the PBMC example on the previous tab. In addition, we also demonstrate:
# Both datasets are available through SeuratData library(SeuratData) #load reference data InstallData("bmcite") bm <- LoadData("bmcite") bm[['RNA']] <- as(bm[['RNA']], Class = 'Assay5') #load query data InstallData('hcabm40k') hcabm40k <- LoadData("hcabm40k") hcabm40k[['RNA']] <- as(hcabm40k[['RNA']], Class = 'Assay5')
The reference dataset contains a WNN graph, reflecting a weighted combination of the RNA and protein data in this CITE-seq experiment.
We can compute a UMAP visualization based on this graph. We set return.model = TRUE
, which will enable us to project query datasets onto this visualization.
bm <- RunUMAP(bm, nn.name = "weighted.nn", reduction.name = "wnn.umap", reduction.key = "wnnUMAP_", return.model = TRUE) DimPlot(bm, group.by = "celltype.l2", reduction = "wnn.umap")
As described in our manuscript, we first compute a 'supervised' PCA. This identifies the transformation of the transcriptome data that best encapsulates the structure of the WNN graph. This allows a weighted combination of the protein and RNA measurements to 'supervise' the PCA, and highlight the most relevant sources of variation. After computing this transformation, we can project it onto a query dataset. We can also compute and project a PCA projection, but recommend the use of sPCA when working with multimodal references that have been constructed with WNN analysis.
The sPCA calculation is performed once, and then can be rapidly projected onto each query dataset.
bm <- ScaleData(bm, assay = 'RNA') bm <- RunSPCA(bm, assay = 'RNA', graph = 'wsnn')
Since we will be mapping multiple query samples to the same reference, we can cache particular steps that only involve the reference. This step is optional but will improve speed when mapping multiple samples.
We compute the first 50 neighbors in the sPCA space of the reference. We store this information in the spca.annoy.neighbors
object within the reference Seurat object and also cache the annoy index data structure (via cache.index = TRUE
).
bm <- FindNeighbors( object = bm, reduction = "spca", dims = 1:50, graph.name = "spca.annoy.neighbors", k.param = 50, cache.index = TRUE, return.neighbor = TRUE, l2.norm = TRUE )
How can I save and load a cached annoy index?
If you want to save and load a cached index for a Neighbor
object generated with method = "annoy"
and cache.index = TRUE
, use the SaveAnnoyIndex()
/LoadAnnoyIndex()
functions. Importantly, this index cannot be saved normally to an RDS or RDA file, so it will not persist correctly across R session restarts or saveRDS
/readRDS
for the Seurat object containing it. Instead, use LoadAnnoyIndex()
to add the Annoy index to the Neighbor
object every time R restarts or you load the reference Seurat object from RDS. The file created by SaveAnnoyIndex()
can be distributed along with a reference Seurat object, and added to the Neighbor
object in the reference.
bm[["spca.annoy.neighbors"]] SaveAnnoyIndex(object = bm[["spca.annoy.neighbors"]], file = "../data/reftmp.idx") bm[["spca.annoy.neighbors"]] <- LoadAnnoyIndex(object = bm[["spca.annoy.neighbors"]], file = "../data/reftmp.idx")
Here we will demonstrate mapping multiple donor bone marrow samples to the multimodal bone marrow reference. These query datasets are derived from the Human Cell Atlas (HCA) Immune Cell Atlas Bone marrow dataset and are available through SeuratData. This dataset is provided as a single merged object with 8 donors. We first split the data back into 8 separate Seurat objects, one for each original donor to map individually.
library(dplyr) library(SeuratData) hcabm40k <- LoadData('hcabm40k') hcabm40k[['RNA']] <- as(hcabm40k[['RNA']], Class = 'Assay5') hcabm40k.batches <- SplitObject(hcabm40k, split.by = "orig.ident")
We then normalize the query in the same manner as the reference. Here, the reference was normalized using log-normalization via NormalizeData()
. If the reference had been normalized using SCTransform()
, the query must be normalized with SCTransform()
as well.
hcabm40k.batches <- lapply(X = hcabm40k.batches, FUN = NormalizeData, verbose = FALSE)
We then find anchors between each donor query dataset and the multimodal reference. This command is optimized to minimize mapping time, by passing in a pre-computed set of reference neighbors, and turning off anchor filtration.
anchors <- list() for (i in 1:length(hcabm40k.batches)) { anchors[[i]] <- FindTransferAnchors( reference = bm, query = hcabm40k.batches[[i]], k.filter = NA, reference.reduction = "spca", reference.neighbors = "spca.annoy.neighbors", dims = 1:50 ) }
We then individually map each of the datasets.
for (i in 1:length(hcabm40k.batches)) { hcabm40k.batches[[i]] <- MapQuery( anchorset = anchors[[i]], query = hcabm40k.batches[[i]], reference = bm, refdata = list( celltype = "celltype.l2", predicted_ADT = "ADT"), reference.reduction = "spca", reduction.model = "wnn.umap" ) }
Now that mapping is complete, we can visualize the results for individual objects
p1 <- DimPlot(hcabm40k.batches[[1]], reduction = 'ref.umap', group.by = 'predicted.celltype', label.size = 3) p2 <- DimPlot(hcabm40k.batches[[2]], reduction = 'ref.umap', group.by = 'predicted.celltype', label.size = 3) p1 + p2 + plot_layout(guides = "collect")
We can also merge all the objects into one dataset. Note that they have all been integrated into a common space, defined by the reference. We can then visualize the results together.
# Merge the batches hcabm40k <- merge(hcabm40k.batches[[1]], hcabm40k.batches[2:length(hcabm40k.batches)], merge.dr = "ref.umap") DimPlot(hcabm40k, reduction = "ref.umap", group.by = "predicted.celltype", label = TRUE, repel = TRUE, label.size = 3) + NoLegend()
We can visualize gene expression, cluster prediction scores, and (imputed) surface protein levels in the query cells:
p3 <- FeaturePlot(hcabm40k, features = c("rna_TRDC", "rna_MPO", "rna_AVP"), reduction = 'ref.umap', max.cutoff = 3, ncol = 3) # cell type prediction scores DefaultAssay(hcabm40k) <- 'prediction.score.celltype' p4 <- FeaturePlot(hcabm40k, features = c("CD16 Mono", "HSC", "Prog-RBC"), ncol = 3, cols = c("lightgrey", "darkred")) # imputed protein levels DefaultAssay(hcabm40k) <- 'predicted_ADT' p5 <- FeaturePlot(hcabm40k, features = c("CD45RA", "CD16", "CD161"), reduction = 'ref.umap', min.cutoff = 'q10', max.cutoff = 'q99', cols = c("lightgrey", "darkgreen") , ncol = 3) p3 / p4 / p5
write.csv(x = t(as.data.frame(all_times)), file = "../output/timings/seurat5_reference_mapping_times.csv")
Session Info
sessionInfo()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.