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
¶
Voom
dataclass
¶
voom's log2-CPM and precision weights.
Attributes:
| Name | Type | Description |
|---|---|---|
e |
ndarray
|
|
weights |
ndarray
|
|
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
|
|
required |
design
|
Design
|
|
required |
lib_size
|
Vector | None
|
Library size per sample. Defaults to the column sums. |
None
|
norm_factors
|
Vector | None
|
Normalisation factors, multiplied into |
None
|
span
|
float
|
Lowess span, used only when |
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 |
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
|
|
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
|
span
|
float
|
Lowess span, used only when |
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 |
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
|
|
required |
design
|
Design
|
|
required |
weights
|
ndarray | None
|
|
None
|
Returns:
| Type | Description |
|---|---|
LinearFit
|
The |
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. |
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
|
|
Examples:
contrasts_fit
¶
Re-express the fit in terms of contrasts, limma's contrasts.fit.
Run it before ebayes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fit
|
LinearFit
|
The |
required |
contrasts
|
Contrast
|
|
required |
Returns:
| Type | Description |
|---|---|
LinearFit
|
A new |
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 |
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 |
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 |
required |
coef
|
int | str | None
|
0-based coefficient or its name. |
None
|
feature_ids
|
Labels | None
|
One id per gene of the fit. |
None
|
number
|
int | None
|
Maximum number of rows. |
None
|
sort_by
|
TableSort
|
Ranking column. |
'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
|
|
DataFrame
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |