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, units = "secs") all_times[[options$label]] <<- res } } })) knitr::opts_chunk$set( tidy = TRUE, tidy.opts = list(width.cutoff = 95), warning = FALSE, error = FALSE, message = FALSE, fig.width = 8, time_it = TRUE, error = TRUE )
In this vignette, we first build an integrated reference and then demonstrate how to leverage this reference to annotate new query datasets. Generating an integrated reference follows the same workflow described in more detail in the integration introduction vignette. Once generated, this reference can be used to analyze additional query datasets through tasks like cell type label transfer and projecting query cells onto reference UMAPs. Notably, this does not require correction of the underlying raw query data and can therefore be an efficient strategy if a high quality reference is available.
For the purposes of this example, we've chosen human pancreatic islet cell datasets produced across four technologies, CelSeq (GSE81076) CelSeq2 (GSE85241), Fluidigm C1 (GSE86469), and SMART-Seq2 (E-MTAB-5061). For convenience, we distribute this dataset through our SeuratData package. The metadata contains the technology (tech
column) and cell type annotations (celltype
column) for each cell in the four datasets.
library(Seurat) library(SeuratData) library(ggplot2)
InstallData('panc8')
As a demonstration, we will use a subset of technologies to construct a reference. We will then map the remaining datasets onto this reference. We start by selecting cells from four technologies, and performing an analysis without integration.
panc8 <- LoadData('panc8') table(panc8$tech) # we will use data from 2 technologies for the reference pancreas.ref <- subset(panc8, tech %in% c("celseq2", "smartseq2")) pancreas.ref[["RNA"]] <- split(pancreas.ref[["RNA"]], f = pancreas.ref$tech) # pre-process dataset (without integration) pancreas.ref <- NormalizeData(pancreas.ref) pancreas.ref <- FindVariableFeatures(pancreas.ref) pancreas.ref <- ScaleData(pancreas.ref) pancreas.ref <- RunPCA(pancreas.ref) pancreas.ref <- FindNeighbors(pancreas.ref, dims=1:30) pancreas.ref <- FindClusters(pancreas.ref) pancreas.ref <- RunUMAP(pancreas.ref, dims = 1:30) DimPlot(pancreas.ref,group.by = c("celltytpe","tech"))
Next, we integrate the datasets into a shared reference. Please see our introduction to integration vignette
pancreas.ref <- IntegrateLayers( object = pancreas.ref, method = CCAIntegration, orig.reduction = "pca", new.reduction = 'integrated.cca', verbose = FALSE) pancreas.ref <- FindNeighbors(pancreas.ref,reduction='integrated.cca',dims=1:30) pancreas.ref <- FindClusters(pancreas.ref) pancreas.ref <- RunUMAP(pancreas.ref,reduction='integrated.cca',dims=1:30) DimPlot(pancreas.ref,group.by = c("tech","celltype"))
Seurat also supports the projection of reference data (or meta data) onto a query object. While many of the methods are conserved (both procedures begin by identifying anchors), there are two important distinctions between data transfer and integration:
After finding anchors, we use the TransferData()
function to classify the query cells based on reference data (a vector of reference cell type labels). TransferData()
returns a matrix with predicted IDs and prediction scores, which we can add to the query metadata.
# select two technologies for the query datasets pancreas.query <- subset(panc8, tech %in% c("fluidigmc1","celseq")) pancreas.query <- NormalizeData(pancreas.query) pancreas.anchors <- FindTransferAnchors(reference = pancreas.ref, query = pancreas.query, dims = 1:30, reference.reduction="pca") predictions <- TransferData(anchorset = pancreas.anchors, refdata = pancreas.ref$celltype, dims = 1:30) pancreas.query <- AddMetaData(pancreas.query, metadata = predictions)
Because we have the original label annotations from our full integrated analysis, we can evaluate how well our predicted cell type annotations match the full reference. In this example, we find that there is a high agreement in cell type classification, with over 96% of cells being labeled correctly.
pancreas.query$prediction.match <- pancreas.query$predicted.id == pancreas.query$celltype table(pancreas.query$prediction.match)
To verify this further, we can examine some canonical cell type markers for specific pancreatic islet cell populations. Note that even though some of these cell types are only represented by one or two cells (e.g. epsilon cells), we are still able to classify them correctly.
table(pancreas.query$predicted.id) VlnPlot(pancreas.query, c("REG1A", "PPY", "SST", "GHRL", "VWF", "SOX10"), group.by = "predicted.id")
We also enable projection of a query onto the reference UMAP structure. This can be achieved by computing the reference UMAP model and then calling MapQuery()
instead of TransferData()
.
pancreas.ref <- RunUMAP(pancreas.ref, dims = 1:30, reduction = "integrated.cca", return.model = TRUE) pancreas.query <- MapQuery( anchorset = pancreas.anchors, reference = pancreas.ref, query = pancreas.query, refdata = list(celltype = 'celltype'), reference.reduction = "pca", reduction.model = '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()
is used to integrate reference with query by correcting the query's projected low-dimensional embeddings; and finally ProjectUMAP()
is 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:
pancreas.query <- TransferData( anchorset = pancreas.anchors, reference = pancreas.ref, query = pancreas.query, refdata = list(celltype = "celltype") ) pancreas.query <- IntegrateEmbeddings( anchorset = pancreas.anchors, reference = pancreas.ref, query = pancreas.query, new.reduction.name = "ref.pca" ) pancreas.query <- ProjectUMAP( query = pancreas.query, query.reduction = "ref.pca", reference = pancreas.ref, reference.reduction = "pca", reduction.model = "umap" )
We can now visualize the query cells alongside our reference.
p1 <- DimPlot(pancreas.ref, reduction = "umap", group.by = "celltype", label = TRUE, label.size = 3 ,repel = TRUE) + NoLegend() + ggtitle("Reference annotations") p2 <- DimPlot(pancreas.query, reduction = "ref.umap", group.by = "predicted.celltype", label = TRUE, label.size = 3 ,repel = TRUE) + NoLegend() + ggtitle("Query transferred labels") p1 + p2
write.csv(x = t(as.data.frame(all_times)), file = "../output/timings/integration_reference_mapping.csv")
Session Info
sessionInfo()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.