Palantir Basic Tutorial

Table of contents

  • Introduction

  • Loading data

  • Data Processing

  • Running Palantir

  • Visualizing Palantir results

  • Gene expression trends

  • Clustering of gene expression trends

Introduction

Palantir is an algorithm to align cells along differentiation trajectories. Palantir models differentiation as a stochastic process where stem cells differentiate to terminally differentiated cells by a series of steps through a low dimensional phenotypic manifold. Palantir effectively captures the continuity in cell states and the stochasticity in cell fate determination.

See our manuscript for more details.

Imports

[1]:
import palantir
import scanpy as sc
import pandas as pd
import os

# Plotting
import matplotlib
import matplotlib.pyplot as plt

# warnings
import warnings
from numba.core.errors import NumbaDeprecationWarning

warnings.filterwarnings(action="ignore", category=NumbaDeprecationWarning)
warnings.filterwarnings(
    action="ignore", module="scanpy", message="No data for colormapping"
)

# Inline plotting
%matplotlib inline

Loading data

We recommend the use of scanpy Anndata objects as the preferred mode of loading and filtering data.

A sample RNA-seq csv data is available at . This sample data will be used to demonstrate the utilization and capabilities of the Palantir package. This dataset contains ~4k cells and ~16k genes and is pre-filtered. Check the scanpy introductory tutorial for filtering cells and genes.

[2]:
# Load sample data
data_dir = os.path.expanduser("./")
download_url = "https://dp-lab-data-public.s3.amazonaws.com/palantir/marrow_sample_scseq_counts.h5ad"
file_path = os.path.join(data_dir, "marrow_sample_scseq_counts.h5ad")
ad = sc.read(file_path, backup_url=download_url)
ad
[2]:
AnnData object with n_obs × n_vars = 4142 × 16106

NOTE: Counts are assumed to the normalized. If you have already normalized the data, skip past the Normalization section

Data processing

Normalization

Normalize the data for molecule count distribution using the scanpy interface

[3]:
sc.pp.normalize_per_cell(ad)

We recommend that the data be log transformed. Note that, some datasets show better signal in the linear scale while others show stronger signal in the log scale.

The function below uses a pseudocount of 0.1 instead of 1.

[4]:
palantir.preprocess.log_transform(ad)

Highly variable gene selection

Highly variable gene selection can also be performed using the scanpy interface

[5]:
sc.pp.highly_variable_genes(ad, n_top_genes=1500, flavor="cell_ranger")

PCA

PCA is the first step in data processing for Palantir. This representation is necessary to overcome the extensive dropouts that are pervasive in single cell RNA-seq data.

Rather than use a fixed number of PCs, we recommend the use of components that explain 85% of the variance in the data after highly variable gene selection.

[6]:
# Note in the manuscript, we did not use highly variable genes but scanpy by default uses only highly variable genes
sc.pp.pca(ad)
[7]:
ad
[7]:
AnnData object with n_obs × n_vars = 4142 × 16106
    obs: 'n_counts'
    var: 'highly_variable', 'means', 'dispersions', 'dispersions_norm'
    uns: 'hvg', 'pca'
    obsm: 'X_pca'
    varm: 'PCs'

Diffusion maps

Palantir next determines the diffusion maps of the data as an estimate of the low dimensional phenotypic manifold of the data.

[8]:
# Run diffusion maps
dm_res = palantir.utils.run_diffusion_maps(ad, n_components=5)

The low dimensional embeddeing of the data is estimated based on the eigen gap using the following function

[9]:
ms_data = palantir.utils.determine_multiscale_space(ad)

If you are specifying the number of eigen vectors manually in the above step, please ensure that the specified parameter is > 2

Visualization

In the manuscript, we used tSNE projection using diffusion components to visualize the data. We now recommend the use of force-directed layouts for visualization of trajectories. Force-directed layouts can be computed by the same adaptive kernel used for determining diffusion maps.

scanpy can be used to compute force directed layouts. We recommened the use of the diffusion kernel (see below) for computing force directed layouts Force Atlas package should be installed for this analysis and can be installed using conda install -c conda-forge fa2. Note that fa2 is not supported by python3.9

UMAPs are a good alternative to visualize trajectories in addition to force directed layouts.

[10]:
sc.pp.neighbors(ad)
sc.tl.umap(ad)
[11]:
# Use scanpy functions to visualize umaps or FDL
sc.pl.embedding(
    ad,
    basis="umap",
    frameon=False,
)
../_images/notebooks_Palantir_sample_notebook_33_0.png

