Tutorials › Statistical Methods for Single-Cell RNA Sequencing › Nearest-Neighbor Scores for Doublet Detection

Quality Control, Doublets and Ambient RNA · Tutorial 32 of 700

Nearest-Neighbor Scores for Doublet Detection

You will learn to construct, calculate, and diagnose nearest-neighbor doublet scores without mistaking them for probabilities or automatic cell calls.

Intermediate 11 min read

What You'll Learn

  • Define a nearest-neighbor score using simulated and observed barcodes in a shared expression-space representation.
  • Calculate raw and simulation-ratio-adjusted scores from a barcode’s local neighborhood.
  • Explain why scores rank evidence rather than estimate the probability that a barcode is a doublet.
  • Assess how neighborhood size, embedding choices, and artificial-doublet sampling affect scores.
  • Recognize diagnostic patterns that suggest the neighborhood method is not representing doublet evidence reliably.

Why Local Proximity Can Indicate Doublet Evidence

An artificial doublet is constructed to approximate the combined expression profile of two cells. If those cells have distinct expression patterns, their combined profile may lie between the corresponding singlet populations or in a region of expression space with few observed singlets. A nearest-neighbor method asks whether an observed barcode is close to many artificial doublets in that space. The answer is a local score: useful for ranking barcodes by evidence, but not by itself a probability or a decision to remove a barcode.

The preceding material on artificial doublets establishes how simulated profiles can be formed from observed cells. Here, the focus is how those profiles contribute to a neighborhood score. The method depends on treating simulated and observed barcodes comparably: they must be represented using the same selected features, transformations, and embedding. If their distances are not meaningful, counting their neighbors will not provide meaningful evidence.

Definition: Let \(z_i\) be the expression-space coordinate of an observed barcode \(i\), and let simulated artificial doublets also have coordinates in that space. For a chosen integer \(k\), the \(k\)-nearest-neighbor set of \(i\) contains the \(k\) closest other points to \(z_i\), according to a specified distance. The raw nearest-neighbor doublet score is the fraction of those neighbors that are simulated artificial doublets: $$ f_i = \frac{m_i}{k}, $$ where \(m_i\) is the number of simulated neighbors among the \(k\) nearest neighbors. The score ranges from 0 to 1.

Constructing Comparable Neighborhoods

A typical analysis begins with observed cell-associated barcodes and a set of simulated artificial doublets. A simulated profile may be made by adding the count vectors of two sampled observed barcodes. Observed and simulated profiles are then processed together so that the same genes, normalization, feature selection, and dimensionality reduction define their coordinates. A distance—often Euclidean distance in selected principal-component coordinates—is used to find neighbors.

The query barcode itself should not count as one of its own neighbors. Otherwise, each observed query would have at least one observed neighbor simply because it is present in the data, changing the denominator or displacing a relevant nearby point. The same neighborhood rule should be used consistently for every query. If several points have tied distances at the \(k\)-th position, a reproducible tie-breaking rule is needed.

The score relies on several substantive assumptions. First, artificial doublets should resemble the profiles that real doublets would produce under the experiment’s count-generation and preprocessing pipeline. Second, the observed cells sampled to construct them should adequately represent the cell populations that could contribute to doublets. Third, the chosen representation and distance should preserve local distinctions relevant to mixed-cell profiles. Finally, technical structure such as sample or batch effects should not dominate the distances in a way that makes neighbors reflect processing artifacts rather than expression similarity.

These assumptions are approximations, not guarantees. For example, summing two observed count vectors can create a reasonable artificial profile, but normalization and projection can still place it differently from a real doublet. The score is only as informative as the simulation and geometry that produced it.

Raw Scores and Simulation-Ratio Adjustment

The raw score depends on how many simulated profiles were added. If artificial doublets make up a large fraction of the combined data, a barcode may have many simulated neighbors even when its neighborhood is no more enriched for simulations than the overall data. Let \(q\) denote the fraction of all points in the combined observed-and-simulated set that are simulated. A commonly used adjustment centers the raw score at this global fraction and rescales its maximum:

$$ a_i = \frac{f_i-q}{1-q}, \qquad q<1. $$

