Skip to content

edgeR quasi-likelihood

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.

QLFit

A fitted quasi-likelihood negative binomial GLM, edgeR's DGEGLM.

ave_log_cpm property

ave_log_cpm: ndarray

Average log2-CPM per gene.

average_ql_dispersion property

average_ql_dispersion: float | None

Average quasi-dispersion the NB dispersion was divided by. None on the legacy pipeline.

coef_names property

coef_names: list[str]

Design column names, empty when the design had none.

coefficients property

coefficients: ndarray

(n_genes, n_coef) coefficients on the natural log scale.

deviance property

deviance: ndarray

Residual deviance per gene.

df_prior property

df_prior: ndarray

Prior degrees of freedom, length one or one per gene.

n_coef property

n_coef: int

Number of design columns.

n_genes property

n_genes: int

Number of genes.

n_samples property

n_samples: int

Number of samples.

s2_post property

s2_post: ndarray

Posterior quasi-likelihood dispersion per gene.

s2_prior property

s2_prior: ndarray

Prior quasi-likelihood dispersion, length one or one per gene.

QLTest dataclass

QLTest(
    *,
    log_fc: ndarray,
    log_cpm: ndarray,
    f_stat: ndarray,
    p_value: ndarray,
    df_test: float,
    df_total: ndarray | None,
)

A quasi-likelihood F-test, one entry per gene of the fit.

Attributes:

Name Type Description
log_fc ndarray

Log2 fold change of the first tested coefficient or contrast.

log_cpm ndarray

Average log2-CPM.

f_stat ndarray

Quasi-likelihood F statistic.

p_value ndarray

Raw p-value.

df_test float

Numerator degrees of freedom.

df_total ndarray | None

Denominator degrees of freedom per gene.

EdgeRsError

Bases: Exception

glm_ql_fit

glm_ql_fit(
    counts: ndarray | DgeList,
    design: Design,
    *,
    lib_size: Vector | None = None,
    norm_factors: Vector | None = None,
    ave_log_cpm: Vector | None = None,
    robust: bool = False,
    legacy: bool = False,
) -> QLFit

Fit a quasi-likelihood negative binomial GLM per gene, edgeR's glmQLFit.

The current (edgeR 4) pipeline estimates the negative binomial dispersion itself, so there is no estimateDisp step. legacy=True takes the pre-4.0 pipeline, which runs estimateDisp first and uses its trend.

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. A polars frame's column names can later be used as coef.

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
ave_log_cpm Vector | None

Average log2-CPM per gene. None computes it.

None
robust bool

Robust empirical Bayes, so outlier genes get a smaller prior.

False
legacy bool

edgeR's pre-4.0 pipeline.

False

Returns:

Type Description
QLFit

The QLFit, to hand to glm_ql_ftest.

Raises:

Type Description
ValueError

On mismatched shapes or negative counts.

EdgeRsError

If the design is not full rank.

References

Chen, Lun and Smyth, F1000Research, 2016.

glm_ql_ftest

glm_ql_ftest(
    fit: QLFit,
    *,
    coef: Coef | None = None,
    contrast: Contrast | None = None,
) -> QLTest

Quasi-likelihood F-test per gene, edgeR's glmQLFTest.

Parameters:

Name Type Description Default
fit QLFit

The QLFit.

required
coef Coef | None

0-based design column(s) or names to drop from the null model. Several give an F-test on all of them. None tests the last column.

None
contrast Contrast | None

Weights over the design columns, a vector or an (n_coef, n_contrasts) matrix (see make_contrasts). Exclusive with coef.

None

Returns:

Type Description
QLTest

The QLTest, to hand to top_tags.

Raises:

Type Description
ValueError

On an unknown coefficient, or both coef and contrast.

top_tags

top_tags(
    test: QLTest,
    *,
    feature_ids: Labels | None = None,
    n: int | None = None,
    sort_by: Literal[
        "p_value", "log_fc", "none"
    ] = "p_value",
    p_cutoff: float = 1.0,
) -> DataFrame

The test as a table, edgeR's topTags.

The FDR is Benjamini-Hochberg over every gene, before n or p_cutoff thin the table.

Parameters:

Name Type Description Default
test QLTest

The QLTest.

required
feature_ids Labels | None

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

None
n int | None

Maximum number of rows. None keeps every gene.

None
sort_by Literal['p_value', 'log_fc', 'none']

Ascending p-value, descending absolute fold change, or input order.

'p_value'
p_cutoff float

Keep rows with FDR at most this.

1.0

Returns:

Type Description
DataFrame

feature_id, log_fc, log_cpm, f_stat, p_value and fdr.