-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy patharchaeopteryx.js
8301 lines (7441 loc) · 334 KB
/
archaeopteryx.js
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
/**
* Copyright (C) 2024 Christian M. Zmasek
* Copyright (C) 2024 Yun Zhang
* Copyright (C) 2024 J. Craig Venter Institute
* All rights reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
*
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
// v 2.1.1.a1
// 2024-06-25
//
// Archaeopteryx.js is a software tool for the visualization and
// analysis of highly annotated phylogenetic trees.
//
// Availability:
// /~https://github.com/cmzmasek/archaeopteryx-js
// https://www.npmjs.com/package/archaeopteryx
//
// Dependencies:
// * forester.js: https://www.npmjs.com/package/archaeopteryx
// * phyloxml.js: https://www.npmjs.com/package/phyloxml
// * d3.js (version 3): https://www.npmjs.com/package/d3/v/3.5.17
// * jQuery (1.12.4): https://www.npmjs.com/package/jquery/v/1.12.4
// * jQuery UI (1.12.1): https://www.npmjs.com/package/jquery-ui/v/1.12.1
// * sax.js (1.2.4): https://www.npmjs.com/package/sax/v/1.2.4
//
// For file (Newick/New Hampshire, phyloXML) and graphics (PNG, SVG)
// download/export, the following five libraries are required as well:
// * canvg: https://www.npmjs.com/package/canvg
// * rgbcolor: https://www.npmjs.com/package/rgbcolor
// * Blob.js: /~https://github.com/eligrey/Blob.js
// * canvas-toBlob.js (needed in some versions of Internet Explorer and Opera): /~https://github.com/eligrey/canvas-toBlob.js
// * FileSaver.js: /~https://github.com/eligrey/FileSaver.js
//
// Additionally, Archaeopteryx.js also requires the following CSS:
// * jquery-ui.css: https://code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css
//
//
// Developer documentation:
// https://docs.google.com/document/d/1COVe0iYbKtcBQxGTP4_zuimpk2FH9iusOVOgd5xCJ3A
//
// User documentation:
// https://docs.google.com/document/d/16PjoaNeNTWPUNVGcdYukP6Y1G35PFhq39OiIMmD03U8
if (!d3) {
throw "no d3.js";
}
if (!forester) {
throw "no forester.js";
}
if (!phyloXml) {
throw "no phyloxml.js";
}
(function archaeopteryx() {
"use strict";
const VERSION = '2.1.1.a1';
const WEBSITE = 'https://sites.google.com/view/archaeopteryxjs';
const NAME = 'Archaeopteryx.js';
// -----------------------------
// Named colors and orientations
// -----------------------------
const LIGHT_BLUE = '#2590FD';
const WHITE = '#ffffff';
const HORIZONTAL = 'horizontal';
const VERTICAL = 'vertical';
// ------------------------------
// File suffixes
// ------------------------------
const NH_SUFFIX = '.tre';
const PNG_SUFFIX = '.png';
const SVG_SUFFIX = '.svg';
const XML_SUFFIX = '.xml';
const FASTA_SUFFIX = '.fasta';
// ---------------------------
// Default values for options
// ---------------------------
const BACKGROUND_COLOR_DEFAULT = '#f0f0f0';
const BACKGROUND_COLOR_FOR_PRINT_EXPORT_DEFAULT = '#ffffff';
const BRANCH_COLOR_DEFAULT = '#909090';
const BRANCH_DATA_FONT_SIZE_DEFAULT = 6;
const BRANCH_WIDTH_DEFAULT = 1;
const COLLAPSED_LABEL_LENGTH_DEFAULT = 7;
const DECIMALS_FOR_LINEAR_RANGE_MEAN_VALUE_DEFAULT = 0;
const EXTERNAL_NODE_FONT_SIZE_DEFAULT = 9;
const FONT_DEFAULTS = ['Arial', 'Helvetica', 'Times'];
const FOUND0_COLOR_DEFAULT = '#66cc00';
const FOUND0AND1_COLOR_DEFAULT = '#0000ee';
const FOUND1_COLOR_DEFAULT = '#ff00ff';
const SELECTED_COLOR_DEFAULT = '#ff0000';
const INTERNAL_NODE_FONT_SIZE_DEFAULT = 6;
const LABEL_COLOR_DEFAULT = '#202020';
const NAME_FOR_NH_DOWNLOAD_DEFAULT = 'archaeopteryx_js' + NH_SUFFIX;
const NAME_FOR_PHYLOXML_DOWNLOAD_DEFAULT = 'archaeopteryx_js' + XML_SUFFIX;
const NAME_FOR_PNG_DOWNLOAD_DEFAULT = 'archaeopteryx_js' + PNG_SUFFIX;
const NAME_FOR_SVG_DOWNLOAD_DEFAULT = 'archaeopteryx_js' + SVG_SUFFIX;
const NAME_FOR_FASTA_DOWNLOAD_DEFAULT = 'archaeopteryx_js' + FASTA_SUFFIX;
const NODE_LABEL_GAP_DEFAULT = 10;
const NODE_SIZE_DEFAULT_DEFAULT = 3;
const NODE_VISUALIZATIONS_OPACITY_DEFAULT = 1;
const VISUALIZATIONS_LEGEND_ORIENTATION_DEFAULT = VERTICAL;
const VISUALIZATIONS_LEGEND_XPOS_DEFAULT = 220;
const VISUALIZATIONS_LEGEND_YPOS_DEFAULT = 30;
// ---------------------------
// Default values for settings
// ---------------------------
const COLLAPSE_LABEL_WIDTH_DEFAULT = '20px';
const CONTROLS_0_LEFT_DEFAULT = 20;
const CONTROLS_0_TOP_DEFAULT = 10;
const CONTROLS_1_TOP_DEFAULT = 10;
const CONTROLS_1_WIDTH_DEFAULT = 120;
const CONTROLS_BACKGROUND_COLOR_DEFAULT = '#c0c0c0';
const CONTROLS_FONT_COLOR_DEFAULT = '#505050';
const CONTROLS_FONT_DEFAULTS = ['Arial', 'Helvetica', 'Times'];
const CONTROLS_FONT_SIZE_DEFAULT = 8;
const DISPLY_HEIGHT_DEFAULT = 600;
const DISPLAY_WIDTH_DEFAULT = 800;
const MOLSEQ_FONT_DEFAULTS = ['Courier', 'Courier New', 'Arial', 'Helvetica', 'Times'];
const ROOTOFFSET_DEFAULT = 220;
const SEARCH_FIELD_WIDTH_DEFAULT = '38px';
const TEXT_INPUT_FIELD_DEFAULT_HEIGHT = '10px';
// ------------------------------
// Various constants and settings
// ------------------------------
const ACC_GENBANK = "GENBANK";
const ACC_NCBI = "NCBI";
const ACC_REFSEQ = "REFSEQ";
const ACC_UNIPROT = "UNIPROT";
const ACC_UNIPROTKB = "UNIPROTKB";
const ACC_SWISSPROT = "SWISSPROT";
const ACC_TREMBL = "TREMBL";
const BRANCH_EVENT_APPLIES_TO = 'parent_branch';
const BRANCH_EVENT_DATATYPE = 'xsd:string';
const BRANCH_EVENT_REF = 'aptx:branch_event';
const BRANCH_LENGTH_DIGITS_DEFAULT = 6;
const BRANCH_WIDTH_MAX = 9;
const BRANCH_WIDTH_MIN = 0.5;
const BUTTON_ZOOM_IN_FACTOR = 1.1;
const BUTTON_ZOOM_IN_FACTOR_SLOW = 1.05;
const BUTTON_ZOOM_OUT_FACTOR = 1 / BUTTON_ZOOM_IN_FACTOR;
const BUTTON_ZOOM_OUT_FACTOR_SLOW = 1 / BUTTON_ZOOM_IN_FACTOR_SLOW;
const COLOR_FOR_ACTIVE_ELEMENTS = LIGHT_BLUE;
const COLOR_PICKER_BACKGROUND_BORDER_COLOR = '#808080';
const COLOR_PICKER_CLICKED_ORIG_COLOR_BORDER_COLOR = '#000000';
const CONFIDENCE_VALUE_DIGITS_DEFAULT = 2;
const DEFAULT = 'default';
const DUPLICATION_AND_SPECIATION_COLOR_COLOR = '#ffff00';
const DUPLICATION_COLOR = '#ff0000';
const FASTA_EXPORT_FORMAT = 'Fasta';
const FONT_SIZE_MAX = 26;
const FONT_SIZE_MIN = 2;
const KEY_FOR_COLLAPSED_FEATURES_SPECIAL_LABEL = 'collapsed_spec_label';
const LABEL_SIZE_CALC_ADDITION = 80;
const LABEL_SIZE_CALC_FACTOR = 0.5;
const LEGEND_LABEL_COLOR = 'legendLabelColor';
const LEGEND_NODE_FILL_COLOR = 'legendNodeFillColor';
const LEGEND_NODE_SHAPE = 'legendNodeShape';
const LEGEND_NODE_SIZE = 'legendNodeSize';
const LINEAR_SCALE = 'linear';
const MAX_LENGTH_FOR_COLLAPSE_BY_FEATURE_LABEL = 10;
const MOVE_INTERVAL = 150;
const NH_EXPORT_FORMAT = 'Newick';
const HEIGHT_OFFSET = 40;
const NODE_SIZE_MAX = 9;
const NODE_SIZE_MIN = 1;
const NODE_TOOLTIP_BACKGROUND_COLOR = '#606060';
const NODE_TOOLTIP_TEXT_ACTIVE_COLOR = COLOR_FOR_ACTIVE_ELEMENTS;
const NODE_TOOLTIP_TEXT_COLOR = WHITE;
const OFF_FEATURE = 'off';
const ORDINAL_SCALE = 'ordinal';
const PDF_EXPORT_FORMAT = 'PDF';
const PHYLOXML_EXPORT_FORMAT = 'phyloXML';
const PNG_EXPORT_FORMAT = 'PNG';
const MSA_RESIDUE = 'MSA Residue';
const RESET_SEARCH_A_BTN_TOOLTIP = 'reset (remove) search result A';
const RESET_SEARCH_B_BTN_TOOLTIP = 'reset (remove) search result B';
const SHORTEN_NAME_MAX_LENGTH = 18;
const SLIDER_STEP = 0.5;
const SPECIATION_COLOR = '#00ff00';
const SPECIES_FEATURE = 'Species';
const SVG_EXPORT_FORMAT = 'SVG';
const TOP_AND_BOTTOM_BORDER_HEIGHT = 10;
const TRANSITION_DURATION_DEFAULT = 750;
const WARNING = 'ArchaeopteryxJS: WARNING';
const MESSAGE = 'ArchaeopteryxJS: ';
const ERROR = 'ArchaeopteryxJS: ERROR: ';
const WIDTH_OFFSET = 14; // Needed in Firefox Quantum (2018-02-22)
const ZOOM_INTERVAL = 200;
// ---------------------------
// Names for GUI elements
// ---------------------------
const BASE_BACKGROUND = 'basebackground';
const BL_COLLAPSE_LABEL = 'bl_col_label';
const BRANCH_COLORS_CB = 'brnch_col_cb';
const BRANCH_DATA_FONT_SIZE_SLIDER = 'bdfs_sl';
const BRANCH_EVENTS_CB = 'brevts_cb';
const BRANCH_LENGTH_VALUES_CB = 'bl_cb';
const BRANCH_VIS_CB = 'branchvis_cb';
const BRANCH_WIDTH_SLIDER = 'bw_sl';
const CLADOGRAM_BUTTON = 'cla_b';
const COLLAPSE_BY_FEATURE_SELECT = 'coll_by_feat_sel';
const COLOR_PICKER = 'col_pick';
const COLOR_PICKER_LABEL = 'colorPickerLabel';
const CONFIDENCE_VALUES_CB = 'conf_cb';
const CONTROLS_0 = 'controls0';
const CONTROLS_1 = 'controls1';
const DECR_BL_COLLAPSE_LEVEL = 'decr_blcl';
const DECR_DEPTH_COLLAPSE_LEVEL = 'decr_dcl';
const DEPTH_COLLAPSE_LABEL = 'depth_col_label';
const DISPLAY_DATA_CONTROLGROUP = 'display_data_g';
const DOWNLOAD_BUTTON = 'dl_b';
const SUBMIT_SELECTED_NODES_BUTTON = 'submit_sel_nodes_b';
const DYNAHIDE_CB = 'dynahide_cb';
const EXPORT_FORMAT_SELECT = 'exp_f_sel';
const EXTERNAL_FONT_SIZE_SLIDER = 'entfs_sl';
const EXTERNAL_LABEL_CB = 'extl_cb';
const EXTERNAL_NODES_CB = 'extn_cb';
const INCR_BL_COLLAPSE_LEVEL = 'incr_blcl';
const INCR_DEPTH_COLLAPSE_LEVEL = 'incr_dcl';
const INTERNAL_FONT_SIZE_SLIDER = 'intfs_sl';
const INTERNAL_LABEL_CB = 'intl_cb';
const INTERNAL_NODES_CB = 'intn_cb';
const LABEL_COLOR_SELECT_MENU = 'lcs_menu';
const LEGEND = 'legend';
const LEGEND_DESCRIPTION = 'legendDescription';
const LEGEND_LABEL = 'legendLabel';
const LEGENDS_HORIZ_VERT_BTN = 'legends_horizvert';
const LEGENDS_MOVE_DOWN_BTN = 'legends_mdown';
const LEGENDS_MOVE_LEFT_BTN = 'legends_mleft';
const LEGENDS_MOVE_RIGHT_BTN = 'legends_mright';
const LEGENDS_MOVE_UP_BTN = 'legends_mup';
const LEGENDS_RESET_BTN = 'legends_rest';
const LEGENDS_SHOW_BTN = 'legends_show';
const MIDPOINT_ROOT_BUTTON = 'midpointr_b';
const MSA_RESIDUE_VIS_CURR_RES_POS_LABEL = 'seq_pos_label_curr_pos';
const MSA_RESIDUE_VIS_CURR_RES_POS_SLIDER_1 = 'seq_pos_slider_1';
const MSA_RESIDUE_VIS_DECR_CURR_RES_POS_BTN = 'seq_pos_decr_pos';
const MSA_RESIDUE_VIS_INCR_CURR_RES_POS_BTN = 'seq_pos_incr_pos';
const NODE_DATA = 'node_data_dialog';
const NODE_EVENTS_CB = 'nevts_cb';
const NODE_FILL_COLOR_SELECT_MENU = 'nfcolors_menu';
const NODE_NAME_CB = 'nn_cb';
const NODE_SHAPE_SELECT_MENU = 'nshapes_menu';
const NODE_SIZE_SELECT_MENU = 'nsizes_menu';
const NODE_SIZE_SLIDER = 'ns_sl';
const NODE_VIS_CB = 'nodevis_cb';
const ORDER_BUTTON = 'ord_b';
const PHYLOGRAM_ALIGNED_BUTTON = 'phya_b';
const PHYLOGRAM_BUTTON = 'phy_b';
const PHYLOGRAM_CLADOGRAM_CONTROLGROUP = 'phy_cla_g';
const PROG_NAME = 'progname';
const PROGNAMELINK = 'prognamelink';
const TREE_DESC = 'tree_desc';
const RESET_SEARCH_A_BTN = 'reset_s_a';
const RESET_SEARCH_B_BTN = 'reset_s_b';
const RETURN_TO_SUPERTREE_BUTTON = 'ret_b';
const RETURN_TO_SUPERTREE_BUTTON_BY_ONE = 'ret1_b';
const SEARCH_FIELD_0 = 'sf0';
const SEARCH_FIELD_1 = 'sf1';
const SEARCH_OPTIONS_CASE_SENSITIVE_CB = 'so_cs_cb';
const SEARCH_OPTIONS_COMPLETE_TERMS_ONLY_CB = 'so_cto_cb';
const SEARCH_OPTIONS_PROPERTIES_CB = 'so_prp_cb';
const SEARCH_OPTIONS_GROUP = 'search_opts_g';
const SEARCH_OPTIONS_NEGATE_RES_CB = 'so_neg_cb';
const SEARCH_OPTIONS_REGEX_CB = 'so_regex_cb';
const SEQUENCE_CB = 'seq_cb';
const SHORTEN_NODE_NAME_CB = 'shortennodename_cb';
const TAXONOMY_CB = 'tax_cb';
const UNCOLLAPSE_ALL_BUTTON = 'unc_b';
const ZOOM_IN_X = 'zoomin_x';
const ZOOM_IN_Y = 'zoomout_y';
const ZOOM_OUT_X = 'zoomout_x';
const ZOOM_OUT_Y = 'zoomin_y';
const ZOOM_TO_FIT = 'zoomtofit';
const ZOOM_TO_EXPAND_Y = 'zoomtoexpandy';
const LABEL_COLOR_SELECT_MENU_2 = 'lcs_2_menu';
const NODE_FILL_COLOR_SELECT_MENU_2 = 'nfcolors_2_menu';
const LABEL_COLOR_SELECT_MENU_3 = 'lcs_3_menu';
const NODE_FILL_COLOR_SELECT_MENU_3 = 'nfcolors_3_menu';
const LABEL_COLOR_SELECT_MENU_4 = 'lcs_4_menu';
const NODE_FILL_COLOR_SELECT_MENU_4 = 'nfcolors_4_menu';
// ---------------------------
// Key codes
// ---------------------------
const VK_ESC = 27;
const VK_A = 65;
const VK_C = 67;
const VK_L = 76;
const VK_M = 77;
const VK_O = 79;
const VK_P = 80;
const VK_R = 82;
const VK_S = 83;
const VK_U = 85;
const VK_0 = 48;
const VK_9 = 57;
const VK_0_NUMPAD = 96;
const VK_9_NUMPAD = 105;
const VK_DELETE = 46;
const VK_BACKSPACE = 8;
const VK_HOME = 36;
const VK_UP = 38;
const VK_DOWN = 40;
const VK_LEFT = 37;
const VK_RIGHT = 39;
const VK_PLUS = 187;
const VK_MINUS = 189;
const VK_PLUS_N = 107;
const VK_MINUS_N = 109;
const VK_PAGE_UP = 33;
const VK_PAGE_DOWN = 34;
const VK_OPEN_BRACKET = 219;
const VK_CLOSE_BRACKET = 221;
// ---------------------------
// Regular Expressions
// ---------------------------
const RE_SWISSPROT_TREMBL = new RegExp('^(?=.*[A-Z].*_.*[A-Z].*)[A-Z0-9]{2,10}_[A-Z0-9]{3,5}$');
const RE_SWISSPROT_TREMBL_PFAM = new RegExp('^((?=.*[A-Z].*_.*[A-Z].*)[A-Z0-9]{2,10}_[A-Z0-9]{3,5})/[0-9]+-[0-9]+$');
const RE_GENBANK_PROT = new RegExp('^[A-Z]{3}[0-9\\\\.]+$');
const RE_GENBANK_NUC = new RegExp('^[A-Z]{1,2}[0-9\\\\.]+$');
const RE_REFSEQ = new RegExp('^[A-Z]{2}_[0-9\\\\.]+$');
const RE_UNIPROTKB = new RegExp('^[OPQ][0-9][A-Z0-9]{3}[0-9]|[A-NR-Z][0-9]([A-Z][A-Z0-9]{2}[0-9]){1,2}$');
// ---------------------------
// Colors
// ---------------------------
const col_category50 = [// 1 Red
'#FF1744', // 2 Purple
'#D500F9', // 3 Deep Purple
'#651FFF', // 4 Indigo
'#3D5AFE', // 5 Blue
'#2979FF', // 6 Cyan
'#00E5FF', // 7 Teal
'#1DE9B6', // 8 Green
'#00E676', // 9 Light Green
'#76FF03', // 10 Lime
'#C6FF00', // 11 Yellow
'#FFEA00', // 12 Amber
'#FFC400', // 13 Orange
'#FF9100', // 13 Deep Orange
'#FF3D00', // 15 Brown
'#6D4C41', // 16 Grey
'#757575', //
// 17 Red
'#B71C1C', // 18 Pink
'#880E4F', // 19 Purple
'#4A148C', // 20 Deep Purple
'#311B92', // 21 Indigo
'#1A237E', // 22 Blue
'#0D47A1', // 23 Cyan
'#006064', // 24 Teal
'#004D40', // 25 Green
'#1B5E20', // 26 Light Green
'#33691E', // 27 Lime
'#827717', // 28 Yellow
'#F57F17', // 29 Amber
'#FF6F00', // 30 Orange
'#E65100', // 31 Deep Orange
'#BF360C', // 32 Brown
'#4E342E', // 33 Grey
'#424242', //
// 34 Red
'#EF9A9A', // 35 Pink
'#F48FB1', // 36 Purple
'#CE93D8', // 37 Deep Purple
'#B39DDB', // 38 Indigo
'#9FA8DA', // 39 Blue
'#90CAF9', // 40 Cyan
'#80DEEA', // 41 Teal
'#80CBC4', // 42 Green
'#A5D6A7', // 43 Light Green
'#C5E1A5', // 44 Lime
'#E6EE9C', // 45 Amber
'#FFE082', // 46 Orange
'#FFCC80', // 47 Deep Orange
'#FFAB91', // 48 Brown
'#BCAAA4', // 49 Grey
'#E0E0E0', // 50 Grey
'#505050'];
const col_category50b = ["#1CE6FF", "#FF34FF", "#FF4A46", "#008941", "#006FA6", "#A30059", "#7A4900", "#0000A6", "#63FFAC", "#B79762", "#004D43", "#8FB0FF", "#997D87", "#5A0007", "#809693", "#1B4400", "#4FC601", "#3B5DFF", "#4A3B53", "#FF2F80", "#61615A", "#BA0900", "#6B7900", "#00C2A0", "#FFAA92", "#FF90C9", "#D16100", "#000035", "#7B4F4B", "#A1C299", "#300018", "#0AA6D8", "#013349", "#00846F", "#372101", "#FFB500", "#C2FFED", "#A079BF", "#CC0744", "#C0B9B2", "#C2FF99", "#001E09", "#00489C", "#6F0062", "#0CBD66", "#EEC3FF", "#456D75", "#B77B68", "#7A87A1", "#788D66"];
const col_category50c = [// Red
'#FF5252', '#FF1744', '#D50000', // Pink
'#FF4081', '#F50057', '#C51162', // Purple
'#E040FB', '#D500F9', '#AA00FF', // Deep Purple
'#7C4DFF', '#651FFF', '#6200EA', // Indigo
'#536DFE', '#3D5AFE', '#304FFE', // Blue
'#448AFF', '#2979FF', '#2962FF', // Cyan
'#18FFFF', '#00E5FF', '#00B8D4', // Teal
'#64FFDA', '#1DE9B6', '#00BFA5', // Green
'#69F0AE', '#00E676', '#00C853', // Light Green
'#B2FF59', '#76FF03', '#64DD17', // Lime
'#EEFF41', '#C6FF00', '#AEEA00', // Yellow
'#FFFF00', '#FFEA00', '#FFD600', // Amber
'#FFD740', '#FFC400', '#FFAB00', // Orange
'#FFAB40', '#FF9100', '#FF6D00', // Deep Orange
'#FF6E40', '#FF3D00', '#DD2C00', // Brown
'#5D4037', '#4E342E', '#3E2723', // Grey
'#9E9E9E', '#616161'];
const category50 = function () {
return d3.scale.ordinal().domain([]).range(col_category50);
};
const category50b = function () {
return d3.scale.ordinal().domain([]).range(col_category50b);
};
const category50c = function () {
return d3.scale.ordinal().domain([]).range(col_category50c);
};
// ---------------------------
// "Instance variables"
// ---------------------------
let _baseSvg = null;
let _basicTreeProperties = null;
let _branch_length_collapse_data = {};
let _branch_length_collapse_level = -1;
let _colorPickerData = null;
let _colorsForColorPicker = null;
let _currentLabelColorVisualization = null;
let _currentNodeFillColorVisualization = null;
let _currentNodeShapeVisualization = null;
let _currentNodeSizeVisualization = null;
let _depth_collapse_level = -1;
let _displayHeight = 0;
let _displayWidth = 0;
let _dynahide_counter = 0;
let _dynahide_factor = 0;
let _external_nodes = 0;
let _foundNodes0 = new Set();
let _foundNodes1 = new Set();
let _selectedNodes = new Set();
let _foundSum = 0;
let _i = 0;
let _id = null;
let _intervalId = 0;
let _legendColorScales = {};
let _legendShapeScales = {};
let _legendSizeScales = {};
let _maxLabelLength = 0;
let _msa_residue_vis_curr_res_pos = 0;
let _nodeVisualizations = null;
let _nodeLabels = null;
let _specialVisualizations = null;
let _offsetTop = 0;
let _options = null;
let _options_orig = null;
let _rank_collapse_level = -1;
let _root = null;
let _root_const = null;
let _in_subtree = false;
let _scale = null;
let _searchBox0Empty = true;
let _searchBox1Empty = true;
let _settings = null;
let _showColorPicker = false;
let _showLegends = true;
let _svgGroup = null;
let _totalSearchedWithData = 0;
let _translate = null;
let _treeData = null;
let _treeFn = null;
let _usedColorCategories = new Set();
let _visualizations = null;
let _w = null;
let _yScale = null;
let _zoomListener = null;
let _zoomed_x_or_y = false;
let _node_mouseover_div;
let _visualizations2_color = null;
let _visualizations3_color = null;
let _visualizations4_color = null;
let _visualizations2_applies_to_ref = null;
let _visualizations3_applies_to_ref = null;
let _visualizations4_applies_to_ref = null;
let _visualizations2_property_datatype = null;
let _visualizations3_property_datatype = null;
let _visualizations4_property_datatype = null;
let _visualizations2_property_applies_to = null;
let _visualizations3_property_applies_to = null;
let _visualizations4_property_applies_to = null;
function branchLengthScaling(nodes, width) {
if (_root.parent) {
_root.parent.distToRoot = 0;
}
forester.preOrderTraversalAll(_root, function (n) {
n.distToRoot = (n.parent ? n.parent.distToRoot : 0) + bl(n);
});
let distsToRoot = nodes.map(function (n) {
return n.distToRoot;
});
let yScale = d3.scale.linear()
.domain([0, d3.max(distsToRoot)])
.range([0, width]);
forester.preOrderTraversalAll(_root, function (n) {
n.y = yScale(n.distToRoot);
});
return yScale;
function bl(node) {
if (!node.branch_length || node.branch_length < 0) {
return 0;
} else if (!node.parent || !node.parent.parent) {
return _basicTreeProperties.averageBranchLength * 0.5;
}
return node.branch_length;
}
}
function zoom() {
if (d3.event.sourceEvent && d3.event.sourceEvent.shiftKey) {
if (_scale === null) {
_scale = _zoomListener.scale();
_translate = _zoomListener.translate();
}
} else {
if (_scale !== null && _translate !== null) {
_zoomListener.scale(_scale);
_zoomListener.translate(_translate);
_svgGroup.attr('transform', 'translate(' + _translate + ')scale(' + _scale + ')');
_scale = null;
_translate = null;
} else {
_svgGroup.attr('transform', 'translate(' + d3.event.translate + ')scale(' + d3.event.scale + ')');
}
}
}
function centerNode(source, x, y) {
let scale = _zoomListener.scale();
if (!x) {
x = -source.y0;
if (_settings.enableDynamicSizing) {
x = x * scale + (_baseSvg.attr('width')) / 2;
} else {
x = x * scale + _displayWidth / 2;
}
}
if (!y) {
y = 0;
}
d3.select('g')
.attr('transform', 'translate(' + x + ',' + y + ')scale(' + scale + ')');
_zoomListener.scale(scale);
_zoomListener.translate([x, y]);
}
function calcMaxTreeLengthForDisplay() {
return _settings.rootOffset + _options.nodeLabelGap + LABEL_SIZE_CALC_ADDITION + (_maxLabelLength * _options.externalNodeFontSize * LABEL_SIZE_CALC_FACTOR);
}
function isCanDoMsaResidueVisualizations() {
return ((_settings.enableNodeVisualizations === true) && (_settings.enableMsaResidueVisualizations === true) && (_basicTreeProperties.alignedMolSeqs === true) && (_basicTreeProperties.maxMolSeqLength && (_basicTreeProperties.maxMolSeqLength > 1)));
}
function isAddVisualization2() {
return _settings.enableSpecialVisualizations2;
}
function isAddVisualization3() {
return _settings.enableSpecialVisualizations3;
}
function isAddVisualization4() {
return _settings.enableSpecialVisualizations4;
}
// ----------------------------
// Functions for node tooltips
// ----------------------------
function mouseover() {
_node_mouseover_div.transition()
.duration(300)
.style('opacity', 0.95)
.style('text-align', 'left')
.style('position', 'absolute')
.style('font', '12px sans-serif')
.style('pointer-events', 'none')
.style('background', '#dddddd')
.style('border', 'solid 1px #aaa')
.style('border-radius', '4px')
}
function mousemove(d) {
let mo_text = '';
if (d.name) {
mo_text += 'Name: ' + d.name + '<br>';
}
if (d.branch_length) {
mo_text += 'Distance to Parent: ' + d.branch_length + '<br>';
}
mo_text += 'Depth: ' + forester.calcDepth(d) + '<br>';
let i = 0;
if (d.confidences) {
for (i = 0; i < d.confidences.length; ++i) {
let c = d.confidences[i];
if (c.type) {
mo_text += 'Confidence [' + c.type + ']: ' + c.value + '<br>';
} else {
mo_text += 'Confidence: ' + c.value + '<br>';
}
if (c.stddev) {
mo_text += '- stdev: ' + c.stddev + '<br>';
}
}
}
if (d.taxonomies) {
for (i = 0; i < d.taxonomies.length; ++i) {
mo_text += 'Taxonomy<br>';
let t = d.taxonomies[i];
if (t.id) {
if (t.id.provider) {
mo_text += '- Id [' + t.id.provider + ']: ' + t.id.value + '<br>';
} else {
mo_text += '- Id: ' + t.id.value + '<br>';
}
}
if (t.code) {
mo_text += '- Code: ' + t.code + '<br>';
}
if (t.scientific_name) {
mo_text += '- Scientific name: ' + t.scientific_name + '<br>';
}
if (t.common_name) {
mo_text += '- Common name: ' + t.common_name + '<br>';
}
if (t.rank) {
mo_text += '- Rank: ' + t.rank + '<br>';
}
}
}
if (d.sequences) {
for (i = 0; i < d.sequences.length; ++i) {
mo_text += 'Sequence<br>';
let s = d.sequences[i];
if (s.accession) {
if (s.accession.source) {
mo_text += '- Accession [' + s.accession.source + ']: ' + s.accession.value + '<br>';
} else {
mo_text += '- Accession: ' + s.accession.value + '<br>';
}
if (s.accession.comment) {
mo_text += '-- comment: ' + s.accession.comment + '<br>';
}
}
if (s.symbol) {
mo_text += '- Symbol: ' + s.symbol + '<br>';
}
if (s.name) {
mo_text += '- Name: ' + s.name + '<br>';
}
if (s.gene_name) {
mo_text += '- Gene name: ' + s.gene_name + '<br>';
}
if (s.location) {
mo_text += '- Location: ' + s.location + '<br>';
}
if (s.type) {
mo_text += '- Type: ' + s.type + '<br>';
}
}
}
if (d.distributions) {
let distributions = d.distributions;
for (i = 0; i < distributions.length; ++i) {
mo_text += 'Distribution: ';
if (distributions[i].desc) {
mo_text += distributions[i].desc + '<br>';
}
}
}
if (d.date) {
mo_text += 'Date: ';
let date = d.date;
if (date.desc) {
mo_text += date.desc + '<br>';
}
}
if (d.events) {
mo_text += 'Events<br>';
let ev = d.events;
if (ev.type && ev.type.length > 0) {
mo_text += '- Type: ' + ev.type + '<br>';
}
if (ev.duplications && ev.duplications > 0) {
mo_text += '- Duplications: ' + ev.duplications + '<br>';
}
if (ev.speciations && ev.speciations > 0) {
mo_text += '- Speciations: ' + ev.speciations + '<br>';
}
if (ev.losses && ev.losses > 0) {
mo_text += '- Losses: ' + ev.losses + '<br>';
}
}
if (d.properties && d.properties.length > 0) {
let propertiesLength = d.properties.length;
for (i = 0; i < propertiesLength; ++i) {
let property = d.properties[i];
if (property.ref && property.value) {
let prop_ref = property.ref
if (prop_ref.indexOf(':') > 0) {
prop_ref = prop_ref.substring(prop_ref.indexOf(':') + 1)
}
if (property.unit) {
mo_text += prop_ref + ': ' + property.value + property.unit + '<br>';
} else {
mo_text += prop_ref + ': ' + property.value + '<br>';
}
}
}
}
if (d.children || d._children) {
mo_text += 'Sum of Subtree Tips: ' + forester.calcSumOfAllExternalDescendants(d) + '<br>';
}
_node_mouseover_div
.html(mo_text)
.style('left', (d3.event.pageX) + 'px')
.style('top', (d3.event.pageY) + 'px');
}
function mouseout() {
_node_mouseover_div
.html('')
_node_mouseover_div.transition()
.duration(300)
.style('opacity', 1e-6);
}
// ----------------------------
function createVisualization(label, description, field, cladePropertyRef, isRegex, mapping, mappingFn, // mappingFn is a scale
scaleType, altMappingFn) {
if (arguments.length < 8) {
throw('expected at least 8 arguments, got ' + arguments.length);
}
if (!label || label.length < 1) {
throw('need to have label');
}
let visualization = {};
visualization.label = label;
if (description) {
visualization.description = description;
}
if (field) {
if (cladePropertyRef) {
throw('need to have either field or clade property ref (but not both)');
}
visualization.field = field;
} else if (cladePropertyRef) {
visualization.cladePropertyRef = cladePropertyRef;
} else {
throw('need to have either field or clade property ref');
}
visualization.isRegex = isRegex;
if (mapping) {
if (mappingFn) {
throw('need to have either mapping or mappingFn');
}
visualization.mapping = mapping;
} else if (mappingFn) {
visualization.mappingFn = mappingFn;
if (scaleType === ORDINAL_SCALE) {
if (mappingFn.domain() && mappingFn.range() && mappingFn.domain().length > mappingFn.range().length) {
if (altMappingFn && altMappingFn.domain() && altMappingFn.range()) {
visualization.mappingFn = altMappingFn;
scaleType = LINEAR_SCALE;
} else {
let s = cladePropertyRef ? cladePropertyRef : field;
console.log(WARNING + ': Ordinal scale mapping for ' + label + ' (' + s + '): domain > range: ' + mappingFn.domain().length + ' > ' + mappingFn.range().length);
}
}
}
} else {
throw('need to have either mapping or mappingFn');
}
visualization.scaleType = scaleType;
return visualization;
}
function initializeNodeVisualizations(nodeProperties) {
if (_nodeVisualizations) {
for (let key in _nodeVisualizations) {
if (_nodeVisualizations.hasOwnProperty(key)) {
let nodeVisualization = _nodeVisualizations[key];
if (nodeVisualization.label) {
let scaleType = '';
if (nodeVisualization.shapes && Array.isArray(nodeVisualization.shapes) && (nodeVisualization.shapes.length > 0)) {
let shapeScale = null;
if (nodeVisualization.label === MSA_RESIDUE) {
shapeScale = d3.scale.ordinal()
.range(nodeVisualization.shapes)
.domain(_basicTreeProperties.molSeqResiduesPerPosition[0]);
scaleType = ORDINAL_SCALE;
} else if (nodeVisualization.cladeRef && nodeProperties[nodeVisualization.cladeRef] && forester.setToArray(nodeProperties[nodeVisualization.cladeRef]).length > 0) {
shapeScale = d3.scale.ordinal()
.range(nodeVisualization.shapes)
.domain(forester.setToSortedArray(nodeProperties[nodeVisualization.cladeRef]));
scaleType = ORDINAL_SCALE;
} else if (nodeVisualization.field && nodeProperties[nodeVisualization.field] && forester.setToArray(nodeProperties[nodeVisualization.field]).length > 0) {
shapeScale = d3.scale.ordinal()
.range(nodeVisualization.shapes)
.domain(forester.setToSortedArray(nodeProperties[nodeVisualization.field]));
scaleType = ORDINAL_SCALE;
}
if (shapeScale) {
addNodeShapeVisualization(nodeVisualization.label, nodeVisualization.description, nodeVisualization.field ? nodeVisualization.field : null, nodeVisualization.cladeRef ? nodeVisualization.cladeRef : null, nodeVisualization.regex, null, shapeScale, scaleType);
}
}
if (nodeVisualization.colors) {
// TODO: Not dealing with nodeVisualization.field, yet.
if ((nodeVisualization.cladeRef && nodeProperties[nodeVisualization.cladeRef] && forester.setToArray(nodeProperties[nodeVisualization.cladeRef]).length > 0) || (nodeVisualization.label === MSA_RESIDUE)) {
let colorScale = null;
let altColorScale = null;
if (Array.isArray(nodeVisualization.colors)) {
scaleType = LINEAR_SCALE;
if (nodeVisualization.colors.length === 3) {
colorScale = d3.scale.linear()
.range(nodeVisualization.colors)
.domain(forester.calcMinMeanMaxInSet(nodeProperties[nodeVisualization.cladeRef]));
} else if (nodeVisualization.colors.length === 2) {
colorScale = d3.scale.linear()
.range(nodeVisualization.colors)
.domain(forester.calcMinMaxInSet(nodeProperties[nodeVisualization.cladeRef]));
} else {
throw 'Number of colors has to be either 2 or 3';
}
}
if (Array.isArray(nodeVisualization.colorsAlt)) {
if (nodeVisualization.colorsAlt.length === 3) {
altColorScale = d3.scale.linear()
.range(nodeVisualization.colorsAlt)
.domain(forester.calcMinMeanMaxInSet(nodeProperties[nodeVisualization.cladeRef]));
} else if (nodeVisualization.colorsAlt.length === 2) {
altColorScale = d3.scale.linear()
.range(nodeVisualization.colorsAlt)
.domain(forester.calcMinMaxInSet(nodeProperties[nodeVisualization.cladeRef]));
} else {
throw 'Number of colors has to be either 2 or 3';
}
}
if (forester.isString(nodeVisualization.colors) && nodeVisualization.colors.length > 0) {
scaleType = ORDINAL_SCALE;
if (nodeVisualization.label === MSA_RESIDUE) {
colorScale = d3.scale.category20()
.domain(_basicTreeProperties.molSeqResiduesPerPosition[0]);
_usedColorCategories.add('category20');
} else {
if (nodeVisualization.colors === 'category20') {
colorScale = d3.scale.category20()
.domain(forester.setToSortedArray(nodeProperties[nodeVisualization.cladeRef]));
_usedColorCategories.add('category20');
} else if (nodeVisualization.colors === 'category20b') {
colorScale = d3.scale.category20b()
.domain(forester.setToSortedArray(nodeProperties[nodeVisualization.cladeRef]));
_usedColorCategories.add('category20b');
} else if (nodeVisualization.colors === 'category20c') {
colorScale = d3.scale.category20c()
.domain(forester.setToSortedArray(nodeProperties[nodeVisualization.cladeRef]));
_usedColorCategories.add('category20c');
} else if (nodeVisualization.colors === 'category10') {
colorScale = d3.scale.category10()
.domain(forester.setToSortedArray(nodeProperties[nodeVisualization.cladeRef]));
_usedColorCategories.add('category10');
} else if (nodeVisualization.colors === 'category50') {
colorScale = category50()
.domain(forester.setToSortedArray(nodeProperties[nodeVisualization.cladeRef]));
_usedColorCategories.add('category50');
} else if (nodeVisualization.colors === 'category50b') {
colorScale = category50b()
.domain(forester.setToSortedArray(nodeProperties[nodeVisualization.cladeRef]));
_usedColorCategories.add('category50b');
} else if (nodeVisualization.colors === 'category50c') {
colorScale = category50c()
.domain(forester.setToSortedArray(nodeProperties[nodeVisualization.cladeRef]));
_usedColorCategories.add('category50c');
} else {
throw 'do not know how to process ' + nodeVisualization.colors;
}
}
}
if (colorScale) {
addLabelColorVisualization(nodeVisualization.label, nodeVisualization.description, null, nodeVisualization.cladeRef, nodeVisualization.regex, null, colorScale, scaleType, altColorScale);
addNodeFillColorVisualization(nodeVisualization.label, nodeVisualization.description, null, nodeVisualization.cladeRef, nodeVisualization.regex, null, colorScale, scaleType, altColorScale);
}
}
}
if (nodeVisualization.sizes && Array.isArray(nodeVisualization.sizes) && (nodeVisualization.sizes.length > 0)) {
if (nodeVisualization.cladeRef && nodeProperties[nodeVisualization.cladeRef] && forester.setToArray(nodeProperties[nodeVisualization.cladeRef]).length > 0) {
let sizeScale = null;
let scaleType = LINEAR_SCALE;
if (nodeVisualization.sizes.length === 3) {
sizeScale = d3.scale.linear()
.range(nodeVisualization.sizes)
.domain(forester.calcMinMeanMaxInSet(nodeProperties[nodeVisualization.cladeRef]));
} else if (nodeVisualization.sizes.length === 2) {
sizeScale = d3.scale.linear()
.range(nodeVisualization.sizes)
.domain(forester.calcMinMaxInSet(nodeProperties[nodeVisualization.cladeRef]));
} else {
throw 'Number of sizes has to be either 2 or 3';
}
if (sizeScale) {
addNodeSizeVisualization(nodeVisualization.label, nodeVisualization.description, null, nodeVisualization.cladeRef, nodeVisualization.regex, null, sizeScale, scaleType);
}
}
}
}
}
}
}
}
function addNodeSizeVisualization(label, description, field, cladePropertyRef, isRegex, mapping, mappingFn, scaleType) {
if (arguments.length !== 8) {
throw('expected 8 arguments, got ' + arguments.length);
}
if (!_visualizations) {
_visualizations = {};
}
if (!_visualizations.nodeSize) {
_visualizations.nodeSize = {};
}
if (_visualizations.nodeSize[label]) {
console.log(MESSAGE + 'node size visualization for "' + label + '" already exists');
}
let vis = createVisualization(label, description, field, cladePropertyRef, isRegex, mapping, mappingFn, scaleType);
if (vis) {
_visualizations.nodeSize[vis.label] = vis;
}
}
function addNodeFillColorVisualization(label, description, field, cladePropertyRef, isRegex, mapping, mappingFn, scaleType, altMappingFn) {
if (arguments.length < 8) {
throw('expected at least 8 arguments, got ' + arguments.length);
}
if (!_visualizations) {
_visualizations = {};
}