-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmouse_gastrulation.py
653 lines (574 loc) · 15.6 KB
/
mouse_gastrulation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
# %%
from pathlib import Path
import shutil
import sys
import time
import warnings
import numpy as np
import pandas as pd
import scvelo as scv
import torch
from umap import UMAP
from sklearn.decomposition import PCA
from matplotlib import pyplot as plt
import wandb
sys.path.append("..")
import deepvelo as dv
from deepvelo.utils import continuity_confidence, update_dict, velocity
from deepvelo.utils.scatter import scatter
from deepvelo.utils.preprocess import autoset_coeff_s
from deepvelo import train, Constants
warnings.resetwarnings()
hyperparameter_defaults = dict(
do_sweep=False,
seed=123,
pearson_scale=18.0,
pp_hvg=3000,
pp_neighbors=30,
velocity_genes=False,
use_scaled_u=False,
grad_clip=True,
stop_pearson_after=10,
new_gene_selector=False,
remove_extreme_expr_genes=True,
)
run = wandb.init(
entity="deepvelo-team",
config=hyperparameter_defaults,
project="deepvelo-sweep-gastrulation",
reinit=True,
)
wargs = wandb.config
# %%
# fix random seeds for reproducibility
SEED = wargs.seed
torch.manual_seed(SEED)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(SEED)
save_dir = (
Path(f"saved/gastrulation/sweep")
if wargs.do_sweep
else Path(f"saved/gastrulation/{time.strftime('%b-%d-%H%M')}")
)
save_dir.mkdir(parents=True, exist_ok=True)
shutil.copy(__file__, save_dir)
scv.settings.verbosity = 3 # show errors(0), warnings(1), info(2), hints(3)
scv.settings.set_figure_params(
"scvelo", transparent=False
) # for beautified visualization
scv.settings.figdir = str(save_dir)
scv.settings.plot_prefix = ""
use_methods = ["DeepVelo", "Dynamical", "Steady-state"]
remove_murk_genes = False
additional_run = False
# %% [markdown]
# # Load gastrulation data and preprocess
# %%
adata = scv.datasets.gastrulation_erythroid()
adata.obs["clusters"] = adata.obs["celltype"]
print(f"expression value stats, mean {adata.X.mean()}, max {adata.X.max()}")
print(
f"spliced value stats, mean {adata.layers['spliced'].mean()}, max {adata.layers['spliced'].max()}"
)
# murk_gene_file = "data/Gastrulation/MURK_genes.csv"
# murk_genes = pd.read_csv(murk_gene_file, index_col=0).index.tolist()
# if remove_murk_genes:
# ori_n_genes = adata.n_vars
# adata = adata[:, ~adata.var_names.isin(murk_genes)]
# print(f"remove {ori_n_genes - adata.n_vars} murk genes")
# %%
scv.pp.filter_and_normalize(adata, min_shared_counts=20, n_top_genes=wargs.pp_hvg)
if wargs.remove_extreme_expr_genes:
# remove genes have extreme high expression
# 98% quantile of expression in adata.X.mean
mean_expr = adata.X.mean(axis=0)
top95 = np.quantile(mean_expr, 0.95)
adata = adata[:, mean_expr < top95]
# %%
scv.pp.moments(adata, n_neighbors=wargs.pp_neighbors, n_pcs=30)
print(f"processed expression, mean {adata.X.mean()}, max {adata.X.max()}")
print(
f"processed spliced stats, mean {adata.layers['spliced'].mean()}, max {adata.layers['spliced'].max()}"
)
print(f"Ms stats, mean {adata.layers['Ms'].mean()}, max {adata.layers['Ms'].max()}")
print(f"Ms stats, mean {adata.layers['Mu'].mean()}, max {adata.layers['Mu'].max()}")
adata_raw = adata.copy()
result_adatas = {}
# %% [markdown]
# # DeepVelo
method = "DeepVelo"
adata = adata_raw.copy()
# %%
# specific configs to overide the default configs
configs = {
"name": "DeepVelo", # name of the experiment
"data_loader": {
"args": {
"velocity_genes": wargs.velocity_genes,
"use_scaled_u": wargs.use_scaled_u,
},
},
"loss": {
"args": {
"coeff_s": autoset_coeff_s(adata),
"pearson_scale": wargs.pearson_scale,
"stop_pearson_after": wargs.stop_pearson_after,
},
},
"trainer": {
"verbosity": 1,
"grad_clip": wargs.grad_clip,
}, # increase verbosity to show training progress
}
configs = update_dict(Constants.default_configs, configs)
# %%
# initial velocity
if wargs.new_gene_selector:
dv.tl.get_velo_genes(adata)
else:
velocity(adata)
trainer = train(adata, configs)
# %%
# adata.var["velocity_genes"] = True # use all genes for plotting
scv.tl.velocity_graph(adata, n_jobs=8)
# %%
# velocity plot
if wargs.do_sweep:
ax = scv.pl.velocity_embedding_stream(
adata,
basis="umap",
color="clusters",
legend_fontsize=9,
dpi=150, # increase dpi for higher resolution
show=False,
)
wandb.log({"velocity_embedding_stream": wandb.Image(ax.figure)})
else:
scv.pl.velocity_embedding_stream(
adata,
basis="umap",
color="clusters",
legend_fontsize=9,
dpi=150, # increase dpi for higher resolution
save=f"{method}_velocity.png",
)
cluster_edges = [
("Blood progenitors 1", "Blood progenitors 2"),
("Blood progenitors 2", "Erythroid1"),
("Erythroid1", "Erythroid2"),
("Erythroid2", "Erythroid3"),
]
from deepvelo.utils import cross_boundary_correctness
cbcs, avg_cbc = cross_boundary_correctness(
adata,
"clusters",
"velocity",
cluster_edges,
x_emb_key="umap", # or Ms
)
print(f"Average cross-boundary correctness of DeepVelo: {avg_cbc:.2f}")
wandb.log({"direction_score": avg_cbc})
if wargs.do_sweep:
sys.exit(0)
# %%
continuity_confidence(adata, trainer)
# %%
# show histogram of cell continuity
scv.pl.velocity_embedding_stream(
adata,
basis="umap",
color="cell_continuity",
# color_map="heat",
legend_fontsize=6,
# perc=[2, 98],
dpi=150, # increase dpi for higher resolution
save=f"{method}_continuity.png",
)
# %%
from deepvelo.utils import latent_time
latent_time(adata)
scv.pl.scatter(
adata,
color="velocity_pseudotime",
color_map="gnuplot",
save=f"{method}_pseudotime.png",
)
# %%
if "velocity_unspliced" not in adata.layers:
adata.layers["velocity_unspliced"] = np.zeros_like(adata.layers["velocity"])
scatter(
adata,
basis=[
"Blvrb",
"Alad",
"Clta",
"Cpox",
"Gmpr",
"Gnai2",
"Tpm4",
"Ccnd2",
"Rbms1",
"Fech",
],
add_quiver=True,
# dpi=150,
legend_loc_lines="none",
ncols=2,
)
# %% [markdown]
# ### plot MURK genes
# %%
if not remove_murk_genes:
scatter(
adata,
basis=["Gclm", "Abcg2", "Hemgn", "Hebp1", "Smim1", "Hba-x"],
add_quiver=True,
# dpi=150,
legend_loc_lines="none",
ncols=2,
)
# %% show genes with direction evaluation
cluster_edges = [
("Blood progenitors 1", "Blood progenitors 2"),
("Blood progenitors 2", "Erythroid1"),
("Erythroid1", "Erythroid2"),
("Erythroid2", "Erythroid3"),
]
from deepvelo.utils import genewise_cross_boundary_correctness
genewise_cross_boundary_correctness(
adata,
cluster_key="clusters",
velocity_key="velocity",
cluster_edges=cluster_edges,
)
# visualize adta.var genewise direction scores
gene_ds_sorted = adata.var[adata.var["velocity_genes"]][
"gene_direction_scores"
].sort_values(ascending=False)
assert not gene_ds_sorted.isna().any()
# histogram of gene direction scores
scv.pl.hist(
gene_ds_sorted,
labels=f"gene direction score, avg {gene_ds_sorted.mean():.2f}",
kde=True,
normed=True,
bins=100,
fontsize=18,
legend_fontsize=12,
)
# Top 10 velocity genes with highest direction scores
scatter(
adata,
basis=gene_ds_sorted.index[:10].tolist(),
add_quiver=True,
legend_loc_lines="none",
ncols=2,
)
# Last 10 velocity genes with lowest direction scores
scatter(
adata,
basis=gene_ds_sorted.index[-10:].tolist(),
add_quiver=True,
legend_loc_lines="none",
ncols=2,
)
# %% plot MURK genes in the order of their direction scores
if not remove_murk_genes:
scatter(
adata,
basis="Smim1",
add_quiver=True,
quiver_size=0.3,
legend_loc_lines="none",
ncols=2,
save=f"{method}_Smim1.png",
)
scatter(
adata,
basis="Gypa",
add_quiver=True,
quiver_size=0.1,
legend_loc_lines="none",
ncols=2,
save=f"{method}_Gypa.png",
)
# %%
scv.pl.velocity_embedding(
adata,
basis="umap",
arrow_length=6,
arrow_size=1.2,
dpi=150,
)
# %%
scv.pl.velocity_embedding_grid(
adata,
basis="umap",
arrow_length=4,
# alpha=0.1,
arrow_size=2,
arrow_color="tab:blue",
dpi=150,
)
result_adatas["DeepVelo"] = adata.copy()
# %% [markdown]
# # scVelo (dynamical) or steady-state
for method in [m for m in use_methods if m != "DeepVelo"]:
adata = adata_raw.copy()
if method == "Dynamical":
scv.tl.recover_dynamics(adata, n_jobs=8)
scv.tl.velocity(adata, mode="dynamical")
elif method == "Steady-state":
scv.tl.velocity(adata, mode="stochastic") # deterministic
scv.tl.velocity_graph(adata, n_jobs=8)
# velocity plot
scv.pl.velocity_embedding_stream(
adata,
basis="umap",
color="clusters",
legend_fontsize=9,
legend_loc="none",
dpi=150, # increase dpi for higher resolution
save=f"{method}_velocity_embedding_stream.png",
)
scv.pl.velocity_embedding(
adata,
basis="umap",
arrow_length=6,
arrow_size=1.2,
dpi=150,
save=f"{method}_velocity_embedding.png",
)
latent_time(adata)
scv.pl.scatter(
adata,
color="velocity_pseudotime",
color_map="gnuplot",
save=f"{method}_pseudotime.png",
)
if not remove_murk_genes:
if "velocity_unspliced" not in adata.layers:
adata.layers["velocity_unspliced"] = np.zeros_like(adata.layers["velocity"])
scatter(
adata,
basis="Smim1",
add_quiver=True,
# quiver_size=0.3,
legend_loc_lines="none",
ncols=2,
save=f"{method}_Smim1.png",
)
scatter(
adata,
basis="Gypa",
add_quiver=True,
# quiver_size=0.1,
legend_loc_lines="none",
ncols=2,
save=f"{method}_Gypa.png",
)
# save adata for next steps
result_adatas[method] = adata.copy()
# %% [markdown]
# # Evaluation
dv.pipe.evaluate(
result_adatas,
cluster_edges=cluster_edges,
cluster_key="clusters",
vkey="velocity",
save_dir=save_dir,
)
# %% skip the following cells by default
if not additional_run:
sys.exit(0)
# %%
adata = result_adatas["DeepVelo"]
continuity_confidence(adata, trainer)
# %%
# show histogram of cell continuity
cell_continuity_mse = adata.layers["continuity_relative_error"].mean(axis=1) / (
adata.layers["Ms"].mean(axis=1) + 1e-6
)
scv.pl.velocity_embedding_stream(
adata,
basis="umap",
color="cell_continuity",
# color_map="heat",
legend_fontsize=6,
# perc=[2, 98],
dpi=150, # increase dpi for higher resolution
)
# %%
# get kinetic_rates
if "cell_specific_alpha" in adata.layers:
all_rates = np.concatenate(
[
adata.layers["cell_specific_beta"],
adata.layers["cell_specific_gamma"],
adata.layers["cell_specific_alpha"],
],
axis=1,
)
gene_wise_rates = np.concatenate(
[
adata.layers["cell_specific_beta"],
adata.layers["cell_specific_gamma"],
adata.layers["cell_specific_alpha"],
],
axis=0,
).T
else:
all_rates = np.concatenate(
[
adata.layers["cell_specific_beta"],
adata.layers["cell_specific_gamma"],
],
axis=1,
)
gene_wise_rates = np.concatenate(
[
adata.layers["cell_specific_beta"],
adata.layers["cell_specific_gamma"],
],
axis=0,
).T
# pca and umap of all rates
rates_pca = PCA(n_components=30, random_state=SEED).fit_transform(all_rates)
adata.obsm["X_rates_pca"] = rates_pca
rates_umap = UMAP(
n_neighbors=60,
min_dist=0.6,
spread=0.9,
random_state=SEED,
).fit_transform(rates_pca)
adata.obsm["X_rates_umap"] = rates_umap
# pca and umap of gene-wise rates
rates_pca_gene_wise = PCA(n_components=30, random_state=SEED).fit_transform(
adata.layers["Ms"].T
)
adata.varm["rates_pca"] = rates_pca_gene_wise
rates_umap_gene_wise = UMAP(
n_neighbors=60,
min_dist=0.6,
spread=0.9,
random_state=SEED,
).fit_transform(rates_pca_gene_wise)
adata.varm["rates_umap"] = rates_umap_gene_wise
# %%
# plot kinetic rates umap
scv.pl.scatter(
adata,
basis="rates_umap",
# omit_velocity_fit=True,
add_outline="Granule mature, Granule immature, Neuroblast",
outline_width=(0.15, 0.3),
title="umap of cell-specific kinetic rates",
legend_loc="none",
dpi=150,
)
# %%
num_show_genes = 10
top_ranked_genes_corr = adata.var.sort_values("gene_corr", ascending=False).index[
:num_show_genes
]
least_ranked_genes_corr = adata.var.sort_values("gene_corr", ascending=False).index[
-num_show_genes:
]
ax = scv.pl.velocity(
adata,
var_names=top_ranked_genes_corr,
basis="umap",
show=False,
)
ax.get_figure().suptitle("top ranked genes by gene_corr")
ax = scv.pl.velocity(
adata,
var_names=least_ranked_genes_corr,
basis="umap",
show=False,
)
ax.get_figure().suptitle("least ranked genes by gene_corr")
# %%
top_ranked_genes_continuity = adata.var.sort_values(
"gene_continuity", ascending=False
).index[:num_show_genes]
least_ranked_genes_continuity = adata.var.sort_values(
"gene_continuity", ascending=False
).index[-num_show_genes:]
ax = scv.pl.velocity(
adata,
var_names=top_ranked_genes_continuity,
basis="umap",
show=False,
)
ax.get_figure().suptitle("top ranked genes by gene_continuity")
# NOTE: we have Ppp3ca listed here nicely
# And nicely it is explainable in the way, in most of the top ranked ones, the
# clear difference of spliced gene expression horisontally can be observed
# among the large portion of neuralblasts to granule immature and mature cells.
ax = scv.pl.velocity(
adata,
var_names=least_ranked_genes_continuity,
basis="umap",
show=False,
)
ax.get_figure().suptitle("least ranked genes by gene_continuity")
# %%
adata.var["gene_confidence"] = adata.var["gene_continuity"] # + adata.var["gene_corr"]
# select the top 300 genes by gene_confidence
top_ranked_genes_confidence = adata.var.sort_values(
"gene_confidence", ascending=False
).index[:300]
adata.var["velocity_genes"] = False
adata.var.loc[top_ranked_genes_confidence, "velocity_genes"] = True
# %%
# recompute velocity with selected genes only
# %% [markdown]
# # scVelo stochastic
adata = adata_raw.copy()
scv.tl.velocity(adata, mode="stochastic")
scv.tl.velocity_graph(adata, n_jobs=8)
# velocity plot
scv.pl.velocity_embedding_stream(
adata,
basis="umap",
color="clusters",
legend_fontsize=9,
dpi=150, # increase dpi for higher resolution
)
if "velocity_unspliced" not in adata.layers:
adata.layers["velocity_unspliced"] = np.zeros_like(adata.layers["velocity"])
scatter(
adata,
basis=["Blvrb", "Alad", "Clta", "Cpox"],
add_quiver=True,
# dpi=150,
legend_loc_lines="none",
ncols=2,
)
# %% [markdown]
# # scVelo dynamical
adata = adata_raw.copy()
scv.tl.recover_dynamics(adata, n_jobs=8)
scv.tl.velocity(adata, mode="dynamical")
scv.tl.velocity_graph(adata, n_jobs=8)
# velocity plot
scv.pl.velocity_embedding_stream(
adata,
basis="umap",
color="clusters",
legend_fontsize=9,
dpi=150, # increase dpi for higher resolution
)
if "velocity_unspliced" not in adata.layers:
adata.layers["velocity_unspliced"] = np.zeros_like(adata.layers["velocity"])
scatter(
adata,
basis=["Blvrb", "Alad", "Clta", "Cpox"],
add_quiver=True,
# dpi=150,
legend_loc_lines="none",
ncols=2,
)
# %%