Expression per gene and sample as CPM or count-based TPM from a count matrix
ngs_expression_normalise
Works out counts per million (CPM) or count-based TPM per gene and sample of a count matrix.
- Runs
- On the server
- Level
- Checked against a reference: rnanorm 2.2.0's CPM on random column-total cases (the outside comparison), with exact rational arithmetic in a script of our own
What it is
Returns counts per million (CPM) or count-based transcripts per million (TPM) per gene and sample of a count matrix. Use when putting samples of different depth, or genes of different length, on one scale to compare or plot them. Not for differential expression, which no library tool tests. Not for qPCR: use `assay_qpcr_relative_expression`. measure and countKind are required, libraryBasis with CPM, lengthBasis with TPM; TPM and column-total CPM depend on the genes sent; at most 6,000 values, lengths included; lengths at most 10 Mb.
Field: Sequencing and genomics.
What it does
You give
measure(required): cpm or count-based-tpm. Required; RPKM and FPKM are not offered.countKind(required): read-counts (whole numbers) or estimated-counts (non-negative reals from a quantifier). Required.genes(required): The gene ids, in the order every vector follows.samples(required): The samples.libraryBasis: With cpm, required: column-total (the sum over the supplied genes) or stated-library-size (librarySize in each sample). Optional.lengthBasis: With count-based-tpm, required: annotated or effective lengths. Optional.
and 1 more, listed below
No request is recorded for this tool.
Limits
- Research arithmetic for planning sequencing libraries and runs and for genomic coordinate bookkeeping; it does arithmetic on the inputs it receives, and does not measure the library, the instrument or the data.
- Per-gene arithmetic on the counts you supply, for a spot check of up to 6,000 values, lengths included (2,000 genes and 50 samples at most), not a whole transcriptome.
- Counts per million use a library size you state or the column total over the supplied genes; the column total is not the total sequenced or mapped reads unless every feature was supplied.
- Count-based TPM uses counts in place of Li and Dewey's estimated abundances, so reads that map to several genes are not resolved and the values are not comparable with TPM from an expectation-maximisation quantifier; it sums to one million over the supplied genes only, and the lengths are used as given.
- A different gene set changes every count-based TPM and every column-total CPM.
- RPKM and FPKM are not offered.
- This is a transformation, not a test for differential expression.
- Every computed number a reply returns, other than an integer or an exact decimal, is rounded once to 12 significant figures: to the nearest, a change of at most 5 × 10⁻¹² of its value, except that a lower bound is rounded down and an upper bound up, a change of less than 1 × 10⁻¹¹; the method computes, and decides any pass or fail, on the unrounded value.
It will not answer when
- a sample's counts, or a length vector, does not have one value per gene.
- the counts and lengths together are more numeric cells than the tool's cap; a smaller gene subset changes the results.
- geneLengths outside at most 10000000 bp.
Every input, with its units7 inputs
measure(required): cpm or count-based-tpm. Required; RPKM and FPKM are not offered.countKind(required): read-counts (whole numbers) or estimated-counts (non-negative reals from a quantifier). Required.genes(required): The gene ids, in the order every vector follows.samples(required): The samples.libraryBasis: With cpm, required: column-total (the sum over the supplied genes) or stated-library-size (librarySize in each sample). Optional.lengthBasis: With count-based-tpm, required: annotated or effective lengths. Optional.geneLengths: One length per gene in bp or kb, in the order of genes, used for every sample; with count-based-tpm. Optional.
Every refusal, with its code5 refusals
| Code | When |
|---|---|
VALUES_MISALIGNED | a sample's counts, or a length vector, does not have one value per gene. |
MATRIX_ABOVE_CAP | the counts and lengths together are more numeric cells than the tool's cap; a smaller gene subset changes the results. |
VALUES_MISALIGNED | a sample's counts, or a length vector, does not have one value per gene. |
MATRIX_ABOVE_CAP | the counts and lengths together are more numeric cells than the tool's cap; a smaller gene subset changes the results. |
OUT_OF_RANGE | geneLengths outside at most 10000000 bp. |
The calculation
2 methods, each with its equations, constants and worked example.
Equation 1
N_j = sum over the supplied genes g of c_gj (column-total), or N_j as stated (stated-library-size): this project's ruling
the column total is a compensated (Neumaier) sum in input order, exact for read counts and within about one ulp for estimated counts
inputs are read as the exact value of their shortest decimal, and the range and whole-number tests are decided on that value
CPM is in double precision
order of checks: the kit's checks of each argument in schema order (a bare array where { values, unit } is expected is UNIT_REQUIRED, an object with no unit is MISSING_UNIT, a count above 1e10 or below 0 and a length above 10 Mb or not above 0 are OUT_OF_RANGE), then DUPLICATE_NAME (genes, then samples), then MATRIX_ABOVE_CAP, then per sample in order VALUES_MISALIGNED and, for read counts, NOT_AN_INTEGER, then the measure's argument checks (with cpm: MISSING_ARGUMENT without libraryBasis, then CONFLICTING_ARGUMENT for lengthBasis, then for lengths, then per sample MISSING_ARGUMENT or CONFLICTING_ARGUMENT for librarySize)
| Symbol | Meaning |
|---|---|
N_j | library size of sample j |
c_gj | count of gene g in sample j |
Source not recorded.
Equation 2
| Symbol | Meaning |
|---|---|
CPM_gj | counts per million, countsPerMillion |
Source not recorded.
Conventions
- measure = cpm: counts per million: each count times one million over its sample's library size
- libraryBasis = column-total: the library size is the sum of the sample's supplied counts
- libraryBasis = stated-library-size: the library size is the one the caller states for each sample
A definition, not a model: the equation states what the quantity means. A different convention gives a different number.
Worked example
Worked example: one case of ngs-expression-cpm@1.1.0, from its inputs through each step to the answer. Values are written as a person writes them; the working carries more digits, so your own figure may differ in the last digit shown.
Inputs
- measure = cpm
- countKind = read-counts
- libraryBasis = column-total
- genes[0] = geneA
- genes[1] = geneB
- genes[2] = geneC
- samples[0].id = s1
- samples[0].counts.values[0] = 10
- samples[0].counts.values[1] = 30
- samples[0].counts.values[2] = 60
- samples[0].counts.unit = 1
Steps
- N = 100, from samples[0].counts; by equation 1; gives result.samples[0].librarySize.
- cA = 100000 countsPerMillion, from samples[0].counts, N; by equation 2; gives result.samples[0].expression.values[0].
The working, as the case writes it:
s1: N = 100
CPM = 100000, 300000, 600000
Answer
result.samples[0].librarySize = 100
result.samples[0].expression.values[0] = 100000 countsPerMillion
Where these values came from: our own exact-arithmetic script and a hand-worked case; Worked by hand by Drybench on 11 November 2026 (ruling 7). This is not an independent check.
At build the tool returned this answer within the tolerance the file states: CPM relative 1e-15 (zero exact); library sizes, texts, statuses, codes and withheld rules exact.
TPM_gj = (c_gj / l_gj) / sum over the supplied genes h of (c_hj / l_hj) x 10^6: Li and Dewey's tau x 10^6, with counts c in place of their estimated theta (a stated convention) and l their effective length l'
annotated lengths are the record's extension
the sum is compensated (Neumaier) in input order
lengths are at most 10 Mb, above the longest human gene spans read (NCBI Gene, GRCh38.p14, RS_2025_08, read 14 November 2026: CNTNAP2 2,304,198 bp, DMD 2,220,167 bp
genes of other species not checked), so titin's longest transcript, 109,224 bp, is taken
inputs are read as the exact value of their shortest decimal, and the range and whole-number tests are decided on that value
TPM is in double precision
order of checks: the kit's checks of each argument in schema order (a bare array where { values, unit } is expected is UNIT_REQUIRED, an object with no unit is MISSING_UNIT, a count above 1e10 or below 0 and a length above 10 Mb or not above 0 are OUT_OF_RANGE), then DUPLICATE_NAME (genes, then samples), then MATRIX_ABOVE_CAP, then per sample in order VALUES_MISALIGNED and, for read counts, NOT_AN_INTEGER, then the measure's argument checks (with count-based-tpm, in this order: MISSING_ARGUMENT without lengthBasis
CONFLICTING_ARGUMENT for libraryBasis, for a librarySize, for geneLengths with effectiveLengths, for per-sample lengths under annotated, then for effectiveLengths in some samples only
MISSING_ARGUMENT with no lengths
then VALUES_MISALIGNED for geneLengths, then per sample for effectiveLengths)
| Symbol | Meaning |
|---|---|
TPM_gj | count-based transcripts per million, transcriptsPerMillion |
c_gj | count of gene g in sample j |
l_gj | length of gene g, for sample j where given per sample, bp |
Source 1.
Conventions
- measure = count-based-tpm: Li and Dewey's TPM with counts in place of their estimated abundances: each count over its length, as a share of the sample's sum, times one million
- lengthBasis = annotated: the lengths are annotated gene lengths, one vector for every sample
- lengthBasis = effective: the lengths are effective lengths, one vector for every sample or one per sample
A definition, not a model: the equation states what the quantity means. A different convention gives a different number.
Worked example
Worked example: one case of ngs-expression-count-tpm@2.0.0, from its inputs through each step to the answer. Values are written as a person writes them; the working carries more digits, so your own figure may differ in the last digit shown.
Inputs
- measure = count-based-tpm
- countKind = read-counts
- lengthBasis = annotated
- genes[0] = geneA
- genes[1] = geneB
- genes[2] = geneC
- samples[0].id = s1
- samples[0].counts.values[0] = 100
- samples[0].counts.values[1] = 100
- samples[0].counts.values[2] = 0
- samples[0].counts.unit = 1
- geneLengths.values[0] = 1000
- geneLengths.values[1] = 4000
- geneLengths.values[2] = 500
- geneLengths.unit = bp
Steps
- tA = 800000 transcriptsPerMillion, from samples[0].counts, geneLengths; by the equation above; gives result.samples[0].expression.values[0].
- tB = 200000 transcriptsPerMillion, from samples[0].counts, geneLengths; by the equation above; gives result.samples[0].expression.values[1].
The working, as the case writes it:
s1: TPM = 800000, 200000, 0
Answer
result.samples[0].expression.values[0] = 800000 transcriptsPerMillion
result.samples[0].expression.values[1] = 200000 transcriptsPerMillion
Where these values came from: our own exact-arithmetic script and a hand-worked case; Worked by hand by Drybench on 11 November 2026 (ruling 7). This is not an independent check.
At build the tool returned this answer within the tolerance the file states: TPM relative 1e-14 (zero exact); texts, statuses, codes and withheld rules exact.
Sources
Li B, Dewey CN (2011), RSEM: accurate transcript quantification from RNA-Seq data with or without a reference genome, BMC Bioinformatics 12:323, doi 10.1186/1471-2105-12-323
Used for: Count-based transcripts per million per gene and sample. At: section tau_i = (theta_i / l'_i) / sum over j != 0 of theta_j / l'_j at Methods, Statistical model, "Expectation-Maximation" (so printed; equation image 1471-2105-12-323-i9.gif); TPM = tau x 10^6 at Implementation, "Read mapping and abundance estimation".
Read in full, 11 November 2026
How it is checked
- Level
Checked against a reference, the lowest of its 2 methods.
- Checked against
Counts per million per gene and sample: Our own exact-arithmetic script and a hand-worked case.
Count-based transcripts per million per gene and sample: Our own exact-arithmetic script and a hand-worked case.
- Cases
Counts per million per gene and sample: No case set is linked to this version.
Count-based transcripts per million per gene and sample: No case set is linked to this version.
- Not checked
Counts per million per gene and sample: Whether the supplied genes are the whole library; log-CPM and prior counts (not offered).
Count-based transcripts per million per gene and sample: Agreement with TPM from an expectation-maximisation quantifier (not the same quantity); gene lengths derived from isoforms (not done).
What this project's records still lack1 thing
- The source's location for each equation for ngs-expression-cpm@1.1.0.
Changes to its methods2 changes
- 14 November 2026, ngs-expression-count-tpm@2.0.0: In 2.0.0 the sum of the rates is compensated (Neumaier), so each TPM is within a few ulps of the exact value and the 6,000-value case gives exactly 1,000. Lengths are taken up to 10 Mb, above the longest human gene spans read (CNTNAP2 and DMD, NCBI Gene, GRCh38.p14; other species not checked), and rates that would overflow are scaled by the smallest length first. The record corrects Li and Dewey's locators and states the order of checks; rnanorm 2.2.0's TPM is recorded as an outside check.
- 14 November 2026, ngs-expression-cpm@1.1.0: In 1.1.0 such a sample is withheld under the rule cpm-above-double, with its reason, and the column total is a compensated sum.