Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Mapping cells and transferring labels

Mapping finds each new cell’s nearest cells in a fixed reference analysis and labels the new cell from them, without rebuilding the reference. This page maps 10,111 human peripheral blood mononuclear cells (PBMCs) stimulated with interferon beta onto a reference of 8,486 control PBMCs from the same experiment (Kang et al. 2018), transfers the reference cells’ annotated cell types, stored in cluster_labels, and checks the transferred labels against the annotated cell types of the stimulated cells.

Of the stimulated cells, 9,499 receive a label and 612 receive none because their nearest reference cells disagree, and 55% of the labeled cells agree with their annotated label. Most CD4 naive T cells (86%), CD8 T cells (91%) and NK cells (87%) keep their labels, against 36% of CD4 memory T cells and 56% of B cells. The CD14 monocytes and dendritic cells almost never do: 99.7% of the 2,532 CD14 monocytes and 96.7% of the 214 dendritic cells are labeled CD16 monocytes, most of them by a unanimous vote of their three nearest reference cells. Interferon beta induces interferon-stimulated genes such as ISG15 in every stimulated cell type, and in the control reference ISG15 is highest in a small group of monocytes labeled CD16 monocytes. The stimulated CD14 monocytes take those few cells as their nearest neighbors.

The dataset merging tutorial takes the other route with the same two samples: it merges them into one store and analyzes their cells together.

Mapping supports RNA data only. The reference and the query stay in separate stores, and the query store must be writable, because the mapping and the transferred labels are saved there.

Open the reference and the query

DataStore has 8486 (8487) cells with 1 assays: RNA Cell metadata: 'I', 'ids', 'names', 'RNA_UMAP1', 'RNA_UMAP2', 'RNA_clusters', 'RNA_leiden_cluster', 'RNA_nCounts', 'RNA_nFeatures', 'RNA_percentMito', 'RNA_percentRibo', 'cluster_labels' RNA assay has 35635 features and following metadata: 'I', 'ids', 'names', 'dropOuts', 'nCells'

Started: 2026-11-19T08:31:03.340458Z | Wall time: 9.548 s

DataStore has 10111 (10111) cells with 1 assays: RNA Cell metadata: 'I', 'ids', 'names', 'RNA_UMAP1', 'RNA_UMAP2', 'RNA_clusters', 'RNA_leiden_cluster', 'RNA_nCounts', 'RNA_nFeatures', 'RNA_percentMito', 'RNA_percentRibo', 'cluster_labels' RNA assay has 35635 features and following metadata: 'I', 'ids', 'names', 'dropOuts', 'nCells'

Started: 2026-11-19T08:31:12.889956Z | Wall time: 8.876 s

Both stores hold an annotated cell type for each cell in cluster_labels, 13 types in all, and a saved RNA analysis of their cells. Each figure shows one sample on its own UMAP.

UMAP of the 8,486 control reference cells, colored and labeled by the 13 annotated cell types. Lymphocytes form a large island on the left, with B cells and activated B cells at the top, NK cells, CD8 T cells and CD4 memory T cells below them, and CD4 naive T cells at the bottom; monocytes form a separate island on the right, with CD16 monocytes on its left, dendritic cells in a thin strip at its top left and CD14 monocytes filling the rest.
UMAP of the 10,111 stimulated query cells on its own layout, colored and labeled by the same 13 annotated cell types. NK and CD8 T cells sit at the top left, CD4 memory and naive T cells below them, B cells and activated B cells at the bottom center, and the monocyte island at the top right, with CD14 monocytes at its top, dendritic cells on its left edge and CD16 monocytes at its bottom.

Started: 2026-11-19T08:31:21.767271Z | Wall time: 0.309 s

The two UMAPs were computed separately, so the same cell type sits in different places: the B cells are at the top left of the control map and at the bottom of the stimulated map. Mapping leaves both layouts as they are: it finds each stimulated cell’s nearest control cells in the PCA space of the control analysis, and the control UMAP then shows which control cells the stimulated cells reached.

