Plot aggregated cellular fates

This example shows how to aggregate fate probabilities from the single cell level to a cluster level and how to visualize these in various ways.

import scanpy as sc

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 and the lineages, as well as scanpy’s PAGA [Wolf19].

cr.tl.terminal_states(
    adata,
    cluster_key="clusters",
    weight_connectivities=0.2,
    n_states=3,
    softmax_scale=4,
    show_progress_bar=False,
)
cr.tl.lineages(adata)

sc.tl.paga(adata, "clusters")

Out:

  0%|          | 0/90 [00:00<?, ?/s]
  7%|6         | 6/90 [00:00<00:01, 47.07/s]
 16%|#5        | 14/90 [00:00<00:01, 53.82/s]
 24%|##4       | 22/90 [00:00<00:01, 56.50/s]
 33%|###3      | 30/90 [00:00<00:01, 57.70/s]
 42%|####2     | 38/90 [00:00<00:00, 58.65/s]
 51%|#####1    | 46/90 [00:00<00:00, 58.97/s]
 60%|######    | 54/90 [00:00<00:00, 59.21/s]
 69%|######8   | 62/90 [00:01<00:00, 57.99/s]
 78%|#######7  | 70/90 [00:01<00:00, 52.91/s]
 86%|########5 | 77/90 [00:01<00:00, 50.52/s]
 93%|#########3| 84/90 [00:01<00:00, 48.71/s]
100%|##########| 90/90 [00:01<00:00, 52.87/s]

We can visualize the aggregate absorption probabilities as a bar plot. The whiskers correspond to the standard error of the mean.

cr.pl.cluster_fates(adata, mode="bar")
Ngn3 low EP, Ngn3 high EP, Fev+, Beta, Alpha, Delta, Epsilon

Out:

/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/seaborn/cm.py:1582: UserWarning: Trying to register the cmap 'rocket' which already exists.
  mpl_cm.register_cmap(_name, _cmap)
/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/seaborn/cm.py:1583: UserWarning: Trying to register the cmap 'rocket_r' which already exists.
  mpl_cm.register_cmap(_name + "_r", _cmap_r)
/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/seaborn/cm.py:1582: UserWarning: Trying to register the cmap 'mako' which already exists.
  mpl_cm.register_cmap(_name, _cmap)
/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/seaborn/cm.py:1583: UserWarning: Trying to register the cmap 'mako_r' which already exists.
  mpl_cm.register_cmap(_name + "_r", _cmap_r)
/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/seaborn/cm.py:1582: UserWarning: Trying to register the cmap 'icefire' which already exists.
  mpl_cm.register_cmap(_name, _cmap)
/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/seaborn/cm.py:1583: UserWarning: Trying to register the cmap 'icefire_r' which already exists.
  mpl_cm.register_cmap(_name + "_r", _cmap_r)
/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/seaborn/cm.py:1582: UserWarning: Trying to register the cmap 'vlag' which already exists.
  mpl_cm.register_cmap(_name, _cmap)
/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/seaborn/cm.py:1583: UserWarning: Trying to register the cmap 'vlag_r' which already exists.
  mpl_cm.register_cmap(_name + "_r", _cmap_r)
/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/seaborn/cm.py:1582: UserWarning: Trying to register the cmap 'flare' which already exists.
  mpl_cm.register_cmap(_name, _cmap)
/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/seaborn/cm.py:1583: UserWarning: Trying to register the cmap 'flare_r' which already exists.
  mpl_cm.register_cmap(_name + "_r", _cmap_r)
/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/seaborn/cm.py:1582: UserWarning: Trying to register the cmap 'crest' which already exists.
  mpl_cm.register_cmap(_name, _cmap)
/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/seaborn/cm.py:1583: UserWarning: Trying to register the cmap 'crest_r' which already exists.
  mpl_cm.register_cmap(_name + "_r", _cmap_r)

Similarly, the aggregate information can be visualized using a heatmap or a clustermap.

cr.pl.cluster_fates(adata, mode="heatmap")
cr.pl.cluster_fates(adata, mode="clustermap")
  • average fate per cluster
  • average fate per cluster

Violin plots are helpful to visualize the distribution of fate probabilities per cluster. It is also possible to restrict this plot only to a subset of clusters using the clusters parameter.

cr.pl.cluster_fates(adata, mode="violin", cluster_key="clusters")
to Epsilon, to Alpha, to Beta

We can also plot the PAGA graph, using an UMAP embedding while visualizing the aggregate absorption probabilities as pie charts.

However, using scvelo.tl.paga() and cellrank’s initial and terminal state probabilities, we can also visualize a directed version of PAGA, see Plot directed PAGA.

cr.pl.cluster_fates(adata, mode="paga_pie", basis="umap", cluster_key="clusters")
clusters

Out:

WARNING: transitions_confidence not found, using connectivites instead.
/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/scvelo/plotting/paga.py:1090: MatplotlibDeprecationWarning:
The inverse_transformed function was deprecated in Matplotlib 3.3 and will be removed two minor releases later. Use transformed(transform.inverted()) instead.
  bboxes.append(result.inverse_transformed(ax.transData))
/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/scvelo/plotting/paga.py:1090: MatplotlibDeprecationWarning:
The inverse_transformed function was deprecated in Matplotlib 3.3 and will be removed two minor releases later. Use transformed(transform.inverted()) instead.
  bboxes.append(result.inverse_transformed(ax.transData))
/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/scvelo/plotting/paga.py:1090: MatplotlibDeprecationWarning:
The inverse_transformed function was deprecated in Matplotlib 3.3 and will be removed two minor releases later. Use transformed(transform.inverted()) instead.
  bboxes.append(result.inverse_transformed(ax.transData))
/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/scvelo/plotting/paga.py:1090: MatplotlibDeprecationWarning:
The inverse_transformed function was deprecated in Matplotlib 3.3 and will be removed two minor releases later. Use transformed(transform.inverted()) instead.
  bboxes.append(result.inverse_transformed(ax.transData))
/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/scvelo/plotting/paga.py:1090: MatplotlibDeprecationWarning:
The inverse_transformed function was deprecated in Matplotlib 3.3 and will be removed two minor releases later. Use transformed(transform.inverted()) instead.
  bboxes.append(result.inverse_transformed(ax.transData))
/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/scvelo/plotting/paga.py:1090: MatplotlibDeprecationWarning:
The inverse_transformed function was deprecated in Matplotlib 3.3 and will be removed two minor releases later. Use transformed(transform.inverted()) instead.
  bboxes.append(result.inverse_transformed(ax.transData))
/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/scvelo/plotting/paga.py:1090: MatplotlibDeprecationWarning:
The inverse_transformed function was deprecated in Matplotlib 3.3 and will be removed two minor releases later. Use transformed(transform.inverted()) instead.
  bboxes.append(result.inverse_transformed(ax.transData))
/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/scvelo/plotting/paga.py:1045: MatplotlibDeprecationWarning: normalize=None does not normalize if the sum is less than 1 but this behavior is deprecated since 3.3 until two minor releases later. After the deprecation period the default value will be normalize=True. To prevent normalization pass normalize=False
  pie_axs[count].pie(fracs, colors=color_single, wedgeprops=wedgeprops)
/home/docs/checkouts/readthedocs.org/user_builds/cellrank/envs/latest/lib/python3.8/site-packages/scvelo/plotting/paga.py:1045: MatplotlibDeprecationWarning: normalize=None does not normalize if the sum is less than 1 but this behavior is deprecated since 3.3 until two minor releases later. After the deprecation period the default value will be normalize=True. To prevent normalization pass normalize=False
  pie_axs[count].pie(fracs, colors=color_single, wedgeprops=wedgeprops)

Lastly, we can visualize the absorption probabilities in PAGA graph by coloring the nodes.

cr.pl.cluster_fates(adata, mode="paga", legend_loc="on data", basis="umap")
to Epsilon, to Alpha, to Beta

Out:

WARNING: transitions_confidence not found, using connectivites instead.
WARNING: transitions_confidence not found, using connectivites instead.
WARNING: transitions_confidence not found, using connectivites instead.

Total running time of the script: ( 0 minutes 28.016 seconds)

Estimated memory usage: 714 MB

Gallery generated by Sphinx-Gallery