scCODA: single-cell Compositional Data Analysis

repository·master·Indexed 41 days ago

https://github.com/theislab/sccoda

A tool for identifying compositional changes in high-throughput sequencing count data, specifically designed for cell compositions in scRNA-seq. It utilizes a Bayesian model for differential composition analysis, integrates with scanpy and anndata, and provides MCMC inference methods including Hamiltonian Monte Carlo (HMC) and NUTS. It also includes wrappers for other compositional methods such as ALDEx2, ANCOMBC, and SCDC.

Tokens
25K
Snippets
109
Records
142
Agent score
51%

What's inside scCODA

  1. Overview of scCODA for single-cell differential composition analysis

    master

    scCODA is a statistical toolbox designed to analyze changes in compositional data, specifically for single-cell RNA-seq experiments. It implements a fully Bayesian model (the scCODA model) to address common limitations in single-cell population analysis, such as:

    • Compositional nature of scRNA-seq data: Prevents inflation of false-positive results by accounting for the fact that cell population proportions must sum to one.
    • Small sample sizes: Uses Bayesian modeling to provide accurate results even when datasets consist of only a few samples, where frequentist tests often fail.
    • Sparse effects: Designed to identify conditions that affect only a fraction of cell types.

    scCODA is fully integrable with scanpy but uses its own specialized data structures for aggregating, plotting, and analyzing compositional data. It also includes implementations of other statistical models for comparative analysis.

  2. What is scCODA and when should I use it?

    master

    scCODA is a Bayesian modeling framework specifically designed for compositional data analysis in single-cell RNA sequencing (scRNA-seq).

    When to use scCODA:

    • Compositional Data: Use scCODA when analyzing cell population proportions. Because the total number of cells in a sample is predetermined (limited by throughput), cell populations are compositional and exhibit negative correlative bias. scCODA interprets these changes in terms of ratios relative to a reference factor.
    • Small Sample Sizes: Use scCODA when you have very few biological replicates. Unlike frequentist tests that produce uncertain estimates with large confidence intervals in low-sample settings, scCODA uses Bayesian modeling to provide accurate results.
    • Multiple Comparisons & Sparsity: Use scCODA when biological factors only affect a fraction of the total cell population. It uses spike-and-slab priors to automatically identify the most statistically credible effects, effectively handling the problem of multiple comparisons without relying on p-values.
  3. Understand the scCODA data structure

    master

    scCODA operates on compositional datasets represented by two primary matrices:

    1. Cell Count Matrix (Y): An $N imes K$ matrix where $N$ is the number of samples and $K$ is the number of cell types. Each row is a vector of integer counts for a biological sample. Note: Data does not need to be normalized; scCODA requires raw integer counts.
    2. Covariate Matrix (X): An $N imes M$ matrix containing information about each sample (e.g., disease status, age, BMI).

    scCODA uses the anndata format to store these components:

    • data.X: Stores the cell count matrix.
    • data.obs: Stores the covariates (the actual design matrix is generated during model execution).
    • data.var and data.uns: Currently unused.
  4. Understand the scCODA Bayesian model structure

    master

    scCODA implements a Dirichlet-multinomial model where cell type compositions are linked to covariates through a log-linear relationship.

    Key components:

    • Linkage: $\log(\phi) = \alpha + x \beta$, where $\phi$ is the composition, $\alpha$ are intercepts, and $\beta$ are covariate effects.
    • Priors: Intercepts $\alpha$ are modeled via a normal prior $N(0, 5)$.
    • Model Selection: For covariate effects $\beta$, scCODA uses a spike-and-slab prior (implemented via a continuous Logit-normal approximation) to identify significant changes.
    • Reference Cell Type: The effects for the reference cell type $\hat{k}$ are always constrained to $0$.
    • Scaling: Significant effects use a normal prior with a covariate-specific scaling factor $\tau$.
  5. Set up scCODA model and perform inference

    master

    The most efficient way to use scCODA is by instantiating sccoda.util.comp_ana.CompositionalAnalysis.

    Key features:

    • Formula Syntax: You can specify covariates using patsy syntax, allowing for complex transformations and combinations without manually redefining the covariate matrix.
    • Reference Cell Type: You must specify the reference cell type during this step.

    Core model classes:

    • sccoda.util.comp_ana.CompositionalAnalysis: High-level analysis wrapper.
    • sccoda.model.scCODA_model.CompositionalModel: Base model class.
    • sccoda.model.scCODA_model.scCODAModel: Specific scCODA implementation.
    # Example setup pattern
    # analysis = ana.CompositionalAnalysis(data, formula='condition ~ covariate', reference='CellTypeA')
  6. Import compositional data into scCODA

    master

    You can import count data into the scCODA anndata structure using sccoda.util.cell_composition_data. Depending on your data source, use one of the following methods:

    • From a pandas DataFrame: Use from_pandas to convert a DataFrame directly into the required format.
    • From a single Scanpy AnnData object: If all cells from all samples are in one object, use from_scanpy to generate the compositional dataset.
    • From multiple Scanpy AnnData objects (In-memory): If you have a list of AnnData objects (one per sample), use from_scanpy_list.
    • From multiple Scanpy AnnData objects (On-disk): If your sample data is stored in separate files on disk, use from_scanpy_dir.
  7. Analyze scCODA results with summary()

    master

    To identify statistically credible changes in cell type abundance, call the summary() method on your result object (the CAResult instance).

    Interpreting the output:

    • Significance: Check the Final Parameter column. A value of 0 indicates the effect is not statistically credible. Any non-zero value indicates a statistically credible effect.
    • Direction: The sign of the effect indicates whether the abundance increased (positive) or decreased (negative) relative to the chosen reference cell type.
    • Note on Magnitude: Do not use the raw numerical values of the effects for quantitative analysis, as they are derived from multiple parameters in the Bayesian model.
    # Example result analysis
    results = analysis.sample_hmc()
    summary_df = results.summary()
    print(summary_df)
    # Look for non-zero values in the 'Final Parameter' column
  8. Import scCODA for analysis

    master

    To begin a workflow, import the main package and its utility submodules for data handling, analysis, and visualization.

    Recommended import pattern:

    import sccoda
    dat = sccoda.util.cell_composition_data
    ana = sccoda.util.compositional_analysis
    viz = sccoda.util.data_visualization
    import sccoda
    dat = sccoda.util.cell_composition_data
    ana = sccoda.util.compositional_analysis
    viz = sccoda.util.data_visualization
  9. Install scCODA via pip

    master

    To install the standard version of scCODA, use pip. Ensure you have a working Python environment (>=3.8). Note that scCODA uses tensorflow (>=2.8) and tensorflow-probability (>=0.16). GPU computation features for these packages are not recommended as they have not been tested with scCODA.

    pip install sccoda
  10. Install scCODA with R functionality enabled

    master

    scCODA provides wrappers for other compositional data analysis methods written in R. To enable this functionality, it is recommended to use conda to manage an environment with r-base and r-essentials installed before installing scCODA via pip.

    conda create -n scCODA python=3.9 -y
    conda activate scCODA
    conda install -c conda-forge r-base r-essentials -y
    pip install sccoda