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.
|
required |
layer
|
str | None
|
Where the counts are. |
'counts'
|
genes
|
Sequence[str] | ndarray | Index | None
|
Gene names or a boolean mask over var. |
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 |
None
|
offset
|
ndarray | Series | Sequence[float] | None
|
Strictly positive scaling per cell, one per obs. A Series is
matched to |
None
|
method
|
Literal['ln', 'hl']
|
|
'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
|
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']
|
|
'cpu'
|
Returns:
| Type | Description |
|---|---|
NebulaFit
|
The |
NebulaFit
|
input order. Its |
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 |
References
He et al., Communications Biology 4, 629, 2021.
Examples: