-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_parser.py
1014 lines (871 loc) · 38.4 KB
/
data_parser.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
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
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Functions for obtaining data used in Plotly visualizations.
Entry point is ``get_data``.
"""
from copy import deepcopy
import csv
from itertools import compress, islice
import os
from definitions import (GENE_POSITIONS_DICT, NSP_POSITIONS_DICT,
DEFAULT_REFERENCE_HIDDEN_STRAINS,
DEFAULT_REFERENCE_STRAIN_ORDER,
FIRST_REGION, LAST_REGION)
def map_pos_to_gene(pos):
"""Map a nucleotide position to a gene.
:param pos: Nucleotide position
:type pos: int
:return: Gene at nucleotide position ``pos``
:rtype: str
"""
for gene in GENE_POSITIONS_DICT:
start = GENE_POSITIONS_DICT[gene]["start"]
end = GENE_POSITIONS_DICT[gene]["end"]
if start <= pos <= end:
return gene
return "INTERGENIC"
def map_pos_to_nsp(pos):
"""Map a nucleotide position to a NSP.
NSP == non-structural protein
:param pos: Nucleotide position
:type pos: int
:return: NSP at nucleotide position ``pos``
:rtype: str
"""
for nsp in NSP_POSITIONS_DICT:
start = NSP_POSITIONS_DICT[nsp]["start"]
end = NSP_POSITIONS_DICT[nsp]["end"]
if start <= pos <= end:
return nsp
return "n/a"
def parse_gvf_sample_name(path):
"""Parse sample group name from gvf file header.
:param path: Path to gvf file to parse
:type path: str
:return: Gvf file sample group
:rtype: str
"""
with open(path, encoding="utf-8") as fp:
# Skip first two lines
next(fp)
next(fp)
# "##sample-description sample_desc=x;sample_group=y;"
sample_desc = fp.readline()
# "sample_desc=x;sample_group=y"
sample_desc_vals_str = sample_desc.split()[1].strip(";")
# [["sample_desc", "x"], ["sample_group", "y"]]
sample_desc_vals_list = \
[e.split("=") for e in sample_desc_vals_str.split(";")]
# {"sample_desc": "x", "sample_group": "y"}
return dict(sample_desc_vals_list)["sample_group"]
def parse_gvf_sample_variants(path):
"""Parse gvf file variant data relevant to viz.
:param path: Path to gvf file to parse
:type path: str
:return: Relevant variant data from gvf files used by viz
:rtype: dict
"""
with open(path, encoding="utf-8") as fp:
# Skip gvf header rows
reader = csv.DictReader(islice(fp, 4, None), delimiter="\t")
parsing_first_row = True
for row in reader:
attrs_first_split = row["#attributes"].split(";")[:-1]
attrs_second_split = \
[x.split("=", 1) for x in attrs_first_split]
attrs = {k: v for k, v in attrs_second_split}
if parsing_first_row:
# Default values
variant = None
variant_type = None
status = None
sample_size = attrs["sample_size"]
if "variant" in attrs:
variant = attrs["variant"]
if "variant_type" in attrs:
variant_type = attrs["variant_type"]
if "status" in attrs:
status = attrs["status"]
ret = {
"mutations": {},
"variant": variant,
"variant_type": variant_type,
"status": status,
"sample_size": sample_size
}
parsing_first_row = False
pos = row["#start"]
if pos not in ret["mutations"]:
ret["mutations"][pos] = []
mutation_name = attrs["Name"]
cond = attrs["alias"] not in {"n/a", mutation_name}
mutation_alias = attrs["alias"] if cond else ""
alt = attrs["Variant_seq"]
mutation_dict = {}
for existing_dict in ret["mutations"][pos]:
cond1 = existing_dict["mutation_name"] == mutation_name
cond2 = existing_dict["alt"] == alt
if cond1 and cond2:
mutation_dict = existing_dict
break
if not mutation_dict:
mutation_dict = {
"ref": attrs["Reference_seq"],
"alt": alt,
"gene": attrs["vcf_gene"],
"ao": float(attrs["ao"]),
"dp": float(attrs["dp"]),
"multi_aa_name": attrs["multi_aa_name"],
"clade_defining":
attrs["clade_defining"] == "True",
"hidden_cell": False,
"mutation_name": mutation_name,
"mutation_alias": mutation_alias,
"functions": {}
}
alt_freq = mutation_dict["ao"] / mutation_dict["dp"]
mutation_dict["alt_freq"] = str(round(alt_freq, 4))
mutation_type = row["#type"]
if mutation_type == "ins":
mutation_dict["mutation_type"] = "insertion"
elif mutation_type == "del":
mutation_dict["mutation_type"] = "deletion"
else:
mutation_dict["mutation_type"] = mutation_type
ret["mutations"][pos].append(mutation_dict)
fn_dict = mutation_dict["functions"]
fn_category = attrs["function_category"].strip('"')
if not fn_category or fn_category == "n/a":
continue
if fn_category not in fn_dict:
fn_dict[fn_category] = {}
fn_desc = attrs["function_description"].strip('"')
fn_source = attrs["source"].strip('"')
fn_citation = attrs["citation"].strip('"')
fn_dict[fn_category][fn_desc] = \
{"source": fn_source, "citation": fn_citation}
return ret
def filter_parsed_mutations_by_clade_defining(parsed_mutations):
"""Hide non-clade defining mutations from parsed gvf mutations.
:param parsed_mutations: ``parse_gvf_sample_variants`` ret "mutations" vals
:type parsed_mutations: dict
:return: ``parsed_gvf_dirs`` with non-clade defining mutations
labeled as hidden.
:rtype: dict
"""
ret = deepcopy(parsed_mutations)
for strain in parsed_mutations:
for pos in parsed_mutations[strain]:
for i, mutation in enumerate(parsed_mutations[strain][pos]):
if not mutation["clade_defining"]:
ret[strain][pos][i]["hidden_cell"] = True
return ret
def filter_parsed_mutations_by_freq(parsed_mutations, min_mutation_freq,
max_mutation_freq):
"""Hide mutations of specific frequencies from parsed gvf file.
:param parsed_mutations: ``parse_gvf_sample_variants`` ret "mutations" vals
:type parsed_mutations: dict
:param min_mutation_freq: Minimum mutation frequency required to
not hide mutations.
:type min_mutation_freq: float
:param max_mutation_freq: Maximum mutation frequency required to
not hide mutations.
:type max_mutation_freq: float
:return: ``parsed_gvf_dirs`` with mutations of specified
frequencies labeled as hidden.
:rtype: dict
"""
ret = deepcopy(parsed_mutations)
for strain in parsed_mutations:
for pos in parsed_mutations[strain]:
for i, mutation in enumerate(parsed_mutations[strain][pos]):
alt_freq = float(mutation["alt_freq"])
cond1 = alt_freq < min_mutation_freq
cond2 = alt_freq > max_mutation_freq
if cond1 or cond2:
ret[strain][pos][i]["hidden_cell"] = True
return ret
def get_data(dirs, show_clade_defining=False, hidden_strains=None,
strain_order=None, min_mutation_freq=None,
max_mutation_freq=None):
"""Get relevant data for Plotly visualizations in this application.
This will include table and histogram data, which is straight
forward. But this will also include various information related to
the main heatmap, including heatmap x y coordinates for mutations,
insertions, deletions, and hover text. This will also include
information for rendering the mutation frequency slider.
Basically, this function gives us data to plug into the
visualization functions of Plotly.
This relevant data is parsed from gvf files across one or more
folders, with each gvf file in the form of the files found in
``reference_data/``.
:param dirs: List of paths to folders to obtain data from
:type dirs: list[str]
:param show_clade_defining: Set non-clade defining mutations as
hidden.
:type show_clade_defining: bool
:param hidden_strains: List of strains from the dirs that the user
does not want to display in the heatmap and table.
:type hidden_strains: None | list[str]
:param strain_order: Order of strains from the dirs that the user
wants to display in the heatmap.
:type strain_order: list[str]
:param min_mutation_freq: Set mutations with a lower frequency than
this as hidden.
:type min_mutation_freq: int|float
:param max_mutation_freq: Set mutations with a higher frequency
than this as hidden.
:type max_mutation_freq: int|float
:return: Information on relevant columns in gvf files stored in
folders listed in dirs.
:rtype: dict
"""
# Default view
if not strain_order:
strain_order = DEFAULT_REFERENCE_STRAIN_ORDER
if hidden_strains is None:
hidden_strains = DEFAULT_REFERENCE_HIDDEN_STRAINS
# Only display top 100 strains
more_hidden_strains = \
[e for i, e in enumerate(strain_order) if e not in hidden_strains]
more_hidden_strains = \
[e for i, e in enumerate(more_hidden_strains) if i > 100]
hidden_strains += more_hidden_strains
# Faster obj to work with
hidden_strains_set = set(hidden_strains)
# Faster sort with this obj
strain_order_dict = {s: i for i, s in enumerate(strain_order)}
def strain_path_sort_key(strain_path):
strain = strain_path[0]
if strain in strain_order_dict:
return strain_order_dict[strain], strain
else:
return len(strain_order_dict), strain
dir_strains_dict = {}
parsed_gvf_dirs = {}
for dir_ in dirs:
dir_entry_paths = \
[e.path for e in os.scandir(dir_) if e.path.endswith(".gvf")]
dir_entry_strains = \
[parse_gvf_sample_name(e) for e in dir_entry_paths]
strain_paths_dict = dict(zip(dir_entry_strains, dir_entry_paths))
sorted_strain_paths_dict = \
dict(sorted(strain_paths_dict.items(), key=strain_path_sort_key))
dir_strains_dict[dir_] = list(sorted_strain_paths_dict.keys())
visible_sorted_strain_paths_dict = \
{k: v for k, v in sorted_strain_paths_dict.items()
if k not in hidden_strains_set}
# Heatmap displays rows in reverse
reversed_items = reversed(visible_sorted_strain_paths_dict.items())
parsed_gvf_dir = \
{s: parse_gvf_sample_variants(p) for s, p in reversed_items}
parsed_gvf_dirs = {**parsed_gvf_dirs, **parsed_gvf_dir}
parsed_mutations = \
{k: v["mutations"] for k, v in parsed_gvf_dirs.items()}
sample_sizes = {k: v["sample_size"] for k, v in parsed_gvf_dirs.items()}
# These are dictionary because they are in the return val, which
# needs to be json compatible (how Dash moves content across
# network).
voc_strains = {k: None for k in parsed_gvf_dirs
if parsed_gvf_dirs[k]["variant_type"] == "VOC"}
voi_strains = {k: None for k in parsed_gvf_dirs
if parsed_gvf_dirs[k]["variant_type"] == "VOI"}
circulating_strains = \
{k: None for k in parsed_gvf_dirs
if parsed_gvf_dirs[k]["status"] == "actively_circulating"}
variants_dict = {k: v["variant"] for k, v in parsed_gvf_dirs.items()}
visible_parsed_mutations = parsed_mutations
if show_clade_defining:
visible_parsed_mutations = \
filter_parsed_mutations_by_clade_defining(visible_parsed_mutations)
mutation_freq_slider_vals = \
get_mutation_freq_slider_vals(visible_parsed_mutations)
if min_mutation_freq is not None and max_mutation_freq is not None:
visible_parsed_mutations = \
filter_parsed_mutations_by_freq(visible_parsed_mutations,
min_mutation_freq,
max_mutation_freq)
intra_col_mutation_pos_dict = \
get_intra_col_mutation_pos_dict(parsed_mutations)
ret = {
"heatmap_cells_tickvals":
get_heatmap_cells_tickvals(intra_col_mutation_pos_dict),
"heatmap_x_nt_pos":
get_heatmap_x_nt_pos(intra_col_mutation_pos_dict),
"heatmap_y_strains":
get_heatmap_y_strains(visible_parsed_mutations),
"heatmap_y_sample_sizes":
get_heatmap_y_sample_sizes(visible_parsed_mutations, sample_sizes),
"tables":
get_tables(visible_parsed_mutations),
"histogram_x":
get_histogram_x(visible_parsed_mutations),
"dir_strains_dict":
dir_strains_dict,
"hidden_strains":
hidden_strains,
"voc_strains":
voc_strains,
"voi_strains":
voi_strains,
"circulating_strains":
circulating_strains,
"variants_dict":
variants_dict,
"all_strains":
[i for v in dir_strains_dict.values() for i in v],
"mutation_freq_slider_vals":
mutation_freq_slider_vals,
"insertions_x":
get_insertions_x(visible_parsed_mutations,
intra_col_mutation_pos_dict),
"insertions_y":
get_insertions_y(visible_parsed_mutations),
"deletions_x":
get_deletions_x(visible_parsed_mutations,
intra_col_mutation_pos_dict),
"deletions_y":
get_deletions_y(visible_parsed_mutations),
"heatmap_z":
get_heatmap_z(visible_parsed_mutations,
intra_col_mutation_pos_dict,
sample_sizes),
"heatmap_hover_text":
get_heatmap_hover_text(visible_parsed_mutations,
intra_col_mutation_pos_dict),
"heatmap_mutation_names":
get_heatmap_mutation_names(visible_parsed_mutations,
intra_col_mutation_pos_dict),
"heatmap_mutation_fns":
get_heatmap_mutation_fns(visible_parsed_mutations,
intra_col_mutation_pos_dict),
"heatmap_x_genes":
get_heatmap_x_genes(intra_col_mutation_pos_dict),
"heatmap_x_nsps":
get_heatmap_x_nsps(intra_col_mutation_pos_dict),
"jump_to_info_dict":
get_jump_to_info_dict(visible_parsed_mutations)
}
ret["heatmap_x_tickvals"] = \
get_heatmap_x_tickvals(ret["heatmap_cells_tickvals"])
ret["heatmap_x_aa_pos"] = \
get_heatmap_x_aa_pos(ret["heatmap_x_nt_pos"], ret["heatmap_x_genes"])
ret["heatmap_cells_fig_height"] = \
max(10*40, len(ret["heatmap_y_strains"]) * 40)
ret["heatmap_cells_container_height"] = \
min(10*40, ret["heatmap_cells_fig_height"])
ret["heatmap_cells_fig_width"] = len(ret["heatmap_x_nt_pos"]) * 36
ret["jump_to_dropdown_search_options"] = \
get_jump_to_dropdown_search_options(ret["jump_to_info_dict"])
return ret
def get_mutation_freq_slider_vals(parsed_mutations):
"""Get the mutation freq slider vals from ``parsed_gvf_dirs``.
This value is ultimately used when rendering the mutation frequency
slider. This is really just a sorted list of unique mutation
frequencies from visible cells in ``parsed_gvf_dirs``.
:param parsed_mutations: A dictionary containing multiple merged
``get_parsed_gvf_dir`` return "mutations" values.
:type parsed_mutations: dict
:return: Lowest to highest sorted list of unique mutation
frequencies from visible cells in ``parsed_gvf_dirs``.
:rtype: list[str]
"""
alt_freq_set = set()
for strain in parsed_mutations:
for pos in parsed_mutations[strain]:
for mutation in parsed_mutations[strain][pos]:
if not mutation["hidden_cell"]:
alt_freq_set.add(mutation["alt_freq"])
ret = sorted(list(alt_freq_set), key=float)
return ret
def get_intra_col_mutation_pos_dict(parsed_mutations):
"""Get dict sorting mutations into intra-position indices.
The keys of the returned value are nt positions, and the values are
dicts sorting n unique mutations at that position into indices
0, ..., n.
The returned value is sorted by nt pos to make things easier in
other functions.
:param parsed_mutations: A dictionary containing multiple merged
``get_parsed_gvf_dir`` return "mutations" values.
:type parsed_mutations: dict
:return: Dict with nt pos as key, and nested dicts as values
assigning mutations to intra-position indices.
:rtype: dict
"""
intra_col_mutation_pos_dict = {}
# In the first pass, we assign sets of mutation names as dict vals
for strain in parsed_mutations:
for pos in parsed_mutations[strain]:
mutation_names_set = \
{e["mutation_name"] for e in parsed_mutations[strain][pos]}
if pos not in intra_col_mutation_pos_dict:
intra_col_mutation_pos_dict[pos] = mutation_names_set
else:
intra_col_mutation_pos_dict[pos] |= mutation_names_set
# In the second pass, we assign col indices to the mutation names
for pos in intra_col_mutation_pos_dict:
intra_col_mutation_pos_dict[pos] = \
{e: i for i, e in enumerate(intra_col_mutation_pos_dict[pos])}
def sort_by_key(items): return int(items[0])
sorted_dict = \
dict(sorted(intra_col_mutation_pos_dict.items(), key=sort_by_key))
return sorted_dict
def get_heatmap_cells_tickvals(intra_col_mutation_pos_dict):
"""Get tickvals for the heatmap cells fig.
:param intra_col_mutation_pos_dict: See
``get_intra_col_mutation_pos_dict`` return value.
:type intra_col_mutation_pos_dict: dict
:return: List of numerically indexed tickvals for heatmap cells fig
:rtype: list[int]
"""
ret = [-0.5]
for pos in intra_col_mutation_pos_dict:
# Need extra space for heterozygous mutations
ret.append(ret[-1] + len(intra_col_mutation_pos_dict[pos]))
return ret
def get_heatmap_x_tickvals(heatmap_cells_tickvals):
"""Get tickvals for the heatmap x-axis figs.
:param heatmap_cells_tickvals: See ``get_heatmap_cells_tickvals``
return value.
:type heatmap_cells_tickvals: list
:return: List of numerically indexed tickvals for heatmap x-axis
figs.
:rtype: list[int]
"""
ret = []
for i in range(0, len(heatmap_cells_tickvals)-1):
# Place it in the middle of the heatmap cell gridlines
avg = (heatmap_cells_tickvals[i] + heatmap_cells_tickvals[i+1]) / 2
ret.append(avg)
return ret
def get_heatmap_x_nt_pos(intra_col_mutation_pos_dict):
"""Get heatmap nt pos x-axis vals.
:param intra_col_mutation_pos_dict: See
``get_intra_col_mutation_pos_dict`` return value.
:type intra_col_mutation_pos_dict: dict
:return: List of nt pos x-axis vals
:rtype: list[str]
"""
ret = []
for pos in intra_col_mutation_pos_dict:
for _ in range(len(intra_col_mutation_pos_dict[pos])):
ret.append(pos)
return ret
def get_heatmap_x_genes(intra_col_mutation_pos_dict):
"""Get gene values corresponding to x-axis values in heatmap.
:param intra_col_mutation_pos_dict: See
``get_intra_col_mutation_pos_dict`` return value.
:type intra_col_mutation_pos_dict: dict
:return: List of genes for each x in ``heatmap_x``
:rtype: list[str]
"""
ret = []
for pos in intra_col_mutation_pos_dict:
gene = map_pos_to_gene(int(pos))
for _ in range(len(intra_col_mutation_pos_dict[pos])):
ret.append(gene)
return ret
def get_heatmap_x_nsps(intra_col_mutation_pos_dict):
"""Get NSP values corresponding to x-axis values in heatmap.
:param intra_col_mutation_pos_dict: See
``get_intra_col_mutation_pos_dict`` return value.
:type intra_col_mutation_pos_dict: dict
:return: List of NSPs for each x in ``heatmap_x``
:rtype: list[str]
"""
ret = []
for pos in intra_col_mutation_pos_dict:
nsp = map_pos_to_nsp(int(pos))
for _ in range(len(intra_col_mutation_pos_dict[pos])):
ret.append(nsp)
return ret
def get_heatmap_x_aa_pos(heatmap_x_nt_pos, heatmap_x_genes):
"""Get aa positions corresponding to x axis values in heatmap.
These are in the form of {gene}.{aa position in gene}, or if they
are intergenic, in the form of
{downstream gene}.1-{number of nt upstream}.
:param heatmap_x_nt_pos: ``get_heatmap_x_nt_pos`` return value
:type heatmap_x_nt_pos: list[str]
:param heatmap_x_genes: ``get_heatmap_x_genes`` return value
:type heatmap_x_genes: list[str]
:return: List of amino acid positions relative to their gene for
each x in ``heatmap_x``, of the form
{gene}.{aa position in gene} or
{downstream gene}.1-{number of nt upstream}.
:rtype: list[str]
"""
gene_start_positions = \
{k: GENE_POSITIONS_DICT[k]["start"] for k in GENE_POSITIONS_DICT}
last_gene_seen = "3'UTR"
ret = ["" for _ in heatmap_x_nt_pos]
# Iterate through nt pos in reverse
for i, pos in enumerate(reversed(heatmap_x_nt_pos)):
# Negative index
_i = -1 - i
gene = heatmap_x_genes[_i]
if gene in {FIRST_REGION, LAST_REGION}:
ret[_i] = gene
continue
if gene == "INTERGENIC":
last_gene_start_pos = gene_start_positions[last_gene_seen]
downstream_diff = last_gene_start_pos - int(pos)
ret[_i] = "<b>%s.1 - %s</b>" % (last_gene_seen, downstream_diff)
continue
last_gene_seen = gene
gene_start_pos = gene_start_positions[gene]
ret[_i] = gene + "." + str(int((int(pos) - gene_start_pos) / 3) + 1)
return ret
def get_heatmap_y_strains(parsed_mutations):
"""Get strain y axis values of heatmap cells.
:param parsed_mutations: A dictionary containing multiple merged
``get_parsed_gvf_dir`` return "mutations" values.
:type parsed_mutations: dict
:return: List of strain y axis values
:rtype: list[str]
"""
ret = []
for strain in parsed_mutations:
ret.append(strain)
return ret
def get_heatmap_y_sample_sizes(parsed_mutations, sample_sizes):
"""Get sample size y axis values of heatmap cells.
:param parsed_mutations: A dictionary containing multiple merged
``get_parsed_gvf_dir`` return "mutations" values.
:type parsed_mutations: dict
:param sample_sizes: A dictionary containing multiple merged
``get_parsed_gvf_dir`` return "sample_size" values.
:type sample_sizes: dict
:return: List of sample size y axis values
:rtype: list[str]
"""
ret = []
for strain in parsed_mutations:
ret.append(sample_sizes[strain])
return ret
def get_heatmap_z(parsed_mutations, intra_col_mutation_pos_dict, sample_sizes):
"""Get z values of heatmap cells.
These are the mutation frequencies, and the z values dictate the
colours of the heatmap cells.
:param parsed_mutations: A dictionary containing multiple merged
``get_parsed_gvf_dir`` return "mutations" values.
:type parsed_mutations: dict
:param intra_col_mutation_pos_dict: See
``get_intra_col_mutation_pos_dict`` return value.
:type intra_col_mutation_pos_dict: dict
:param sample_sizes: A dictionary containing multiple merged
``get_parsed_gvf_dir`` return "sample_size" values.
:type sample_sizes: dict
:return: List of z values
:rtype: list[list[str]]
"""
ret = []
for strain in parsed_mutations:
row = []
for pos in intra_col_mutation_pos_dict:
cols = [None for _ in range(len(intra_col_mutation_pos_dict[pos]))]
if pos in parsed_mutations[strain]:
for mutation in parsed_mutations[strain][pos]:
if not mutation["hidden_cell"]:
mutation_name = mutation["mutation_name"]
col = intra_col_mutation_pos_dict[pos][mutation_name]
# Set to 0 if sample size == 1, which allows it
# to be displayed as white with our colorscale.
if sample_sizes[strain] == "1":
cols[col] = 0
else:
cols[col] = mutation["alt_freq"]
row.extend(cols)
ret.append(row)
return ret
def get_heatmap_hover_text(parsed_mutations, intra_col_mutation_pos_dict):
"""Get hover text of heatmap cells.
:param parsed_mutations: A dictionary containing multiple merged
``get_parsed_gvf_dir`` return "mutations" values.
:type parsed_mutations: dict
:param intra_col_mutation_pos_dict: See
``get_intra_col_mutation_pos_dict`` return value.
:type intra_col_mutation_pos_dict: dict
:return: List of D3 formatted text values for each x y coordinate
in ``heatmap_x_nt_pos``.
:rtype: list[list[str]]
"""
ret = []
for strain in parsed_mutations:
row = []
for pos in intra_col_mutation_pos_dict:
cols = [None for _ in range(len(intra_col_mutation_pos_dict[pos]))]
if pos in parsed_mutations[strain]:
for mutation in parsed_mutations[strain][pos]:
mutation_name = mutation["mutation_name"]
col = intra_col_mutation_pos_dict[pos][mutation_name]
if not mutation_name:
mutation_name = "No recorded name"
multi_aa_name = mutation["multi_aa_name"]
if not multi_aa_name:
multi_aa_name = "False"
functions_str = ""
for j, fn_category in enumerate(mutation["functions"]):
if j == 7:
functions_str += "...click for more<br>"
break
functions_str += fn_category + "<br>"
if not functions_str:
functions_str = "None recorded so far"
cell_text_str = "<b>Mutation name:</b> %s<br>" \
"Multiple AA mutations?: %s<br>" \
"<br>" \
"Reference: %s<br>" \
"Alternate: %s<br>" \
"Alternate frequency: %s<br>" \
"<br>" \
"<b>Functions:</b> <br>%s"
cell_text_params = (mutation_name,
multi_aa_name,
mutation["ref"],
mutation["alt"],
mutation["alt_freq"],
functions_str)
cols[col] = cell_text_str % cell_text_params
row.extend(cols)
ret.append(row)
return ret
def get_jump_to_info_dict(parsed_mutations):
"""Get dict containing info needed for "jump to" modal fns.
The keys in this dict are mutation names. The values are:
* mutation alias
* top-left most strain seen in heatmap with mutation
* top-right most strain seen in heatmap with mutation
:param parsed_mutations: A dictionary containing multiple merged
``get_parsed_gvf_dir`` return "mutations" values.
:type parsed_mutations: dict
:return: Dict with info needed for "jump to" modal fns
:type: dict
"""
ret = {}
for strain in reversed(parsed_mutations):
for nt_pos in parsed_mutations[strain]:
for mutation in parsed_mutations[strain][nt_pos]:
mutation_name = mutation["mutation_name"]
mutation_alias = mutation["mutation_alias"]
hidden = mutation["hidden_cell"]
if mutation_name and not hidden and mutation_name not in ret:
ret[mutation_name] = {"strain": strain,
"nt_pos": nt_pos,
"mutation_alias": mutation_alias}
return ret
def get_jump_to_dropdown_search_options(jump_to_info_dict):
"""List of dash bootstrap opts for "jump to" modal.
@param jump_to_info_dict: ``get_jump_to_info_dict`` ret val
@type jump_to_info_dict: dict
@return: List of dicts with a label and val key
@rtype: list
"""
ret = []
for mutation_name in jump_to_info_dict:
alias = jump_to_info_dict[mutation_name]["mutation_alias"]
alias_suffix = " (%s)" % alias if alias else ""
ret.append({"label": mutation_name + alias_suffix,
"value": mutation_name})
return ret
def get_heatmap_mutation_names(parsed_mutations, intra_col_mutation_pos_dict):
"""Get mutation names associated with heatmap cells.
This is useful when allowing users to click on heatmap cells for
mutation details.
:param parsed_mutations: A dictionary containing multiple merged
``get_parsed_gvf_dir`` return "mutations" values.
:type parsed_mutations: dict
:param intra_col_mutation_pos_dict: See
``get_intra_col_mutation_pos_dict`` return value.
:type intra_col_mutation_pos_dict: dict
:return: Mutation names for each x y coordinate in heatmap.
:rtype: list[list[str]]
"""
ret = []
for strain in parsed_mutations:
row = []
for pos in intra_col_mutation_pos_dict:
cols = [None for _ in range(len(intra_col_mutation_pos_dict[pos]))]
if pos in parsed_mutations[strain]:
for mutation in parsed_mutations[strain][pos]:
mutation_name = mutation["mutation_name"]
if mutation_name:
col = intra_col_mutation_pos_dict[pos][mutation_name]
cols[col] = mutation_name
row.extend(cols)
ret.append(row)
return ret
def get_heatmap_mutation_fns(parsed_mutations, intra_col_mutation_pos_dict):
"""Get mutation fns associated with heatmap cells.
This is useful when allowing users to click on heatmap cells for
mutation details.
:param parsed_mutations: A dictionary containing multiple merged
``get_parsed_gvf_dir`` return "mutations" values.
:type parsed_mutations: dict
:param intra_col_mutation_pos_dict: See
``get_intra_col_mutation_pos_dict`` return value.
:type intra_col_mutation_pos_dict: dict
:return: Mutation functions for each x y coordinate in heatmap, as
structured in dict format used by ``parsed_gvf_dirs``.
:rtype: list[list[dict]]
"""
ret = []
for strain in parsed_mutations:
row = []
for pos in intra_col_mutation_pos_dict:
cols = [None for _ in range(len(intra_col_mutation_pos_dict[pos]))]
if pos in parsed_mutations[strain]:
for mutation in parsed_mutations[strain][pos]:
if mutation["functions"]:
mutation_name = mutation["mutation_name"]
col = intra_col_mutation_pos_dict[pos][mutation_name]
cols[col] = mutation["functions"]
row.extend(cols)
ret.append(row)
return ret
def get_insertions_x(parsed_mutations, intra_col_mutation_pos_dict):
"""Get x coordinates of insertion markers to overlay in heatmap.
These are the linear x coordinates used in the Plotly graph object.
i.e., the indices of data["heatmap_x_nt_pos"]
:param parsed_mutations: A dictionary containing multiple merged
``get_parsed_gvf_dir`` return "mutations" values.
:type parsed_mutations: dict
:param intra_col_mutation_pos_dict: See
``get_intra_col_mutation_pos_dict`` return value.
:type intra_col_mutation_pos_dict: dict
:return: List of x coordinate values to display insertion markers
:rtype: list[int]
"""
ret = []
for strain in parsed_mutations:
# How far markers need to be pushed right due to earlier
# heterozygous mutations.
x_offset = 0
for i, pos in enumerate(intra_col_mutation_pos_dict):
num_of_mutations = len(intra_col_mutation_pos_dict[pos])
if pos in parsed_mutations[strain]:
for mutation in parsed_mutations[strain][pos]:
insertion = mutation["mutation_type"] == "insertion"
hidden = mutation["hidden_cell"]
if insertion and not hidden:
mutation_name = mutation["mutation_name"]
col_index = \
intra_col_mutation_pos_dict[pos][mutation_name]
ret.append(i + col_index + x_offset)
x_offset += num_of_mutations - 1
return ret
def get_insertions_y(parsed_mutations):
"""Get y coordinates of insertion markers to overlay in heatmap.
These are the linear y coordinates used in the Plotly graph object.
i.e., the indices of data["heatmap_y_strains"]
:param parsed_mutations: A dictionary containing multiple merged
``get_parsed_gvf_dir`` return "mutations" values.
:type parsed_mutations: dict
:return: List of y coordinate values to display insertion markers
:rtype: list[str]
"""
ret = []
for y, strain in enumerate(parsed_mutations):
for pos in parsed_mutations[strain]:
for mutation in parsed_mutations[strain][pos]:
insertion = mutation["mutation_type"] == "insertion"
hidden = mutation["hidden_cell"]
if insertion and not hidden:
ret.append(y)
return ret
def get_deletions_x(parsed_mutations, intra_col_mutation_pos_dict):
"""Get x coordinates of deletion markers to overlay in heatmap.
These are the linear x coordinates used in the Plotly graph object.
i.e., the indices of data["heatmap_x_nt_pos"]
:param parsed_mutations: A dictionary containing multiple merged
``get_parsed_gvf_dir`` return "mutations" values.
:type parsed_mutations: dict
:param intra_col_mutation_pos_dict: See
``get_intra_col_mutation_pos_dict`` return value.
:type intra_col_mutation_pos_dict: dict
:return: List of x coordinate values to display insertion markers
:rtype: list[int]
"""
ret = []
for strain in parsed_mutations:
# How far markers need to be pushed right due to earlier
# heterozygous mutations.
x_offset = 0
for i, pos in enumerate(intra_col_mutation_pos_dict):
num_of_mutations = len(intra_col_mutation_pos_dict[pos])
if pos in parsed_mutations[strain]:
for mutation in parsed_mutations[strain][pos]:
deletion = mutation["mutation_type"] == "deletion"
hidden = mutation["hidden_cell"]
if deletion and not hidden:
mutation_name = mutation["mutation_name"]
col_index = \
intra_col_mutation_pos_dict[pos][mutation_name]
ret.append(i + col_index + x_offset)
x_offset += num_of_mutations - 1
return ret
def get_deletions_y(parsed_mutations):
"""Get y coordinates of deletion markers to overlay in heatmap.
These are the linear y coordinates used in the Plotly graph object.
i.e., the indices of data["heatmap_y_strains"]
:param parsed_mutations: A dictionary containing multiple merged
``get_parsed_gvf_dir`` return "mutations" values.
:type parsed_mutations: dict
:return: List of y coordinate values to display deletion markers
:rtype: list[str]
"""
ret = []
for y, strain in enumerate(parsed_mutations):
for pos in parsed_mutations[strain]:
for mutation in parsed_mutations[strain][pos]:
deletion = mutation["mutation_type"] == "deletion"
hidden = mutation["hidden_cell"]
if deletion and not hidden:
ret.append(y)
return ret
def get_tables(parsed_mutations):
"""Get table column data for each y axis value or strain.
The columns are represented as lists.
:param parsed_mutations: A dictionary containing multiple merged
``get_parsed_gvf_dir`` return "mutations" values.
:type parsed_mutations: dict
:return: Dictionary with keys for each strain, and a list of lists
values representing columns for each strain.
:rtype: dict[str, list[list]]
"""
ret = {}
for strain in parsed_mutations:
pos_col = []
mutation_name_col = []
ref_col = []
alt_col = []
alt_freq_col = []
functions_col = []
for pos in parsed_mutations[strain]:
for mutation in parsed_mutations[strain][pos]:
pos_col.append(pos)
mutation_name_col.append(mutation["mutation_name"])
ref_col.append(mutation["ref"])
alt_col.append(mutation["alt"])
alt_freq_col.append(mutation["alt_freq"])
functions_col.append([fn for fn in mutation["functions"]])
ret[strain] = [
pos_col, mutation_name_col, ref_col, alt_col, alt_freq_col,
functions_col
]
return ret
def get_histogram_x(parsed_mutations):
"""Get x data values binned by Plotly when producing the histogram.
This is just the positions containing mutations, with duplicates
permitted for mutations shared by strains.