-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvelocity.py
202 lines (150 loc) · 5.44 KB
/
velocity.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
import scanpy as sc
import numpy as np
import pandas as pd
import scvelo as scv
from pathlib import Path
import pickle
import os
import cellrank as cr
clusters = 'integrated_snn_res.0.3'
##################
## RNA Velocity ##
##################
## Preparing sample data.
samples = {
'DMSO' : 'results/py_objects/DMSO_seurat.h5ad',
'EZH2i' : 'results/py_objects/EZH2i_seurat.h5ad',
'RACi' : 'results/py_objects/RACi_seurat.h5ad',
'Combo' : 'results/py_objects/Combo_seurat.h5ad'
}
samples = {x:scv.read(y) for x,y in samples.items()}
## Change the metadata to categorical.
for key in samples.keys():
samples[key].obs = samples[key].obs.astype('category')
## Preprocess the data.
for key in samples.keys():
scv.pp.filter_and_normalize(samples[key], min_shared_counts=20, n_top_genes=3000)
scv.pp.moments(samples[key], n_pcs=30, n_neighbors=30)
## Calculate RNA velocities.
for key in samples.keys():
scv.tl.velocity(samples[key])
scv.tl.velocity_graph(samples[key])
## Plot RNA velocity streams.
outdir = 'results/trajectory/velocity/velocity_plots'
if not os.path.exists(outdir):
os.makedirs(outdir)
scv.settings.figdir = outdir
for key,value in samples.items():
scv.pl.velocity_embedding_stream(
value, basis='umap', color=clusters,
save = '%s.png' % key, title = key, show = False,
figsize = (10, 10), size = 50, dpi = 300, legend_fontsize = 0
)
## Plot RNA velocity arrows.
outdir = 'results/trajectory/velocity/velocity_arrows'
if not os.path.exists(outdir):
os.makedirs(outdir)
scv.settings.figdir = outdir
for key,value in samples.items():
scv.pl.velocity_embedding(
value, arrow_length=3, arrow_size=2, dpi=300,
basis ='umap', color=clusters,
figsize = (10, 10), size = 50, show = False,
save = '%s.png' % key, title = key
)
## Plot velocity speed and coherence.
outdir = 'results/trajectory/velocity/velocity_speed_coherence'
if not os.path.exists(outdir):
os.makedirs(outdir)
scv.settings.figdir = outdir
metrics = ['velocity_length', 'velocity_confidence']
for key,value in samples.items():
scv.tl.velocity_confidence(value)
scv.pl.scatter(
value, c = metrics, cmap = 'gnuplot', perc=[5, 95],
size = 50, show = False, dpi = 300, figsize = (10, 10),
save = '%s.png' % key
)
## Plot cell connections.
outdir = 'results/trajectory/velocity/velocity_connections'
if not os.path.exists(outdir):
os.makedirs(outdir)
scv.settings.figdir = outdir
for key,value in samples.items():
scv.pl.velocity_graph(
value, threshold = .2, size = 50, show = False, dpi = 300,
figsize = (10, 10), color = clusters,
save = '%s.png' % key, title = key
)
## Plot velocity pseudotime.
outdir = 'results/trajectory/velocity/velocity_pseudotime'
if not os.path.exists(outdir):
os.makedirs(outdir)
scv.settings.figdir = outdir
for key,value in samples.items():
scv.tl.velocity_pseudotime(value)
scv.pl.scatter(
value, color='velocity_pseudotime', cmap='gnuplot', dpi = 300,
show = False, figsize = (10, 10), title = key, size = 50,
save = '%s.png' % key
)
## PAGA.
outdir = 'results/trajectory/velocity/velocity_paga'
if not os.path.exists(outdir):
os.makedirs(outdir)
scv.settings.figdir = outdir
for key in samples.keys():
samples[key].uns['neighbors']['distances'] = samples[key].obsp['distances']
samples[key].uns['neighbors']['connectivities'] = samples[key].obsp['connectivities']
for key,value in samples.items():
scv.tl.paga(value, groups = clusters)
scv.pl.paga(
value, basis = 'umap', color = clusters,
dpi = 300, show = False, figsize = (10, 10), title = key, size = 50,
save = '%s.png' % key
)
## Export UMAP plots.
outdir = 'results/trajectory/velocity/velocity_umap'
if not os.path.exists(outdir):
os.makedirs(outdir)
scv.settings.figdir = outdir
for key,value in samples.items():
scv.pl.umap(
value, color = clusters, show = False, figsize = (10, 10), title = key,
size = 50, save = '{}.png'.format(key)
)
scv.pl.umap(
value, show = False, figsize = (10, 10), title = key,
size = 50, save = '{}_nocolor.png'.format(key)
)
## Get important genes.
outdir = 'results/trajectory/velocity/velocity_genes'
if not os.path.exists(outdir):
os.makedirs(outdir)
for value in samples.values():
scv.tl.rank_velocity_genes(value, groupby = clusters, min_corr=.3)
for key,value in samples.items():
df = scv.DataFrame(value.uns['rank_velocity_genes']['names'])
df.to_csv("{}/{}.tsv".format(outdir, key), sep = '\t', header = True, index = False)
## Save the velocities.
with open('results/py_objects/velocities.pickle', 'wb') as handle:
pickle.dump(samples, handle)
## Genes different between treatments and DMSO.
outdir = 'results/trajectory/velocity/velocity_diff_genes'
if not os.path.exists(outdir):
os.makedirs(outdir)
comparisons = [
('EZH2i', 'DMSO'),
('RACi', 'DMSO'),
('Combo', 'DMSO')
]
diff_samples = {}
for x,y in comparisons:
comp = samples[x].concatenate(samples[y])
comp.obs['groups'] = comp.obs[['orig.ident', clusters]].astype(str).agg('_'.join, axis = 1)
diff_samples['{}_vs_{}'.format(x, y)] = comp
for value in diff_samples.values():
scv.tl.rank_velocity_genes(value, groupby = 'groups', min_corr=.3)
for key,value in diff_samples.items():
df = scv.DataFrame(value.uns['rank_velocity_genes']['names'])
df.to_csv("{}/{}.tsv".format(outdir, key), sep = '\t', header = True, index = False)