This adjustment gives \(a_i=0\) when \(f_i=q\), the global simulated fraction, and \(a_i=1\) when every one of the barcode’s neighbors is simulated. It can be negative when the local simulated fraction is below the global fraction. The adjustment is an enrichment index, not an estimated probability that barcode \(i\) is a doublet. In particular, it does not correct a poor simulation or an uninformative embedding.

Worked Example: Calculating a Raw and Adjusted Score

Suppose a barcode has \(k=10\) nearest neighbors, of which \(m_i=7\) are simulated. In the combined data, \(q=0.20\) of all points are simulated. Calculate both scores.

The raw score is the simulated-neighbor fraction: $$ f_i = \frac{m_i}{k} = \frac{7}{10} = 0.700. $$

The adjusted score is: $$ a_i = \frac{f_i-q}{1-q} = \frac{0.700-0.200}{1-0.200} = \frac{0.500}{0.800} = 0.625. $$

This barcode’s neighborhood is enriched for simulated doublets relative to the global simulated fraction. The value \(0.625\) is not a 62.5% probability of being a doublet; it is a ratio-based score used to compare local enrichment under this simulation setup.

What the Adjustment Does—and Does Not—Establish

The adjustment follows directly from two chosen reference points. When the local simulated fraction equals the global fraction \(q\), the adjusted score should be zero. When the local fraction is one, it should be one. An affine transformation \(a_i=c f_i+d\) satisfying those conditions has \(d=-cq\) and \(c(1-q)=1\). Therefore \(c=1/(1-q)\) and \(d=-q/(1-q)\), which gives \(a_i=(f_i-q)/(1-q)\).

This argument justifies the scaling, but it does not establish that \(a_i\) is a calibrated measure of a barcode’s true doublet status. The raw fraction estimates the composition of a finite neighborhood in the augmented dataset. The adjustment accounts for the chosen global simulation fraction; it does not turn synthetic labels into known labels for real barcodes. It also does not remove sampling variability: when \(k\) is small, changing one neighbor changes the raw score by \(1/k\).

Worked Example: Finding the Nearest Neighbors From Coordinates

Consider an observed query at coordinate \((0,0)\) and eight candidate neighbors in a two-dimensional embedding. Distances are Euclidean, so the distance from \((0,0)\) to \((u,v)\) is \(\sqrt{u^2+v^2}\). The candidate coordinates and labels are:

  • Simulated: \((0.2,0)\), distance \(\sqrt{0.2^2}=0.2\).
  • Observed: \((0.1,0)\), distance \(\sqrt{0.1^2}=0.1\).
  • Simulated: \((0,0.3)\), distance \(\sqrt{0.3^2}=0.3\).
  • Observed: \((0.4,0)\), distance \(\sqrt{0.4^2}=0.4\).
  • Observed: \((-0.5,0)\), distance \(\sqrt{(-0.5)^2}=0.5\).
  • Simulated: \((0,0.6)\), distance \(\sqrt{0.6^2}=0.6\).
  • Observed: \((0.7,0)\), distance \(\sqrt{0.7^2}=0.7\).
  • Simulated: \((0.8,0)\), distance \(\sqrt{0.8^2}=0.8\).

For \(k=6\), order the candidates by distance. Their labels from nearest to farthest are observed, simulated, simulated, observed, observed, simulated. Thus \(m_i=3\), and: $$ f_i = \frac{3}{6}=0.500. $$

If \(q=0.25\), then: $$ a_i = \frac{0.500-0.250}{1-0.250} = \frac{0.250}{0.750} = 0.333. $$

The calculation shows exactly how geometry becomes a score: distances identify the local set, and labels within that set determine the fraction. It does not show that the query is a doublet; it shows that three of its six nearest points in this constructed dataset are artificial.

Choosing \(k\) and Reading the Ranking

The neighborhood size \(k\) controls the scale of the comparison. A small \(k\) is sensitive to very local structure, but each neighbor has a large effect on the score and random variation can be substantial. A larger \(k\) averages over more points and can stabilize the fraction, but may combine distinct cell populations or blur a small region of doublet-like profiles. There is no universally correct \(k\); it should be selected with the data size, representation, and intended use in mind.

