-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_plotting.py
More file actions
981 lines (802 loc) · 37.2 KB
/
04_plotting.py
File metadata and controls
981 lines (802 loc) · 37.2 KB
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
import networkx as nx
import scanpy as sc
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
import os
from scipy.stats import pearsonr
from switchtfi.utils import load_grn_json
from validation.val_utils import combine_p_vals_fisher
def plot_step_fct_and_trends():
# ### Script for plotting Figure 2
from validation.plotting import plot_step_function, plot_gam_gene_trend, plot_gam_gene_trend_heatmap
from switchtfi.utils import get_regulons
# Load AnnData
bdata = sc.read_h5ad('./results/03_validation/anndata/trend_pre-endocrine_beta.h5ad')
# Load unpruned and pruned GRNs
grn = load_grn_json('./results/02_switchtfi/endocrine/beta/grn.json')
pgrn = load_grn_json('./results/02_switchtfi/endocrine/beta/sigpvals_wy0.05_grn.json')
grn = grn.sort_values(by=['weight'], axis=0, ascending=False)
grn = grn.reset_index(drop=True)
print(grn.head(20))
print(pgrn.head(20))
# Load results dataframe
res_tfs = pd.read_csv('./results/02_switchtfi/endocrine/beta/ranked_tfs.csv', index_col=[0])
fig = plt.figure(figsize=(12, 13), constrained_layout=True, dpi=300)
axd = fig.subplot_mosaic(
"""
AABB
AABB
CDEF
GGGG
HHII
HHII
"""
)
layer_key = 'magic_imputed'
title_fs = 20
ax_fs = 18
legend_fs = 18
gene_trend_anno_fs = 14
letter_fs = 20
e0 = tuple(grn[['TF', 'target']].iloc[1])
e1 = tuple(grn[['TF', 'target']].iloc[grn.shape[0] - 1])
print(e0, e1)
plot_step_function(adata=bdata,
grn=grn,
which=e0,
layer_key=layer_key,
plot_threshold=True,
ax_label_fontsize=ax_fs,
title_fontsize=title_fs,
legend_fontsize=legend_fs,
legend_loc='upper right',
show=False,
axs=axd['A'])
plot_step_function(adata=bdata,
grn=grn,
which=e1,
layer_key=layer_key,
plot_threshold=True,
ax_label_fontsize=ax_fs,
title_fontsize=title_fs,
legend_fontsize=legend_fs,
legend_loc='upper center',
show=False,
axs=axd['B'])
plot_gam_gene_trend(adata=bdata,
gene_names=[e0[0]],
plot_cells=True,
ax_label_fontsize=ax_fs,
show=False,
axs=axd['C'],
layer_key='magic_imputed')
plot_gam_gene_trend(adata=bdata,
gene_names=[e0[1]],
plot_cells=True,
ax_label_fontsize=ax_fs,
show=False,
axs=axd['D'],
layer_key='magic_imputed')
plot_gam_gene_trend(adata=bdata,
gene_names=[e1[0]],
plot_cells=True,
ax_label_fontsize=ax_fs,
show=False,
axs=axd['E'],
layer_key='magic_imputed')
plot_gam_gene_trend(adata=bdata,
gene_names=[e1[1]],
plot_cells=True,
ax_label_fontsize=ax_fs,
show=False,
axs=axd['F'],
layer_key='magic_imputed')
tf_names = res_tfs['gene'].tolist()[0:10]
plot_gam_gene_trend_heatmap(adata=bdata,
gene_names=tf_names,
use_trend=True,
gene_names_fontsize=gene_trend_anno_fs,
ax_label_fontsize=ax_fs,
show=False,
axs=axd['G'],
colorbar_pad=0.0)
regulons = get_regulons(grn=pgrn, gene_names=['Pdx1', 'Pax4'])
targets0 = regulons['Pdx1']['targets']
targets1 = regulons['Pax4']['targets']
plot_gam_gene_trend_heatmap(adata=bdata,
gene_names=targets0,
use_trend=True,
annotate_gene_names=False,
ax_label_fontsize=ax_fs,
show=False,
axs=axd['H'],
plot_colorbar=False,
title='Targets of TF Pdx1',
title_fontsize=title_fs)
plot_gam_gene_trend_heatmap(adata=bdata,
gene_names=targets1,
use_trend=True,
annotate_gene_names=False,
ax_label_fontsize=ax_fs,
show=False,
axs=axd['I'],
colorbar_pad=0.0,
title='Targets of TF Pax4',
title_fontsize=title_fs)
# Annotate subplot mosaic tiles with labels
for label, ax in axd.items():
# ax = fig.add_subplot(axd[label])
# ax.annotate(label, xy=(0.1, 1.1), xycoords='axes fraction', ha='center', fontsize=16)
# label physical distance to the left and up:
trans = mtransforms.ScaledTranslation(-20 / 72, 7 / 72, fig.dpi_scale_trans)
ax.text(0.0, 0.95, label, transform=ax.transAxes + trans,
fontsize=letter_fs, va='bottom', fontfamily='sans-serif', fontweight='bold')
# plt.show()
plt.savefig('./results/04_plots/stepfct_trends_beta.png', dpi=fig.dpi)
def plot_quantitative_analyses():
# ### Script for plotting Figure 3
from validation.plotting import plot_defrac_lineplot, plot_sdeqvals_vs_weight, plot_cc_score_hist
from validation.val_utils import compare_grn_vs_rand_background
from switchtfi.tf_ranking import grn_to_nx
# Load AnnData
bdata = sc.read_h5ad('./results/03_validation/anndata/switchde_magic_nozeroinflated_pre-endocrine_beta.h5ad')
erydata = sc.read_h5ad('./results/03_validation/anndata/switchde_magic_nozeroinflated_erythrocytes.h5ad')
# bdata = sc.read_h5ad('./results/03_validation/anndata/switchde_log1p_norm_zeroinflated_pre-endocrine_beta.h5ad')
# Load result dataframes
bgrn = pd.read_csv('./results/02_switchtfi/endocrine/beta/grn.csv', index_col=0)
bbase_grn = load_grn_json('./results/02_switchtfi/endocrine/beta/grn.json')
erygrn = pd.read_csv('./results/02_switchtfi/hematopoiesis/grn.csv', index_col=0)
erybase_grn = load_grn_json('./results/02_switchtfi/hematopoiesis/grn.json')
# Combine ptDE q-values using Fishers method for each edge (TF, target) => edgewise ptDE q-values
# Add minimal eps to 0 q-values for numeric stability, then combine q-values => joint q-value per edge
bq = bdata.var['switchde_qval'].to_numpy()
bq[bq == 0] = np.finfo(np.float64).eps
bdata.var['switchde_qval'] = bq
eryq = erydata.var['switchde_qval'].to_numpy()
eryq[eryq == 0] = np.finfo(np.float64).eps
erydata.var['switchde_qval'] = eryq
bbase_grn = combine_p_vals_fisher(grn=bbase_grn, anndata=bdata)
bminw = bgrn['weight'].to_numpy().min()
erybase_grn = combine_p_vals_fisher(grn=erybase_grn, anndata=erydata)
eryminw = erygrn['weight'].to_numpy().min()
# Compute correlation
bcorr = pearsonr(bbase_grn['weight'].to_numpy(), bbase_grn['switchde_qvals_combined_fisher'].to_numpy())
erycorr = pearsonr(erybase_grn['weight'].to_numpy(), erybase_grn['switchde_qvals_combined_fisher'].to_numpy())
print('###### Correlations(weights, combined q-vals): ')
print(f'### Beta: {bcorr}')
print(f'### Erythrocytes: {erycorr}')
# Again for plotting add eps
bqcomb = bbase_grn['switchde_qvals_combined_fisher'].to_numpy()
bqcomb[bqcomb == 0] = np.finfo(np.float64).eps
bbase_grn['switchde_qvals_combined_fisher'] = bqcomb
eryqcomb = erybase_grn['switchde_qvals_combined_fisher'].to_numpy()
eryqcomb[eryqcomb == 0] = np.finfo(np.float64).eps
erybase_grn['switchde_qvals_combined_fisher'] = eryqcomb
fig = plt.figure(figsize=(12, 8), constrained_layout=True, dpi=300)
axd = fig.subplot_mosaic(
"""
ABC
DEF
"""
)
# General
title_fs = 16
ax_fs = 16
legend_fs = 14
letter_fs = 20
# q-vals vs weight
dot_size0 = 6
# defrac lineplot
dot_size1 = 14
plot_sdeqvals_vs_weight(
grn=bbase_grn, comb_qval_alpha=0.01, weight_threshold=bminw, title=r'$\beta$-cell transition',
legend_pos='upper center', size=dot_size0, ax_label_fontsize=ax_fs, title_fontsize=title_fs,
legend_fontsize=legend_fs, axs=axd['A'])
plot_sdeqvals_vs_weight(
grn=erybase_grn, comb_qval_alpha=0.01, weight_threshold=eryminw, title='Erythrocyte differentiation',
legend_pos='upper center', size=dot_size0, ax_label_fontsize=ax_fs, title_fontsize=title_fs,
legend_fontsize=legend_fs, axs=axd['D'])
plot_defrac_lineplot(
adata=bdata, base_grn=bbase_grn, pruned_grn=bgrn, switchde_alpha=0.01, title=r'$\beta$-cell transition',
size=dot_size1, ax_label_fontsize=ax_fs, title_fontsize=title_fs, legend_fontsize=legend_fs,
legend_pos=(0.5, 0.65), axs=axd['B'], verbosity=1)
plot_defrac_lineplot(
adata=erydata, base_grn=erybase_grn, pruned_grn=erygrn, switchde_alpha=0.01,
title='Erythrocyte differentiation', size=dot_size1, ax_label_fontsize=ax_fs, title_fontsize=title_fs,
legend_fontsize=legend_fs, legend_pos=(0.5, 0.65), axs=axd['E'], verbosity=1)
# ### Plot histograms of percentage of Lcc nodes in randomly sampled GRNs of same size as transition GRN
n = 10000
bccs_list, bn_vert_list = compare_grn_vs_rand_background(base_grn=bbase_grn, transition_grn=bgrn, n=n)
eryccs_list, eryn_vert_list = compare_grn_vs_rand_background(base_grn=erybase_grn, transition_grn=erygrn, n=n)
btgrn = grn_to_nx(grn=bgrn).to_undirected()
erytgrn = grn_to_nx(grn=erygrn).to_undirected()
bccs = list(nx.connected_components(btgrn))
eryccs = list(nx.connected_components(erytgrn))
plot_cc_score_hist(
ccs=bccs, n_vert=btgrn.number_of_nodes(), ccs_list=bccs_list, n_vert_list=bn_vert_list,
titel=r'$\beta$-cell transition', ax_label_fontsize=ax_fs, title_fontsize=title_fs,
legend_fontsize=legend_fs, axs=axd['C'])
plot_cc_score_hist(
ccs=eryccs, n_vert=erytgrn.number_of_nodes(), ccs_list=eryccs_list, n_vert_list=eryn_vert_list,
titel='Erythrocyte differentiation', ax_label_fontsize=ax_fs, title_fontsize=title_fs,
legend_fontsize=legend_fs, axs=axd['F'])
# Annotate subplot mosaic tiles with labels
for label, ax in axd.items():
trans = mtransforms.ScaledTranslation(-20 / 72, 7 / 72, fig.dpi_scale_trans)
ax.text(0.0, 1.0, label, transform=ax.transAxes + trans,
fontsize=letter_fs, va='bottom', fontfamily='sans-serif', fontweight='bold')
# plt.show()
plt.savefig('./results/04_plots/quantitative_beta_ery.png', dpi=fig.dpi)
def plot_qualitative_analysis():
# ### Script for plotting Figure 4
from validation.plotting import plot_circled_tfs, plot_enrichr_results, plot_gam_gene_trend_heatmap
# ### Load ranked TFs for alpha- and beta-cell transition
atfs = pd.read_csv('./results/02_switchtfi/endocrine/alpha/ranked_tfs.csv', index_col=[0])
btfs = pd.read_csv('./results/02_switchtfi/endocrine/beta/ranked_tfs.csv', index_col=[0])
p = False
if p:
top_k = 10
print(f'### Top {top_k} TFs, alpha:')
for tf in atfs['gene'][0:top_k].tolist():
print(tf)
print(f'### Top {top_k} TFs, beta:')
for tf in btfs['gene'][0:top_k].tolist():
print(tf)
# ### Load AnnData with gene trends for alpha-cell transition
adata = sc.read_h5ad('./results/03_validation/anndata/trend_pre-endocrine_alpha.h5ad')
# ### Load ENRICHR GSEA results for beta-cell transition driver TFs and targets of Ybx1
b_gobp = pd.read_csv(
os.path.join(
os.getcwd(), 'results/03_validation/gsea_results/beta_pr_GO_Biological_Process_2023_table.txt'),
delimiter='\t'
)
b_reactome = pd.read_csv(
os.path.join(
os.getcwd(), 'results/03_validation/gsea_results/beta_pr_Reactome_2022_table.txt'),
delimiter='\t'
)
# ### Plot results for Beta dataset ### #
fig = plt.figure(figsize=(13, 9), constrained_layout=True, dpi=300)
mosaic = [
['A', 'A', '.'],
['A', 'A', 'B'],
['A', 'A', 'B'],
['A', 'A', '.'],
['C', 'C', 'C'],
]
axd = fig.subplot_mosaic(
mosaic=mosaic,
gridspec_kw={"height_ratios": [1/4, 1/4, 1/4, 1/4, 3], }
# "width_ratios": [3, 1]},
)
# General
title_fs = 20
ax_fs = 16
letter_fs = 20
# Top 10 driver TFs plot
gene_name_fs = 16
# Gene trends plot
gene_trend_anno_fs = 14
# Enrichr results plots
term_fs = 16
legend_fs = 16
# ### Plot Top 10 driver TFs
plot_circled_tfs(
res_df=atfs, topk=10, fontsize=gene_name_fs, res_df2=btfs, title=None, title_fontsize=None, plottwoinone=True,
y_ticks=(r'$\alpha$:', r'$\beta$:'), ax_label_fontsize=ax_fs, axs=axd['A'])
# ### Plot gene trends of top 10 alpha-cell transition driver TFs
plot_gam_gene_trend_heatmap(
adata=adata,
gene_names=atfs['gene'][0:10].tolist(),
use_trend=True,
annotate_gene_names=True,
gene_names_fontsize=gene_trend_anno_fs,
ax_label_fontsize=ax_fs,
show=False,
axs=axd['B'],
plot_colorbar=False,
title=r'$\alpha$-cell transition',
title_fontsize=title_fs)
# ### Plot GSEA results for top 10 beta-cell transition driver TFs
plot_enrichr_results(
res_dfs=[b_gobp, b_reactome],
x='Adjusted P-value',
top_k=[6, 6],
reference_db_names=['GO_Biological_Process_2023', 'Reactome_2022'],
title=r'$\beta$-cell transition',
title_fontsize=title_fs,
term_fontsize=term_fs,
ax_label_fontsize=ax_fs,
legend_fontsize=legend_fs,
axs=axd['C']
)
# Annotate subplot mosaic tiles with labels
for label, ax in axd.items():
trans = mtransforms.ScaledTranslation(-20 / 72, 7 / 72, fig.dpi_scale_trans)
ax.text(0.0, 1.0, label, transform=ax.transAxes + trans,
fontsize=letter_fs, va='bottom', fontfamily='sans-serif', fontweight='bold')
# plt.show()
plt.savefig('./results/04_plots/qualitative_beta.png', dpi=fig.dpi)
def plot_hypotheses_generation_ybx1():
# ### Script for plotting Figure 5
from validation.plotting import plot_enrichr_results
from switchtfi.plotting import plot_regulon
# ### Load transition GRN of beta-cell transition
bgrn = pd.read_csv('./results/02_switchtfi/endocrine/beta/grn.csv', index_col=0)
# ### Load ENRICHR GSEA results for beta-cell transition driver TFs and targets of Ybx1
ybx1_gobp = pd.read_csv(
os.path.join(
os.getcwd(), 'results/03_validation/gsea_results/beta_ybx1_targets_GO_Biological_Process_2023_table.txt'),
delimiter='\t'
)
ybx1_gocc = pd.read_csv(
os.path.join(
os.getcwd(), 'results/03_validation/gsea_results/beta_ybx1_targets_GO_Cellular_Component_2023_table.txt'),
delimiter='\t'
)
ybx1_reactome = pd.read_csv(
os.path.join(
os.getcwd(), 'results/03_validation/gsea_results/beta_ybx1_targets_Reactome_2022_table.txt'),
delimiter='\t'
)
# ### Plot results for Beta dataset ### #
fig = plt.figure(figsize=(13, 8), constrained_layout=True, dpi=300)
mosaic = [
['A', 'A', 'A', 'A'],
['B', 'B', 'B', 'B']
]
axd = fig.subplot_mosaic(
mosaic=mosaic,
gridspec_kw={'height_ratios': [5, 6], }
# "width_ratios": [3, 1]},
)
# General
ax_fs = 16
letter_fs = 20
# Enrichr results plots
term_fs = 16
legend_fs = 16
# Regulon
nodes_size = 1000
gene_fs = 16
# ### Plot the top 20 targets of Ybx1
plot_regulon(
grn=bgrn, tf='Ybx1', sort_by='score', top_k=20, title=None, title_fontsize=None,
font_size=gene_fs, node_size=nodes_size, show=False, dpi=300, axs=axd['A'])
# ### Plot the GSEA results for the top 20 targets of Ybx1
plot_enrichr_results(
res_dfs=[ybx1_gobp, ybx1_reactome, ybx1_gocc],
x='Adjusted P-value',
top_k=[6, 3, 3],
reference_db_names=['GO_Biological_Process_2023', 'Reactome_2022', 'GO_Cellular_Component_2023'],
title=None,
title_fontsize=None,
term_fontsize=term_fs,
truncate_term_k=80,
ax_label_fontsize=ax_fs,
legend_fontsize=legend_fs,
axs=axd['B']
)
# Annotate subplot mosaic tiles with labels
for label, ax in axd.items():
trans = mtransforms.ScaledTranslation(-20 / 72, 7 / 72, fig.dpi_scale_trans)
ax.text(0.0, 1.0, label, transform=ax.transAxes + trans,
fontsize=letter_fs, va='bottom', fontfamily='sans-serif', fontweight='bold')
# plt.show()
plt.savefig('./results/04_plots/hypothesis_gen_ybx1.png', dpi=fig.dpi)
def plot_method_comparison():
# ### Script for plotting Figure 6
from validation.plotting import plot_upset_plot
from validation.plotting import plot_defrac_in_top_k_lineplot, plot_digest_results
# Load AnnData
bdata = sc.read_h5ad('./results/03_validation/anndata/switchde_log1p_norm_zeroinflated_pre-endocrine_beta.h5ad')
# bdata = sc.read_h5ad('./results/03_validation/anndata/switchde_magic_nozeroinflated_pre-endocrine_beta.h5ad')
# sc.pl.umap(bdata, color='palantir_pseudotime', show=False)
# plt.savefig('./results/04_plots/bumap.png')
# ### Load result dataframes
btfs = pd.read_csv('./results/02_switchtfi/endocrine/beta/ranked_tfs.csv', index_col=[0])
btfs_outdeg = pd.read_csv('./results/02_switchtfi/endocrine/beta/outdeg_ranked_tfs.csv', index_col=[0])
res_base_p = './results/03_validation/driver_genes/'
bcr_genes = pd.read_csv(os.path.join(res_base_p, 'beta_cellrank_driver_genes.csv'), index_col=[0])
bspjc_genes = pd.read_csv(os.path.join(res_base_p, 'beta_splicejac_driver_genes.csv'), index_col=[0])
bdrivaer_genes = pd.read_csv(os.path.join(res_base_p, 'beta_drivaer_driver_genes.csv'), index_col=[0])
b_res_list = [bcr_genes, bspjc_genes, bdrivaer_genes, btfs_outdeg, btfs]
# Define names of method and color to be used in plots
name_list = ['CellRank', 'spliceJAC', 'DrivAER', 'SwitchTFI outdeg', 'SwitchTFI PageRank']
colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#f52891cc', '#d62728']
# ### Define paths to DIGEST results
digest_base_p = './results/03_validation/digest_results'
bp1 = os.path.join(digest_base_p, 'top20_beta_cellrank/542a7e3c-f24d-4856-9a7f-1e2b7c777e8e_result.json')
bp2 = os.path.join(digest_base_p, 'top20_beta_splicejac/9706eeaa-ba4d-4189-aa71-a6248b60a0f6_result.json')
bp3 = os.path.join(digest_base_p, 'top20_beta_drivaer/59f0306c-3da6-49bb-8391-55a8c59f1e70_result.json')
bp4 = os.path.join(digest_base_p, 'top20_beta_switchtfi_outdeg/6adfd6ec-b32d-418e-8451-0e81f1c89914_result.json')
bp5 = os.path.join(digest_base_p, 'top20_beta_switchtfi/c0932ef3-f281-46d5-acdd-358551495dbe_result.json')
bpl = [bp1, bp2, bp3, bp4, bp5]
# Same for erythrocytes, except for no splicejac
erydata = sc.read_h5ad('./results/03_validation/anndata/switchde_log1p_norm_zeroinflated_erythrocytes.h5ad')
# erydata = sc.read_h5ad('./results/03_validation/anndata/switchde_magic_nozeroinflated_erythrocytes.h5ad')
erytfs = pd.read_csv('./results/02_switchtfi/hematopoiesis/ranked_tfs.csv', index_col=[0])
erytfs_outdeg = pd.read_csv('./results/02_switchtfi/hematopoiesis/outdeg_ranked_tfs.csv', index_col=[0])
erycr_genes = pd.read_csv(os.path.join(res_base_p, 'ery_cellrank_driver_genes.csv'), index_col=[0])
erydrivaer_genes = pd.read_csv(os.path.join(res_base_p, 'ery_drivaer_driver_genes.csv'), index_col=[0])
ery_res_list = [erycr_genes, erydrivaer_genes, erytfs_outdeg, erytfs]
eryp1 = os.path.join(digest_base_p, 'top20_ery_cellrank/a4077393-4050-42ed-bf1a-a9ab3ea3726b_result.json')
eryp2 = os.path.join(digest_base_p, 'top20_ery_drivaer/0d0d89e4-ea15-4ee7-8cc2-da8fe8d39a4f_result.json')
eryp3 = os.path.join(digest_base_p, 'top20_ery_switchtfi_outdeg/70becca4-53b6-4ede-a7f5-419b0e3da377_result.json')
eryp4 = os.path.join(digest_base_p, 'top20_ery_switchtfi/4dfe96bb-48ff-4d23-b9c3-7e94f8bf3da0_result.json')
erypl = [eryp1, eryp2, eryp3, eryp4]
# Define names of method and color to be used in plots
ery_name_list = ['CellRank', 'DrivAER', 'SwitchTFI outdeg', 'SwitchTFI PageRank']
ery_colors = ['#1f77b4', '#2ca02c', '#f52891cc', '#d62728']
# ### Plot results for Beta dataset ###
fig = plt.figure(figsize=(14, 8), constrained_layout=True, dpi=300)
axd = fig.subplot_mosaic(
[
['A', 'C', 'E'], # ['A.β', 'B.β', 'C.β'],
['B', 'D', 'F'], # ['A.ery', 'B.ery', 'C.ery'],
],
)
# General
title_fs = 20
ax_fs = 18
legend_fs = 12
letter_fs = 20
# Digest res
dataset_fs = 18
plt_xlabel = False
dotsize = 9
# ### Plot Upsetplot
plot_upset_plot(
res_list=b_res_list,
names=tuple(name_list),
title=r'$\beta$-cell transition',
title_fontsize=title_fs,
axs=axd['A'],
plot_folder='./results/04_plots',
fn_prefix='beta')
plot_upset_plot(
res_list=ery_res_list,
names=tuple(ery_name_list),
title='Erythrocyte differentiation',
title_fontsize=title_fs,
axs=axd['B'],
plot_folder='./results/04_plots',
fn_prefix='ery')
plot_defrac_in_top_k_lineplot(
res_df_list=b_res_list,
adata=bdata,
switchde_alpha=0.01,
max_top_k=20,
interval=5,
axs=axd['C'],
title=r'$\beta$-cell transition',
title_fontsize=title_fs,
ax_label_fontsize=ax_fs,
legend_fontsize=legend_fs,
show=False,
names=name_list,
markers=['s', 'd', '^', 'o', 'o'],
palette=colors,
hue_order=('CellRank', 'spliceJAC', 'DrivAER', 'SwitchTFI outdeg', 'SwitchTFI PageRank'),
legend_loc='lower right')
plot_defrac_in_top_k_lineplot(
res_df_list=ery_res_list,
adata=erydata,
switchde_alpha=0.01,
max_top_k=20,
interval=5,
axs=axd['D'],
title='Erythrocyte differentiation',
title_fontsize=title_fs,
ax_label_fontsize=ax_fs,
legend_fontsize=legend_fs,
show=False,
names=ery_name_list,
markers=['s', '^', 'o', 'o'],
palette=ery_colors,
hue_order=('CellRank', 'DrivAER', 'SwitchTFI outdeg', 'SwitchTFI PageRank'),
legend_loc='center right')
plot_digest_results(
digest_res_path_list=bpl,
name_list=name_list,
color_list=colors,
title=r'$\beta$-cell transition',
size=dotsize,
title_fontsize=title_fs,
ax_label_fontsize=ax_fs,
legend_fontsize=legend_fs,
x_ticks_fontsize=dataset_fs,
plot_xlabel=plt_xlabel,
axs=axd['E'],
verbosity=1)
plot_digest_results(
digest_res_path_list=erypl,
name_list=ery_name_list,
color_list=ery_colors,
title='Erythrocyte differentiation',
size=dotsize,
title_fontsize=title_fs,
ax_label_fontsize=ax_fs,
legend_fontsize=legend_fs,
x_ticks_fontsize=dataset_fs,
plot_xlabel=plt_xlabel,
axs=axd['F'],
verbosity=1)
for label, ax in axd.items():
trans = mtransforms.ScaledTranslation(-20 / 72, 7 / 72, fig.dpi_scale_trans)
if label != 'B':
ax.text(0.0, 1.0, label, transform=ax.transAxes + trans,
fontsize=letter_fs, va='bottom', fontfamily='sans-serif', fontweight='bold')
else:
ax.text(-0.2, 1.0, label, transform=ax.transAxes + trans,
fontsize=letter_fs, va='bottom', fontfamily='sans-serif', fontweight='bold')
# plt.show()
plt.savefig('./results/04_plots/method_comparison.png', dpi=fig.dpi)
# plt.savefig('./results/04_plots/method_comparison_imputed.png', dpi=fig.dpi)
def plot_alpha_res_appendix():
# ### Script for plotting Supplementary Figure 1
from validation.plotting import (
plot_defrac_lineplot, plot_sdeqvals_vs_weight, plot_cc_score_hist, plot_enrichr_results,
plot_defrac_in_top_k_lineplot, plot_digest_results, plot_upset_plot
)
from validation.val_utils import compare_grn_vs_rand_background
from switchtfi.tf_ranking import grn_to_nx
# ### Load AnnData with switchde p-values
adata = sc.read_h5ad('./results/03_validation/anndata/switchde_magic_nozeroinflated_pre-endocrine_alpha.h5ad')
# ### Load input and transition GRN
agrn = pd.read_csv('./results/02_switchtfi/endocrine/alpha/grn.csv', index_col=0)
abase_grn = load_grn_json('./results/02_switchtfi/endocrine/alpha/grn.json')
# ### Compute edg-wise combined ptDE q-values
# Add minimal eps to 0 q-values for numeric stability, then combine q-values => joint q-value per edge
aq = adata.var['switchde_qval'].to_numpy()
aq[aq == 0] = np.finfo(np.float64).eps
adata.var['switchde_qval'] = aq
abase_grn = combine_p_vals_fisher(grn=abase_grn, anndata=adata)
aminw = agrn['weight'].to_numpy().min()
# Compute correlation between edge-weights (from SwitchTFI) and ptDE edge-wise combined q-values
acorr = pearsonr(abase_grn['weight'].to_numpy(), abase_grn['switchde_qvals_combined_fisher'].to_numpy())
print('###### Correlations(weights, combined q-vals): ')
print(f'### Alpha: {acorr}')
# Again for plotting add eps to the combined q-values
aqcomb = abase_grn['switchde_qvals_combined_fisher'].to_numpy()
aqcomb[aqcomb == 0] = np.finfo(np.float64).eps
abase_grn['switchde_qvals_combined_fisher'] = aqcomb
# ### Load ENRICHR GSEA results
a_gobp = pd.read_csv(
os.path.join(
os.getcwd(), 'results/03_validation/gsea_results/alpha_pr_GO_Biological_Process_2023_table.txt'),
delimiter='\t'
)
a_reactome = pd.read_csv(
os.path.join(
os.getcwd(), 'results/03_validation/gsea_results/alpha_pr_Reactome_2022_table.txt'),
delimiter='\t'
)
# ### Load AnnData with ptDE p-values
adata = sc.read_h5ad('./results/03_validation/anndata/switchde_log1p_norm_zeroinflated_pre-endocrine_alpha.h5ad')
# ### Load result dataframes with predicted driver TFs from SwitchTFI and competitor methods
atfs = pd.read_csv('./results/02_switchtfi/endocrine/alpha/ranked_tfs.csv', index_col=[0])
atfs_outdeg = pd.read_csv('./results/02_switchtfi/endocrine/alpha/outdeg_ranked_tfs.csv', index_col=[0])
res_base_p = './results/03_validation/driver_genes/'
acr_genes = pd.read_csv(os.path.join(res_base_p, 'alpha_cellrank_driver_genes.csv'), index_col=[0])
aspjc_genes = pd.read_csv(os.path.join(res_base_p, 'alpha_splicejac_driver_genes.csv'), index_col=[0])
adrivaer_genes = pd.read_csv(os.path.join(res_base_p, 'alpha_drivaer_driver_genes.csv'), index_col=[0])
a_res_list = [acr_genes, aspjc_genes, adrivaer_genes, atfs_outdeg, atfs]
# ### Define paths to DIGEST results
digest_base_p = './results/03_validation/digest_results'
ap1 = os.path.join(digest_base_p, 'top20_alpha_cellrank/b8d14a52-62ad-49ca-871e-9b8456086669_result.json')
ap2 = os.path.join(digest_base_p, 'top20_alpha_splicejac/694d78ec-0fcd-4055-84fc-d29c53ffbde5_result.json')
ap3 = os.path.join(digest_base_p, 'top20_alpha_drivaer/c1c20b4f-4c58-4973-9cae-4fd9ced7f7d3_result.json')
ap4 = os.path.join(digest_base_p, 'top20_alpha_switchtfi_outdeg/2a70a4b4-a78e-4ade-a2a6-e0a9c2d93901_result.json')
ap5 = os.path.join(digest_base_p, 'top20_alpha_switchtfi/0abd6509-83f7-4952-bde6-717d9b6b0648_result.json')
apl = [ap1, ap2, ap3, ap4, ap5]
# ### Define names of method and color to be used in method comparison plots
name_list = ['CellRank', 'spliceJAC', 'DrivAER', 'SwitchTFI outdeg', 'SwitchTFI PageRank']
colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#f52891cc', '#d62728']
fig = plt.figure(figsize=(13, 12), constrained_layout=True, dpi=300)
mosaic = [
['A', 'B', 'C'],
['D', 'D', 'D'],
['E', 'F', 'G'],
]
axd = fig.subplot_mosaic(
mosaic=mosaic,
gridspec_kw={'height_ratios': [5, 6, 5], }
)
# General
ax_fs = 16
legend_fs0 = 14
legend_fs1 = 12
letter_fs = 20
# q-vals vs weight
dot_size0 = 6
# defrac lineplot
dot_size1 = 14
# Enrichr
term_fs = 16
# Digest res
dot_size2 = 9
dataset_fs = 18
plt_xlabel = False
# ### Plot quantitative analysis
plot_sdeqvals_vs_weight(grn=abase_grn, comb_qval_alpha=0.01, weight_threshold=aminw, title=None,
legend_fontsize=legend_fs0, ax_label_fontsize=ax_fs, legend_pos='upper center',
size=dot_size0, axs=axd['A'])
plot_defrac_lineplot(adata=adata, base_grn=abase_grn, pruned_grn=agrn, switchde_alpha=0.01, title=None,
legend_fontsize=legend_fs0, legend_pos=(0.5, 0.65), ax_label_fontsize=ax_fs, size=dot_size1,
axs=axd['B'], verbosity=1)
# ### Plot histograms of percentage of Lcc nodes in randomly sampled GRNs of same size as transition GRN
# First run comparison against random background model ...
# n = 10000
n = 10000
accs_list, an_vert_list = compare_grn_vs_rand_background(base_grn=abase_grn, transition_grn=agrn, n=n)
atgrn = grn_to_nx(grn=agrn).to_undirected()
bccs = list(nx.connected_components(atgrn))
# ... then plot the results
plot_cc_score_hist(ccs=bccs, n_vert=atgrn.number_of_nodes(), ccs_list=accs_list, n_vert_list=an_vert_list,
titel=None, ax_label_fontsize=ax_fs, legend_fontsize=legend_fs0, axs=axd['C'])
# ### Plot qualitative analysis
# Plot GSEA results for top 10 beta-cell transition driver TFs
plot_enrichr_results(
res_dfs=[a_gobp, a_reactome],
x='Adjusted P-value',
top_k=[6, 6],
reference_db_names=['GO_Biological_Process_2023', 'Reactome_2022'],
term_fontsize=term_fs,
ax_label_fontsize=ax_fs,
legend_fontsize=legend_fs0,
axs=axd['D']
)
# ### Plot method comparison
plot_upset_plot(res_list=a_res_list, names=tuple(name_list), axs=axd['E'], plot_folder='./results/04_plots',
fn_prefix='alpha_')
plot_defrac_in_top_k_lineplot(
res_df_list=a_res_list,
adata=adata,
switchde_alpha=0.01,
max_top_k=20,
interval=5,
axs=axd['F'],
title=None,
ax_label_fontsize=ax_fs,
legend_fontsize=legend_fs1,
show=False,
names=name_list,
markers=['s', 'd', '^', 'o', 'o'],
palette=colors,
hue_order=('CellRank', 'spliceJAC', 'DrivAER', 'SwitchTFI outdeg', 'SwitchTFI PageRank'))
plot_digest_results(digest_res_path_list=apl, name_list=name_list, color_list=colors, size=dot_size2,
ax_label_fontsize=ax_fs, plot_xlabel=plt_xlabel, x_ticks_fontsize=dataset_fs,
legend_fontsize=legend_fs1, axs=axd['G'], verbosity=1)
# Annotate subplot mosaic tiles with labels
for label, ax in axd.items():
trans = mtransforms.ScaledTranslation(-20 / 72, 7 / 72, fig.dpi_scale_trans)
ax.text(0.0, 1.0, label, transform=ax.transAxes + trans,
fontsize=letter_fs, va='bottom', fontfamily='sans-serif', fontweight='bold')
# plt.show()
plt.savefig('./results/04_plots/supplementary_alpha.png', dpi=fig.dpi)
def plot_ery_res_appendix():
# ### Script for plotting Supplementary Figure 2
from validation.plotting import plot_gam_gene_trend_heatmap, plot_circled_tfs, plot_enrichr_results
# ### Load AnnData with trand and with ptDE p-values
adata_trend = sc.read_h5ad('./results/03_validation/anndata/trend_erythrocytes.h5ad')
adata_sde = sc.read_h5ad('./results/03_validation/anndata/switchde_magic_nozeroinflated_erythrocytes.h5ad')
# ### Load input and transition GRN
grn = load_grn_json('./results/02_switchtfi/hematopoiesis/grn.json')
transition_grn = pd.read_csv('./results/02_switchtfi/hematopoiesis/grn.csv', index_col=0)
# ### Load ranked TFs
ranked_tfs = pd.read_csv('./results/02_switchtfi/hematopoiesis/outdeg_ranked_tfs.csv', index_col=0)
top_tfs = ranked_tfs['gene'].tolist()[0:10]
# ### Load the GSEA results
ery_gobp = pd.read_csv(
os.path.join(
os.getcwd(), 'results/03_validation/gsea_results/ery_outdeg_GO_Biological_Process_2023_table.txt'),
delimiter='\t'
)
ery_reactome = pd.read_csv(
os.path.join(
os.getcwd(), 'results/03_validation/gsea_results/ery_outdeg_Reactome_2022_table.txt'),
delimiter='\t'
)
# ### Combine the switchde q-values using Fishers method for each edge (TF, target) of the base GRN
# Add minimal eps to 0 q-values for numeric stability, then combine q-values => joint q-value per edge
q = adata_sde.var['switchde_qval'].to_numpy()
q[q == 0] = np.finfo(np.float64).eps
adata_sde.var['switchde_qval'] = q
grn = combine_p_vals_fisher(grn=grn, anndata=adata_sde)
# ### Compute correlation (just for printing, not for plotting)
corr = pearsonr(grn['weight'].to_numpy(), grn['switchde_qvals_combined_fisher'].to_numpy())
print('###### Correlations(weights, combined q-vals): ')
print(f'### q-vals: {corr}')
# Again for plotting add eps
qcomb = grn['switchde_qvals_combined_fisher'].to_numpy()
qcomb[qcomb == 0] = np.finfo(np.float64).eps
grn['switchde_qvals_combined_fisher'] = qcomb
corr = pearsonr(grn['weight'].to_numpy(), -np.log(grn['switchde_qvals_combined_fisher'].to_numpy()))
print(f'### -log(q-vals): {corr}')
# Plot results
fig = plt.figure(figsize=(12, 12), constrained_layout=True, dpi=300)
mosaic = [
['A', ],
['B', ],
['C', ],
]
axd = fig.subplot_mosaic(
mosaic=mosaic,
gridspec_kw={'height_ratios': [5, 4, 10], }
)
# General
ax_fs = 18
letter_fs = 20
# Top 10 driver TFs plot
gene_name_fs = 18
# Gene trends plot
gene_trend_anno_fs = 15
# Enrichr results plots
term_fs = 16
legend_fs = 16
plot_gam_gene_trend_heatmap(
adata=adata_trend,
gene_names=top_tfs,
gene_names_fontsize=gene_trend_anno_fs,
ax_label_fontsize=ax_fs,
use_trend=True,
annotate_gene_names=True,
show=False,
axs=axd['A'],
colorbar_pad=-0.1)
plot_circled_tfs(res_df=ranked_tfs, topk=10, fontsize=gene_name_fs, ax_label_fontsize=ax_fs, axs=axd['B'])
plot_enrichr_results(
res_dfs=[ery_gobp, ery_reactome],
x='Adjusted P-value',
top_k=[6, 6, ],
reference_db_names=['GO_Biological_Process_2023', 'Reactome_2022', ],
term_fontsize=term_fs,
legend_fontsize=legend_fs,
ax_label_fontsize=ax_fs,
axs=axd['C']
)
for label, ax in axd.items():
trans = mtransforms.ScaledTranslation(-20 / 72, 7 / 72, fig.dpi_scale_trans)
ax.text(0.0, 1.0, label, transform=ax.transAxes + trans,
fontsize=letter_fs, va='bottom', fontfamily='sans-serif', fontweight='bold')
# plt.show()
plt.savefig('./results/04_plots/supplementary_ery.png', dpi=fig.dpi)
def plot_grns():
# ### Script for plotting Supplementary Figure 3
from switchtfi.plotting import plot_grn
# Load GRNs
# abasegrn = load_grn_json('./results/02_switchtfi/endocrine/alpha/grn.json')
# agrn = pd.read_csv('./results/02_switchtfi/endocrine/alpha/grn.csv', index_col=0)
# bbasegrn = load_grn_json('./results/02_switchtfi/endocrine/beta/grn.json')
# bgrn = pd.read_csv('./results/02_switchtfi/endocrine/beta/grn.csv', index_col=0)
erybasegrn = load_grn_json('./results/02_switchtfi/hematopoiesis/grn.json')
erygrn = pd.read_csv('./results/02_switchtfi/hematopoiesis/grn.csv', index_col=0)
fig = plt.figure(figsize=(6, 12), constrained_layout=True, dpi=600)
axd = fig.subplot_mosaic(
"""
A
B
"""
)
letter_fs = 26
plot_grn(grn=erybasegrn, plot_folder='./results/04_plots', fn_prefix='base_', axs=axd['A'])
plot_grn(grn=erygrn, plot_folder='./results/04_plots', fn_prefix='', axs=axd['B'])
for label, ax in axd.items():
trans = mtransforms.ScaledTranslation(-20 / 72, 7 / 72, fig.dpi_scale_trans)
ax.text(0.0, 1.0, label, transform=ax.transAxes + trans,
fontsize=letter_fs, va='bottom', fontfamily='sans-serif', fontweight='bold')
# plt.show()
plt.savefig('./results/04_plots/supplementary_grns.png')
if __name__ == '__main__':
# plot_step_fct_and_trends()
# plot_quantitative_analyses()
# plot_qualitative_analysis()
# plot_hypotheses_generation_ybx1()
# plot_method_comparison()
# plot_alpha_res_appendix()
# plot_ery_res_appendix()
# plot_grns()
print('done')