Tutorials › Statistical Methods for Single-Cell RNA Sequencing › Aggregating Cells Into Donor-Level Count Tables

Differential Abundance and Compositional Effects · Tutorial 531 of 700

Aggregating Cells Into Donor-Level Count Tables

You will learn to construct and validate donor-by-cell-type count tables while avoiding pooled counts and cell-level pseudoreplication.

Advanced 10 min read

What You'll Learn

  • Define the aggregation unit when donors contribute one or multiple samples.
  • Convert cell-level labels into a complete donor-by-cell-type count table.
  • Preserve sample metadata and denominators alongside the counts.
  • Check that totals, labels, and sample identifiers are consistent.
  • Explain why donor-level aggregation supports sample-level abundance inference without treating cells as independent replicates.
  • Recognize when pooling samples or using cell counts as replicates changes the analysis.

Why Aggregate Cell Labels?

A cell-level data set may contain thousands of rows, but a differential-abundance comparison usually asks whether biological samples differ in their cell-type composition. The relevant replication is generally the donor or biological sample, not each cell: cells from one donor share biological and technical context. Counting cells by cell type within each sample creates a data structure in which samples can be compared without pretending that every cell is an independent replicate.

Aggregation does not discard the cell-level labels; it summarizes them for a particular analysis. The summary is useful only if the aggregation unit, cell-type categories, and denominator are explicit. In particular, a donor who contributes samples under multiple conditions must not have those samples collapsed together when the comparison concerns condition.

Definition: Let \(i=1,\ldots,m\) index independent biological samples (or donor-condition samples when a donor contributes more than one condition), and let \(k=1,\ldots,K\) index mutually exclusive cell types. The donor-by-cell-type count \(C_{ik}\) is the number of retained cells in sample \(i\) assigned to cell type \(k\). The sample total is \(N_i=\sum_{k=1}^{K} C_{ik}\). A donor-by-cell-type count table has one row per aggregation unit and one column per cell type, accompanied by sample metadata.

The term “donor-level” is appropriate when each donor contributes one sample to the comparison. With repeated samples, the rows should instead identify the donor and the sample occasion or condition. The donor identifier remains essential metadata because repeated rows from one donor are not automatically independent.

Constructing the Count Table

Begin with one record per retained cell, including a sample identifier and a cell-type label. The sample identifier must map each cell to the biological sample from which it came. Define the cell types before counting, and use the same category definitions across samples. If the analysis includes an “Other” category, for example, it must be defined consistently rather than used as an informal receptacle for labels that differ across samples.

For each sample \(i\) and cell type \(k\), count the records satisfying both conditions. Equivalently, if \(z_c\) is the assigned cell type for cell \(c\), and \(s_c\) is its sample, then

$$ C_{ik}=\sum_{c} I(s_c=i,\ z_c=k), $$

where \(I(\cdot)\) equals 1 when the condition is true and 0 otherwise. This definition makes clear that every retained cell contributes to exactly one table entry if its sample and cell-type labels are both valid and each cell has exactly one assigned type. The row total \(N_i\) is the number of retained cells from sample \(i\) represented in the table.

Metadata should be stored at the same row level as the counts. Typical fields include donor ID, condition, time point, batch, and any sample-level covariates relevant to the planned comparison. Metadata values should describe the sample, not be inferred from cell-level repetitions without checking that they are consistent. Keep a stable sample ID even when donor ID and condition together appear unique; a stable ID makes joins and auditing safer.

1
Define the unit and categories.
Decide whether each row represents a donor, a donor-condition sample, or another biological sample, and specify the mutually exclusive cell-type labels to count.
2
Validate cell records.
Check sample IDs, labels, duplicate cell records, and rules for excluding cells with missing or unusable assignments.
3
Count and complete the table.
Count cells for every sample-category combination, including zero-count combinations, then calculate each sample total.
4
Attach and verify metadata.
Join one consistent metadata record to each row and confirm that counts, totals, identifiers, and conditions agree with the source data.

Worked Construction and Checks

Worked Example: Counting Three Cell Types

