Skip to content

Single cell

NEBULA on an AnnData, returning a pandas frame. Needs the anndata extra. Worked through in NEBULA on AnnData.

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.

nebula

nebula(
    adata: AnnData,
    subject_key: str,
    design: str | ndarray | DataFrame,
    *,
    layer: str | None = "counts",
    genes: Sequence[str] | ndarray | Index | None = None,
    mask_obs: str | ndarray | Series | None = None,
    offset: ndarray
    | Series
    | Sequence[float]
    | None = None,
    method: Literal["ln", "hl"] = "ln",
    cpc: float = 0.005,
    mincp: int = 5,
    min_subjects: int = 0,
    shrink_dispersion: bool = True,
    gene_block_size: int = 1000,
    device: Literal["cpu", "gpu"] = "cpu",
) -> NebulaFit

NEBULA's negative binomial gamma mixed model, per gene.

The variance splits into a subject-level random effect and a cell-level overdispersion, so donors are modelled rather than pseudoreplicated. A fit takes milliseconds to seconds per gene, so pass the HVGs or a candidate set as genes rather than all of them. Cells need not be sorted by subject.

Parameters:

Name Type Description Default
adata AnnData

Annotated data matrix with raw counts.

required
subject_key str

obs column with the subject (donor), the random effect.

required
design str | ndarray | DataFrame

A Formulaic formula over obs, e.g. "~ condition * age", or a precomputed design with one row per obs. A DataFrame is matched to obs_names by its index and its columns name the coefficients; an array goes by position and its coefficients by number. Formulaic treatment codes against the first level (category order, else sorted) and names the columns condition[T.treated].

required
layer str | None

Where the counts are. None reads X, "raw" reads adata.raw.X unless a layer of that name exists.

'counts'
genes Sequence[str] | ndarray | Index | None

Gene names or a boolean mask over var. None fits every gene.

None
mask_obs str | ndarray | Series | None

Fit only these cells, e.g. one cell type, without subsetting the AnnData. A boolean array over obs, a boolean Series matched to obs_names by its index, or the name of a boolean obs column. None fits every cell. The default offset is still each cell's library size over every gene.

None
offset ndarray | Series | Sequence[float] | None

Strictly positive scaling per cell, one per obs. A Series is matched to obs_names by its index, anything else by position. None uses the library size over every gene of the layer.

None
method Literal['ln', 'hl']

"ln" for NEBULA-LN, "hl" for NEBULA-HL. LN falls back to HL below 30 cells per subject, as in R.

'ln'
cpc float

Drop a gene whose mean count per cell is at most this.

0.005
mincp int

Drop a gene expressed in fewer cells than this.

5
min_subjects int

Drop a gene fewer subjects than this express. 0 is off.

0
shrink_dispersion bool

Also shrink the cell-level overdispersions towards an empirical Bayes prior. Does not change the test.

True
gene_block_size int

Genes transposed and fitted at a time. Bounds the memory on top of the counts.

1000
device Literal['cpu', 'gpu']

"gpu" runs the penalised fits on the wgpu device (Metal, Vulkan or DX12) in float32, so results land close to the CPU path but not on it. A design of categoricals only runs the CPU path anyway, which is faster there. Check edge_rs.gpu_available() first.

'cpu'

Returns:

Type Description
NebulaFit

The NebulaFit over the genes that passed the expression filter, in

NebulaFit

input order. Its test runs the Wald test.

Raises:

Type Description
ValueError

On a missing layer or column, non-integral counts, a two-sided formula, a design of the wrong shape or not of full rank, a bad offset, or no gene passing the filter.

RuntimeError

With device="gpu" and no usable GPU.

References

He et al., Communications Biology 4, 629, 2021.

Examples:

>>> fit = edge_rs.nebula(adata, "donor", "~ condition + age", genes=hvgs)
>>> fit.test(coef="condition[T.treated]").sort_values("p_value").head()
>>> fit.test(coef="age")

gpu_available builtin

gpu_available() -> bool

Whether device="gpu" can run here.

Returns

True only when this build has the gpu feature and wgpu resolves an adapter. Safe to call on any machine.