MAGIC imputation

MAGIC is an imputation technique developed in the Pe’er lab for single cell data imputation. Palantir uses MAGIC to impute the data for visualization and determining gene expression trends.

[12]:
imputed_X = palantir.utils.run_magic_imputation(ad)
/fh/fast/setty_m/user/dotto/mamba/envs/da2/lib/python3.10/site-packages/joblib/externals/loky/backend/fork_exec.py:38: RuntimeWarning: os.fork() was called. os.fork() is incompatible with multithreaded code, and JAX is multithreaded, so this will likely lead to a deadlock.
  pid = os.fork()

Gene expression can be visualized on umaps using the scanpy functions. The genes parameter is an string iterable of genes, which are a subset of the expression of column names. The below function plots the expression of HSC gene CD34, myeloid gene MPO and erythroid precursor gene GATA1 and dendritic cell gene IRF8.

[13]:
sc.pl.embedding(
    ad,
    basis="umap",
    layer="MAGIC_imputed_data",
    color=["CD34", "MPO", "GATA1", "IRF8"],
    frameon=False,
)
plt.show()
../_images/notebooks_Palantir_sample_notebook_38_0.png

Diffusion maps visualization

The computed diffusion components can be visualized with the following snippet.

[14]:
palantir.plot.plot_diffusion_components(ad)
plt.show()
../_images/notebooks_Palantir_sample_notebook_41_0.png

Running Palantir

Palantir can be run by specifying an approxiate early cell.

Palantir can automatically determine the terminal states as well. In this dataset, we know the terminal states and we will set them using the terminal_states parameter

The start cell for this dataset was chosen based on high expression of CD34.

[15]:
terminal_states = pd.Series(
    ["DC", "Mono", "Ery"],
    index=["Run5_131097901611291", "Run5_134936662236454", "Run4_200562869397916"],
)

The cells can be highlighted on the UMAP map using the highlight_cells_on_umap function

[16]:
palantir.plot.highlight_cells_on_umap(ad, terminal_states)
plt.show()
../_images/notebooks_Palantir_sample_notebook_46_0.png
[17]:
start_cell = "Run5_164698952452459"
pr_res = palantir.core.run_palantir(
    ad, start_cell, num_waypoints=500, terminal_states=terminal_states
)
Sampling and flocking waypoints...
Time for determining waypoints: 0.0031865517298380534 minutes
Determining pseudotime...
Shortest path distances using 30-nearest neighbor graph...
/fh/fast/setty_m/user/dotto/mamba/envs/da2/lib/python3.10/site-packages/joblib/externals/loky/backend/fork_exec.py:38: RuntimeWarning: os.fork() was called. os.fork() is incompatible with multithreaded code, and JAX is multithreaded, so this will likely lead to a deadlock.
  pid = os.fork()
Time for shortest paths: 0.208814803759257 minutes
Iteratively refining the pseudotime...
Correlation at iteration 1: 0.9999
Entropy and branch probabilities...
Markov chain construction...
Computing fundamental matrix and absorption probabilities...
Project results to all cells...

Palantir generates the following results

  1. Pseudotime: Pseudo time ordering of each cell

  2. Terminal state probabilities: Matrix of cells X terminal states. Each entry represents the probability of the corresponding cell reaching the respective terminal state

  3. Entropy: A quantiative measure of the differentiation potential of each cell computed as the entropy of the multinomial terminal state probabilities

Visualizing Palantir results

Palantir results can be visualized on the tSNE or UMAP using the plot_palantir_results function

[18]:
palantir.plot.plot_palantir_results(ad, s=3)
plt.show()
../_images/notebooks_Palantir_sample_notebook_51_0.png

Terminal state probability distributions of individual cells can be visualized using the plot_terminal_state_probs function

[19]:
cells = [
    "Run5_164698952452459",
    "Run5_170327461775790",
    "Run4_121896095574750",
]
palantir.plot.plot_terminal_state_probs(ad, cells)
plt.show()
../_images/notebooks_Palantir_sample_notebook_53_0.png
[20]:
palantir.plot.highlight_cells_on_umap(ad, cells)
plt.show()
../_images/notebooks_Palantir_sample_notebook_54_0.png

Save results

[31]:
file_path = os.path.join(data_dir, "marrow_sample_scseq_processed.h5ad")
ad.write(file_path)
[ ]: