Plot projection

This example shows how to project a transition matrix onto a low-dimensional embedding.

import scvelo as scv
import cellrank as cr

adata = cr.datasets.pancreas_preprocessed("../example.h5ad")
adata

Out:

  0%|          | 0.00/140M [00:00<?, ?B/s]
  0%|          | 56.0k/140M [00:00<06:32, 375kB/s]
  0%|          | 240k/140M [00:00<02:48, 872kB/s]
  1%|          | 1.02M/140M [00:00<00:50, 2.89MB/s]
  3%|2         | 3.58M/140M [00:00<00:16, 8.60MB/s]
  5%|4         | 6.58M/140M [00:00<00:10, 12.8MB/s]
  7%|6         | 9.59M/140M [00:00<00:08, 15.4MB/s]
  9%|8         | 12.6M/140M [00:01<00:07, 17.0MB/s]
 11%|#1        | 15.6M/140M [00:01<00:07, 18.1MB/s]
 13%|#3        | 18.6M/140M [00:01<00:06, 18.8MB/s]
 15%|#5        | 21.6M/140M [00:01<00:06, 19.3MB/s]
 18%|#7        | 24.6M/140M [00:01<00:06, 19.6MB/s]
 20%|#9        | 27.6M/140M [00:01<00:05, 21.1MB/s]
 21%|##        | 29.2M/140M [00:01<00:05, 19.5MB/s]
 23%|##2       | 32.2M/140M [00:02<00:05, 19.8MB/s]
 25%|##5       | 35.2M/140M [00:02<00:05, 20.0MB/s]
 27%|##7       | 38.2M/140M [00:02<00:04, 21.4MB/s]
 28%|##8       | 39.7M/140M [00:02<00:05, 19.7MB/s]
 30%|###       | 42.7M/140M [00:02<00:04, 21.4MB/s]
 32%|###1      | 44.2M/140M [00:02<00:05, 19.6MB/s]
 34%|###3      | 47.2M/140M [00:02<00:04, 21.3MB/s]
 35%|###4      | 48.7M/140M [00:02<00:04, 19.5MB/s]
 37%|###6      | 51.7M/140M [00:03<00:04, 21.3MB/s]
 38%|###7      | 53.2M/140M [00:03<00:04, 19.4MB/s]
 40%|####      | 56.2M/140M [00:03<00:04, 21.3MB/s]
 41%|####1     | 57.8M/140M [00:03<00:04, 19.4MB/s]
 43%|####3     | 60.8M/140M [00:03<00:04, 19.7MB/s]
 45%|####5     | 63.8M/140M [00:03<00:04, 20.0MB/s]
 48%|####7     | 66.8M/140M [00:03<00:03, 21.5MB/s]
 49%|####8     | 68.3M/140M [00:03<00:03, 19.7MB/s]
 51%|#####     | 71.3M/140M [00:04<00:03, 21.4MB/s]
 52%|#####1    | 72.8M/140M [00:04<00:03, 19.6MB/s]
 54%|#####4    | 75.8M/140M [00:04<00:03, 21.3MB/s]
 55%|#####5    | 77.3M/140M [00:04<00:03, 19.5MB/s]
 57%|#####7    | 80.3M/140M [00:04<00:02, 21.3MB/s]
 58%|#####8    | 81.8M/140M [00:04<00:03, 19.4MB/s]
 61%|######    | 84.8M/140M [00:04<00:02, 19.8MB/s]
 63%|######2   | 87.8M/140M [00:04<00:02, 20.0MB/s]
 65%|######4   | 90.8M/140M [00:05<00:02, 21.5MB/s]
 66%|######5   | 92.4M/140M [00:05<00:02, 19.7MB/s]
 68%|######8   | 95.4M/140M [00:05<00:02, 19.9MB/s]
 70%|#######   | 98.4M/140M [00:05<00:02, 20.1MB/s]
 72%|#######2  | 101M/140M [00:05<00:01, 21.5MB/s]
 73%|#######3  | 103M/140M [00:05<00:01, 19.8MB/s]
 76%|#######5  | 106M/140M [00:05<00:01, 21.4MB/s]
 77%|#######6  | 107M/140M [00:05<00:01, 19.6MB/s]
 79%|#######8  | 110M/140M [00:06<00:01, 21.4MB/s]
 80%|#######9  | 112M/140M [00:06<00:01, 19.5MB/s]
 82%|########1 | 115M/140M [00:06<00:01, 21.3MB/s]
 83%|########3 | 116M/140M [00:06<00:01, 19.5MB/s]
 85%|########5 | 119M/140M [00:06<00:01, 21.3MB/s]
 86%|########6 | 121M/140M [00:06<00:01, 19.4MB/s]
 88%|########8 | 124M/140M [00:06<00:00, 19.7MB/s]
 91%|######### | 127M/140M [00:06<00:00, 21.4MB/s]
 92%|#########1| 128M/140M [00:07<00:00, 19.6MB/s]
 94%|#########3| 131M/140M [00:07<00:00, 21.4MB/s]
 95%|#########4| 133M/140M [00:07<00:00, 19.5MB/s]
 97%|#########7| 136M/140M [00:07<00:00, 21.3MB/s]
 98%|#########8| 138M/140M [00:07<00:00, 19.4MB/s]
100%|##########| 140M/140M [00:07<00:00, 19.3MB/s]

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 create the kernel using the high-level function cellrank.tl.transition_matrix(), but any instance of cellrank.tl.kernels.Kernel would do.

k = cr.tl.transition_matrix(
    adata, weight_connectivities=0.2, softmax_scale=4, show_progress_bar=False
)
k

Out:

/home/docs/checkouts/readthedocs.org/user_builds/cellrank/checkouts/stable/examples/kernels/plot_projection.py:17: DeprecationWarning: `cellrank.tl.transition_matrix` will be removed in version `2.0`. Please use the `cellrank.kernels` or `cellrank.estimators` interface instead.
  k = cr.tl.transition_matrix(

((0.8 * <VelocityKernel>) + (0.2 * <ConnectivityKernel>))

Next, we compute the projection of the transition matrix onto an embedding, in this case UMAP. The projected transition matrix can be found in adata.obsm['T_fwd_umap'].

k.compute_projection(basis="umap")

The projected transition matrix can now be visualized using scvelo. Below we use scvelo.pl.velocity_embedding_stream() to visualize it as streamlines.

scv.pl.velocity_embedding_stream(adata, vkey="T_fwd", basis="umap")
clusters

Another way of visualizing the transition matrix is to simulate random walks on Markov chain and plot them in an embedding, see Plot random walks for more information.

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

Estimated memory usage: 417 MB

Gallery generated by Sphinx-Gallery