Palantir: Modeling Continuous Cell State and Cell Fate Choices

repository·master·Indexed 43 days ago

https://github.com/dpeerlab/palantir

Palantir is an algorithm for aligning cells along differentiation trajectories in multidimensional single-cell data, such as Mass cytometry and single-cell RNA-seq. It models differentiation as a stochastic process through a low-dimensional phenotypic manifold. Version 1.4.5 provides compatibility with Python 3.12/3.13, pandas 2/3, and AnnData >= 0.11. The package includes optional GAM support for computing gene expression trends.

Tokens
15.8K
Snippets
51
Records
64
Agent score
56%

What's inside Palantir

  1. Install Palantir via conda, mamba, or micromamba

    master

    Palantir is available through the bioconda and conda-forge channels. Use the following commands depending on your package manager:

    Using conda:

    conda install -c conda-forge -c bioconda palantir

    Using mamba:

    mamba install -c conda-forge -c bioconda palantir

    Using micromamba:

    micromamba install -c conda-forge -c bioconda palantir
  2. Install Palantir with optional GAM support

    master

    Palantir allows for optional installation of pygam for computing gene expression trends. You can install the full suite of features using the [gam] or [full] extras.

    pip install palantir[gam]
    # OR
    pip install palantir[full]
    pip install palantir[gam]
  3. Compatibility and breaking changes in Palantir v1.4.5

    master

    Version 1.4.5 introduced several critical compatibility fixes and changes:

    • Pandas Compatibility: Supports both pandas 2 and 3. Fixed ValueError: assignment destination is read-only errors in run_palantir and select_branch_cells caused by pandas 3's mandatory Copy-on-Write.
    • AnnData Compatibility: Fixed TypeError in cluster_gene_trends for anndata >= 0.11 by casting arrays with .astype(np.float32) instead of using the removed dtype keyword in AnnData.__init__.
    • NumPy Randomness: Now uses numpy.random.Generator instead of the legacy numpy.random.RandomState. Requires numpy >= 1.17.
      • Note: The default PRNG is now PCG64 instead of Mersenne Twister, so numerical outputs will differ from previous versions. For exact reproducibility, pin to a prior version.
    • Python Support: Compatible with Python 3.12 and 3.13.
  4. Overview of Palantir modules

    master

    Palantir is an algorithm designed to align cells along differentiation trajectories, identify differentiation endpoints, and estimate cell-fate probabilities in single-cell data. The package is organized into the following functional modules:

    • config: Configuration settings for Palantir.
    • core: Core functions for running the Palantir algorithm.
    • presults: Class for storing and accessing Palantir results.
    • io: Input/output functions for loading and saving data.
    • preprocess: Preprocessing functions for single-cell data.
    • utils: Utility functions for analysis.
    • plot: Visualization functions.
  5. Access DPT results in AnnData

    master

    After running sc.tl.dpt, the following information is added to the AnnData object:

    • Pseudotime: Stored in ad.obs['dpt_pseudotime']. This represents the progression of cells along the diffusion trajectories.
    • Branching Groups: Stored in ad.obs['dpt_groups']. This identifies which lineage or branch a cell belongs to.

    You can use these columns to color embeddings like t-SNE (ad.obsm['X_tsne']) to visualize developmental trajectories and lineage splits.

  6. Access and analyze Scanpy anndata objects

    master

    The Palantir manuscript provides three replicates of human_cd34_bm data in .h5ad format. You can access these using scanpy in Python. Each object contains processed counts, raw counts, MAGIC-imputed data, t-SNE maps, clustering information, and Palantir-specific outputs like pseudotime, differentiation potential, and branch probabilities.

    Available Replicates

    import scanpy as sc
    
    # Read in the data, with backup URLs provided
    adata_Rep1 = sc.read(
        "../data/human_cd34_bm_rep1.h5ad",
        backup_url="https://s3.amazonaws.com/dp-lab-data-public/palantir/human_cd34_bm_rep1.h5ad",
    )
    adata_Rep2 = sc.read(
        "../data/human_cd34_bm_rep2.h5ad",
        backup_url="https://s3.amazonaws.com/dp-lab-data-public/palantir/human_cd34_bm_rep2.h5ad",
    )
    adata_Rep3 = sc.read(
        "../data/human_cd34_bm_rep3.h5ad",
        backup_url="https://s3.amazonaws.com/dp-lab-data-public/palantir/human_cd34_bm_rep3.h5ad",
    )
  7. Visualize gene expression trends along DPT paths

    master

    You can extract and visualize how specific genes change along defined developmental paths (lineages).

    1. Define Paths: Create a list of paths where each path is a tuple containing a description and a list of category indices representing the sequence of clusters/groups.
    2. Extract Data: Use sc.pl.paga_path with return_data=True to retrieve the underlying trend data for a specific path.
    3. Plot Trends: Use the returned data to plot gene expression values against the pseudotime ordering (distance) along that path.
    # Example: Extracting trend data for a path
    # path = (description, [list_of_cluster_indices])
    paths = [('Ery', [0, 5, 4]), ('Mono', [0, 3, 8, 9])]
    
    trends = pd.Series()
    for ipath, (descr, path) in enumerate(paths):
        _, data = sc.pl.paga_path(
            ad, 
            path, 
            genes,                         
            groups_key='dpt_groups', 
            return_data=True,
            show=False
        )
        trends[descr] = data
    
    # Now 'trends[descr]' contains the distance and gene expression data
  8. Integrate FateID results with Palantir

    master

    To use FateID results within the Palantir ecosystem, you must export your single-cell data to a CSV format compatible with the FateID R script, run the R analysis, and then load the exported results back into Python.

    1. Export Data: Convert your AnnData object's raw counts to a transposed CSV.
    2. Run FateID (R): Use the RaceID and FateID R packages to perform clustering and compute fate biases.
    3. Load Results: Import the resulting probs.csv (fate probabilities), tsne.csv (dimensionality reduction coordinates), and *_order.csv (pseudo-time ordering for each cell type) files into Python using pandas.
    # Exporting data for FateID
    import os
    import pandas as pd
    
    fateid_dir = 'fateid/'
    os.makedirs(fateid_dir, exist_ok=True)
    
    # Extract counts from AnnData
    counts = pd.DataFrame(ad.raw.X.todense(), index=ad.obs_names, columns=ad.var_names)
    # Transpose and save to CSV
    pd.DataFrame(counts.T).to_csv(f'{fateid_dir}/counts.csv')
  9. Compare Palantir with Monocle 2

    master

    This notebook demonstrates how to compare Palantir results with Monocle 2 by exporting data from an AnnData object, running a Monocle 2 R wrapper, and visualizing the resulting trajectories and gene expressions.

    Workflow Overview

    1. Prepare Data: Load an AnnData object and identify boundary clusters and gene subsets.
    2. Export for R: Export a count matrix (counts.csv) and a list of ordering genes (genes.csv) to a directory.
    3. Run Monocle 2: Execute an R script (monocle_wrapper.R) via Rscript to perform dimensionality reduction (DDRTree) and cell ordering.
    4. Analyze Results: Load the exported phenodata.csv and red_dims.csv back into Python to plot states and gene expression across the trajectory.
  10. Export data for Slingshot analysis

    master

    To use Slingshot, you must export Palantir's multi-scale data, cluster assignments, and gene expression matrices to a directory. The Slingshot R script expects three specific CSV files:

    • data.csv: The multi-scale data (from ms_data).
    • clusters.csv: Cluster assignments from ad.obs['clusters'].
    • exprs.csv: Gene expression matrix where rows are genes and columns are cell names.

    Note: Ensure the directory exists before exporting.

    # Export data 
    slignshot_dir = 'slingshot/'
    os.makedirs(slignshot_dir, exist_ok=True)
    
    # Genes
    # Expression matrices
    pd.DataFrame(ms_data).to_csv(f'{slignshot_dir}/data.csv', index=None, header=None)
    pd.DataFrame(ad.obs['clusters']).to_csv(f'{slignshot_dir}/clusters.csv', index=None, header=None)
    pd.DataFrame(ad[:, genes].X.T, index=genes, columns=ad.obs_names).to_csv(f'{slignshot_dir}/exprs.csv')