CellRank

repository·main·Indexed 19 days ago

https://github.com/scverse/cellrank

A modular framework for studying cellular dynamics through Markov state modeling of multi-view single-cell data. CellRank is used for fate mapping and trajectory inference, enabling the estimation of differentiation direction, computation of macrostates, and inference of fate probabilities and driver genes. It features a decoupled two-step workflow consisting of kernels (cellrank.kernels) to estimate transition matrices and estimators (cellrank.estimators), such as GPCCA, to analyze dynamics.

Tokens
14.8K
Snippets
44
Records
72
Agent score
66%

What's inside cellrank

  1. Overview of CellRank 2

    main
    CellRank is a modular framework designed for studying cellular dynamics. It uses Markov state modeling on multi-view single-cell data to perform fate mapping. It is built to scale to large cell numbers and is fully compatible with the [scverse] ecosystem. The backend is powered by pyGPCCA.
  2. Visualize CellRank outputs with cellrank.pl

    main

    The cellrank.pl module provides plotting functions to visualize results computed by CellRank kernels, estimators, or models. These functions are designed to interpret fate mapping, gene expression trends, and probability distributions across single-cell datasets.

    import cellrank.pl as pl
  3. Use Signals to define quantities for model fitting

    main

    Signals identify the observation-aligned quantity that a model is fit on. They are used to specify what data the model should track (e.g., a specific gene, a covariate in .obs, or a matrix in .obsm).

    You can pass Signals to cellrank.pl.gene_trends or cellrank.models.BaseModel.prepare to plot or prepare data for modeling.

    Supported Signal types:

    • Signal: The base class for signals.
    • Gene: Represents a specific gene's expression.
    • Obs: Represents a covariate found in AnnData.obs (e.g., a gene module score).
    • Obsm: Represents a column from an AnnData.obsm array.
  4. Understand the CellRank modular API architecture

    main

    CellRank's API is organized into several functional modules that work together to perform fate mapping:

    1. Kernels (cellrank.kernels): These are the foundation of the analysis. They compute cell-cell transition matrices based on various data modalities, such as RNA velocity, pseudotime, developmental potential, or experimental time points.
    2. Estimators (cellrank.estimators): These modules consume the transition matrices produced by kernels to derive biological insights. They are used to compute terminal states, initial states, fate probabilities, and driver genes. The recommended estimator for most workflows is cellrank.estimators.GPCCA.
    3. Models (cellrank.models): Used for fitting gene trends over time.
    4. Plotting (cellrank.pl): Provides visualization tools for the results of the analysis.
    5. Datasets (cellrank.datasets): Provides sample data to help you get started.
  5. Understand the CellRank workflow: Kernels and Estimators

    main

    CellRank follows a decoupled two-step modeling framework that allows you to separate the estimation of cellular transitions from the analysis of those transitions:

    1. Step 1: Estimating transitions with cellrank.kernels You use kernels to take multi-view single-cell input data and estimate a sparse transition matrix $T$. In this matrix, row $i$ contains the transition probabilities from cell $i$ towards its putative descendants. All entries are between 0 and 1, and rows sum to one.

    2. Step 2: Analyzing dynamics with cellrank.estimators You take the transition matrix $T$ produced by a kernel and apply Markov chain theory to derive biological insights. This includes identifying initial, terminal, and intermediate macrostates, and computing fate probabilities.

    Because these steps are decoupled, you can change how you derive transition probabilities (e.g., switching from RNA velocity to pseudotime) without changing how you infer terminal states or fate probabilities.

  6. Key applications of CellRank

    main

    CellRank can be used for several downstream tasks in single-cell analysis, including:

    • Estimating differentiation direction: Uses biological priors such as RNA velocity, pseudotime, developmental potential, experimental time points, or metabolic labels.
    • Computing macrostates: Identifies initial, terminal, and intermediate macrostates in cellular trajectories.
    • Inferring fates and drivers: Computes fate probabilities and identifies driver genes.
    • Visualizing trends: Visualizes and clusters gene expression trends across trajectories.
  7. Identify high-risk areas in CellRank development

    main

    When modifying the codebase, pay special attention to these high-risk components where regressions are most likely to occur:

    • Kernel composition (src/cellrank/kernels/_base_kernel.py): Watch for weight normalization issues in KernelAdd and direction flipping in bidirectional kernels.
    • AnnData serialization: Changes to write_to_adata, from_adata, or estimator shadow AnnData can break saved analyses and downstream notebooks if the round-trip behavior changes.
    • RealTimeKernel (src/cellrank/kernels/_real_time_kernel.py): This is a complex path involving assembling per-timepoint couplings (via from_moscot or from_wot) into a global block transition matrix.
    • Spectral estimators (src/cellrank/estimators/mixins/): Mixins for Schur, eigen, fate-probability, and lineage-driver are highly sensitive to correctness.
    • Lineage (src/cellrank/_utils/_lineage.py): This ndarray subclass has specific public slicing and coloring semantics.
    • Optional dependency guards: New usage of jax, moscot, petsc4py, slepc4py, rpy2, wot, scvelo, or adjusttext must use existing guards to prevent leaking into top-level imports.
    • Public API surface: Any new re-exports in src/cellrank/__init__.py or within kernels, estimators, models, pl, or datasets.
  8. Use CellRank estimators for fate mapping

    main

    Estimators in CellRank are used to perform quantitative analysis on Markov transition matrices (kernels). They enable tasks such as:

    • Automatic detection of initial and terminal states.
    • Computation of fate probabilities.

    While several estimators exist, the recommended estimator for most use cases is GPCCA.

  9. How plotting tests and ground-truth figures work

    main

    Plotting tests in tests/test_plotting.py use three strategies:

    1. Visual-regression tests: Pixel-by-pixel comparison against baselines in tests/_ground_truth_figures/. Use sparingly.
    2. Introspection tests: Assert on Figure/Axes properties (e.g., title, colormap). Preferred for parameter checks.
    3. Smoke tests: Assert that the function runs and returns a Figure. Use for non-critical paths.

    Important: Baselines must be produced by the Linux CI environment (hatch-test.py3.12-stable). Do not commit figures rendered on macOS or other OSs. To update a baseline:

    1. Push your branch and run the CI.
    2. Download the rendered-figures artifact from the Linux hatch-test.py3.12-stable job.
    3. Copy the PNGs to tests/_ground_truth_figures/ and commit them.
  10. How CellRank uses Markov chains for fate mapping

    main

    CellRank describes cellular dynamics using Markov chains, where each cell represents a state in the chain. This approach assumes:

    • Gradual transitions: Cellular state changes occur in small, molecular steps captured by the data.
    • Memoryless transitions: A state transition depends only on the current molecular state, not on the history of previous states. This allows CellRank to describe average cellular dynamics rather than individual cell histories.

    Transitions are summarized in a transition matrix $T$, where $T_{ij}$ is the probability of transitioning from state $i$ to state $j$ in one step.

  11. What are Kernels in CellRank?

    main

    In CellRank, a Kernel is a class that takes multi-view single-cell data as input and outputs a cell-cell transition matrix. These matrices represent the probabilities of transitioning from one cell to another based on various data modalities.

    Kernels are used to compute transition probabilities based on:

    • Molecular similarity
    • RNA velocity
    • Experimental time points
    • Other data modalities

    Kernels also provide methods for qualitative visualization, such as vector field or random walk plots. For quantitative analysis of the resulting transition matrices, you should use the estimators instead.

  12. Use the Estimator fit/predict workflow in CellRank 2

    main

    To accommodate various data modalities, CellRank 2 has moved away from the high-level cellrank.tl functions in favor of a modular estimator-based workflow.

    Every estimator in cellrank.estimators now implements a consistent two-step interface:

    1. .fit(): Computes macrostates (such as initial and terminal states).
    2. .predict(): Classifies macrostates as terminal states.

    This workflow is designed to be easier to use than the previous low-level interaction modes while remaining more flexible than the deprecated cellrank.tl functions.