Tutorials › High-Dimensional Genomic Data Analysis › The Gene-by-Sample Count Matrix

Count-Based Differential Expression for RNA-seq · Tutorial 414 of 795

The Gene-by-Sample Count Matrix

Learn to organize RNA-seq counts by gene and sample, check the accompanying metadata, and calculate the basic summaries needed to describe a differential-expression comparison.

Intermediate 10 min read

What You'll Learn

  • Define the dimensions, entries, and indexing of a gene-by-sample count matrix.
  • Distinguish gene-level count summaries from sample-level library-size summaries.
  • Calculate row totals, column totals, means, and a simple reads-per-million summary.
  • Explain why raw counts alone do not establish differential expression between samples.
  • Check matrix and metadata alignment before interpreting a comparison.

From Sequencing Data to a Gene-by-Sample Table

RNA-seq data are often introduced for analysis as a rectangular table: genes run down the rows, samples run across the columns, and each cell records how many sequencing reads were assigned to a gene in a sample. This table is the gene-by-sample count matrix. Understanding its layout is essential because a row summary describes one gene across samples, while a column summary describes one sample across genes.

The matrix is a data structure, not a differential-expression result. In particular, a larger count for a gene in one sample can reflect greater sequencing depth, a different composition of reads across genes, or a biological difference. The matrix must be read alongside sample information and interpreted with those distinctions in mind.

Definition: Let \(G\) be the number of genes and \(n\) the number of samples. A gene-by-sample count matrix is \(K=(K_{ij})\), a \(G\)-by-\(n\) array in which \(K_{ij}\) is the nonnegative integer count assigned to gene \(i\) in sample \(j\). Rows index genes; columns index samples. The column total \(L_j=\sum_{i=1}^{G}K_{ij}\) is the total count across the genes included in this matrix for sample \(j\).

The term “count” refers to the number of reads or read fragments assigned under a specified processing and counting procedure. It does not mean the number of RNA molecules in the original biological sample. Assignment depends on choices such as the gene annotation and the rules used to count reads that overlap genes. Those choices should be consistent across samples and recorded with the analysis.

Rows, Columns, and Sample Information

Each row should identify a defined genomic feature, typically a gene specified by a stable identifier. Each column should identify one sample. A separate sample-information table records attributes such as experimental group, subject, batch, or collection time. Those attributes are not encoded by the counts themselves: the matrix cannot tell which samples belong to which condition unless the metadata provide that information.

The sample-information table must match the matrix columns exactly. If the count matrix columns are ordered Sample A, Sample B, Sample C, the metadata rows must refer to those same samples in that same order, or be explicitly joined by sample identifier before analysis. A group label attached to the wrong column changes the scientific comparison, even though every count in the matrix remains unchanged.

A zero entry means that no reads were assigned to that gene in that sample under the chosen counting procedure. It is not the same as an unrecorded or missing entry. Missing values usually indicate a data-handling problem, not a biological count of zero. Similarly, a gene absent from the matrix may have been excluded by the feature definition or preprocessing; its absence should not be silently interpreted as a measured zero.

Worked Example: Reading a Small Count Matrix

Consider the following hypothetical matrix. The rows are three genes and the columns are four samples; all entries are assigned read counts.

GeneSample ASample BSample CSample D
Gene 120244044
Gene 28076160156
Gene 30400

The matrix has \(G=3\) rows and \(n=4\) columns. The entry \(K_{2,3}=160\) is the count for Gene 2 in Sample C. It is not the total for Gene 2 or for Sample C.

The sample totals are column sums. For Sample A, \(L_1=20+80+0=100\). For Sample B, \(L_2=24+76+4=104\). For Sample C, \(L_3=40+160+0=200\). For Sample D, \(L_4=44+156+0=200\). Thus the included-gene totals are 100, 104, 200, and 200 counts.

The gene totals are row sums. Gene 1 has \(20+24+40+44=128\) counts across the four samples. Gene 2 has \(80+76+160+156=472\), and Gene 3 has \(0+4+0+0=4\). These row totals summarize counts across samples; they do not by themselves give a condition-specific effect.

Library Size and Gene-Level Summaries

A library size is a sample-level measure of sequencing output, commonly represented by the total number of reads or fragments counted for that sample. When it is calculated from a gene count matrix, the column sum \(L_j\) is the total over the genes in that matrix. Depending on the pipeline, the library-size measure used in analysis may instead be a total assigned count defined over a broader set of features. Therefore, state which total is being reported and use a consistent definition across samples.

Library size matters because samples with more counted reads have more opportunities to produce counts for genes. This means raw counts from two samples are not automatically on a common scale. A simple descriptive summary is counts per million (CPM): divide a gene’s count in a sample by that sample’s library size and multiply by one million. CPM is useful for a basic scale comparison, but it is not, by itself, a complete normalization method or a differential-expression test.

$$ \operatorname{CPM}_{ij}=\frac{K_{ij}}{L_j}\times 10^6 $$

This formula expresses the gene’s share of the sample’s counted reads in units of counts per million. It is meaningful only if \(K_{ij}\) and \(L_j\) use compatible count definitions. More refined methods may account for differences in composition as well as depth; their estimation and use belong to later analysis steps.

Worked Example: Comparing Raw Counts With a Simple Scale Summary

Suppose a hypothetical gene has 500 counts in Sample A and 800 counts in Sample B. The corresponding library sizes are 10,000,000 and 20,000,000 counts. Calculate the CPM values.

For Sample A, \(500/10{,}000{,}000\times 1{,}000{,}000=50\) CPM. For Sample B, \(800/20{,}000{,}000\times 1{,}000{,}000=40\) CPM.

