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
|
|
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 |
samples |
DataFrame | None
|
One row per sample, or |
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
|
|
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 |
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
|
|
required |
group
|
Labels | None
|
One group label per sample. Takes precedence over |
None
|
design
|
Design | None
|
|
None
|
lib_size
|
Vector | None
|
Library size per sample. Defaults to the column sums. |
None
|
norm_factors
|
Vector | None
|
Normalisation factors, multiplied into |
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 |
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:
calc_norm_factors
¶
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
|
|
required |
method
|
str
|
|
'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
|
Returns:
| Type | Description |
|---|---|
ndarray | DgeList
|
One factor per sample, with a geometric mean of one. Given a |
ndarray | DgeList
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
On an unknown method, mismatched shapes, or |
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
|
|
required |
lib_size
|
Vector | None
|
Library size per sample. Defaults to the column sums. |
None
|
norm_factors
|
Vector | None
|
Normalisation factors, multiplied into |
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
|
|
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
|
|
required |
lib_size
|
Vector | None
|
Library size per sample. Defaults to the column sums. |
None
|
norm_factors
|
Vector | None
|
Normalisation factors, multiplied into |
None
|
prior_count
|
float
|
Count added before the log, scaled by library size. |
2.0
|
dispersion
|
float | None
|
Negative binomial dispersion. |
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
|
|
required |
batch
|
Labels | None
|
One batch label per sample. |
None
|
batch2
|
Labels | None
|
A second batch label per sample. |
None
|
covariates
|
ndarray | None
|
|
None
|
design
|
Design | None
|
|
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
The corrected |