Tutorials › Statistical Methods for Single-Cell RNA Sequencing › Aggregating Counts by Donor and Cell Type

Differential Expression With Pseudobulk and Mixed Models · Tutorial 475 of 700

Aggregating Counts by Donor and Cell Type

You will learn to sum cell-level raw UMI counts into donor–cell-type pseudobulk profiles while keeping each profile correctly linked to its sample and metadata.

Advanced 10 min read

What You'll Learn

  • Define the cells that belong to each donor–cell-type pseudobulk group.
  • Sum raw UMI counts gene by gene and verify the resulting totals.
  • Retain sample identifiers and aligned group-level metadata.
  • Distinguish a genuinely absent group from a group with zero counts.
  • Diagnose grouping, annotation, and metadata errors before analysis.
  • Recognize when repeated samples from one donor must remain separate.

Why the Aggregation Step Matters

A pseudobulk profile is built from cell-level measurements, but its observational unit is a defined biological sample group, not an individual cell. Constructing it therefore requires more than adding numbers: the cells must be assigned to the correct donor and cell type, and every resulting profile must remain linked to its sample and relevant group-level metadata. A grouping mistake can merge distinct samples or create an apparent replicate that does not exist.

The focus here is the data construction step underlying the cell-type-specific estimands introduced in the earlier material on pseudobulk estimands. The output is a matrix of summed raw UMI counts, accompanied by metadata whose rows correspond exactly to the matrix columns. This tutorial does not interpret the resulting expression comparisons; it explains how to create and audit the inputs for them.

Definition: Let \(Y_{ig}\) be the raw UMI count for gene \(g\) in cell \(i\). For donor \(d\) and cell type \(t\), let \(C_{dt}\) be the set of cells assigned to that donor and cell type. The pseudobulk count for gene \(g\) in that group is \(Y^{PB}_{gdt}=\sum_{i\in C_{dt}}Y_{ig}\). It is an integer sum of the observed cell-level counts, not a mean or a normalized expression value.

Use a consistent gene set across cells: \(g\) indexes the same gene in every cell-level count vector. A convenient matrix orientation is genes in rows and pseudobulk groups in columns. Each matrix column then represents one defined aggregation group, and the corresponding metadata table has one row for that group. Other software may use the transposed orientation, but the group definitions and alignment must be identical.

Define the Grouping Key Before Summing

For a simple cross-sectional design with one sample per donor, the grouping key is donor identifier plus cell-type label. If a donor contributes multiple distinct samples, however, donor plus cell type is not a sufficient key. Samples from different visits, conditions, tissues, or collection times must not be collapsed merely because they share a donor and cell type. In that setting, retain the sample identifier as part of the key and aggregate within donor–sample–cell-type groups.

A sample identifier should identify the biological sample represented by the cells. If there are technical partitions of a single biological sample, they may be combined only when that is the intended sample definition and their cells are not duplicated. Conversely, separate biological samples must stay separate even if they come from the same donor. Store donor identity and sample identity as distinct fields: the former identifies the biological individual, while the latter identifies the sampled unit or occasion.

Before aggregation, the cell-level table should have one row per cell, a stable cell identifier, raw UMI counts for the genes, and metadata sufficient to assign the cell to the intended group. For each cell, donor and sample identifiers should be unambiguous. A cell-type label should be present for cells included in a cell-type-specific aggregation. If cell-type labels are uncertain or missing, decide in advance whether to exclude those cells or handle them in a separate category; do not silently assign them to a known type.

1
Specify the group key.
Choose donor and cell type for one sample per donor, or include sample and any other identifier needed to keep distinct biological samples separate.
2
Check cell-level inputs.
Confirm that each cell appears once, has a valid group assignment, and has nonnegative integer raw UMI counts on a consistent gene set.
3
Sum within each group.
For every gene, add the counts of all cells with the same complete grouping key.
4
Build and align metadata.
Create one metadata row per pseudobulk column and verify that each row describes the same group as its matrix column.
5
Audit the result.
Compare cell numbers and count totals with the input, and inspect group labels, missing groups, and unexpectedly small or large profiles.

Worked Construction and Checks

Worked Example: Summing Counts for One Donor–Cell-Type Group

