delnx.tl.log2fc¶

delnx.tl.log2fc(adata, condition_key, reference=None, mode='all_vs_all', layer=None, data_type='auto', min_samples=2, verbose=True)[source]¶

Calculate log2 fold changes between condition levels.

This function computes log2 fold changes (log2FC) between different experimental conditions for all features in the dataset. It supports various comparison modes, different data types, and can normalize by size factors when appropriate.

Parameters:
  • adata (AnnData) – AnnData object containing expression data and metadata.

  • condition_key (str) – Column name in adata.obs containing condition labels.

  • reference (str | tuple[str, str] | None (default: None)) – Reference condition for comparisons, specified as: - Single string: reference condition for all comparisons - Tuple (reference, comparison): specific pair to compare - None: automatically determined based on mode parameter

  • mode (Literal['all_vs_ref', 'all_vs_all', '1_vs_1', 'continuous'] (default: 'all_vs_all')) – Comparison strategy: - “all_vs_ref”: Compare all condition levels against reference level - “all_vs_all”: Compare all pairs of condition levels - “1_vs_1”: Compare only reference vs comparison (requires tuple reference)

  • layer (str | None (default: None)) – Layer in adata.layers to use for expression data. If None, uses adata.X.

  • data_type (Literal['counts', 'lognorm', 'binary', 'scaled', 'auto'] (default: 'auto')) – Type of expression data: - “auto”: Automatically infer from data characteristics - “counts”: Raw count data - “lognorm”: Log-normalized data (log1p of normalized counts) - “binary”: Binary expression data (0/1)

  • min_samples (int (default: 2)) – Minimum number of samples required per condition level. Comparisons with fewer samples are skipped.

  • verbose (bool (default: True)) – Whether to print progress information and data type inference results.

Return type:

DataFrame

Returns:

pd.DataFrame DataFrame containing log2 fold change results with columns: - “feature”: Feature/gene names - “test_condition”: Test condition label - “ref_condition”: Reference condition label - “log2fc”: Log2 fold change values (positive means up-regulated in test condition)

Examples

Basic usage with automatic data type inference:

>>> import scanpy as sc
>>> import delnx as dx
>>> adata = sc.read_h5ad("dataset.h5ad")
>>> results = dx.tl.log2fc(adata, condition_key="treatment")

Comparing specific conditions:

>>> results = dx.tl.log2fc(adata, condition_key="treatment", reference=("control", "treated"), mode="1_vs_1")