Skip to content

Filtering and normalisation

edgeR and limma-voom differential expression, in Rust.

NumPy in, polars out. Counts are (n_genes, n_samples) as in edgeR, every index is 0-based, and a design is a NumPy matrix or a polars frame whose column names become coefficient names.

One call:

>>> res = edge_rs.run_edger_ql(counts, design, feature_ids=genes, coef="treated")
>>> res = edge_rs.run_limma_voom(counts, design, feature_ids=genes, coef="treated")

Step by step, as in edgeR and limma:

>>> keep = edge_rs.filter_by_expr(counts, design=design)
>>> lib = counts.sum(axis=0)
>>> nf = edge_rs.calc_norm_factors(counts[keep], lib_size=lib)
>>> fit = edge_rs.glm_ql_fit(counts[keep], design, lib_size=lib, norm_factors=nf)
>>> edge_rs.top_tags(edge_rs.glm_ql_ftest(fit, coef="treated"))

NEBULA on an AnnData, cells x genes as scanpy keeps them, with the anndata extra:

>>> fit = edge_rs.nebula(adata, "donor", "~ condition", genes=hvgs)
>>> res = fit.test(coef="condition[T.treated]")

device="gpu" runs its penalised fits on the wgpu device; gpu_available() says whether this machine has one.

Every function that takes counts also takes a DgeList from make_dgelist, which carries lib_size and norm_factors and keeps the library sizes of the full matrix through a gene subset.

DgeList dataclass

DgeList(
    *,
    counts: ndarray,
    lib_size: ndarray,
    norm_factors: ndarray,
    genes: DataFrame | None = None,
    samples: DataFrame | None = None,
)

Counts, library sizes, normalisation factors and annotation.

Build it with make_dgelist, which checks the shapes. Every function that takes counts takes a DgeList in their place and reads lib_size and norm_factors from it.

Attributes:

Name Type Description
counts ndarray

(n_genes, n_samples) raw counts.

lib_size ndarray

Library size per sample, of the matrix before any gene subset.

norm_factors ndarray

Normalisation factor per sample.

genes DataFrame | None

One row per gene, or None.

samples DataFrame | None

One row per sample, or None.

effective_lib_size property

effective_lib_size: ndarray

lib_size * norm_factors, edgeR's effective library size.

shape property

shape: tuple[int, int]

(n_genes, n_samples).

make_dgelist

make_dgelist(
    counts: ndarray,
    *,
    lib_size: Vector | None = None,
    norm_factors: Vector | None = None,
    genes: DataFrame | None = None,
    samples: DataFrame | None = None,
) -> DgeList

A DgeList from its parts, edgeR's DGEList.

Parameters:

Name Type Description Default
counts ndarray

(n_genes, n_samples) raw counts, before any gene filter.

required
lib_size Vector | None

Library size per sample. Defaults to the column sums.

None
norm_factors Vector | None

Normalisation factor per sample. Defaults to ones.

None
genes DataFrame | None

Gene annotation, one row per gene.

None
samples DataFrame | None

Sample annotation, one row per sample.

None

Returns:

Type Description
DgeList

The DgeList.

Raises:

Type Description
ValueError

On negative counts, a non-positive library size, or annotation of the wrong height.

filter_by_expr

filter_by_expr(
    counts: ndarray | DgeList,
    *,
    group: Labels | None = None,
    design: Design | None = None,
    lib_size: Vector | None = None,
    norm_factors: Vector | None = None,
    min_count: float = 10.0,
    min_total_count: float = 15.0,
    large_n: float = 10.0,
    min_prop: float = 0.7,
) -> ndarray

Keep genes with enough counts to be worth testing, edgeR's filterByExpr.

Parameters:

Name Type Description Default
counts ndarray | DgeList

(n_genes, n_samples) raw counts, or a DgeList.

required
group Labels | None

One group label per sample. Takes precedence over design.

None
design Design | None

(n_samples, n_coef) design. Without group or design all samples are one group.

None
lib_size Vector | None

Library size per sample. Defaults to the column sums.

None
norm_factors Vector | None

Normalisation factors, multiplied into lib_size.

None
min_count float

Count a gene must reach in the median-sized library.

10.0
min_total_count float

Total count across all samples a gene must reach.

15.0
large_n float

Group size past which the sample requirement is damped.

10.0
min_prop float

