Tutorials › Statistical Methods for Single-Cell RNA Sequencing › Allocating Counts During Ambient RNA Correction

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

Allocating Counts During Ambient RNA Correction

Translate cell-level contamination estimates into gene-level corrected counts, and understand the assumptions and trade-offs behind fractional and integer allocations.

Intermediate 10 min read

What You'll Learn

  • Calculate expected ambient counts from a cell’s contamination estimate and the ambient gene profile.
  • Apply a nonnegative gene-level correction and distinguish estimated removal from observed counts.
  • Explain why expected corrected counts can be fractional even when observed counts are integers.
  • Relate probabilistic molecule assignment to the expected number of endogenous counts.
  • Compare profile-based subtraction with posterior allocation and identify their limitations.

From a Cell-Level Estimate to Gene-Level Counts

An estimated contamination fraction describes how much of a cell barcode’s observed RNA is attributed to ambient RNA. It does not, by itself, say which genes should lose counts. To correct expression at gene level, the estimate must be allocated across genes using information about the ambient profile or the probability that each observed molecule is ambient.

This allocation is an estimate, not a direct observation of the molecules’ origins. A sequencing count records how many molecules of a gene were captured in a barcode, but ordinarily does not label each molecule as cellular or ambient. Consequently, a corrected count can be a non-integer expected value even when the observed count is an integer.

Definition: Let \(Y_{ig}\) be the observed count for gene \(g\) in cell barcode \(i\), and let \(N_i=\sum_g Y_{ig}\) be the total observed count over the genes being analyzed. Let \(\rho_i\) be the estimated fraction of those counts attributed to ambient RNA. In a profile-based allocation, \(q_g\) is the estimated fraction of ambient RNA assigned to gene \(g\), with \(q_g\geq 0\) and \(\sum_g q_g=1\). The expected ambient allocation to gene \(g\) in barcode \(i\) is \(a_{ig}=\rho_iN_iq_g\).

The definitions must refer to the same feature set: the genes used to compute \(N_i\) and the genes over which \(q_g\) sums to one. The simple profile-based allocation assumes that the estimated ambient fraction applies to the barcode total and that the ambient profile is appropriately represented by \(q_g\). The fraction and profile are themselves estimated, so the allocation inherits their uncertainty.

Profile-Based Subtraction and the Nonnegative Constraint

A direct gene-level estimate subtracts the expected ambient allocation from the observed count. Since a gene cannot have a negative corrected count, the subtraction is limited to the available observed count:

$$ \widehat{Y}^{\,\mathrm{corr}}_{ig} =\max\left(0,\;Y_{ig}-a_{ig}\right) =\max\left(0,\;Y_{ig}-\rho_iN_iq_g\right). $$

The amount removed under this rule is \(r_{ig}=\min(Y_{ig},a_{ig})\). Thus \(0\leq r_{ig}\leq Y_{ig}\), and the corrected estimate satisfies \(0\leq\widehat{Y}^{\,\mathrm{corr}}_{ig}\leq Y_{ig}\). This is the precise sense in which the correction preserves nonnegative expression estimates: it does not create negative gene counts, and it does not increase a gene’s observed count.

The cap matters when the expected ambient allocation exceeds the observed count. If \(Y_{ig}=1\) and \(a_{ig}=4\), the corrected estimate is \(\max(0,1-4)=0\), so only one observed count is removed. The value 4 is an unconstrained expected allocation, not four molecules that can be subtracted from a gene with only one observed molecule.

Key result: If \(q_g\) sums to one over the analyzed genes, then \(\sum_g a_{ig}=\rho_iN_i\). With the nonnegative cap, total removal cannot exceed either the observed total or the estimated ambient total: $$ \sum_g r_{ig} =\sum_g\min(Y_{ig},a_{ig}) \leq \min\left(\sum_gY_{ig},\sum_ga_{ig}\right) =\rho_iN_i, $$ where the last equality uses \(\rho_i\leq1\) and \(\sum_gY_{ig}=N_i\).

The correction may remove less than \(\rho_iN_i\): if some genes have fewer observed counts than their proposed ambient allocations, those genes are capped at their observed counts. The remaining proposed allocation is not automatically reassigned to other genes. Reallocation would be an additional modeling choice, not a consequence of the subtraction rule.

Worked Example: Allocating Ambient Counts Across Genes

Suppose a hypothetical barcode has \(N_i=1{,}000\) observed counts, an estimated contamination fraction \(\rho_i=0.08\), and an ambient profile assigning \(q_A=0.05\) to gene A and \(q_B=0.01\) to gene B. The observed counts are \(Y_{iA}=9\) and \(Y_{iB}=1\). Assume the ambient profile over the full analyzed gene set sums to one.