Prepare a reusable reference

The control store holds a saved RNA analysis labeled rna, with its selected genes, normalization, PCA, neighbors and UMAP. pipeline.open(label="rna") reopens that analysis as a PipelineRun, and indexing the run by a step’s output name, as in run["neighbors"], returns an ArtifactRef, the handle of that step’s saved result. mapping.build_reference saves what mapping needs from the analysis as one artifact: the gene panel, which holds the analysis’s highly variable genes, the scaling of each gene and the PCA model, with a link to the analysis’s neighbor index. mapping.load_reference loads the artifact as a MappingReference. The UMAP is not part of the reference; the code keeps it to draw the reference cells.

{'reference': ArtifactRef(assay='RNA', kind='mapping_reference', artifact_id='95bc3b206085...'), 'reference cells': 8486, 'panel genes': 2000, 'PCA dimensions': 25}

Started: 2026-11-19T08:31:22.077889Z | Wall time: 0.577 s

The reference covers all 8,486 control cells, with a panel of 2,000 genes and 25 principal components. A MappingReference is immutable: its genes, scaling, PCA model and neighbor index stay fixed, so every query maps onto the same reference. The reference holds no cell labels; label transfer reads them when it runs. reference_ref is the ArtifactRef of the new artifact, and later calls take such handles to find their inputs.

Map the query

ds_stim.mapping.run matches the query’s genes to the reference panel by gene ID, normalizes and scales the query counts with the reference’s parameters, projects them into the reference PCA space, and saves the three nearest reference cells of each query cell (save_k=3, the default) in the query store. The call maps the cells in query_run["analysis_cell_selection"], the frozen selection of cells that the stimulated sample’s own analysis used, so every mapped cell also has a position on the query UMAP.

ArtifactRef(assay='RNA', kind='projection', artifact_id='27074d927074...')

Started: 2026-11-19T08:31:22.656032Z | Wall time: 2.130 s

The saved projection reports how closely the query matches the reference:

{'featureCoverage': 1.0, 'queryBatchCount': 1, 'algorithmVariant': 'scaled_pca', 'uninformativeCellCount': 0, 'queryScaledDispersion': 1.329895474385472}

Started: 2026-11-19T08:31:24.787536Z | Wall time: 0.034 s

  • featureCoverage is the share of panel genes that the query measured, 1.0 here.

  • uninformativeCellCount counts the query cells without counts in any panel gene that the query measured, none here. Such cells receive no label.

  • queryScaledDispersion is the query’s mean squared deviation from the reference gene means, with each gene scaled by its reference standard deviation, so the reference cells themselves score exactly 1. The stimulated cells score 1.33: they lie farther from the reference’s average expression than the reference cells do. With a featureCoverage below 1 the value is not comparable with 1, because each query cell is then normalized over fewer genes than the reference cells were.

  • algorithmVariant and queryBatchCount record the projection method, here a scaled PCA without batch correction, and the number of query batches, here one.

Where did the query land?

Each query cell gives a weight to each of its three nearest reference cells, more to nearer ones. A reference cell’s mapping score adds up the weight that the cell receives from one group of query cells, scaled by the size of the group and shown on a log scale. Scored as one group, the whole query would spread its weight over many reference cells, so the code splits the query into four populations by their annotated labels and pools the remaining cells as other. The annotated labels spell CD14 monocytes CD 14 Mono, with a space.

cells
query population
other3462
CD 14 Mono2532
CD4 naive T2390
CD4 Memory T1297
NK430

Started: 2026-11-19T08:31:24.822074Z | Wall time: 0.032 s

Each panel draws one group’s mapping score on the reference UMAP; reference cells that received no weight from the group are gray.

Five panels of the control reference UMAP, one for each query group: other cells, CD4 memory T cells, CD4 naive T cells, CD14 monocytes and NK cells. Reference cells are colored and sized by the mapping score that they received from the group, on a dark-to-light scale, and drawn small and gray where they received none. The T-cell and NK groups light up the matching T-cell and NK regions of the reference, and the other cells light up the B cells, scattered T cells and the lower edge of the CD16 monocytes. The CD14 monocytes light up a narrow band that runs along the lower edge of the CD16 monocytes to the CD14 monocytes, and a few low-scoring cells among the T cells and at the edges of the CD14 monocytes.

