Tutorials › Statistical Methods for Single-Cell RNA Sequencing › Identifying Anchors in Shared Latent Spaces

Integration Methods and Their Benchmarking · Tutorial 168 of 700

Identifying Anchors in Shared Latent Spaces

You will calculate reciprocal-neighbor anchor pairs in a shared embedding and learn what the resulting matches do—and do not—establish.

Advanced 11 min read

What You'll Learn

  • Define cross-dataset nearest-neighbor sets in a shared latent space.
  • Identify an anchor by checking reciprocal nearest-neighbor membership.
  • Calculate Euclidean distances and anchor pairs for small embedded datasets.
  • Explain why reciprocal matching excludes some one-sided nearest-neighbor matches.
  • Recognize how representation comparability, neighborhood size, and ties affect candidate anchors.
  • Distinguish candidate anchors from proof that two cells are biologically equivalent.

Why Search for Anchors in a Shared Space?

To integrate two single-cell datasets, we need a way to identify cells that may represent the same underlying state despite being measured in different batches. The earlier material on anchor pairs established their role as evidence for shared cell states. Here the task is operational: given cell coordinates in a shared latent space, determine which cross-dataset pairs qualify as anchors under a reciprocal-neighbor rule.

A latent representation assigns each cell a vector of coordinates, usually in fewer dimensions than the original expression data. The coordinates can be compared across datasets only when they are expressed in a common, comparable space. Having the same number of coordinates is not sufficient: coordinate axes, scales, and distances must have a meaningful relationship across the datasets.

Definition: Let datasets \(A\) and \(B\) contain cells represented in a common \(d\)-dimensional latent space. Write the coordinates of cell \(i\) in \(A\) as \(\mathbf{z}_{Ai}\in\mathbb{R}^d\), and those of cell \(j\) in \(B\) as \(\mathbf{z}_{Bj}\in\mathbb{R}^d\). For a chosen distance \(d(\cdot,\cdot)\) and neighborhood size \(k\), let \(N_B^k(i)\) be the \(k\) nearest cells in \(B\) to cell \(i\) in \(A\), and let \(N_A^k(j)\) be the \(k\) nearest cells in \(A\) to cell \(j\) in \(B\). The pair \((i,j)\) is a reciprocal nearest-neighbor anchor if \(j\in N_B^k(i)\) and \(i\in N_A^k(j)\).

The search is cross-dataset: when finding \(N_B^k(i)\), compare cell \(i\) with cells in \(B\), not with other cells in \(A\). Then reverse the roles of the datasets to check whether the relationship is reciprocal. Unless stated otherwise, the examples use Euclidean distance, \(d(\mathbf{u},\mathbf{v})=\sqrt{\sum_{\ell=1}^{d}(u_\ell-v_\ell)^2}\). The same rule can be defined with another distance, but the choice must be made consistently for both directions.

How the Reciprocal Rule Works

A one-way nearest-neighbor search answers, “Which cell in \(B\) is closest to this cell in \(A\)?” That may produce a match even when the \(B\) cell is much closer to a different \(A\) cell. Reciprocal matching requires both cells to select each other within their respective cross-dataset neighborhoods. It is therefore a rule for proposing pairs, not a statistical test and not a guarantee of biological identity.

1
Fix the representation and distance.
Confirm that all compared coordinates lie in the same interpretable latent space, and specify the distance used for neighbor searches.
2
Search from \(A\) to \(B\).
For every cell \(i\) in \(A\), rank cells in \(B\) by distance and record the first \(k\).
3
Search from \(B\) to \(A\).
For every cell \(j\) in \(B\), rank cells in \(A\) and record the first \(k\).
4
Keep reciprocal pairs.
Record \((i,j)\) only when each cell appears in the other cell’s recorded neighbor set.

If distances tie at the \(k\)th position, the neighbor-set convention matters. One option is to include all cells tied at the boundary, which can make a neighborhood larger than \(k\). Another is to apply a stated deterministic tie-breaking rule and retain exactly \(k\) cells. The examples below have no ties. A reported anchor set should be reproducible under the chosen convention.

Worked Example: Finding Reciprocal Pairs in Two Dimensions

Suppose a hypothetical shared embedding contains two cells in each dataset: \(A_1=(0,0)\), \(A_2=(4,0)\), \(B_1=(0.8,0)\), and \(B_2=(3.2,0)\). Use Euclidean distance and \(k=1\).