Fraction of samples beyond large_n that still count.

0.7

Returns:

Type Description
ndarray

A boolean mask over the genes.

Raises:

Type Description
ValueError

On mismatched shapes or negative counts.

References

Chen, Lun and Smyth, F1000Research, 2016.

Examples:

>>> keep = edge_rs.filter_by_expr(counts, design=design)
>>> counts = counts[keep]

calc_norm_factors

calc_norm_factors(
    counts: ndarray,
    *,
    method: str = ...,
    lib_size: Vector | None = ...,
    ref_column: int | None = ...,
) -> ndarray
calc_norm_factors(
    counts: DgeList,
    *,
    method: str = ...,
    lib_size: None = ...,
    ref_column: int | None = ...,
) -> DgeList
calc_norm_factors(
    counts: ndarray | DgeList,
    *,
    method: str = "TMM",
    lib_size: Vector | None = None,
    ref_column: int | None = None,
) -> ndarray | DgeList

Library size normalisation factors, edgeR's calcNormFactors.

Parameters:

Name Type Description Default
counts ndarray | DgeList

(n_genes, n_samples) raw counts, or a DgeList.

required
method str

"TMM", "TMMwsp", "RLE", "upperquartile" or "none".

'TMM'
lib_size Vector | None

Library size per sample. Defaults to the column sums. Pass the unfiltered column sums after a gene filter, as edgeR does.

None
ref_column int | None

0-based reference sample for TMM. None picks edgeR's.

None

Returns:

Type Description
ndarray | DgeList

One factor per sample, with a geometric mean of one. Given a

ndarray | DgeList

DgeList, a copy with these as its norm_factors.

Raises:

Type Description
ValueError

On an unknown method, mismatched shapes, or lib_size alongside a DgeList.

References

Robinson and Oshlack, Genome Biology, 2010.

cpm

cpm(
    counts: ndarray | DgeList,
    *,
    lib_size: Vector | None = None,
    norm_factors: Vector | None = None,
    log: bool = False,
    prior_count: float = 2.0,
) -> ndarray

Counts per million, edgeR's cpm.

Parameters:

Name Type Description Default
counts ndarray | DgeList

(n_genes, n_samples) raw counts, or a DgeList.

required
lib_size Vector | None

Library size per sample. Defaults to the column sums.

None
norm_factors Vector | None

Normalisation factors, multiplied into lib_size.

None
log bool

Return log2-CPM.

False
prior_count float

Count added before the log, scaled by library size.

2.0

Returns:

Type Description
ndarray

(n_genes, n_samples) CPM or log2-CPM.

ave_log_cpm

ave_log_cpm(
    counts: ndarray | DgeList,
    *,
    lib_size: Vector | None = None,
    norm_factors: Vector | None = None,
    prior_count: float = 2.0,
    dispersion: float | None = None,
) -> ndarray

Average log2-CPM per gene, edgeR's aveLogCPM.

Not the mean of cpm(log=True): an intercept-only negative binomial fit per gene.

Parameters:

Name Type Description Default
counts ndarray | DgeList

(n_genes, n_samples) raw counts, or a DgeList.

required
lib_size Vector | None

Library size per sample. Defaults to the column sums.

None
norm_factors Vector | None

Normalisation factors, multiplied into lib_size.

None
prior_count float

Count added before the log, scaled by library size.

2.0
dispersion float | None

Negative binomial dispersion. None is edgeR's 0.05.

None

Returns:

Type Description
ndarray

One value per gene.

remove_batch_effect

remove_batch_effect(
    x: ndarray,
    batch: Labels | None = None,
    *,
    batch2: Labels | None = None,
    covariates: ndarray | None = None,
    design: Design | None = None,
) -> ndarray

Regress batch effects out of log-expression, limma's removeBatchEffect.

For plots and clustering. Do not feed the result into a linear model: put the batch in the design there instead.

Parameters:

Name Type Description Default
x ndarray

(n_genes, n_samples) log-expression, e.g. cpm(..., log=True).

required
batch Labels | None

One batch label per sample.

None
batch2 Labels | None

A second batch label per sample.

None
covariates ndarray | None

(n_samples, n_cov) numeric covariates to regress out.

None
design Design | None

(n_samples, n_coef) design of the effects to keep.

None

Returns:

Type Description
ndarray

The corrected (n_genes, n_samples) matrix.