Plot cluster lineage

This example shows how to cluster and plot genes in a specific lineage.

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 absorption probabilities and select a model that will be used for gene trend smoothing.

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)

model = cr.ul.models.GAM(adata)

Out:

INFO: Using pre-computed schur decomposition

Next, we can fit the model for some subset of genes for a specific lineage, as seen below. After the model has been fitted, we use it to get the smoothed gene expression for the test points (by default, it is 200 points uniformly spaced along the pseudotime). Afterwards, we reduce the dimension using PCA and cluster using the louvain algorithm.

Note that calling this function twice will use the already computed values, unless recompute=True is specified.

cr.pl.cluster_lineage(
    adata,
    model,
    adata.var_names[:200],
    lineage="Alpha",
    time_key="dpt_pseudotime",
    show_progress_bar=False,
)
Cluster 0, Cluster 1, Cluster 2, Cluster 3, Cluster 4, Cluster 5, Cluster 6

Out:

  0%|          | 0/200 [00:00<?, ?gene/s]
  0%|          | 1/200 [00:00<00:37,  5.35gene/s]
  1%|1         | 2/200 [00:00<00:31,  6.28gene/s]
  2%|1         | 3/200 [00:00<00:30,  6.49gene/s]
  2%|2         | 4/200 [00:00<00:28,  6.86gene/s]
  2%|2         | 5/200 [00:00<00:28,  6.89gene/s]
  3%|3         | 6/200 [00:00<00:29,  6.54gene/s]
  4%|3         | 7/200 [00:01<00:28,  6.66gene/s]
  4%|4         | 8/200 [00:01<00:28,  6.70gene/s]
  4%|4         | 9/200 [00:01<00:29,  6.40gene/s]
  5%|5         | 10/200 [00:01<00:29,  6.50gene/s]
  6%|5         | 11/200 [00:01<00:28,  6.74gene/s]
  6%|6         | 13/200 [00:01<00:26,  6.99gene/s]
  7%|7         | 14/200 [00:02<00:28,  6.63gene/s]
  8%|7         | 15/200 [00:02<00:27,  6.69gene/s]
  8%|8         | 16/200 [00:02<00:26,  6.99gene/s]
  8%|8         | 17/200 [00:02<00:35,  5.10gene/s]
  9%|9         | 18/200 [00:02<00:32,  5.56gene/s]
 10%|9         | 19/200 [00:03<00:31,  5.78gene/s]
 10%|#         | 20/200 [00:03<00:30,  5.91gene/s]
 10%|#         | 21/200 [00:03<00:29,  6.00gene/s]
 11%|#1        | 22/200 [00:03<00:30,  5.83gene/s]
 12%|#1        | 23/200 [00:03<00:30,  5.82gene/s]
 12%|#2        | 24/200 [00:03<00:29,  5.94gene/s]
 12%|#2        | 25/200 [00:03<00:27,  6.26gene/s]
 13%|#3        | 26/200 [00:04<00:26,  6.65gene/s]
 14%|#3        | 27/200 [00:04<00:25,  6.84gene/s]
 14%|#4        | 28/200 [00:04<00:27,  6.31gene/s]
 14%|#4        | 29/200 [00:04<00:29,  5.85gene/s]
 16%|#5        | 31/200 [00:04<00:28,  5.87gene/s]
 16%|#6        | 32/200 [00:05<00:27,  6.03gene/s]
 16%|#6        | 33/200 [00:05<00:26,  6.41gene/s]
 17%|#7        | 34/200 [00:05<00:25,  6.50gene/s]
 18%|#7        | 35/200 [00:05<00:24,  6.66gene/s]
 18%|#8        | 36/200 [00:05<00:24,  6.74gene/s]
 18%|#8        | 37/200 [00:05<00:24,  6.73gene/s]
 19%|#9        | 38/200 [00:06<00:35,  4.53gene/s]
 20%|##        | 40/200 [00:06<00:28,  5.69gene/s]
 20%|##        | 41/200 [00:06<00:26,  5.92gene/s]
 21%|##1       | 42/200 [00:06<00:25,  6.12gene/s]
 22%|##1       | 43/200 [00:06<00:24,  6.41gene/s]
 22%|##2       | 44/200 [00:07<00:23,  6.52gene/s]
 22%|##2       | 45/200 [00:07<00:24,  6.38gene/s]
 23%|##3       | 46/200 [00:07<00:23,  6.68gene/s]
 24%|##4       | 48/200 [00:07<00:21,  7.16gene/s]
 24%|##4       | 49/200 [00:07<00:21,  7.06gene/s]
 26%|##5       | 51/200 [00:08<00:32,  4.60gene/s]
 26%|##6       | 53/200 [00:08<00:28,  5.15gene/s]
 27%|##7       | 54/200 [00:08<00:27,  5.32gene/s]
 28%|##7       | 55/200 [00:09<00:25,  5.69gene/s]
 28%|##8       | 57/200 [00:09<00:22,  6.22gene/s]
 30%|##9       | 59/200 [00:09<00:20,  6.93gene/s]
 30%|###       | 60/200 [00:09<00:20,  6.84gene/s]
 30%|###       | 61/200 [00:10<00:28,  4.94gene/s]
 31%|###1      | 62/200 [00:10<00:25,  5.43gene/s]
 32%|###1      | 63/200 [00:10<00:23,  5.84gene/s]
 32%|###2      | 64/200 [00:10<00:21,  6.20gene/s]
 32%|###2      | 65/200 [00:10<00:20,  6.58gene/s]
 33%|###3      | 66/200 [00:10<00:21,  6.30gene/s]
 34%|###3      | 67/200 [00:10<00:20,  6.55gene/s]
 34%|###4      | 68/200 [00:11<00:19,  6.71gene/s]
 34%|###4      | 69/200 [00:11<00:18,  7.00gene/s]
 35%|###5      | 70/200 [00:11<00:18,  7.05gene/s]
 36%|###5      | 71/200 [00:11<00:20,  6.37gene/s]
 36%|###6      | 72/200 [00:11<00:19,  6.58gene/s]
 36%|###6      | 73/200 [00:11<00:18,  6.74gene/s]
 37%|###7      | 74/200 [00:12<00:28,  4.47gene/s]
 38%|###7      | 75/200 [00:12<00:26,  4.76gene/s]
 38%|###8      | 76/200 [00:12<00:23,  5.28gene/s]
 38%|###8      | 77/200 [00:12<00:22,  5.50gene/s]
 39%|###9      | 78/200 [00:13<00:28,  4.32gene/s]
 40%|###9      | 79/200 [00:13<00:25,  4.73gene/s]
 40%|####      | 80/200 [00:13<00:22,  5.33gene/s]
 40%|####      | 81/200 [00:13<00:20,  5.81gene/s]
 41%|####1     | 82/200 [00:13<00:19,  6.01gene/s]
 42%|####1     | 83/200 [00:13<00:22,  5.21gene/s]
 42%|####2     | 84/200 [00:13<00:20,  5.74gene/s]
 42%|####2     | 85/200 [00:14<00:18,  6.07gene/s]
 43%|####3     | 86/200 [00:14<00:17,  6.35gene/s]
 44%|####3     | 87/200 [00:14<00:17,  6.31gene/s]
 44%|####4     | 88/200 [00:14<00:19,  5.83gene/s]
 44%|####4     | 89/200 [00:14<00:17,  6.26gene/s]
 45%|####5     | 90/200 [00:14<00:17,  6.35gene/s]
 46%|####5     | 91/200 [00:15<00:16,  6.45gene/s]
 46%|####6     | 92/200 [00:15<00:16,  6.58gene/s]
 46%|####6     | 93/200 [00:15<00:16,  6.49gene/s]
 48%|####7     | 95/200 [00:15<00:13,  7.82gene/s]
 48%|####8     | 96/200 [00:15<00:13,  7.63gene/s]
 48%|####8     | 97/200 [00:15<00:13,  7.43gene/s]
 49%|####9     | 98/200 [00:15<00:13,  7.42gene/s]
 50%|####9     | 99/200 [00:16<00:15,  6.56gene/s]
 50%|#####     | 100/200 [00:16<00:14,  6.91gene/s]
 50%|#####     | 101/200 [00:16<00:14,  6.70gene/s]
 51%|#####1    | 102/200 [00:16<00:14,  6.80gene/s]
 52%|#####1    | 103/200 [00:16<00:14,  6.91gene/s]
 52%|#####2    | 104/200 [00:16<00:13,  6.87gene/s]
 52%|#####2    | 105/200 [00:17<00:13,  6.95gene/s]
 53%|#####3    | 106/200 [00:17<00:13,  6.94gene/s]
 54%|#####3    | 107/200 [00:17<00:13,  6.89gene/s]
 54%|#####4    | 108/200 [00:17<00:13,  6.84gene/s]
 55%|#####4    | 109/200 [00:17<00:13,  6.65gene/s]
 55%|#####5    | 110/200 [00:17<00:13,  6.65gene/s]
 56%|#####5    | 111/200 [00:17<00:13,  6.72gene/s]
 56%|#####6    | 112/200 [00:18<00:12,  6.96gene/s]
 57%|#####6    | 114/200 [00:18<00:11,  7.48gene/s]
 57%|#####7    | 115/200 [00:18<00:11,  7.39gene/s]
 58%|#####8    | 116/200 [00:18<00:11,  7.04gene/s]
 59%|#####8    | 118/200 [00:18<00:11,  7.29gene/s]
 60%|#####9    | 119/200 [00:19<00:11,  7.15gene/s]
 60%|######    | 120/200 [00:19<00:11,  6.89gene/s]
 60%|######    | 121/200 [00:19<00:11,  6.80gene/s]
 61%|######1   | 122/200 [00:19<00:11,  6.74gene/s]
 62%|######1   | 123/200 [00:19<00:11,  6.70gene/s]
 62%|######2   | 124/200 [00:20<00:20,  3.67gene/s]
 62%|######2   | 125/200 [00:20<00:26,  2.83gene/s]
 63%|######3   | 126/200 [00:21<00:23,  3.14gene/s]
 64%|######3   | 127/200 [00:21<00:19,  3.80gene/s]
 64%|######4   | 128/200 [00:21<00:16,  4.46gene/s]
 64%|######4   | 129/200 [00:21<00:14,  4.82gene/s]
 65%|######5   | 130/200 [00:21<00:13,  5.08gene/s]
 66%|######5   | 131/200 [00:21<00:12,  5.41gene/s]
 66%|######6   | 132/200 [00:21<00:11,  5.87gene/s]
 66%|######6   | 133/200 [00:22<00:10,  6.16gene/s]
 67%|######7   | 134/200 [00:22<00:16,  3.92gene/s]
 68%|######7   | 135/200 [00:22<00:14,  4.56gene/s]
 68%|######8   | 137/200 [00:22<00:11,  5.58gene/s]
 69%|######9   | 138/200 [00:23<00:10,  5.98gene/s]
 70%|######9   | 139/200 [00:23<00:12,  4.71gene/s]
 70%|#######   | 140/200 [00:23<00:11,  5.14gene/s]
 70%|#######   | 141/200 [00:23<00:10,  5.67gene/s]
 71%|#######1  | 142/200 [00:23<00:09,  6.06gene/s]
 72%|#######1  | 143/200 [00:23<00:09,  6.26gene/s]
 72%|#######2  | 144/200 [00:24<00:09,  5.95gene/s]
 72%|#######2  | 145/200 [00:24<00:08,  6.35gene/s]
 73%|#######3  | 146/200 [00:24<00:12,  4.29gene/s]
 74%|#######3  | 147/200 [00:24<00:10,  4.87gene/s]
 74%|#######4  | 148/200 [00:25<00:10,  5.03gene/s]
 74%|#######4  | 149/200 [00:25<00:09,  5.40gene/s]
 75%|#######5  | 150/200 [00:25<00:08,  5.64gene/s]
 76%|#######5  | 151/200 [00:25<00:08,  5.85gene/s]
 76%|#######6  | 152/200 [00:25<00:07,  6.34gene/s]
 76%|#######6  | 153/200 [00:25<00:07,  6.45gene/s]
 78%|#######7  | 155/200 [00:26<00:06,  7.00gene/s]
 78%|#######8  | 157/200 [00:26<00:05,  7.62gene/s]
 79%|#######9  | 158/200 [00:26<00:05,  7.48gene/s]
 80%|#######9  | 159/200 [00:26<00:05,  7.32gene/s]
 80%|########  | 160/200 [00:26<00:05,  7.39gene/s]
 80%|########  | 161/200 [00:26<00:05,  7.20gene/s]
 81%|########1 | 162/200 [00:26<00:05,  6.97gene/s]
 82%|########2 | 164/200 [00:27<00:04,  7.47gene/s]
 82%|########2 | 165/200 [00:27<00:04,  7.28gene/s]
 83%|########2 | 166/200 [00:27<00:04,  7.19gene/s]
 84%|########3 | 167/200 [00:27<00:04,  7.18gene/s]
 84%|########4 | 169/200 [00:27<00:04,  7.74gene/s]
 85%|########5 | 170/200 [00:28<00:04,  7.43gene/s]
 86%|########5 | 171/200 [00:28<00:04,  6.37gene/s]
 86%|########6 | 172/200 [00:28<00:04,  6.33gene/s]
 86%|########6 | 173/200 [00:28<00:04,  6.59gene/s]
 87%|########7 | 174/200 [00:28<00:03,  6.70gene/s]
 88%|########7 | 175/200 [00:28<00:03,  6.79gene/s]
 88%|########8 | 176/200 [00:28<00:03,  6.95gene/s]
 89%|########9 | 178/200 [00:29<00:03,  7.25gene/s]
 90%|########9 | 179/200 [00:29<00:02,  7.19gene/s]
 90%|######### | 180/200 [00:29<00:02,  7.13gene/s]
 90%|######### | 181/200 [00:29<00:02,  7.03gene/s]
 91%|#########1| 182/200 [00:29<00:02,  7.00gene/s]
 92%|#########1| 183/200 [00:29<00:02,  6.72gene/s]
 92%|#########2| 184/200 [00:30<00:02,  6.49gene/s]
 93%|#########3| 186/200 [00:30<00:02,  6.68gene/s]
 94%|#########3| 187/200 [00:30<00:02,  6.41gene/s]
 94%|#########4| 189/200 [00:30<00:01,  7.12gene/s]
 95%|#########5| 190/200 [00:31<00:01,  6.84gene/s]
 96%|#########5| 191/200 [00:31<00:01,  6.92gene/s]
 96%|#########6| 192/200 [00:31<00:01,  6.79gene/s]
 96%|#########6| 193/200 [00:31<00:01,  6.83gene/s]
 97%|#########7| 194/200 [00:31<00:00,  6.86gene/s]
 98%|#########7| 195/200 [00:31<00:00,  6.95gene/s]
 98%|#########8| 196/200 [00:31<00:00,  6.99gene/s]
 99%|#########9| 198/200 [00:32<00:00,  7.15gene/s]
100%|#########9| 199/200 [00:32<00:00,  6.68gene/s]
100%|##########| 200/200 [00:32<00:00,  6.75gene/s]
100%|##########| 200/200 [00:32<00:00,  6.16gene/s]

The clustered genes can be accessed as shown below. In general, adata.uns['lineage_..._trend'] contains anndata object of shape (n_test_points, n_genes).

adata.uns["lineage_Alpha_trend"].obs["clusters"]

Out:

Snhg6      4
Ncoa2      0
Stau2      5
Uggt1      0
Tmem131    5
          ..
Ank3       0
Zwint      1
Gnaz       0
Rab36      0
Bcr        5
Name: clusters, Length: 200, dtype: category
Categories (7, object): ['0', '1', '2', '3', '4', '5', '6']

Total running time of the script: ( 1 minutes 18.199 seconds)

Estimated memory usage: 622 MB

Gallery generated by Sphinx-Gallery