For \(A_1\), the distances to \(B_1\) and \(B_2\) are \(\sqrt{(0-0.8)^2+(0-0)^2}=0.8\) and \(\sqrt{(0-3.2)^2+(0-0)^2}=3.2\). Thus \(N_B^1(A_1)=\{B_1\}\). For \(A_2\), the distances are \(3.2\) to \(B_1\) and \(0.8\) to \(B_2\), so \(N_B^1(A_2)=\{B_2\}\).

Now search in the reverse direction. \(B_1\) is \(0.8\) units from \(A_1\) and \(3.2\) units from \(A_2\), so \(N_A^1(B_1)=\{A_1\}\). Similarly, \(N_A^1(B_2)=\{A_2\}\). Both nearest-neighbor relationships are reciprocal. The anchor pairs are \((A_1,B_1)\) and \((A_2,B_2)\).

The calculation establishes that these pairs are nearest to each other under this representation, distance, and value of \(k\). It does not establish that the cells have identical expression profiles or that they are biologically interchangeable.

Why Reciprocity Removes One-Sided Matches

The reciprocal rule can be understood as intersecting two directed neighbor relations. The first search creates arrows from each cell in \(A\) to its selected neighbors in \(B\); the reverse search creates arrows from \(B\) to \(A\). An anchor is a pair joined by arrows in both directions. This intersection removes a directed match when only one member considers the other sufficiently close.

For \(k=1\), if \(A_i\) and \(B_j\) are mutual nearest neighbors, then each is the closest cell in the other dataset. With \(k>1\), the requirement is weaker: each must fall within the other’s \(k\) nearest cells, but neither must be the single closest. Thus the neighborhood size controls how permissive the reciprocal rule is.

Justification: By definition, \(j\in N_B^k(i)\) means that \(B_j\) ranks among the \(k\) closest cells in \(B\) to \(A_i\). The reverse condition \(i\in N_A^k(j)\) means that \(A_i\) ranks among the \(k\) closest cells in \(A\) to \(B_j\). Requiring both conditions rejects every pair that is close in only one direction. If \(k\) increases while the distance rankings and tie convention stay fixed, each neighbor set can only retain or gain members. Therefore any pair reciprocal at \(k\) remains reciprocal at a larger \(k\), although additional pairs may qualify.

Worked Example: A One-Way Match Is Not an Anchor

Consider hypothetical one-dimensional coordinates \(A_1=0\), \(A_2=0.4\), \(B_1=0.1\), and \(B_2=10\), with Euclidean distance and \(k=1\).

From \(A_1\), the distances are \(|0-0.1|=0.1\) to \(B_1\) and \(|0-10|=10\) to \(B_2\), so \(A_1\) selects \(B_1\). From \(A_2\), the distances are \(|0.4-0.1|=0.3\) and \(|0.4-10|=9.6\), so \(A_2\) also selects \(B_1\).

In the reverse search, \(B_1\) is closer to \(A_1\): its distances are \(0.1\) to \(A_1\) and \(0.3\) to \(A_2\). Hence \(B_1\) selects \(A_1\), making \((A_1,B_1)\) reciprocal. Although \(B_2\) selects \(A_2\), \(A_2\) selects \(B_1\), not \(B_2\). Therefore \((A_2,B_2)\) is a one-way nearest-neighbor match, not an anchor.

Neighborhood Size and Interpretation

The value of \(k\) is part of the anchor definition. A small \(k\) makes the rule selective: a pair must rank near the top in both searches. A larger \(k\) allows more pairs, including pairs that are not each other’s single closest match. Increasing \(k\) does not by itself show that the additional pairs are reliable; it only changes the rank threshold.

Worked Example: How Increasing \(k\) Adds Candidate Pairs

Use hypothetical one-dimensional coordinates \(A_1=0\), \(A_2=2\), \(B_1=0.2\), and \(B_2=1.8\). At \(k=1\), \(A_1\) has distances \(0.2\) to \(B_1\) and \(1.8\) to \(B_2\), while \(A_2\) has distances \(1.8\) to \(B_1\) and \(0.2\) to \(B_2\). The reverse searches give the same closest pairings, so the anchors are \((A_1,B_1)\) and \((A_2,B_2)\).

