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.
Palantir can read single cell RNA-seq data from a csv file, mtx file, 10x counts and HDF files. The csv file should be a cells X genes matrix of counts. This tutorial shows the usage with a csv file.
The following code snippet imports the palantir
package along with other plotting related imports
import palantir
# Plotting and miscellaneous imports
import os
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
A sample RNA-seq csv data is available at <palantir directory>/data/marrow_sample_scseq_counts.csv.gz
. This sample data will be used to demonstrate the utilization and capabilities of the Palantir package. This dataset contains ~4k cells and ~16k genes.
This is a subset of the data from the CD34+ cells from the human marrow, the dataset used in the manuscript. The data comprises of progenitor Hematopoeitic stem cells and cells of the erythroid and myeloid lineages.
Assuming that Palantir was downloaded at ~/repositories/palantir
, the following snipped can be used to load the counts matrix.
# Load sample data
palantir_dir = os.path.expanduser('~/repositories/palantir/')
counts = palantir.io.from_csv(palantir_dir + 'data/marrow_sample_scseq_counts.csv.gz')
For alternative file formats, please use palantir.io.from_mtx
, palantir.io.from_10x
, palantir.io.from_10x_HDF5
functions.
Palantir provides an interface for removing low molecule count cells and genes with low detection rates. The following snippet allows visualzation of molecule counts and gene detection rates
fig, ax = palantir.plot.plot_molecules_per_cell_and_gene(counts)
The dataset provided is already prefiltered. For other datasets, cells and genes can be filtered using
filtered_counts = palantir.preprocess.filter_counts_data(counts, cell_min_molecules=1000, genes_min_cells=10)
The next step is to correct for molecule count distribution. Palantir divides the counts of each cell by the total molecules detected as a normalization technique
norm_df = palantir.preprocess.normalize_counts(counts)
Data can be optionally log transformed. Note that, some datasets show better signal in the linear scale while others show stronger signal in the log scale. We use log transformation for this dataset
norm_df = palantir.preprocess.log_transform(norm_df)
The first step in data processing for Palantir is to determine metagenes using principal component analysis. This representation is necessary to overcome the extensive dropouts that are pervasive in single cell RNA-seq data
pca_projections, _ = palantir.utils.run_pca(norm_df)
Palantir next determines the diffusion maps of the data as an estimate of the low dimensional phenotypic manifold of the data.
# Run diffusion maps
dm_res = palantir.utils.run_diffusion_maps(pca_projections, n_components=5)
Determing nearest neighbor graph...
The low dimensional embeddeing of the data is estimated based on the eigen gap using the following function
ms_data = palantir.utils.determine_multiscale_space(dm_res)
Palantir constructs the tSNE map in the embedded space since these maps better represent the differentiation trajectories
tsne = palantir.utils.run_tsne(ms_data)
For consistency, the following snippet will load the precomputed tSNE
projections.
import pandas as pd
tsne = pd.read_pickle(palantir_dir + 'data/sample_tsne.p')
tSNE results can be visualized by the plot_tsne
and plot_tsne_by_cell_sizes
functions. The plot_tsne_by_cell_sizes
function colors the cells by their molecule counts before normalization
fig, ax = palantir.plot.plot_tsne(tsne)
fig, ax = palantir.plot.plot_tsne_by_cell_sizes(counts, tsne)
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.
imp_df = palantir.utils.run_magic_imputation(norm_df, dm_res)
Gene expression can be visualized on tSNE maps using the plot_gene_expression
function. 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
.
palantir.plot.plot_gene_expression(imp_df, tsne, ['CD34', 'MPO', 'GATA1', 'IRF8'])
The computed diffusion components can be visualized with the following snippet.
palantir.plot.plot_diffusion_components(tsne, dm_res)
Palantir can be run by specifying an approxiate early cell. While Palantir automatically determines the terminal states, they can also specified using the termine_states
parameter.
The start cell for this dataset was chosen based on high expression of CD34.
start_cell = 'Run5_164698952452459'
pr_res = palantir.core.run_palantir(ms_data, start_cell, num_waypoints=500)
Sampling and flocking waypoints... Time for determining waypoints: 0.002986351648966471 minutes Determining pseudotime... Shortest path distances using 30-nearest neighbor graph... Time for shortest paths: 0.06762837966283163 minutes Iteratively refining the pseudotime... Correlation at iteration 1: 0.9999 Entropy and branch probabilities... Markov chain construction... Identification of terminal states... Computing fundamental matrix and absorption probabilities... Project results to all cells...
Palantir generates the following results
The terminal states in this dataset are renamed to reflect the known biology below
mapping = pd.Series(index=['DC', 'Mono', 'Ery'])
mapping['DC'] = tsne.loc[pr_res.branch_probs.columns, 'x'].idxmax()
mapping['Ery'] = tsne.loc[pr_res.branch_probs.columns, 'y'].idxmin()
mapping['Mono'] = pr_res.branch_probs.columns.difference(mapping.values)[0]
mapping = pd.Series(mapping.index, index=mapping)
pr_res.branch_probs.columns = mapping[pr_res.branch_probs.columns]
pr_res.branch_probs = pr_res.branch_probs.loc[:, ['Ery', 'Mono', 'DC']]
Palantir results can be visualized on the tSNE map using the plot_palantir_results
function
palantir.plot.plot_palantir_results(pr_res, tsne)
Terminal state probability distributions of individual cells can be visualized using the plot_terminal_state_probs
function
cells = ['Run5_164698952452459', 'Run5_170327461775790', 'Run4_121896095574750', ]
palantir.plot.plot_terminal_state_probs(pr_res, cells)
The cells can be highlighted on the tSNE map using the highlight_cells_on_tsne
function
palantir.plot.highlight_cells_on_tsne(tsne, cells)
(<Figure size 288x288 with 1 Axes>, <matplotlib.axes._subplots.AxesSubplot at 0x11ae265f8>)
Palantir uses Generalized Additive Models (GAMs) to determine the gene expression trends along different lineages. The marker trends can be determined using the following snippet. This computes the trends for all lineages. A subset of lineages can be used using the lineages
parameter.
genes = ['CD34', 'MPO', 'GATA1', 'IRF8']
gene_trends = palantir.presults.compute_gene_trends( pr_res, imp_df.loc[:, genes])
Ery Time for processing Ery: 0.007062482833862305 minutes Mono Time for processing Mono: 0.006904701391855876 minutes DC Time for processing DC: 0.0072436690330505375 minutes
The determined trends can be visualized with the plot_gene_trends
function. A separate panel is generated for each gene
palantir.plot.plot_gene_trends(gene_trends)
Alternatively, the trends can be visualized on a heatmap using
palantir.plot.plot_gene_trend_heatmaps(gene_trends)
Cells can be clustered and visualized using Phenograph with the following snippet.
clusters = palantir.utils.determine_cell_clusters(pca_projections)
palantir.plot.plot_cell_clusters(tsne, clusters )
Similary, gene expression trends can be clustered and visualized using the following snippet. As an example, the first 1000 genes along the erythroid genes are clustered
gene_trends = palantir.presults.compute_gene_trends(pr_res,
imp_df.iloc[:, 0:1000], ['Ery'])
Ery Time for processing Ery: 0.10731716950734456 minutes
# Cluster
trends = gene_trends['Ery']['trends']
gene_clusters = palantir.presults.cluster_gene_trends(trends)
palantir.plot.plot_gene_trend_clusters(trends, gene_clusters)