Suppose a hypothetical sample S01 has seven retained cells labelled, in order, T, B, T, Myeloid, B, T, Myeloid. The prespecified categories are T, B, and Myeloid.

Count the labels separately: T appears 3 times, B appears 2 times, and Myeloid appears 2 times. Thus the row is \(C_{\mathrm{S01},\cdot}=(3,2,2)\), and its total is \(N_{\mathrm{S01}}=3+2+2=7\) cells.

As a check, the sum of the category counts equals the seven retained records. If one record had a missing cell-type label and was excluded, the total represented in the table would be 6, not 7; the exclusion should be recorded and applied consistently. If the missing label is retained as an explicit category, the counts and total must instead include it.

The following short justification explains why these counts summarize cell labels without changing the sample as the comparison unit. For a fixed sample, each cell belongs to one category, so the indicators across categories sum to 1 for that cell. Summing first across cells and then across categories gives

$$ \sum_{k=1}^{K} C_{ik} =\sum_{k=1}^{K}\sum_{c:s_c=i} I(z_c=k) =\sum_{c:s_c=i}1 =N_i. $$

Thus the table partitions the retained cells in sample \(i\), and its row total is the number of cells assigned to one of the listed categories. Aggregation yields one count vector per biological sample. It does not make cells from that sample independent biological replicates.

Worked Example: Preserving Donor and Condition

Consider a hypothetical paired study with two donors, each contributing a control and a treated sample. The counts are:

Sample IDDonorConditionT cellsB cellsMyeloid cellsTotal
D1-CD1Control30101050
D1-TD1Treated40101060
D2-CD2Control20201050
D2-TD2Treated30201060

For sample D1-C, the total is \(30+10+10=50\); for D1-T it is \(40+10+10=60\). The corresponding totals for D2-C and D2-T are \(20+20+10=50\) and \(30+20+10=60\). The table retains both donor and condition, so later analysis can recognize that each donor appears twice.

For illustration, the T-cell fraction in D1-C is \(30/50=0.600\), or 60.0%, whereas in D1-T it is \(40/60=0.667\), or 66.7% to three significant figures. These are sample-specific summaries; the count table retains the numerators and denominators needed to interpret them. Combining D1-C and D1-T into one donor row would give 70 T cells out of 110 total, but would erase the control-versus-treated distinction and would not represent either condition-specific sample.

Metadata, Denominators, and Unequal Cell Yield

The total \(N_i\) is the number of cells included in the table for sample \(i\), not necessarily the number originally captured, sequenced, or collected. It depends on filtering, quality control, annotation, and the chosen set of cell types. Therefore, record the inclusion rules and retain the totals. A table without totals can obscure whether a count of 20 arose from 25 observed cells or 2,000.

The denominator for an abundance summary must correspond to the stated estimand. If the question concerns a cell type as a share of all retained cells in a sample, its proportion is \(C_{ik}/N_i\). If the analysis intentionally excludes specified categories from the denominator, define the resulting denominator and report it. A count table allows these choices to be made transparently; it does not determine which choice is scientifically appropriate.

Worked Example: Checking a Denominator

Suppose a hypothetical sample has 18 cells of type A, 12 of type B, and 10 of type C. The total is \(N_i=18+12+10=40\). If the target is the fraction of all retained cells that are type A, the calculation is

$$ \frac{C_{iA}}{N_i}=\frac{18}{40}=0.450=45.0\%. $$

If the stated target instead uses only A and B in its denominator, the denominator is \(18+12=30\), and the corresponding fraction is \(18/30=0.600=60.0\%\). These numbers answer different questions. Reporting 60.0% as the fraction of all retained cells would be incorrect because 10 type-C cells were omitted from that denominator.

Unequal totals are not automatically errors: samples can yield different numbers of usable cells. But they affect the precision of sample-level summaries, and extreme differences can make comparisons fragile. Do not solve unequal yield by pooling all cells in a condition and treating the resulting large total as if it represented many independent donors. The independent sample count remains the number of biological units available for comparison.

Diagnostics and Common Failure Modes

