-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_loss.py
599 lines (530 loc) · 26.8 KB
/
util_loss.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
import os
from random import shuffle
import anndata
import numpy as np
import scanpy as sc
from matplotlib import pyplot as plt
from scipy import sparse
from sklearn import preprocessing
import pandas as pd
def data_remover(adata, remain_list, remove_list, cell_type_key, condition_key):
"""
Removes specific cell type in stimulated condition form `adata`.
# Parameters
adata: `~anndata.AnnData`
Annotated data matrix
remain_list: list
list of cell types which are going to be remained in `adata`.
remove_list: list
list of cell types which are going to be removed from `adata`.
# Returns
merged_data: list
returns array of specified cell types in stimulated condition
# Example
```python
import scgen
import anndata
train_data = anndata.read("./data/train_kang.h5ad")
remove_list = ["CD14+Mono", "CD8T"]
remain_list = ["CD4T", "Dendritic"]
filtered_data = data_remover(train_data, remain_list, remove_list)
```
"""
source_data = []
for i in remain_list:
source_data.append(extractor(adata, i, conditions={"ctrl": "control", "stim": "stimulated"},
cell_type_key=cell_type_key, condition_key=condition_key)[3])
target_data = []
for i in remove_list:
target_data.append(extractor(adata, i, conditions={"ctrl": "control", "stim": "stimulated"},
cell_type_key=cell_type_key, condition_key=condition_key)[1])
merged_data = training_data_provider(source_data, target_data)
merged_data.var_names = adata.var_names
return merged_data
def extractor(data, cell_type, conditions, cell_type_key="cell_type", condition_key="condition"):
"""
Returns a list of `data` files while filtering for a specific `cell_type`.
# Parameters
data: `~anndata.AnnData`
Annotated data matrix
cell_type: basestring
specific cell type to be extracted from `data`.
conditions: dict
dictionary of stimulated/control of `data`.
# Returns
list of `data` files while filtering for a specific `cell_type`.
# Example
```python
import scgen
import anndata
train_data = anndata.read("./data/train.h5ad")
test_data = anndata.read("./data/test.h5ad")
train_data_extracted_list = extractor(train_data, "CD4T", conditions={"ctrl": "control", "stim": "stimulated"})
```
"""
cell_with_both_condition = data[data.obs[cell_type_key] == cell_type]
condtion_1 = data[(data.obs[cell_type_key] == cell_type) & (data.obs[condition_key] == conditions["ctrl"])]
condtion_2 = data[(data.obs[cell_type_key] == cell_type) & (data.obs[condition_key] == conditions["stim"])]
training = data[~((data.obs[cell_type_key] == cell_type) & (data.obs[condition_key] == conditions["stim"]))]
return [training, condtion_1, condtion_2, cell_with_both_condition]
def training_data_provider(train_s, train_t):
"""
Concatenates two lists containing adata files
# Parameters
train_s: `~anndata.AnnData`
Annotated data matrix.
train_t: `~anndata.AnnData`
Annotated data matrix.
# Returns
Concatenated Annotated data matrix.
# Example
```python
import scgen
import anndata
train_data = anndata.read("./data/train_kang.h5ad")
test_data = anndata.read("./data/test.h5ad")
whole_data = training_data_provider(train_data, test_data)
```
"""
train_s_X = []
train_s_diet = []
train_s_groups = []
for i in train_s:
train_s_X.append(i.X.A)
train_s_diet.append(i.obs["condition"].tolist())
train_s_groups.append(i.obs["cell_type"].tolist())
train_s_X = np.concatenate(train_s_X)
temp = []
for i in train_s_diet:
temp = temp + i
train_s_diet = temp
temp = []
for i in train_s_groups:
temp = temp + i
train_s_groups = temp
train_t_X = []
train_t_diet = []
train_t_groups = []
for i in train_t:
train_t_X.append(i.X.A)
train_t_diet.append(i.obs["condition"].tolist())
train_t_groups.append(i.obs["cell_type"].tolist())
temp = []
for i in train_t_diet:
temp = temp + i
train_t_diet = temp
temp = []
for i in train_t_groups:
temp = temp + i
train_t_groups = temp
train_t_X = np.concatenate(train_t_X)
train_real = np.concatenate([train_s_X, train_t_X]) # concat all
train_real = anndata.AnnData(train_real)
train_real.obs["condition"] = train_s_diet + train_t_diet
train_real.obs["cell_type"] = train_s_groups + train_t_groups
return train_real
def balancer(adata, cell_type_key="cell_type", condition_key="condition"):
"""
Makes cell type population equal.
# Parameters
adata: `~anndata.AnnData`
Annotated data matrix.
# Returns
balanced_data: `~anndata.AnnData`
Equal cell type population Annotated data matrix.
# Example
```python
import scgen
import anndata
train_data = anndata.read("./train_kang.h5ad")
train_ctrl = train_data[train_data.obs["condition"] == "control", :]
train_ctrl = balancer(train_ctrl)
```
"""
class_names = np.unique(adata.obs[cell_type_key])
class_pop = {}
for cls in class_names:
class_pop[cls] = adata.copy()[adata.obs[cell_type_key] == cls].shape[0]
max_number = np.max(list(class_pop.values()))
all_data_x = []
all_data_label = []
all_data_condition = []
for cls in class_names:
temp = adata.copy()[adata.obs[cell_type_key] == cls]
index = np.random.choice(range(len(temp)), max_number)
if sparse.issparse(temp.X):
temp_x = temp.X.A[index]
else:
temp_x = temp.X[index]
all_data_x.append(temp_x)
temp_ct = np.repeat(cls, max_number)
all_data_label.append(temp_ct)
temp_cc = np.repeat(np.unique(temp.obs[condition_key]), max_number)
all_data_condition.append(temp_cc)
balanced_data = anndata.AnnData(np.concatenate(all_data_x))
balanced_data.obs[cell_type_key] = np.concatenate(all_data_label)
balanced_data.obs[condition_key] = np.concatenate(all_data_label)
class_names = np.unique(balanced_data.obs[cell_type_key])
class_pop = {}
for cls in class_names:
class_pop[cls] = len(balanced_data[balanced_data.obs[cell_type_key] == cls])
return balanced_data
def shuffle_adata(adata):
"""
Shuffles the `adata`.
# Parameters
adata: `~anndata.AnnData`
Annotated data matrix.
labels: numpy nd-array
list of encoded labels
# Returns
adata: `~anndata.AnnData`
Shuffled annotated data matrix.
labels: numpy nd-array
Array of shuffled labels if `labels` is not None.
# Example
```python
import scgen
import anndata
import pandas as pd
train_data = anndata.read("./data/train.h5ad")
train_labels = pd.read_csv("./data/train_labels.csv", header=None)
train_data, train_labels = shuffle_data(train_data, train_labels)
```
"""
if sparse.issparse(adata.X):
adata.X = adata.X.A
ind_list = [i for i in range(adata.shape[0])]
shuffle(ind_list)
new_adata = adata[ind_list, :]
return new_adata
def batch_removal(network, adata, batch_key="batch", cell_label_key="cell_type"):
"""
Removes batch effect of adata
# Parameters
network: `scgen VAE`
Variational Auto-encoder class object after training the network.
adata: `~anndata.AnnData`
Annotated data matrix. adata must have `batch_key` and `cell_label_key` which you pass to the function
in its obs.
# Returns
corrected: `~anndata.AnnData`
Annotated matrix of corrected data consisting of all cell types whether they have batch effect or not.
# Example
```python
import scgen
import anndata
train = anndata.read("data/pancreas.h5ad")
train.obs["cell_type"] = train.obs["celltype"].tolist()
network = scgen.VAEArith(x_dimension=train.shape[1], model_path="./models/batch")
network.train(train_data=train, n_epochs=20)
corrected_adata = scgen.batch_removal(network, train)
```
"""
if sparse.issparse(adata.X):
latent_all = network.to_latent(adata.X.A)
else:
latent_all = network.to_latent(adata.X)
adata_latent = anndata.AnnData(latent_all)
adata_latent.obs = adata.obs.copy(deep=True)
unique_cell_types = np.unique(adata_latent.obs[cell_label_key])
shared_ct = []
not_shared_ct = []
for cell_type in unique_cell_types:
temp_cell = adata_latent[adata_latent.obs[cell_label_key] == cell_type]
if len(np.unique(temp_cell.obs[batch_key])) < 2:
cell_type_ann = adata_latent[adata_latent.obs[cell_label_key] == cell_type]
not_shared_ct.append(cell_type_ann)
continue
temp_cell = adata_latent[adata_latent.obs[cell_label_key] == cell_type]
batch_list = {}
batch_ind = {}
max_batch = 0
max_batch_ind = ""
batches = np.unique(temp_cell.obs[batch_key])
for i in batches:
temp = temp_cell[temp_cell.obs[batch_key] == i]
temp_ind = temp_cell.obs[batch_key] == i
if max_batch < len(temp):
max_batch = len(temp)
max_batch_ind = i
batch_list[i] = temp
batch_ind[i] = temp_ind
max_batch_ann = batch_list[max_batch_ind]
for study in batch_list:
delta = np.average(max_batch_ann.X, axis=0) - np.average(batch_list[study].X, axis=0)
batch_list[study].X = delta + batch_list[study].X
temp_cell[batch_ind[study]].X = batch_list[study].X
shared_ct.append(temp_cell)
all_shared_ann = anndata.AnnData.concatenate(*shared_ct, batch_key="concat_batch", index_unique=None)
if "concat_batch" in all_shared_ann.obs.columns:
del all_shared_ann.obs["concat_batch"]
if len(not_shared_ct) < 1:
corrected = anndata.AnnData(network.reconstruct(all_shared_ann.X, use_data=True))
corrected.obs = all_shared_ann.obs.copy(deep=True)
corrected.var_names = adata.var_names.tolist()
corrected = corrected[adata.obs_names]
if adata.raw is not None:
adata_raw = anndata.AnnData(X=adata.raw.X, var=adata.raw.var)
adata_raw.obs_names = adata.obs_names
corrected.raw = adata_raw
return corrected
else:
all_not_shared_ann = anndata.AnnData.concatenate(*not_shared_ct, batch_key="concat_batch", index_unique=None)
all_corrected_data = anndata.AnnData.concatenate(all_shared_ann, all_not_shared_ann, batch_key="concat_batch", index_unique=None)
if "concat_batch" in all_shared_ann.obs.columns:
del all_corrected_data.obs["concat_batch"]
corrected = anndata.AnnData(network.reconstruct(all_corrected_data.X, use_data=True), )
corrected.obs = pd.concat([all_shared_ann.obs, all_not_shared_ann.obs])
corrected.var_names = adata.var_names.tolist()
corrected = corrected[adata.obs_names]
if adata.raw is not None:
adata_raw = anndata.AnnData(X=adata.raw.X, var=adata.raw.var)
adata_raw.obs_names = adata.obs_names
corrected.raw = adata_raw
return corrected
def label_encoder(adata):
"""
Encode labels of Annotated `adata` matrix using sklearn.preprocessing.LabelEncoder class.
Parameters
----------
adata: `~anndata.AnnData`
Annotated data matrix.
Returns
-------
labels: numpy nd-array
Array of encoded labels
Example
--------
>>> import scgen
>>> import scanpy as sc
>>> train_data = sc.read("./data/train.h5ad")
>>> train_labels, label_encoder = label_encoder(train_data)
"""
le = preprocessing.LabelEncoder()
labels = le.fit_transform(adata.obs["condition"].tolist())
return labels.reshape(-1, 1), le
def visualize_trained_network_results(network, train, cell_type,
conditions={"ctrl": "control", "stim": "stimulated"},
condition_key="condition",
cell_type_key="cell_type",
path_to_save="./figures/",
plot_umap=True,
plot_reg=True):
plt.close("all")
os.makedirs(path_to_save, exist_ok=True)
sc.settings.figdir = os.path.abspath(path_to_save)
if isinstance(network, scgen.VAEArithKeras):
if sparse.issparse(train.X):
latent = network.to_latent(train.X.A)
else:
latent = network.to_latent(train.X)
latent = sc.AnnData(X=latent,
obs={condition_key: train.obs[condition_key].tolist(),
cell_type_key: train.obs[cell_type_key].tolist()})
if plot_umap:
sc.pp.neighbors(latent)
sc.tl.umap(latent)
sc.pl.umap(latent, color=[condition_key, cell_type_key],
save=f"_latent",
show=False)
cell_type_data = train[train.obs[cell_type_key] == cell_type]
pred, delta = network.predict(adata=cell_type_data,
conditions=conditions,
cell_type_key=cell_type_key,
condition_key=condition_key,
celltype_to_predict=cell_type)
pred_adata = anndata.AnnData(pred, obs={condition_key: ["pred"] * len(pred)},
var={"var_names": cell_type_data.var_names})
all_adata = cell_type_data.concatenate(pred_adata)
sc.tl.rank_genes_groups(cell_type_data, groupby=condition_key, n_genes=100)
diff_genes = cell_type_data.uns["rank_genes_groups"]["names"][conditions["stim"]]
if plot_reg:
scgen.plotting.reg_mean_plot(all_adata, condition_key=condition_key,
axis_keys={"x": "pred", "y": conditions["stim"]},
gene_list=diff_genes[:5],
path_to_save=os.path.join(path_to_save, f"reg_mean_all_genes.pdf"))
scgen.plotting.reg_var_plot(all_adata, condition_key=condition_key,
axis_keys={"x": "pred", "y": conditions["stim"]},
gene_list=diff_genes[:5],
path_to_save=os.path.join(path_to_save, f"reg_var_all_genes.pdf"))
all_adata_top_100_genes = all_adata.copy()[:, diff_genes.tolist()]
scgen.plotting.reg_mean_plot(all_adata_top_100_genes, condition_key=condition_key,
axis_keys={"x": "pred", "y": conditions["stim"]},
gene_list=diff_genes[:5],
path_to_save=os.path.join(path_to_save, f"reg_mean_top_100_genes.pdf"))
scgen.plotting.reg_var_plot(all_adata_top_100_genes, condition_key=condition_key,
axis_keys={"x": "pred", "y": conditions["stim"]},
gene_list=diff_genes[:5],
path_to_save=os.path.join(path_to_save, f"reg_var_top_100_genes.pdf"))
all_adata_top_50_genes = all_adata.copy()[:, diff_genes.tolist()[:50]]
scgen.plotting.reg_mean_plot(all_adata_top_50_genes, condition_key=condition_key,
axis_keys={"x": "pred", "y": conditions["stim"]},
gene_list=diff_genes[:5],
path_to_save=os.path.join(path_to_save, f"reg_mean_top_50_genes.pdf"))
scgen.plotting.reg_var_plot(all_adata_top_50_genes, condition_key=condition_key,
axis_keys={"x": "pred", "y": conditions["stim"]},
gene_list=diff_genes[:5],
path_to_save=os.path.join(path_to_save, f"reg_var_top_50_genes.pdf"))
if plot_umap:
sc.pp.neighbors(all_adata)
sc.tl.umap(all_adata)
sc.pl.umap(all_adata, color=condition_key,
save="pred_all_genes",
show=False)
sc.pp.neighbors(all_adata_top_100_genes)
sc.tl.umap(all_adata_top_100_genes)
sc.pl.umap(all_adata_top_100_genes, color=condition_key,
save="pred_top_100_genes",
show=False)
sc.pp.neighbors(all_adata_top_50_genes)
sc.tl.umap(all_adata_top_50_genes)
sc.pl.umap(all_adata_top_50_genes, color=condition_key,
save="pred_top_50_genes",
show=False)
sc.pl.violin(all_adata, keys=diff_genes.tolist()[0], groupby=condition_key,
save=f"_{diff_genes.tolist()[0]}",
show=False)
plt.close("all")
elif isinstance(network, scgen.VAEArith):
if sparse.issparse(train.X):
latent = network.to_latent(train.X.A)
else:
latent = network.to_latent(train.X)
latent = sc.AnnData(X=latent,
obs={condition_key: train.obs[condition_key].tolist(),
cell_type_key: train.obs[cell_type_key].tolist()})
if plot_umap:
sc.pp.neighbors(latent)
sc.tl.umap(latent)
sc.pl.umap(latent, color=[condition_key, cell_type_key],
save=f"_latent",
show=False)
cell_type_data = train[train.obs[cell_type_key] == cell_type]
pred, delta = network.predict(adata=cell_type_data,
conditions=conditions,
cell_type_key=cell_type_key,
condition_key=condition_key,
celltype_to_predict=cell_type)
pred_adata = anndata.AnnData(pred, obs={condition_key: ["pred"] * len(pred)},
var={"var_names": cell_type_data.var_names})
all_adata = cell_type_data.concatenate(pred_adata)
sc.tl.rank_genes_groups(cell_type_data, groupby=condition_key, n_genes=100)
diff_genes = cell_type_data.uns["rank_genes_groups"]["names"][conditions["stim"]]
if plot_reg:
scgen.plotting.reg_mean_plot(all_adata, condition_key=condition_key,
axis_keys={"x": "pred", "y": conditions["stim"]},
gene_list=diff_genes[:5],
path_to_save=os.path.join(path_to_save, f"reg_mean_all_genes.pdf"))
scgen.plotting.reg_var_plot(all_adata, condition_key=condition_key,
axis_keys={"x": "pred", "y": conditions["stim"]},
gene_list=diff_genes[:5],
path_to_save=os.path.join(path_to_save, f"reg_var_all_genes.pdf"))
all_adata_top_100_genes = all_adata.copy()[:, diff_genes.tolist()]
scgen.plotting.reg_mean_plot(all_adata_top_100_genes, condition_key=condition_key,
axis_keys={"x": "pred", "y": conditions["stim"]},
gene_list=diff_genes[:5],
path_to_save=os.path.join(path_to_save, f"reg_mean_top_100_genes.pdf"))
scgen.plotting.reg_var_plot(all_adata_top_100_genes, condition_key=condition_key,
axis_keys={"x": "pred", "y": conditions["stim"]},
gene_list=diff_genes[:5],
path_to_save=os.path.join(path_to_save, f"reg_var_top_100_genes.pdf"))
all_adata_top_50_genes = all_adata.copy()[:, diff_genes.tolist()[:50]]
scgen.plotting.reg_mean_plot(all_adata_top_50_genes, condition_key=condition_key,
axis_keys={"x": "pred", "y": conditions["stim"]},
gene_list=diff_genes[:5],
path_to_save=os.path.join(path_to_save, f"reg_mean_top_50_genes.pdf"))
scgen.plotting.reg_var_plot(all_adata_top_50_genes, condition_key=condition_key,
axis_keys={"x": "pred", "y": conditions["stim"]},
gene_list=diff_genes[:5],
path_to_save=os.path.join(path_to_save, f"reg_var_top_50_genes.pdf"))
if plot_umap:
sc.pp.neighbors(all_adata)
sc.tl.umap(all_adata)
sc.pl.umap(all_adata, color=condition_key,
save="pred_all_genes",
show=False)
sc.pp.neighbors(all_adata_top_100_genes)
sc.tl.umap(all_adata_top_100_genes)
sc.pl.umap(all_adata_top_100_genes, color=condition_key,
save="pred_top_100_genes",
show=False)
sc.pp.neighbors(all_adata_top_50_genes)
sc.tl.umap(all_adata_top_50_genes)
sc.pl.umap(all_adata_top_50_genes, color=condition_key,
save="pred_top_50_genes",
show=False)
sc.pl.violin(all_adata, keys=diff_genes.tolist()[0], groupby=condition_key,
save=f"_{diff_genes.tolist()[0]}",
show=False)
plt.close("all")
elif isinstance(network, scgen.CVAE):
true_labels, _ = scgen.label_encoder(train)
if sparse.issparse(train.X):
latent = network.to_latent(train.X.A, labels=true_labels)
else:
latent = network.to_latent(train.X, labels=true_labels)
latent = sc.AnnData(X=latent,
obs={condition_key: train.obs[condition_key].tolist(),
cell_type_key: train.obs[cell_type_key].tolist()})
if plot_umap:
sc.pp.neighbors(latent)
sc.tl.umap(latent)
sc.pl.umap(latent, color=[condition_key, cell_type_key],
save=f"_latent",
show=False)
cell_type_data = train[train.obs[cell_type_key] == cell_type]
fake_labels = np.ones(shape=(cell_type_data.shape[0], 1))
pred = network.predict(data=cell_type_data, labels=fake_labels)
pred_adata = anndata.AnnData(pred, obs={condition_key: ["pred"] * len(pred)},
var={"var_names": cell_type_data.var_names})
all_adata = cell_type_data.concatenate(pred_adata)
sc.tl.rank_genes_groups(cell_type_data, groupby=condition_key, n_genes=100)
diff_genes = cell_type_data.uns["rank_genes_groups"]["names"][conditions["stim"]]
if plot_reg:
scgen.plotting.reg_mean_plot(all_adata, condition_key=condition_key,
axis_keys={"x": "pred", "y": conditions["stim"]},
gene_list=diff_genes[:5],
path_to_save=os.path.join(path_to_save, f"reg_mean_all_genes.pdf"))
scgen.plotting.reg_var_plot(all_adata, condition_key=condition_key,
axis_keys={"x": "pred", "y": conditions["stim"]},
gene_list=diff_genes[:5],
path_to_save=os.path.join(path_to_save, f"reg_var_all_genes.pdf"))
all_adata_top_100_genes = all_adata.copy()[:, diff_genes.tolist()]
scgen.plotting.reg_mean_plot(all_adata_top_100_genes, condition_key=condition_key,
axis_keys={"x": "pred", "y": conditions["stim"]},
gene_list=diff_genes[:5],
path_to_save=os.path.join(path_to_save, f"reg_mean_top_100_genes.pdf"))
scgen.plotting.reg_var_plot(all_adata_top_100_genes, condition_key=condition_key,
axis_keys={"x": "pred", "y": conditions["stim"]},
gene_list=diff_genes[:5],
path_to_save=os.path.join(path_to_save, f"reg_var_top_100_genes.pdf"))
all_adata_top_50_genes = all_adata.copy()[:, diff_genes.tolist()[:50]]
scgen.plotting.reg_mean_plot(all_adata_top_50_genes, condition_key=condition_key,
axis_keys={"x": "pred", "y": conditions["stim"]},
gene_list=diff_genes[:5],
path_to_save=os.path.join(path_to_save, f"reg_mean_top_50_genes.pdf"))
scgen.plotting.reg_var_plot(all_adata_top_50_genes, condition_key=condition_key,
axis_keys={"x": "pred", "y": conditions["stim"]},
gene_list=diff_genes[:5],
path_to_save=os.path.join(path_to_save, f"reg_var_top_50_genes.pdf"))
if plot_umap:
sc.pp.neighbors(all_adata)
sc.tl.umap(all_adata)
sc.pl.umap(all_adata, color=condition_key,
save="pred_all_genes",
show=False)
sc.pp.neighbors(all_adata_top_100_genes)
sc.tl.umap(all_adata_top_100_genes)
sc.pl.umap(all_adata_top_100_genes, color=condition_key,
save="pred_top_100_genes",
show=False)
sc.pp.neighbors(all_adata_top_50_genes)
sc.tl.umap(all_adata_top_50_genes)
sc.pl.umap(all_adata_top_50_genes, color=condition_key,
save="pred_top_50_genes",
show=False)
sc.pl.violin(all_adata, keys=diff_genes.tolist()[0], groupby=condition_key,
save=f"_{diff_genes.tolist()[0]}",
show=False)
plt.close("all")