Per-sample size factors by median of ratios or TMM
ngs_size_factors
Works out one scaling factor per sample of a count matrix by median of ratios or TMM.
- Runs
- On the server
- Level
- Checked against a reference: pydeseq2 0.5.4, with a 50-digit decimal script of our own
What it is
Returns one scaling factor per sample of a count matrix by median of ratios or TMM. Use when you need each sample's sequencing-depth correction before comparing counts across samples. Not for per-gene normalised values: use `ngs_expression_normalise`. Not for differential expression, which no library tool tests. method and countKind are required; TMM needs a referenceSample; factors depend on the genes sent; at most 6,000 counts, each at most 1e10.
Field: Sequencing and genomics.
What it does
You give
method(required): median-of-ratios or tmm. Required; upper quartile is 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 counts vector follows.samples(required): The samples, at least two.referenceSample: With tmm, required: the id of the sample every other sample is compared with; its factor is exactly one. Leading and trailing white space is removed before the pattern is checked. Optional.
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-sample factors from the counts you supply, for a spot check of up to 6,000 values (2,000 genes and 50 samples at most), not a whole transcriptome; library sizes are column totals over the genes supplied, and a different gene set changes the factors.
- Both methods assume most genes are not differentially expressed, and neither is a test for differential expression.
- Median of ratios uses only genes positive in every sample.
- TMM is computed against the reference sample you name, from the paper's text where its printed equation differs.
- The paper trims each tail and keeps only genes trimmed by neither M nor A; how many are removed (0.3 n and 0.05 n, rounded down) and how ties are ranked (tied values share their average rank, decided exactly from the counts, so tied genes are kept or removed together) are our stated convention; it can leave no gene, which is refused, and other implementations may rank ties differently.
- A size factor divides counts; a normalisation factor multiplies the library size; the two are not interchangeable.
- Upper-quartile factors are not offered.
- 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
- no gene is positive in every sample, so no ratio to a geometric mean exists.
- a sample's counts do not have one value per gene.
- the counts are more numeric cells than the tool's cap; a smaller gene subset changes the factors.
- no gene is non-zero in both the sample and the reference, or none is left after trimming, so no weighted mean of M exists.
Every input, with its units5 inputs
method(required): median-of-ratios or tmm. Required; upper quartile is 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 counts vector follows.samples(required): The samples, at least two.referenceSample: With tmm, required: the id of the sample every other sample is compared with; its factor is exactly one. Leading and trailing white space is removed before the pattern is checked. Optional.
Every refusal, with its code8 refusals
| Code | When |
|---|---|
NO_GENE_IN_EVERY_SAMPLE | no gene is positive in every sample, so no ratio to a geometric mean exists. |
VALUES_MISALIGNED | a sample's counts do not have one value per gene. |
MATRIX_ABOVE_CAP | the counts are more numeric cells than the tool's cap; a smaller gene subset changes the factors. |
NO_GENES_LEFT | no gene is non-zero in both the sample and the reference, or none is left after trimming, so no weighted mean of M exists. |
ZERO_VARIANCE_GENE | a kept gene is the whole library in both the sample and the reference, so its variance is zero and its weight unbounded. |
VALUES_MISALIGNED | a sample's counts do not have one value per gene. |
MATRIX_ABOVE_CAP | the counts are more numeric cells than the tool's cap; a smaller gene subset changes the factors. |
UNKNOWN_SAMPLE | referenceSample names no sample sent. |
The calculation
2 methods, each with its equations, constants and worked example.
s_j = median over genes i positive in every sample of k_ij / (prod over v of k_iv)^(1/m), the median of an even count the mean of the middle two log ratios
rescaled s_j = s_j / (prod over the m samples v of s_v)^(1/m)
order of checks: the kit's checks of each argument (each count at least 0 and at most 1e10, the tool's input bound shared with ngs_expression_normalise, else OUT_OF_RANGE), then DUPLICATE_NAME (genes, then samples), then MATRIX_ABOVE_CAP (samples × genes above 6,000 numeric cells), then per sample in order VALUES_MISALIGNED and, for read counts, NOT_AN_INTEGER, then CONFLICTING_ARGUMENT for a referenceSample, then NO_GENE_IN_EVERY_SAMPLE
| Symbol | Meaning |
|---|---|
s_j | size factor of sample j |
k_ij | count of gene i in sample j |
m | number of samples |
Source 1.
Conventions
- method = median-of-ratios: Anders and Huber's median of ratios: each sample's counts against the per-gene geometric mean across samples
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-size-factors-mor@1.0.1, 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
- method = median-of-ratios
- countKind = read-counts
- genes[0] = geneA
- genes[1] = geneB
- genes[2] = geneC
- samples[0].id = s1
- samples[0].counts.values[0] = 10
- samples[0].counts.values[1] = 20
- samples[0].counts.values[2] = 40
- samples[0].counts.unit = 1
- samples[1].id = s2
- samples[1].counts.values[0] = 20
- samples[1].counts.values[1] = 40
- samples[1].counts.values[2] = 80
- samples[1].counts.unit = 1
Steps
- s1 = 0.70710678, from samples[0].counts, samples[1].counts; by the equation above; gives result.samples[0].sizeFactor.
- s2 = 1.4142136, from samples[0].counts, samples[1].counts; by the equation above; gives result.samples[1].sizeFactor.
The working, as the case writes it:
the pseudo-reference is sqrt(2) times sample 1 for every gene, so s1 = 1/sqrt 2 = 0.70710678118654752440 and s2 = sqrt 2 = 1.4142135623730950488
Answer
result.samples[0].sizeFactor = 0.70710678
result.samples[1].sizeFactor = 1.4142136
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: factors and their geometric mean relative 1e-13 against the 50-digit reference; gene counts, gene lists, texts, statuses and codes exact.
Equation 1
for genes with Y_gk > 0 and Y_gr > 0
| Symbol | Meaning |
|---|---|
Y_gk | count of gene g in sample k |
N_k | column total of sample k |
r | the reference sample |
Source 2.
Equation 2
rank the n genes ascending by M (and, separately, by A)
a run of tied values shares the mean of its ranks, and a gene is kept by M when its rank (or its run's mean rank) r satisfies floor(0.3 n) + 1 <= r <= n - floor(0.3 n), by A when floor(0.05 n) + 1 <= r <= n - floor(0.05 n), so a tied run is kept or trimmed whole
ties are decided exactly from the counts, M_g = M_h when Y_gk Y_hr = Y_hk Y_gr and A_g = A_h when Y_gk Y_gr = Y_hk Y_hr
keep genes kept by both (each tail and the intersection from the paper's text
the rounding and the tie rule our stated convention)
| Symbol | Meaning |
|---|---|
n | genes non-zero in both samples |
Source 2.
Equation 3
f_k = 2^(sum w_g M_g / sum w_g), w_g = [(N_k - Y_gk)/(N_k Y_gk) + (N_r - Y_gr)/(N_r Y_gr)]^-1 over the kept genes
effective library size N_k f_k, with f unrescaled and the reference at 1: the record's choice (the paper's two-library form multiplies or divides by sqrt(f), and edgeR's factors rescaled to geometric mean one, unread, differ from it by a common factor)
rescaled f_k = f_k / (prod over all samples j, the reference included, of f_j)^(1/m)
order of checks: the kit's checks of each argument (each count at least 0 and at most 1e10, the tool's input bound shared with ngs_expression_normalise, else OUT_OF_RANGE), then DUPLICATE_NAME (genes, then samples), then MATRIX_ABOVE_CAP (samples × genes above 6,000 numeric cells), then per sample in order VALUES_MISALIGNED and, for read counts, NOT_AN_INTEGER, then MISSING_ARGUMENT without a referenceSample and UNKNOWN_SAMPLE for one not sent, then the samples other than the reference in input order, the first refusal met being reported (NO_GENES_LEFT, or ZERO_VARIANCE_GENE, which looks at kept genes only)
| Symbol | Meaning |
|---|---|
f_k | normalisation factor of sample k |
w_g | inverse of the delta-method variance of M_g |
Source 2.
Conventions
- method = tmm: Robinson and Oshlack's trimmed mean of M values against a named reference sample, from the paper's text
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-size-factors-tmm@1.0.1, 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
- method = tmm
- countKind = read-counts
- referenceSample = ref
- genes[0] = g0
- genes[1] = g1
- genes[2] = g2
- genes[3] = g3
- genes[4] = g4
- genes[5] = g5
- genes[6] = g6
- genes[7] = g7
- genes[8] = g8
- genes[9] = g9
- samples[0].id = ref
- samples[0].counts.values[0] = 10
- samples[0].counts.values[1] = 20
- samples[0].counts.values[2] = 30
- samples[0].counts.values[3] = 40
- samples[0].counts.values[4] = 50
- samples[0].counts.values[5] = 60
- samples[0].counts.values[6] = 70
- samples[0].counts.values[7] = 80
- samples[0].counts.values[8] = 90
- samples[0].counts.values[9] = 100
- samples[0].counts.unit = 1
- samples[1].id = double
- samples[1].counts.values[0] = 20
- samples[1].counts.values[1] = 40
- samples[1].counts.values[2] = 60
- samples[1].counts.values[3] = 80
- samples[1].counts.values[4] = 100
- samples[1].counts.values[5] = 120
- samples[1].counts.values[6] = 140
- samples[1].counts.values[7] = 160
- samples[1].counts.values[8] = 180
- samples[1].counts.values[9] = 200
- samples[1].counts.unit = 1
Steps
- f = 1, from samples[0].counts, samples[1].counts; by equation 3; gives result.samples[1].normalisationFactor.
The working, as the case writes it:
ref f = 1 (the reference)
double f = 1 (10 genes kept). Robinson and Oshlack 2010's printed equation disagrees with its text at M (a ratio of two logs) and at w (the variance, not its inverse)
no correction is listed (read 11 November 2026)
the text is implemented.
Answer
result.samples[1].normalisationFactor = 1
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: factors, rescaled factors, effective library sizes and their geometric mean relative 1e-13 against the 50-digit reference; library sizes, gene counts, texts, statuses and codes exact.
Sources
Anders S, Huber W (2010), Differential expression analysis for sequence count data, Genome Biology 11:R106, doi 10.1186/gb-2010-11-10-r106
Used for: Median-of-ratios size factor per sample. At: equation Eq. 5.
Read in full, 11 November 2026Robinson MD, Oshlack A (2010), A scaling normalization method for differential expression analysis of RNA-seq data, Genome Biology 11:R25, doi 10.1186/gb-2010-11-3-r25
Used for: TMM normalisation factor per sample against a named reference. At: section Materials and methods, TMM: the text's M, A, trimming and inverse-variance weights (the printed equation differs at M and w; the text is implemented).
Read in full, 11 November 2026
How it is checked
- Level
Checked against a reference, the lowest of its 2 methods.
- Checked against
Median-of-ratios size factor per sample: Our own exact-arithmetic script and a hand-worked case.
TMM normalisation factor per sample against a named reference: Our own exact-arithmetic script and a hand-worked case.
- Cases
Median-of-ratios size factor per sample: No case set is linked to this version.
TMM normalisation factor per sample against a named reference: No case set is linked to this version.
- Not checked
Median-of-ratios size factor per sample: DESeq2 itself (LGPL, build-time only, not run here: R is not available); any minimum-gene threshold (none is applied); whether the supplied genes are representative.
TMM normalisation factor per sample against a named reference: edgeR itself (GPL, build-time only, not run here: R is not available) and its reference-sample rule (unread, so the reference is required); the paper's printed equation, which is not implemented.
Changes to its methods0 changes
No change is logged for these method versions.