Started: 2026-11-19T08:31:24.854868Z | Wall time: 0.343 s

The query’s CD4 naive T, CD4 memory T and NK cells send their weight to the matching regions of the reference. Almost none of the weight of the 2,532 CD14 monocytes reaches the reference CD14 monocytes: it falls on a narrow band that runs along the lower edge of the CD16 monocytes to the CD14 monocytes. A whole population that crowds onto a small part of another reference population points to a query state that the reference lacks.

Transfer labels

mapping.transfer_labels gives each query cell the label with the largest share of its neighbors’ weight, where nearer neighbors weigh more. By default the winning label needs at least half of the weight (threshold_fraction=0.5); otherwise the cell abstains and gets no label. The result is saved in the query store as a label_transfer artifact, together with a frozen copy of the reference labels it read, so a later change to the reference annotation cannot change the result.

label labeled 9499 abstained 612 Name: query cells, dtype: int64

Started: 2026-11-19T08:31:25.199306Z | Wall time: 0.171 s

An abstained cell has a missing label, and the abstentionReason column of transfer.evidence gives the reason, such as below_threshold when the winning label holds less than threshold_fraction of the neighbor weight. The mapping API reference lists the other four reasons.

abstentionReason below_threshold 612 Name: count, dtype: int64

Started: 2026-11-19T08:31:25.371384Z | Wall time: 0.003 s

All 612 abstentions fall below the threshold. The figure compares the annotated and the transferred labels on the query UMAP, with abstained cells in gray. Passing transfer_ref to color_by colors the embedding from the saved transfer, without writing a column into the query’s cell metadata.

Two panels of the stimulated query UMAP, colored by the annotated labels (left) and by the transferred labels (right), with the 612 abstained cells in gray. The lymphocyte island keeps most of its colors, with gray abstained cells scattered among the CD4 memory T, activated T, B and activated B cells, and CD8 T labels among the CD4 memory T cells and B cells. The whole monocyte island, CD14 monocytes and dendritic cells included, is colored as CD16 monocytes after transfer, apart from a handful of cells that keep the DC and CD 14 Mono labels.

Started: 2026-11-19T08:31:25.375230Z | Wall time: 0.279 s

The lymphocytes keep most of their annotated labels, though CD8 T labels and gray abstentions appear among the CD4 memory T cells and B cells. The whole monocyte island, CD14 monocytes and dendritic cells included, is labeled CD16 Mono.

Inspect the evidence behind each label

transfer.evidence holds the evidence for every transferred label, and mapping_evidence plots it for the query cells of each annotated label:

  • voteFraction: the share of neighbor weight behind the winning label.

  • topTwoMargin: the winning share minus the share of the runner-up.

  • referenceDistancePercentile: the percentile of the distance to the nearest reference cell among the reference cells’ own nearest-neighbor distances. At 0.95, a query cell is farther from its nearest reference cell than 95% of reference cells are from theirs.

Here only voteFraction decides whether a cell receives a label; the margin and the distance percentile change no label.

Box plots of three evidence metrics for the query cells of each of the 13 annotated labels: the vote fraction of the winning label (left), the margin between the top two labels (middle) and the reference distance percentile (right). The monocytes, dendritic cells, erythrocytes and pDCs sit at a vote fraction and margin of 1, while B cells, activated B and T cells and CD4 memory T cells have median vote fractions near 0.67 and margins near 0.35. Distance percentiles are high for every population, with medians between about 0.9 and 1.

Started: 2026-11-19T08:31:25.655005Z | Wall time: 0.234 s