The raw count is larger in Sample B, but the CPM is smaller: Sample B has twice the library size, while this gene has only 1.6 times the raw count. The calculation shows why comparing raw counts alone can mislead about relative abundance. It does not establish that the gene is less expressed in Sample B: a differential-expression conclusion requires a suitable analysis of the samples, their conditions, and variation among biological replicates.

Useful gene-level summaries include the count for each sample, the total across samples, and summaries within clearly specified groups. For a condition with \(r\) samples, the arithmetic mean count for gene \(i\) is the sum of its counts in those samples divided by \(r\). It is a descriptive summary in count units. If library sizes differ, group mean raw counts can partly reflect those differences, so they should be accompanied by the relevant sample totals and not presented as a library-size-adjusted comparison.

Worked Example: Summarizing a Gene by Condition

Suppose four hypothetical samples have the following metadata: Samples A and B are control samples, while Samples C and D are treated samples. For Gene 1, the counts are 20, 24, 40, and 44; the corresponding library sizes from the matrix are 100, 104, 200, and 200.

The mean raw count in the control group is \((20+24)/2=22\) counts per sample. The mean raw count in the treated group is \((40+44)/2=42\) counts per sample.

The treated-group mean raw count is \(42/22\approx1.909\) times the control-group mean. But the treated samples also have substantially larger library sizes. The CPM values for Gene 1 are \(20/100\times10^6=200{,}000\) and \(24/104\times10^6\approx230{,}769\) in the controls, and \(40/200\times10^6=200{,}000\) and \(44/200\times10^6=220{,}000\) in the treated samples. The group mean CPMs are therefore approximately 215,385 and 210,000, respectively.

These simple summaries do not show the same apparent increase as the raw means. They illustrate the importance of reporting the scale and the group labels, rather than calling a raw-count difference a biological effect. CPM here is only a basic descriptive adjustment; the calculation does not account for every feature of RNA-seq data or quantify uncertainty.

Formulating a Differential-Expression Question

Before modeling, the matrix and metadata should make the intended comparison explicit. For each candidate gene, the question is typically whether its expression differs between specified conditions, with the comparison interpreted after accounting for the amount and composition of sequencing data and the design of the experiment. Earlier material on gene-level effects and moderated tests develops how to report an estimated effect and its uncertainty; the count matrix supplies the observations from which the count-based analysis begins.

At this stage, distinguish three kinds of information:

  • Gene-level observations: \(K_{ij}\), the assigned count for a particular gene in a particular sample.
  • Sample-level summaries: \(L_j\), or a stated alternative library-size measure, describing the count total associated with a sample.
  • Design information: metadata identifying condition and other relevant sample attributes, with each label correctly matched to a matrix column.

A brief justification explains why these summaries are needed. If two samples differ in sequencing depth, their raw counts are exposed to different opportunities for read assignment. Dividing by a compatible library size creates a scale-free descriptive quantity:

$$ \frac{K_{ij}/L_j}{K_{ik}/L_k} = \frac{K_{ij}L_k}{K_{ik}L_j}, \qquad K_{ik}>0,\ L_j>0,\ L_k>0. $$

The ratio compares the gene’s fraction of the counted library in samples \(j\) and \(k\). This calculation motivates checking sample totals, but it should not be confused with a full count-based model. In particular, a matrix and its column sums do not tell us how much biological variability exists across replicates, nor do they alone supply a test or uncertainty interval.

Checks, Failure Modes, and Limits

A sound summary begins with structural checks. Confirm that every matrix entry is a nonnegative integer count; row identifiers are unique and correspond to the stated gene annotation; column identifiers are unique; and metadata identifiers match the columns one-to-one. Inspect the column totals for unexpectedly small, large, or zero values. A zero library size makes CPM undefined and typically signals that the sample or count table needs investigation.

Check that the library-size total is defined consistently. If one total includes only selected genes while another includes all assigned features, comparisons of those totals may not mean what they appear to mean. Also inspect whether zeros are true recorded counts or missing values that were inadvertently converted. These checks detect data-organization problems; they do not determine which genes are differentially expressed.

A count matrix has important limits. It does not describe the experimental design unless metadata are linked to it. It does not make biological replicates interchangeable with repeated technical measurements, and it does not show how variable counts are among samples in a condition. Simple totals and CPM summaries can orient an analysis, but they do not handle all differences in library composition, provide a count sampling model, or replace uncertainty-aware inference.

Common pitfall: “Gene X has twice as many raw counts in the treated sample, so it is twice as highly expressed.” This reasoning ignores library size and the other information needed for an analysis. First inspect compatible sample-level totals and summarize the counts on a stated scale; then use a suitable count-based analysis that incorporates the design and variation across samples. A raw-count ratio alone is not a differential-expression conclusion.

Check Your Understanding

Answer each question using the definitions and calculations in this tutorial. The solutions are included below for checking.

  1. In a matrix with 12,000 genes and 6 samples, what does \(K_{5,2}\) represent, and which direction of the table does a column total summarize?
  2. A gene has 30 counts in a sample with a library size of 600,000. Calculate its CPM.
  3. Why is a zero count different from a missing matrix entry?
  4. What information must accompany a count matrix to identify which samples belong to each condition?

Solutions. (1) \(K_{5,2}\) is the count assigned to gene 5 in sample 2; a column total summarizes one sample across the genes in the matrix. (2) \((30/600{,}000)\times1{,}000{,}000=50\) CPM. (3) A zero is an observed count of no assigned reads under the counting procedure; a missing entry is an absent or unavailable value and should not be treated as an observed zero. (4) Sample metadata with sample identifiers and condition labels, matched one-to-one and in the correct correspondence with the matrix columns.

Clinical Biostats