Several checks catch aggregation mistakes before analysis:

  • Row-total check: verify \(N_i=\sum_k C_{ik}\) and compare \(N_i\) with the number of eligible cell records for that sample.
  • Category check: confirm that the table has the same cell-type columns for every sample. A missing category should usually appear as a zero count, not as a missing row or absent column.
  • Identifier check: each sample ID should map to one donor, condition, and set of sample-level metadata. Investigate duplicated IDs or conflicting metadata rather than silently selecting one record.
  • Label check: look for spelling variants, unexpected labels, or cells assigned to multiple categories. Such problems can inflate totals or split one biological category across columns.
  • Yield check: inspect totals across samples for zeros and unusually small or large values. A zero total means no retained cells are represented, so sample proportions are undefined; an unexpectedly small total may make the estimated composition unstable.

A frequent mistake is to count each cell as an observation in a group comparison. The reasoning is that thousands of cells provide thousands of replicates. The correction is that those cells are nested within a smaller number of donors or samples; they share sample-level conditions and are not independent replicates of those conditions. The count table makes the biological replication visible by placing counts in rows indexed by samples, but the subsequent analysis must still account for pairing, repeated sampling, or other dependence when present.

Aggregation is also not a substitute for checking the study design. If condition is perfectly confounded with processing batch, a table cannot separate those effects. If one donor contributes samples under several conditions, collapsing by donor loses the comparison, while treating the rows as independent ignores within-donor dependence. These design features must be preserved in metadata and handled by an appropriate analysis.

What Aggregation Does—and Does Not—Provide

RepresentationUseful forImportant limitation
Cell-level labels aloneCell annotation and cell-level quality checksDoes not by itself identify the sample-level replicate structure for abundance comparisons
Donor- or sample-level count tableKeeping sample counts, totals, and metadata together for abundance analysisDoes not choose the estimand, model, or dependence structure
Pooled counts by conditionDescribing combined observed cell totalsHides donor-to-donor variation and cannot serve as a set of donor-level replicates
Sample-level proportionsDisplaying relative abundance per sampleCan conceal differing denominators if counts and totals are not retained

The table is therefore a data-preparation step, not a complete inferential method. Its main advantage over pooled counts is preserving one count vector per biological sample. Its advantage over proportions alone is retaining the integer counts and the total from which each proportion was calculated. The later choice among statistical models depends on the sampling structure, the target comparison, and features such as overdispersion; aggregation alone does not resolve those issues.

The earlier material on donors as independent units in abundance tests motivates keeping biological replication explicit. The practical consequence here is simple: build the table at the unit that the comparison requires, retain donor identity even when a donor has multiple rows, and never let the number of cells stand in for the number of donors.

Key takeaway: A valid donor-by-cell-type table has one row per intended biological sample, one consistently defined column per cell type, counts that sum to a clearly defined sample total, and metadata that preserve donor, condition, and other relevant design information.

Check Your Understanding

For each question, state the reasoning and check the arithmetic where relevant. Solutions follow the questions.

  1. A sample has 14 cells of type X, 6 of type Y, and 5 of type Z. What is its total, and what is the fraction of all listed cells that are type Y?
  2. Each of eight donors contributes one control and one treated sample. How many rows should the table have if both conditions are retained, and which identifiers should accompany those rows?
  3. Why is a condition-level pooled count not equivalent to eight donor-level observations?
  4. Three categories have counts 9, 0, and 4 in one sample. Should the zero category be omitted from the table? What is the row total?
  5. A cell-type fraction is reported as 50%, but its numerator is 20 and its denominator is not recorded. What information is missing to interpret or verify the fraction?

Solutions. (1) The total is \(14+6+5=25\); the type-Y fraction is \(6/25=0.240\), or 24.0%. (2) Use 16 rows, one per donor-condition sample, with sample ID, donor ID, condition, and relevant sample metadata. (3) Pooling removes the donor-specific count vectors and variation, so one pooled total does not provide eight independent donor observations. (4) Keep the zero as an explicit count in the type-Y column; the total is \(9+0+4=13\). (5) The denominator and its definition are missing, including whether it is all retained cells or a specified subset; without it, the 50% cannot be checked or interpreted in context.

Clinical Biostats