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.
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
|
|
required |
design
|
Design
|
|
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 |
None
|
ave_log_cpm
|
Vector | None
|
Average log2-CPM per gene. |
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 |
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 |
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
|
contrast
|
Contrast | None
|
Weights over the design columns, a vector or an
|
None
|
Returns:
| Type | Description |
|---|---|
QLTest
|
The |
Raises:
| Type | Description |
|---|---|
ValueError
|
On an unknown coefficient, or both |
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 |
required |
feature_ids
|
Labels | None
|
One id per gene of the fit. |
None
|
n
|
int | None
|
Maximum number of rows. |
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
|
|