Nine of the 13 populations have a median voteFraction of 1, the monocytes, dendritic cells, CD4 naive T, CD8 T and NK cells among them: the typical cell’s three neighbors agree. The B cells, CD4 memory T cells and activated B and T cells have medians near 0.67, the share when two of three neighbors at similar distances carry the winning label. In every population the median distance percentile is about 0.9 or higher, so the typical stimulated cell lies farther from its nearest reference cell than about 90% of reference cells lie from theirs: the whole stimulated sample sits away from the reference, not only the monocytes.

Check the transferred labels against the annotated labels

The stimulated cells carry their own cluster_labels, so the transferred labels can be scored against them. mapping_confusion cross-tabulates the annotated labels (rows, Known label) against the transferred labels (columns, Predicted label), with abstentions in the first column.

Row-normalized confusion matrix of the annotated query labels (rows, Known label) against the transferred labels and abstentions (columns, Predicted label), with the percentage in each cell. CD14 monocytes and dendritic cells fall almost entirely into the CD16 Mono column (100% and 97%), CD16 monocytes stay CD16 Mono, and CD4 naive T, CD8 T and NK cells keep their labels in 86%, 91% and 87% of cells, while CD4 memory T cells spread over the abstained, CD4 memory T, CD4 naive T and CD8 T columns.

Started: 2026-11-19T08:31:25.889864Z | Wall time: 0.225 s

The table gives, for each annotated label of the stimulated cells, the share of cells that keep their label, that are labeled CD16 Mono or that abstain, with the population’s median vote fraction and distance percentile.

cellsagreementlabeled CD16 Monoabstainedmedian vote fractionmedian distance percentile
annotated label
CD 14 Mono25320.0010.9970.0001.0000.950
CD4 naive T23900.8560.0010.0301.0000.953
CD4 Memory T12970.3620.0010.1800.6710.947
CD16 Mono9561.0001.0000.0001.0000.896
B7460.5590.0420.1970.6690.995
CD8 T6550.9070.0020.0241.0000.970
NK4300.8670.0000.0281.0000.988
T activated2820.3010.0000.1990.6870.987
Mk2470.3040.2960.1131.0000.986
DC2140.0190.9670.0001.0000.990
B activated2050.5800.0000.2000.6780.995
Eryth830.4700.1570.0241.0000.974
pDC740.7570.1220.0681.0000.999

Started: 2026-11-19T08:31:26.115820Z | Wall time: 0.010 s

All 956 CD16 monocytes keep their label, and 99.7% of the CD14 monocytes and 96.7% of the dendritic cells take the same label, CD16 Mono, at a median vote fraction of 1: their wrong labels rest on unanimous votes. Smaller shares of the megakaryocytes (Mk, 29.6%), erythrocytes (Eryth, 15.7%) and pDCs (12.2%) are labeled CD16 Mono as well. Abstentions concentrate where votes split: about a fifth of the B cells, activated B cells, CD4 memory T cells and activated T cells abstain.

mapping_calibration shows what a stricter vote threshold would change. It applies each threshold to the candidate labels saved with the transfer, without recomputing them, and plots the accuracy of the cells that the threshold keeps against their share of all query cells, with a 95% confidence band. Accuracy counts only the kept cells, unlike the agreement column above, which counts an abstained cell as a mismatch. The red point marks the transfer’s own threshold_fraction, and the lowest threshold, 0.333, keeps every cell: with three neighbors, the winning label always holds at least a third of the weight.

Held-out label accuracy (y axis, 0 to 1) against retained coverage (x axis, 0 to 1) across vote thresholds, with a shaded 95% confidence band. The curve runs almost flat between accuracies of about 0.53 and 0.56 as coverage falls from 1 to about 0.66, and a red point marks the transfer's threshold of 0.5 at a coverage of 0.94 and an accuracy of 0.55.
nAcceptedcoverageaccuracy
threshold
1.00066440.6570.531
0.50094990.9390.551
0.333101111.0000.530

Started: 2026-11-19T08:31:26.126656Z | Wall time: 0.091 s