For gene A, the expected allocation is \(a_{iA}=0.08\times1{,}000\times0.05=4\) counts. Therefore, \(\widehat{Y}^{\,\mathrm{corr}}_{iA}=\max(0,9-4)=5\). Four counts are removed from the observed nine.

For gene B, the expected allocation is \(a_{iB}=0.08\times1{,}000\times0.01=0.8\) counts. Therefore, \(\widehat{Y}^{\,\mathrm{corr}}_{iB}=\max(0,1-0.8)=0.2\). The estimated removal is 0.8 count, leaving a nonnegative corrected estimate of 0.2.

The estimated ambient total over all genes is \(\rho_iN_i=80\) counts. The allocations shown for A and B sum to 4.8; the rest belongs to other genes according to the profile. These two genes alone do not determine the barcode’s total corrected count.

Why Fractional Corrected Counts Are Natural

The observed count is discrete, but an estimated allocation need not be. In the example, the expected ambient contribution for gene B was 0.8. This does not mean that a literal fraction of a molecule was sequenced. It means that, given the model and its estimated parameters, the expected number of ambient molecules assigned to that gene is 0.8.

A related allocation is based on molecule-level posterior probabilities. Let \(\pi_{ig}\) denote the estimated probability that an observed molecule of gene \(g\) in barcode \(i\) is ambient, conditional on the model and the gene and cell information used. If each of the \(Y_{ig}\) molecules has that same probability, the expected ambient count is \(Y_{ig}\pi_{ig}\), and the expected endogenous count is:

$$ \widehat{Y}^{\,\mathrm{end}}_{ig} =Y_{ig}(1-\pi_{ig}). $$

This is a soft allocation: each observed count contributes in proportion to its estimated endogenous probability rather than being assigned wholly to one source. It is closely related to mixture-model allocation, while profile-based subtraction starts from a cell-level ambient total and distributes it according to \(q_g\). These methods need not produce the same gene-level values because their allocation assumptions and estimates differ.

Derivation: Suppose each of the \(Y_{ig}\) observed molecules is independently ambient with probability \(\pi_{ig}\), conditional on the model and its parameters. Let \(Z_{ikg}=1\) if molecule \(k\) is endogenous and \(0\) otherwise. Then \(P(Z_{ikg}=1)=1-\pi_{ig}\), so \(E[Z_{ikg}]=1-\pi_{ig}\). The endogenous molecule count is \(C_{ig}=\sum_{k=1}^{Y_{ig}}Z_{ikg}\). By linearity of expectation, $$ E[C_{ig}] =\sum_{k=1}^{Y_{ig}}E[Z_{ikg}] =Y_{ig}(1-\pi_{ig}). $$ Since \(0\leq\pi_{ig}\leq1\), this expected count lies between zero and \(Y_{ig}\).

The independence assumption is useful for this simple binomial allocation calculation; it is not a universal description of all ambient RNA models. In addition, the formula treats \(\pi_{ig}\) as fixed at its estimated value. Uncertainty in the estimated probability is not represented by the resulting point estimate.

Worked Example: Posterior Allocation of Observed Molecules

In a hypothetical barcode, gene C has \(Y_{iC}=12\) observed counts. A mixture model assigns each observed molecule an estimated ambient probability of \(\pi_{iC}=0.25\). Under the conditional independent-allocation model, the expected ambient count is \(12\times0.25=3\), and the expected endogenous count is:

$$ \widehat{Y}^{\,\mathrm{end}}_{iC} =12(1-0.25) =12(0.75) =9. $$

The corrected estimate is 9 counts, not because exactly three particular molecules have been identified as ambient, but because the model assigns an expected three of the twelve to the ambient component. If the assignments were instead sampled as binary labels, the resulting endogenous count would be an integer that could vary from one allocation to another.

Expected Values Versus Integer Allocations

There are two different outputs that are sometimes both called corrected counts. A deterministic expected-count output retains fractional values, such as 0.2 or 9. This avoids adding arbitrary rounding to the point estimate. An integer output requires a discrete allocation, for example by sampling source labels for observed molecules and counting the molecules assigned to the endogenous component.

Under the independent posterior allocation above, the number assigned as endogenous has a binomial distribution with \(Y_{ig}\) trials and success probability \(1-\pi_{ig}\). Its expected value is \(Y_{ig}(1-\pi_{ig})\), but a particular draw need not equal that expectation. Sampling creates additional allocation variability; it does not make the underlying source labels known. Rounding each fractional gene estimate independently is not equivalent to a coherent molecule-level allocation and can distort totals.

Worked Example: Interpreting a Fractional Corrected Count