A score is generally used to rank observed barcodes, with higher scores indicating greater local enrichment for artificial doublets relative to the simulation fraction. Rankings can be compared across parameter settings as a sensitivity check, but raw values from different simulation ratios or embeddings need not be directly comparable. The number of artificial profiles, their sampling procedure, and the fraction \(q\) should be recorded when reporting scores.

Worked Example: How the Simulation Fraction Changes the Adjusted Score

Suppose a barcode has eight nearest neighbors, five of them simulated. Its raw score is: $$ f_i = \frac{5}{8}=0.625. $$

If one artificial profile was generated per four observed profiles, the simulated fraction is \(q=1/(1+4)=0.20\). The adjusted score is: $$ a_i = \frac{0.625-0.200}{1-0.200} = \frac{0.425}{0.800} = 0.531. $$

If instead the augmented set has two simulated profiles per three observed profiles, then \(q=2/(2+3)=0.40\). Using the same raw local fraction: $$ a_i = \frac{0.625-0.400}{1-0.400} = \frac{0.225}{0.600} = 0.375. $$

The raw local fraction is unchanged, but the adjusted score differs because the global simulation fraction differs. This is why the simulation ratio must be documented and why adjusted scores from different setups should not be interpreted as though they shared an identical scale.

Diagnostics, Alternatives, and Limitations

A useful diagnostic is to inspect the score distribution and the locations of high-scoring barcodes in the embedding. If artificial profiles are well represented, some should occur near regions of observed data with mixed or intermediate expression patterns. If nearly all observed barcodes have similar scores, the method may have little ranking information, or the chosen \(k\) and embedding may be too coarse. If scores change sharply under modest changes to \(k\), the ranking is sensitive to neighborhood boundaries. If simulated profiles cluster separately from all observed profiles, the score may largely distinguish the simulation procedure rather than identify plausible real doublets.

Repeat the calculation across reasonable choices of neighborhood size, number of retained dimensions, and artificial-doublet sampling. Check whether the highest-ranked barcodes remain broadly similar. Also examine whether neighborhoods are dominated by sample or batch membership. A distance-based score can be high because of technical structure, unusual cell states, or poor representation—not only because of doublet-like expression.

Nearest-neighbor scoring is one approach among several. A direct classifier would require suitable labeled examples and can learn the limitations of those labels; artificial doublets provide training-like examples but are still approximations. A cluster-level comparison summarizes groups and can miss barcode-level local variation. The nearest-neighbor method avoids requiring a global linear boundary, but its results depend on the chosen metric and representation. No one approach makes synthetic doublets equivalent to validated real doublets.

A common mistake is to read a score of \(0.8\) as an 80% chance that the barcode is a doublet. That is incorrect: the score is a fraction of simulated neighbors (or a rescaled version of that fraction), and its value depends on the augmented dataset and analysis choices. Interpret it as relative local evidence, then use appropriate validation and decision procedures rather than treating the score itself as a probability or a removal rule.

Key takeaway: A nearest-neighbor doublet score measures how strongly an observed barcode’s local expression-space neighborhood is enriched for simulated artificial doublets. Its ranking is informative only to the extent that the simulations, representation, distance, and neighborhood size are appropriate.

Check Your Understanding

Answer each question, then compare with the solutions below.

  1. A barcode has four simulated neighbors among \(k=12\) neighbors. What is its raw score?
  2. If \(f_i=0.60\) and \(q=0.20\), calculate the adjusted score and state what it means.
  3. Why can increasing \(k\) make a score less sensitive to one neighbor but less locally specific?
  4. Name one diagnostic sign that the embedding may not be supporting a useful artificial-doublet score.

Solutions. 1. \(f_i=4/12=0.333\). 2. \(a_i=(0.60-0.20)/(1-0.20)=0.40/0.80=0.500\); this indicates local enrichment relative to \(q\), not a 50% probability of doublet status. 3. Each point contributes \(1/k\), so a larger \(k\) reduces the effect of one neighbor, but it can include points from distinct local populations. 4. One sign is that simulated profiles form a separate cluster far from observed barcodes; then the distances may be reflecting how simulations were constructed rather than informative local doublet evidence.

Clinical Biostats