Accuracy barely changes with the threshold: 0.530 when every cell keeps its candidate label, 0.551 at the transfer’s threshold of 0.5, and 0.531 when only unanimous votes count, which keeps 66% of the cells. A threshold stricter than 0.5 removes cells with split votes, which are right more often than the unanimous cells, and keeps the unanimous monocyte errors.

Why stimulated monocytes take the CD16 monocyte label

Each transferred label comes from a cell’s three nearest reference cells. Loading the projection with load_arrays=True adds those neighbors as positions among the reference cells, and reference.fetch_cell_column reads the reference labels in the same order. The table counts, for five populations of the query, how many distinct reference cells their neighbors include, and how those neighbors are labeled.

query cellsreference cells reachedmost common neighbor labelshare of neighbors
CD 14 Mono253261CD16 Mono0.997
DC21431CD16 Mono0.961
CD16 Mono95630CD16 Mono1.0
CD4 naive T2390153CD4 naive T0.772
NK43079NK0.829

Started: 2026-11-19T08:31:26.218372Z | Wall time: 0.054 s

The 2,532 stimulated CD14 monocytes draw all their neighbors from 61 of the 8,486 reference cells, and 99.7% of those neighbors are labeled CD16 Mono. The neighbors of the 214 dendritic cells come from 31 reference cells and are 96.1% CD16 Mono, and those of the 956 CD16 monocytes come from 30 reference cells, all labeled CD16 Mono. The stimulated CD4 naive T cells and NK cells also crowd onto few reference cells, 153 and 79, but 77.2% and 82.9% of their neighbors carry the matching label.

The figure colors ISG15, an interferon-stimulated gene, on the reference UMAP (left) and the query UMAP (middle) on one color scale, and marks the 61 neighbors of the stimulated CD14 monocytes in black on the faded reference labels (right).

Three UMAP panels. Left: the control reference UMAP colored by ISG15 as log(1 + counts per 1,000) on a scale from 0 to 4.5; most cells are dark, some lymphocytes are moderately bright, and the brightest cells form a narrow band that runs along the lower edge of the CD16 monocytes to the CD14 monocytes. Middle: the stimulated query UMAP on the same scale; every population is bright, and the monocyte island brightest. Right: the control reference UMAP with faded annotated labels, named in a side legend, and the 61 reference neighbors of the stimulated CD14 monocytes in black, gathered on that band, with a few among the T cells and at the edges of the CD14 monocytes.

Started: 2026-11-19T08:31:26.273287Z | Wall time: 0.847 s

In the control cells, ISG15 is low in most cells and highest in a narrow band that runs along the lower edge of the CD16 monocytes to the CD14 monocytes. In the stimulated cells, ISG15 is high in every population and highest in the monocytes. The neighbors of the stimulated CD14 monocytes sit on the band. The table averages four interferon-stimulated genes as log(1 + counts per 1,000) in the control CD14 and CD16 monocytes, in the 61 control cells that the stimulated CD14 monocytes chose, and in the stimulated CD14 monocytes, and checks that the reference panel holds each gene.

in reference panelcontrol CD14 Monocontrol CD16 Monocontrol neighbors of stimulated CD14 Monostimulated CD14 Mono
ISG15True0.170.501.953.76
IFIT3True0.020.150.501.47
MX1True0.030.110.460.93
CXCL10True0.010.280.702.79

Started: 2026-11-19T08:31:27.121581Z | Wall time: 2.550 s

All four genes belong to the reference panel, so the projection carries the interferon response into the reference PCA space. Control CD16 monocytes express each gene more than control CD14 monocytes do (ISG15 at 0.50 against 0.17), and stimulated CD14 monocytes express them far more (ISG15 at 3.76). The 61 control cells that the stimulated CD14 monocytes chose average ISG15 at 1.95, against 0.50 across all control CD16 monocytes, and the other three genes rank the four groups in the same order. The stimulated CD14 monocytes therefore land nearest to the control cells with the strongest interferon response, which mostly carry the CD16 Mono label. The dataset merging tutorial shows how stimulation changes the monocyte markers in a joint analysis of both samples. A reference labels only the states it contains: to label stimulated monocytes, it needs stimulated monocytes.