Suppose gene D has \(Y_{iD}=4\) observed counts and an estimated ambient probability \(\pi_{iD}=0.30\) per observed molecule. The expected endogenous count is:

$$ 4(1-0.30)=4(0.70)=2.8. $$

The value 2.8 is a model-based expected count and is nonnegative and no larger than the observed count of 4. If a discrete allocation were sampled, the endogenous count would be one of 0, 1, 2, 3, or 4, with expectation 2.8 under this model. Rounding 2.8 to 3 is not the same as obtaining that sampled allocation, and rounding every gene separately can change the total in ways unrelated to the allocation model.

Assumptions, Comparisons, and Failure Modes

Profile-based subtraction is attractive when a cell-level contamination fraction and a credible ambient gene profile are available. Its key allocation assumption is that the ambient molecules in each barcode follow the profile \(q_g\). Posterior allocation instead relies on estimated gene- and cell-specific source probabilities. It can express that a gene’s observed molecules are more or less likely to be ambient in a particular cell, but its output depends on the mixture model and its estimated probabilities.

Allocation approachGene-level calculationUseful interpretationMain caution
Profile-based subtraction\(\max(0,Y_{ig}-\rho_iN_iq_g)\)Subtract an expected ambient allocation based on the barcode total and ambient profile.A poor profile or contamination estimate can misallocate counts; the nonnegative cap may make total removal smaller than \(\rho_iN_i\).
Posterior molecule allocation\(Y_{ig}(1-\pi_{ig})\)Expected endogenous count when observed molecules have an estimated ambient probability.Probabilities depend on the mixture model; a point estimate does not display parameter uncertainty.
Sampled integer allocationCount molecules assigned to the endogenous source.A discrete allocation consistent with molecule-level assignments.Results vary across random allocations and require the allocation model and sampling choices to be stated.

Several checks help reveal when the allocation is not behaving as intended. Verify that \(\rho_i\) and \(q_g\) are on compatible scales, that \(0\leq\rho_i\leq1\), and that the profile sums to one over the same genes used for \(N_i\). Corrected values should be nonnegative and should not exceed their observed counts under the subtraction or posterior rules described here. Many genes being driven to zero can indicate that the expected allocation is too large for their observed counts; it can also arise for genuinely sparse genes, so this pattern alone does not establish a failure.

The method cannot identify the true origin of every captured molecule from the observed count alone. Profile-based estimates are sensitive to the estimated ambient composition, and posterior estimates are sensitive to the model’s source probabilities. Both are plug-in estimates unless uncertainty is explicitly propagated. Corrected values should therefore be understood as model-dependent estimates, not as a recovered record of exactly which molecules were ambient.

  • Pitfall: subtracting an allocation without respecting the observed count. If \(Y_{ig}=1\) and the proposed allocation is 4, the incorrect result \(1-4=-3\) is not a valid corrected count. The constrained correction is \(\max(0,1-4)=0\), so one observed count is removed.
  • Pitfall: treating a fractional value as a fractional molecule. A corrected estimate of 2.8 is an expectation under an allocation model, not a claim that 2.8 physical molecules were observed.
  • Pitfall: assuming every cell loses exactly \(\rho_iN_i\) counts after capping. The per-gene cap can reduce total removal below that target; capped excess is not implicitly redistributed.
Key takeaway: Gene-level ambient correction is an allocation of estimated contamination, not a direct observation of molecular origin. State the allocation rule, keep corrected counts nonnegative, and distinguish fractional expected counts from sampled integer assignments.

Check Your Understanding

Answer each question using the allocation definitions above. Brief solutions are provided below the questions.

  1. A barcode has \(N_i=600\), \(\rho_i=0.05\), and \(q_g=0.02\) for a gene with observed count 5. Find its expected ambient allocation and corrected count.
  2. If the observed count is 1 and the proposed ambient allocation is 3, how many observed counts are removed under nonnegative subtraction?
  3. A gene has 10 observed counts and an estimated ambient probability of 0.4 per molecule. What is the expected endogenous count?
  4. Why can a corrected count be fractional even though observed counts are integers?
  5. State one reason why profile-based subtraction and posterior molecule allocation can give different gene-level corrections.

Solutions. (1) \(a_{ig}=0.05\times600\times0.02=0.6\), so the corrected count is \(\max(0,5-0.6)=4.4\). (2) The corrected count is \(\max(0,1-3)=0\), so one observed count is removed. (3) \(10(1-0.4)=6\) expected endogenous counts. (4) It can be an expected allocation under estimated probabilities, rather than a literal count of identified molecules. (5) Profile subtraction uses a cell-level contamination estimate and ambient gene profile, whereas posterior allocation uses estimated molecule-level, gene-specific source probabilities.

Clinical Biostats