Skip to content

limma

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.

LinearFit

A linear model fit, limma's MArrayLM.

amean property

amean: ndarray | None

Average log-expression per gene. limma's Amean.

coef_names property

coef_names: list[str]

Coefficient names, empty when there were none.

coefficients property

coefficients: ndarray

(n_genes, n_coef) coefficients.

df_prior property

df_prior: ndarray | None

Prior degrees of freedom, set by ebayes.

df_residual property

df_residual: ndarray

Residual degrees of freedom per gene.

df_total property

df_total: ndarray | None

Total degrees of freedom per gene, set by ebayes.

f_p_value property

f_p_value: ndarray | None

P-value of the moderated F.

f_stat property

f_stat: ndarray | None

Moderated F per gene, set by ebayes on a full-rank design.

lods property

lods: ndarray | None

(n_genes, n_coef) log-odds of differential expression, set by ebayes.

n_coef property

n_coef: int

Width of the coefficient axis: design columns, or contrasts after contrasts_fit.

n_genes property

n_genes: int

Number of genes.

p_value property

p_value: ndarray | None

(n_genes, n_coef) p-values, set by ebayes.

s2_post property

s2_post: ndarray | None

Posterior variance per gene, set by ebayes.

s2_prior property

s2_prior: ndarray | None

Prior variance, set by ebayes.

sigma property

sigma: ndarray

Residual standard deviation per gene.

stdev_unscaled property

stdev_unscaled: ndarray

(n_genes, n_coef) unscaled standard deviations.

t property

t: ndarray | None

(n_genes, n_coef) moderated t, set by ebayes.

Voom dataclass

Voom(
    *,
    e: ndarray,
    weights: ndarray,
    amean: ndarray,
    trend_x: ndarray,
    trend_y: ndarray,
)

voom's log2-CPM and precision weights.

Attributes:

Name Type Description
e ndarray

(n_genes, n_samples) log2-CPM. limma's E.

weights ndarray

(n_genes, n_samples) precision weights.

amean ndarray

Average log2-CPM per gene.

trend_x ndarray

Abscissae of the fitted mean-variance trend.

trend_y ndarray

Ordinates of the trend, sqrt of the residual standard deviation.

voom

voom(
    counts: ndarray | DgeList,
    design: Design,
    *,
    lib_size: Vector | None = None,
    norm_factors: Vector | None = None,
    span: float = 0.5,
    adaptive_span: bool = True,
    prior_count: float = 0.5,
) -> Voom

Log2-CPM with precision weights from the mean-variance trend, limma's voom.

For a normalised expression matrix. To test, voom_lm_fit is the one-step route and handles structural zeros; lm_fit(v.e, design, weights=v.weights) is limma's classic two-step one.

Parameters:

Name Type Description Default
counts ndarray | DgeList

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

required
design Design

(n_samples, n_coef) design.

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
span float

Lowess span, used only when adaptive_span is off.

0.5
adaptive_span bool

Pick the span from the number of genes.

True
prior_count float

Count added before the log.

0.5

Returns:

Type Description
Voom

The Voom.

References

Law, Chen, Shi and Smyth, Genome Biology, 2014.

voom_lm_fit

voom_lm_fit(
    counts: ndarray | DgeList,
    design: Design,
    *,
    lib_size: Vector | None = None,
    norm_factors: Vector | None = None,
    span: float = 0.5,
    adaptive_span: bool = True,
    prior_count: float = 0.5,
) -> LinearFit

voom and the weighted linear model in one step, edgeR's voomLmFit.

Genes with an all-zero group lose the residual degrees of freedom those zeros would otherwise inflate.

Parameters:

Name Type Description Default
counts ndarray | DgeList

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

required
design Design

(n_samples, n_coef) design with an intercept.

required
lib_size Vector | None

Library size per sample. Defaults to the column sums. After a gene filter, pass the unfiltered column sums.

None
norm_factors Vector | None

Normalisation factors from calc_norm_factors.

None
span float

Lowess span, used only when adaptive_span is off.

0.5
adaptive_span bool

Pick the span from the number of genes.

True
prior_count float

Count added before the log.

0.5

Returns:

Type Description
LinearFit