Adjust the threshold, the neighbors and the gene matching

Each of these settings saves a new result next to the existing one, so the two can be compared:

  • threshold_fraction in mapping.transfer_labels sets the share of neighbor weight that a label needs. A higher value labels fewer cells; the calibration curve shows what that gains in accuracy, here nothing.

  • max_distance in mapping.transfer_labels makes a cell abstain when its nearest reference cell is farther than this distance. On this query a distance limit cannot single out the monocytes: their median distance percentile, 0.950, is no higher than that of the CD4 naive T cells, 0.953.

  • save_k in mapping.run keeps more neighbors per query cell, which changes the votes and the mapping scores.

  • missing_feature_policy in mapping.run sets what happens to panel genes that the query lacks: "reference_mean", the default, fills them with the reference mean, which is zero after scaling, "zero" fills them with a normalized zero, and "error" refuses the query.

  • feature_key="names" in mapping.run matches query genes to the panel by name instead of by ID, for a query that uses another ID scheme, such as gene symbols against Ensembl IDs. Query genes that share a name then add their counts, and a reference whose genes share a name is refused.

This reference is a plain PCA reference. A Symphony reference Kang et al., 2021, built from an analysis corrected with Harmony, also corrects the query: it estimates how the query, or each query batch given in query_batches, is offset from the reference and removes that offset. Use one only when the reference and the query differ by technical batch; on this query it would treat the interferon response as a batch offset and remove it.

Reuse the reference and saved labels

A later session reopens both stores and reloads the reference, the projection and the transfer from their artifact references without recomputing them. The code below reuses reference_ref, mapping_ref and transfer_ref from this session; a new session finds them again with ds_ctrl.artifacts.list(kind="mapping_reference", complete_only=True), and with the same call on ds_stim for kind="projection" and kind="label_transfer". A saved transfer loads from the query store alone.

{'query cells': 10111, 'batch correction': 'none', 'reference labels': 'cluster_labels', 'threshold_fraction': 0.5}

Started: 2026-11-19T08:31:29.672750Z | Wall time: 0.166 s

For repeated use, open the reference store with zarr_mode="r", so that no query can change it. When the query counts come from a read-only source, or from the store that holds the reference, mount_datastore creates a writable query store that reads its counts from that source.

Transferring again with unchanged reference labels and settings reuses the saved transfer. After the reference annotation changes, the same call freezes the new labels as another reference_labels artifact and saves a new transfer next to the old one, which keeps its labels. The reference labels can also come from a cell-label artifact of the reference store, such as a clustering.

The lineage of the transfer follows its inputs into the reference store: from the transfer through the frozen labels, the projection and the query cell selection, to the mapping reference and the reference analysis it was built from.

Loading...

Started: 2026-11-19T08:31:29.839989Z | Wall time: 0.432 s

Artifacts marked external belong to the reference store: the mapping reference and the reference analysis that it was built from. The others belong to the query store, and every artifact is complete; the provenance guide explains how results record their inputs.

Before trusting labels from a reused reference, read featureCoverage and queryScaledDispersion, and look for a query population that crowds onto a few cells of another reference population; without known labels, group the query by its own clusters for that check. On this query, the dispersion of 1.33 and the 2,532 CD14 monocytes crowding onto 61 reference cells point to the problem, while the vote fractions and distance percentiles do not: the wrong labels rest on unanimous votes at distances typical of the whole query. When some query cells carry known labels, the confusion matrix and the calibration curve measure the result directly. The mapping API reference documents the methods and results used here, and the plotting API reference the diagnostic plots.

References
  1. Kang, J. B., Nathan, A., Weinand, K., Zhang, F., Millard, N., Rumker, L., Moody, D. B., Korsunsky, I., & Raychaudhuri, S. (2021). Efficient and precise single-cell reference atlas mapping with Symphony. Nature Communications, 12(1). 10.1038/s41467-021-25957-x