Suppose a hypothetical sample contains three cells from donor D01 assigned to cell type T. Their raw UMI counts for genes A, B, and C are: cell 1, \((4,0,2)\); cell 2, \((1,3,0)\); and cell 3, \((0,2,5)\). The cells share the same sample and group labels, and none is duplicated.

For gene A, the group total is \(4+1+0=5\). For gene B, it is \(0+3+2=5\). For gene C, it is \(2+0+5=7\). Thus the pseudobulk column for D01 and T is \((5,5,7)\), with total count \(5+5+7=17\).

$$ Y^{PB}_{g,\mathrm{D01},T} =\sum_{i\in C_{\mathrm{D01},T}}Y_{ig}, \qquad \mathbf{Y}^{PB}_{\mathrm{D01},T} =(5,5,7)^\mathsf{T} $$

The input total is also \(4+0+2+1+3+0+0+2+5=17\), so the group total is conserved by summation. Its metadata row records donor D01, the actual sample identifier, cell type T, and any sample-level fields such as condition or visit. It should not contain a fabricated cell-level condition that differs among cells in the group.

Worked Example: Keeping Two Samples From One Donor Separate

Consider hypothetical donor D02 sampled at visits V1 and V2. In cell type T, two V1 cells have counts for genes A and B of \((2,1)\) and \((0,3)\). Two V2 cells have counts \((5,0)\) and \((1,2)\). The donor is the same, but the samples represent separate visits.

For V1, the gene totals are A: \(2+0=2\), and B: \(1+3=4\). For V2, they are A: \(5+1=6\), and B: \(0+2=2\). The output therefore has two columns, not one:

$$ \mathbf{Y}^{PB}_{\mathrm{D02,V1},T}=(2,4)^\mathsf{T}, \qquad \mathbf{Y}^{PB}_{\mathrm{D02,V2},T}=(6,2)^\mathsf{T} $$

The associated metadata rows retain donor D02 for both columns, but specify V1 and V2 respectively, along with the correct condition or time point for each sample. If all four cells were incorrectly grouped by donor and cell type alone, the resulting vector would be \((8,6)^\mathsf{T}\). That sum is arithmetically correct across the four cells but erases the distinction between the two samples and cannot represent both sample-level profiles.

Worked Example: Checking Matrix and Metadata Alignment

Suppose an aggregation produces three columns with identifiers PB01, PB02, and PB03. Their gene totals are \( (8,4)^\mathsf{T}\), \( (3,9)^\mathsf{T}\), and \( (6,2)^\mathsf{T}\), respectively. The metadata records PB01 as donor D10, sample S10, type T; PB02 as donor D11, sample S11, type T; and PB03 as donor D10, sample S12, type T.

The total counts by column are \(8+4=12\), \(3+9=12\), and \(6+2=8\). The metadata correctly indicates that PB01 and PB03 come from the same donor but different samples. If the matrix columns are reordered to PB03, PB01, PB02, the metadata must be reordered to that same sequence. Otherwise, a later analysis could attach the D10/S12 label to the counts for D10/S10, for example.

A direct audit is to join or compare using the unique pseudobulk identifier, not merely the row position. After alignment, check that each matrix column identifier occurs exactly once in the metadata and each metadata identifier occurs exactly once in the matrix. The group-defining fields should also reproduce the intended key.

Why the Sum Is the Appropriate Construction

The aggregation formula follows from additivity of counts over disjoint sets of cells. If a group is partitioned into non-overlapping subsets \(C_1,\ldots,C_K\), then for each gene,

$$ \sum_{i\in \bigcup_{k=1}^{K} C_k}Y_{ig} = \sum_{k=1}^{K}\sum_{i\in C_k}Y_{ig}, \qquad C_j\cap C_k=\varnothing\ \text{for }j\ne k. $$

Thus, summing cell counts in one pass or first summing disjoint batches and then adding those batch totals gives the same result. The equality depends on using the same gene definition and on each cell entering the group exactly once. A duplicated cell contributes its counts twice; a cell assigned to the wrong group contributes to the wrong profile. The arithmetic cannot correct faulty inputs.