At \(k=2\), each cell’s neighbor set contains both cells in the other dataset. For example, \(N_B^2(A_1)=\{B_1,B_2\}\), and \(N_A^2(B_2)=\{A_1,A_2\}\). Every cross-dataset pair is now reciprocal: \((A_1,B_1)\), \((A_1,B_2)\), \((A_2,B_1)\), and \((A_2,B_2)\). The extra pairs arise from the less restrictive rank threshold, not from new evidence that those cells represent the same state.

Assumptions, Diagnostics, and Alternatives

Reciprocal-neighbor identification relies on several substantive and computational conditions. First, the coordinates must be comparable across datasets: distances between an \(A\) cell and a \(B\) cell must have an interpretable meaning. Second, shared cell states must be represented in both datasets well enough that corresponding cells can be near each other. Third, the chosen distance should reflect relevant latent-space proximity, and the embedding should not be dominated by an irrelevant scale or dimension. Finally, the chosen \(k\) must be suitable for the intended search; it directly changes the candidate set.

Practical diagnostics include checking that neighbor searches were restricted to the intended cross-dataset comparison, confirming the embedding dimensions and distance metric, and inspecting the number and distribution of reciprocal pairs across the datasets. An unexpectedly empty set can indicate a lack of shared states, incompatible representations, or an overly restrictive search. A very large set can follow from a large \(k\), dense or tied neighborhoods, or weak separation among cells. These observations diagnose the search output; they do not identify a single cause without further investigation.

Reciprocal matching is more restrictive than one-way nearest-neighbor matching because it requires agreement from both cells. It is related to mutual nearest neighbors used in batch alignment: both use cross-dataset reciprocal proximity, while the present definition emphasizes candidate anchors in a shared latent representation. Searching in a shared space is not a substitute for checking whether that space is appropriate. Other approaches can use one-way assignments or compare cells in a different representation, but those choices answer different matching questions. The choice between particular latent representations is a separate methodological decision.

  • Do not equate an anchor with a confirmed cell identity. It is a candidate pair supported by reciprocal proximity under specified choices.
  • Do not assume equal dimension means shared coordinates. Two embeddings with the same number of axes can still be incomparable.
  • Do not report \(k\) without the distance and tie rule. Together, these choices determine the neighbor sets.
  • Do not treat more anchors as automatically better. Increasing \(k\) can add pairs by construction, including weakly specific matches.
Key takeaway: In a comparable shared latent space, an anchor is a cross-dataset pair that lies within each cell’s chosen \(k\)-nearest-neighbor set in the other dataset. The result is evidence of proximity under the specified search, not proof of biological equivalence.

Check Your Understanding

For each question, first determine the relevant neighbor sets; then compare your reasoning with the solutions below.

  1. In a shared one-dimensional embedding, \(A_1=1\), \(A_2=5\), \(B_1=2\), and \(B_2=4\). With Euclidean distance and \(k=1\), which pairs are anchors?
  2. Suppose \(B_1\) is among the two nearest cells in \(B\) to \(A_1\), but \(A_1\) is not among the two nearest cells in \(A\) to \(B_1\). Is \((A_1,B_1)\) an anchor? Explain.
  3. What happens to the neighbor sets when \(k\) increases from 1 to 3, assuming a fixed distance ranking and no change in tie handling?
  4. Why is it not enough for two datasets’ embeddings to have the same number of dimensions?

Solutions. (1) From \(A_1=1\), the distances to \(B_1=2\) and \(B_2=4\) are 1 and 3, so \(A_1\) selects \(B_1\). From \(A_2=5\), the distances are 3 and 1, so \(A_2\) selects \(B_2\). In reverse, \(B_1\) is closer to \(A_1\) than \(A_2\), and \(B_2\) is closer to \(A_2\) than \(A_1\). The anchors are \((A_1,B_1)\) and \((A_2,B_2)\). (2) No. Reciprocity requires both membership conditions; one failed condition is enough to exclude the pair. (3) Each neighbor set retains its previous members and may gain more, so additional pairs may become reciprocal; existing reciprocal pairs remain reciprocal. (4) Equal dimension only means the vectors have the same number of entries. Their axes, scales, or meanings may differ, making cross-dataset distances uninterpretable.

Clinical Biostats