The LinearFit, to hand to contrasts_fit or ebayes.

References

Law, Chen, Shi and Smyth, Genome Biology, 2014.

lm_fit

lm_fit(
    y: ndarray,
    design: Design,
    *,
    weights: ndarray | None = None,
) -> LinearFit

Least squares fit per gene on log-expression, limma's lmFit.

For limma-trend, fit cpm(counts, log=True, ...) and call ebayes(fit, trend=True).

Parameters:

Name Type Description Default
y ndarray

(n_genes, n_samples) log-expression. NaN is missing.

required
design Design

(n_samples, n_coef) design.

required
weights ndarray | None

(n_genes, n_samples) observation weights, e.g. Voom.weights.

None

Returns:

Type Description
LinearFit

The LinearFit.

make_contrasts

make_contrasts(
    contrasts: Mapping[str, str] | Sequence[str],
    levels: Design | Sequence[str],
) -> DataFrame

A contrast matrix from expressions over the design columns, limma's makeContrasts.

Supports + - * / ( ), numbers and column names.

Parameters:

Name Type Description Default
contrasts Mapping[str, str] | Sequence[str]

Contrast name to expression, e.g. {"BvsA": "B - A"}. A plain list of expressions uses them as their own names.

required
levels Design | Sequence[str]

The design column names, or the polars design itself.

required

Returns:

Type Description
DataFrame

One column per contrast, one row per design column. Hand it to

DataFrame

contrasts_fit or glm_ql_ftest(contrast=...).

Examples:

>>> c = edge_rs.make_contrasts({"BvsA": "groupB - groupA"}, design)

contrasts_fit

contrasts_fit(
    fit: LinearFit, contrasts: Contrast
) -> LinearFit

Re-express the fit in terms of contrasts, limma's contrasts.fit.

Run it before ebayes.

Parameters:

Name Type Description Default
fit LinearFit

The LinearFit from voom_lm_fit or lm_fit.

required
contrasts Contrast

(n_coef, n_contrasts) weights, a vector for one contrast, or the frame make_contrasts returns.

required

Returns:

Type Description
LinearFit

A new LinearFit whose coefficients are the contrasts.

ebayes

ebayes(
    fit: LinearFit,
    *,
    proportion: float = 0.01,
    trend: bool = False,
    robust: bool = False,
) -> LinearFit

Empirical Bayes moderated statistics, limma's eBayes.

Parameters:

Name Type Description Default
fit LinearFit

The LinearFit.

required
proportion float

Assumed proportion of differentially expressed genes. Only the B-statistic uses it.

0.01
trend bool

Trend the prior variance against average expression (limma-trend). Leave off after voom.

False
robust bool

Robust against outlier genes.

False

Returns:

Type Description
LinearFit

A new LinearFit with t, p_value, lods and friends set.

References

Smyth, Statistical Applications in Genetics and Molecular Biology, 2004.

top_table

top_table(
    fit: LinearFit,
    *,
    coef: int | str | None = None,
    feature_ids: Labels | None = None,
    number: int | None = None,
    sort_by: TableSort = "B",
    p_value: float = 1.0,
    lfc: float = 0.0,
    confint: bool | float = False,
) -> DataFrame

The moderated t-test for one coefficient as a table, limma's topTable.

The adjusted p-value is Benjamini-Hochberg over every gene, before number, p_value or lfc thin the table.

Parameters:

Name Type Description Default
fit LinearFit

The LinearFit, after ebayes.

required
coef int | str | None

0-based coefficient or its name. None is the last one.

None
feature_ids Labels | None

One id per gene of the fit. None uses the 0-based row.

None
number int | None

Maximum number of rows. None keeps every gene.

None
sort_by TableSort

Ranking column. "B" is limma's default.

'B'
p_value float

Keep rows with adjusted p-value at most this.

1.0
lfc float

Keep rows with absolute log2 fold change at least this.

0.0
confint bool | float

Add a 95% interval on the fold change, or the level to use.

False

Returns:

Type Description
DataFrame

feature_id, log_fc, ci_lower and ci_upper (with confint),

DataFrame

ave_expr, t, p_value, adj_p_value and b.

Raises:

Type Description
ValueError

If ebayes has not run, or on an unknown coefficient.