Note
Click here to download the full example code
Plot terminal states¶
This example shows how to compute and plot the terminal states of the cell-state transition.
CellRank can be applied to any cell-state transition, be it differentiation, regeneration, reprogramming or other.
import cellrank as cr
adata = cr.datasets.pancreas_preprocessed("../example.h5ad")
adata
Out:
AnnData object with n_obs × n_vars = 2531 × 2000
obs: 'day', 'proliferation', 'G2M_score', 'S_score', 'phase', 'clusters_coarse', 'clusters', 'clusters_fine', 'louvain_Alpha', 'louvain_Beta', 'initial_size_unspliced', 'initial_size_spliced', 'initial_size', 'n_counts', 'velocity_self_transition', 'dpt_pseudotime'
var: 'highly_variable_genes', 'gene_count_corr', 'means', 'dispersions', 'dispersions_norm', 'fit_r2', 'fit_alpha', 'fit_beta', 'fit_gamma', 'fit_t_', 'fit_scaling', 'fit_std_u', 'fit_std_s', 'fit_likelihood', 'fit_u0', 'fit_s0', 'fit_pval_steady', 'fit_steady_u', 'fit_steady_s', 'fit_variance', 'fit_alignment_scaling', 'velocity_genes'
uns: 'clusters_colors', 'clusters_fine_colors', 'diffmap_evals', 'iroot', 'louvain_Alpha_colors', 'louvain_Beta_colors', 'neighbors', 'pca', 'recover_dynamics', 'velocity_graph', 'velocity_graph_neg', 'velocity_params'
obsm: 'X_diffmap', 'X_pca', 'X_umap', 'velocity_umap'
varm: 'PCs', 'loss'
layers: 'Ms', 'Mu', 'fit_t', 'fit_tau', 'fit_tau_', 'spliced', 'unspliced', 'velocity', 'velocity_u'
obsp: 'connectivities', 'distances'
First, we compute the terminal states. By default, we’re using the cellrank.tl.estimators.GPCCA
estimator.
The parameter cluster_key
tries to associate the names of the terminal states with cluster labels, whereas
n_cells
controls how many cells we take from each terminal state as categorical observation - this is only
available to the above mentioned estimator. We can show some plots of interest by specifying show_plots=True
.
cr.tl.terminal_states(
adata,
cluster_key="clusters",
n_cells=30,
softmax_scale=4,
n_states=3,
show_progress_bar=False,
)
We can now plot the terminal states. By default, when using cellrank.tl.estimators.GPCCA
, we plot continuous
membership vectors to visualize individual cells associations with an terminal state.
cr.pl.terminal_states(adata)

Out:
/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/scvelo/plotting/utils.py:115: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
ax = pl.figure(None, figsize, dpi=dpi).gca(projection=projection)
We can also plot membership vectors for different terminal states separately state using same_plot=False
.
cr.pl.terminal_states(adata, same_plot=False)

Lastly, we can discretize the assignment of cells to terminal states by showing the cells most
likely to belong to the terminal state by specifying the discrete
parameter.
cr.pl.terminal_states(adata, discrete=True)

Out:
/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/scvelo/plotting/utils.py:115: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
ax = pl.figure(None, figsize, dpi=dpi).gca(projection=projection)
To see how to compute and plot the terminal states or the lineages, see Plot initial states or Plot lineages, respectively.
Total running time of the script: ( 0 minutes 22.811 seconds)
Estimated memory usage: 720 MB