The raw counts are nonnegative integers, so the resulting gene-level group totals are also nonnegative integers. This is a useful structural check, not evidence by itself that the grouping is correct. Summation does not require the cell counts to be statistically independent; it is a data transformation. Modeling assumptions for inference on the aggregated counts are a separate question.

Diagnostics, Missing Groups, and Alternatives

Several checks can reveal errors before the matrix is used. First, verify that each included cell belongs to exactly one intended aggregation group. Second, compare the total UMI counts before and after aggregation for the same set of included cells and genes. Third, compare the number of cells in each group with expectations from the cell-level metadata. Fourth, inspect the number of groups per donor and the donor, sample, cell-type, and condition combinations. Unexpectedly merged groups, duplicate keys, or impossible metadata combinations warrant investigation.

An absent donor–cell-type group is not automatically a column of zero counts. It may mean that no cells of that type were observed or retained for that sample. Those possibilities differ from having sampled cells whose measured counts for a particular gene are zero. Preserve an explicit record of absent groups or the relevant sampling status when needed for analysis; do not invent an all-zero cell-type profile to make a rectangular table. Whether and how to handle samples with missing cell types requires a design-specific decision.

The aggregation choice also depends on the analysis target. Summing raw counts retains an integer count profile for each defined group. Averaging normalized cell-level values is a different operation and has different interpretation; the distinction is treated in the later material on summed counts versus averaged normalized expression. Keeping cells separate and fitting a cell-level model is another possible strategy, but it must account for the dependence of cells from the same donor and does not make an incorrect sample key harmless. For the present task, the essential comparison is between valid group-level sums and constructions that change the unit or lose sample identity.

Common warning signs include non-integer values in a purported raw-count matrix, negative entries, a metadata table with duplicate group IDs, sample labels that vary within a group, and a pseudobulk total that does not match the included cell-level total. Large differences in cell numbers across groups can also matter: record the number of cells per group so that the aggregation process is transparent. A group with very few cells may yield a sparse profile, but the construction step itself does not establish whether that profile should be retained.

Common pitfall: “These cells have the same donor and cell type, so I can sum them together.” This is wrong when they come from distinct biological samples, visits, or conditions. Include the sample-level identifier in the aggregation key whenever those samples must remain distinct; donor identity alone does not preserve repeated sampling.

Practical Limitations

Pseudobulk construction depends on the upstream cell and metadata definitions. Incorrect donor labels, cell-type annotation errors, barcode duplication, or inconsistent gene identifiers can produce a valid-looking matrix with invalid columns. Hard cell-type assignments also treat each retained cell as belonging wholly to one category; aggregation does not represent uncertainty in those labels. These are reasons to document the input definitions and quality-control decisions, not reasons to alter the summation formula.

Finally, the pseudobulk matrix and its metadata are a paired data object. A matrix column without its sample identity is not a fully specified observation. Save a stable group identifier, keep an explicit mapping to donor and sample, and preserve the cell counts contributing to each group. These records make the aggregation reproducible and help distinguish biological replication from the number of cells measured.

Check Your Understanding

Answer each question, then compare your reasoning with the solutions.

  1. Three cells in one donor–sample–cell-type group have gene X counts 2, 0, and 4. What is the pseudobulk count for gene X, and what input check can verify it?
  2. A donor contributes samples at baseline and follow-up. Which identifiers should define separate pseudobulk groups for the same cell type?
  3. Why is a missing cell-type group not necessarily represented by an all-zero count column?
  4. A metadata table has the correct rows but is in a different order from the matrix columns. What must be done before analysis?
  5. State one condition needed for summing batch-level partial totals to equal the total from all cells directly.

Solutions. (1) The total is \(2+0+4=6\) UMIs. Check that the input cells are unique members of the group and that their counts sum to 6. (2) Keep donor, sample or visit, and cell type in the grouping key, so baseline and follow-up remain separate. (3) No cells assigned to that type is different from observed cells with zero counts for a gene; an absent group may reflect sampling or filtering rather than a zero-expression profile. (4) Reorder or join the metadata by a unique pseudobulk identifier, then verify exact one-to-one alignment with the matrix columns. (5) The batches must partition the same cells without overlap or omission, and use the same gene definitions.

Clinical Biostats