phylotree 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/bin/phylotree-validate.js +33 -0
  2. package/bin/phylotree.js +1 -0
  3. package/dist/phylotree.js +120 -114
  4. package/dist/phylotree.min.js +1 -1
  5. package/package.json +4 -4
  6. package/src/branches.js +0 -155
  7. package/src/clustering/cluster-picker.js +0 -115
  8. package/src/clustering/phylopart.js +0 -238
  9. package/src/export.js +0 -25
  10. package/src/extract-dates.js +0 -69
  11. package/src/formats/beast.js +0 -29
  12. package/src/formats/newick.js +0 -272
  13. package/src/formats/nexml.js +0 -42
  14. package/src/formats/nexus.js +0 -96
  15. package/src/formats/phyloxml.js +0 -80
  16. package/src/formats/registry.js +0 -20
  17. package/src/index.js +0 -24
  18. package/src/main.js +0 -311
  19. package/src/max-parsimony.js +0 -81
  20. package/src/metrics/center-of-tree.js +0 -138
  21. package/src/metrics/compute-midpoint.js +0 -81
  22. package/src/metrics/pairwise-distances.js +0 -119
  23. package/src/metrics/root-to-tip.js +0 -220
  24. package/src/metrics/sackins.js +0 -21
  25. package/src/msa-parsers/fasta.js +0 -18
  26. package/src/neighbor-join.js +0 -196
  27. package/src/nodes.js +0 -241
  28. package/src/render/cartesian.js +0 -23
  29. package/src/render/clades.js +0 -116
  30. package/src/render/coordinates.js +0 -7
  31. package/src/render/draw.js +0 -1365
  32. package/src/render/edges.js +0 -134
  33. package/src/render/events.js +0 -166
  34. package/src/render/helpers.js +0 -7
  35. package/src/render/menus.js +0 -452
  36. package/src/render/nodes.js +0 -355
  37. package/src/render/options.js +0 -141
  38. package/src/render/radial.js +0 -79
  39. package/src/rooting.js +0 -183
  40. package/src/transformations.js +0 -20
  41. package/src/traversal.js +0 -114
@@ -1,1365 +0,0 @@
1
- import * as d3 from "d3";
2
- import * as _ from "underscore";
3
-
4
- import { drawArc, cartesianToPolar, arcSegmentPlacer } from "./radial";
5
- import { default as draw_line, lineSegmentPlacer } from "./cartesian";
6
- import { isLeafNode } from "../nodes";
7
- import { xCoord, yCoord } from "./coordinates";
8
- import * as clades from "./clades";
9
- import * as render_nodes from "./nodes";
10
- import * as render_edges from "./edges";
11
- import * as events from "./events";
12
- import { css_classes } from "./options";
13
- import * as opt from "./options";
14
- import * as menus from "./menus";
15
-
16
- // replacement for d3.functor
17
- function constant(x) {
18
- return function() {
19
- return x;
20
- };
21
- }
22
-
23
- class TreeRender {
24
- constructor(phylotree, options = {}) {
25
- this.css_classes = css_classes;
26
- this.phylotree = phylotree;
27
- this.container = options.container;
28
- this.separation = function(_node, _previous) {
29
- return 0;
30
- };
31
-
32
- this._nodeLabel = this.defNodeLabel;
33
- this.svg = null;
34
- this.selectionCallback = null;
35
- this.scales = [1, 1];
36
- this.size = [1, 1];
37
- this.fixed_width = [14, 30];
38
- this.font_size = 12;
39
- this.scale_bar_font_size = 12;
40
- this.offsets = [0, this.font_size / 2];
41
-
42
- this.draw_branch = draw_line;
43
- this.draw_scale_bar = null;
44
- this.edge_placer = lineSegmentPlacer;
45
- this.count_listener_handler = function() {};
46
- this.layout_listener_handler = function() {};
47
- this.node_styler = undefined;
48
- this.edge_styler = undefined;
49
- this.shown_font_size = this.font_size;
50
- this.selection_attribute_name = "selected";
51
- this.right_most_leaf = 0;
52
- this.label_width = 0;
53
- this.radial_center = 0;
54
- this.radius = 1;
55
- this.radius_pad_for_bubbles = 0;
56
- this.rescale_nodeSpan = 1;
57
- this.relative_nodeSpan = function(_node) {
58
- return this.nodeSpan(_node) / this.rescale_nodeSpan;
59
- };
60
-
61
- let default_options = {
62
- layout: "left-to-right",
63
- logger: console,
64
- branches: "step",
65
- scaling: true,
66
- bootstrap: false,
67
- "color-fill": true,
68
- "internal-names": false,
69
- selectable: true,
70
- // restricted-selectable can take an array of predetermined
71
- // selecters that are defined in phylotree.predefined_selecters
72
- // only the defined functions will be allowed when selecting
73
- // branches
74
- "restricted-selectable": false,
75
- collapsible: true,
76
- "left-right-spacing": "fixed-step", //'fit-to-size',
77
- "top-bottom-spacing": "fixed-step",
78
- "left-offset": 0,
79
- "show-scale": "top",
80
- // currently not implemented to support any other positioning
81
- "draw-size-bubbles": false,
82
- "bubble-styler": this.radius_pad_for_bubbles,
83
- "binary-selectable": false,
84
- "is-radial": false,
85
- "attribute-list": [],
86
- "max-radius": 768,
87
- "annular-limit": 0.38196601125010515,
88
- compression: 0.2,
89
- "align-tips": false,
90
- "maximum-per-node-spacing": 100,
91
- "minimum-per-node-spacing": 2,
92
- "maximum-per-level-spacing": 100,
93
- "minimum-per-level-spacing": 10,
94
- node_circle_size: constant(3),
95
- transitions: null,
96
- brush: true,
97
- reroot: true,
98
- hide: true,
99
- "label-nodes-with-name": false,
100
- zoom: false,
101
- "show-menu": true,
102
- "show-labels": true,
103
- "node-styler": null,
104
- "edge-styler": null,
105
- "node-span": null
106
- };
107
-
108
- this.ensure_size_is_in_px = function(value) {
109
- return typeof value === "number" ? value + "px" : value;
110
- };
111
-
112
- this.options = _.defaults(options, default_options);
113
-
114
- this.width = this.options.width || 800;
115
- this.height = this.options.height || 600;
116
-
117
- this.node_styler = this.options['node-styler'];
118
- this.edge_styler = this.options['edge-styler'];
119
-
120
- this.nodeSpan = this.options['node-span'];
121
-
122
- if(!this.nodeSpan) {
123
- this.nodeSpan = function(_node) {
124
- return 1;
125
- };
126
- }
127
-
128
- this.rescale_nodeSpan =
129
- this.phylotree.nodes.children
130
- .map(d => {
131
- if (isLeafNode(d) || this.showInternalName(d))
132
- return this.nodeSpan(d);
133
- })
134
- .reduce(function(p, c) {
135
- return Math.min(c, p || 1e200);
136
- }, null) || 1;
137
-
138
- this.initialize_svg(this.container);
139
- this.links = this.phylotree.nodes.links();
140
- this.initializeEdgeLabels();
141
- this.update();
142
- events.d3PhylotreeAddEventListener();
143
- }
144
-
145
- pad_height() {
146
- if (this.draw_scale_bar) {
147
- return this.scale_bar_font_size + 25;
148
- }
149
-
150
- return 0;
151
- }
152
-
153
- pad_width() {
154
- // reset label_width
155
- this.label_width = this._label_width(this.shown_font_size);
156
-
157
- const _label_width = this.options["show-labels"] ? this.label_width : 0;
158
- return this.offsets[1] + this.options["left-offset"] + _label_width;
159
- }
160
-
161
- /**
162
- * Collapses a given node.
163
- *
164
- * @param {Node} node A node to be collapsed.
165
- */
166
- collapse_node(n) {
167
- if (!render_nodes.isNodeCollapsed(n)) {
168
- n.collapsed = true;
169
- }
170
- }
171
-
172
- /**
173
- * Get or set the size of tree in pixels.
174
- *
175
- * @param {Array} attr (optional) An array of the form ``[height, width]``.
176
- * @returns {Phylotree} The current ``size`` array if getting, or the current ``phylotree``
177
- * if setting.
178
- */
179
- set_size(attr) {
180
- if (!arguments.length) {
181
- return this.size;
182
- }
183
-
184
- let phylo_attr = attr;
185
-
186
- if (this.options["top-bottom-spacing"] != "fixed-step") {
187
- this.size[0] = phylo_attr[0];
188
- }
189
- if (this.options["left-right-spacing"] != "fixed-step") {
190
- this.size[1] = phylo_attr[1];
191
- }
192
-
193
- return this;
194
- }
195
-
196
- /**
197
- * Getter/setter for the SVG element for the Phylotree to be rendered in.
198
- *
199
- * @param {d3-selection} svg_element (Optional) SVG element to render within, selected by D3.
200
- * @returns The selected SVG element if getting, or the current ``phylotree`` if setting.`
201
- */
202
- initialize_svg(svg_element) {
203
- //if (!arguments.length) return this.svg;
204
-
205
- if (this.svg !== svg_element) {
206
- d3.select(svg_element)
207
- .select("svg")
208
- .remove();
209
-
210
- this.svg = d3
211
- .create("svg")
212
- .attr("width", this.width)
213
- .attr("height", this.height);
214
-
215
- this.set_size([this.height, this.width]);
216
-
217
- if (this.css_classes["tree-container"] == "phylotree-container") {
218
- this.svg.selectAll("*").remove();
219
- this.svg.append("defs");
220
- }
221
-
222
- d3.select(this.container).on(
223
- "click",
224
- d => {
225
- this.handle_node_click(null);
226
- },
227
- true
228
- );
229
- }
230
-
231
- return this;
232
- }
233
-
234
- update_layout(new_json, do_hierarchy) {
235
- if (do_hierarchy) {
236
- this.nodes = d3.hierarchy(new_json);
237
- this.nodes.each(function(d) {
238
- d.id = null;
239
- });
240
- }
241
-
242
- this.update();
243
- this.syncEdgeLabels();
244
- }
245
-
246
- /**
247
- * Update the current phylotree, i.e., alter the svg
248
- * elements.
249
- *
250
- * @param {Boolean} transitions (Optional) Toggle whether transitions should be shown.
251
- * @returns The current ``phylotree``.
252
- */
253
- update(transitions) {
254
-
255
- var self = this;
256
-
257
- //if (!this.svg) return this;
258
-
259
- this.placenodes();
260
-
261
- transitions = this.transitions(transitions);
262
-
263
- let node_id = 0;
264
-
265
- let enclosure = this.svg
266
- .selectAll("." + css_classes["tree-container"])
267
- .data([0]);
268
-
269
- enclosure = enclosure
270
- .enter()
271
- .append("g")
272
- .attr("class", css_classes["tree-container"])
273
- .merge(enclosure)
274
- .attr("transform", d => {
275
- return this.d3PhylotreeSvgTranslate([
276
- this.offsets[1] + this.options["left-offset"],
277
- this.pad_height()
278
- ]);
279
- });
280
-
281
- if (this.draw_scale_bar) {
282
- let scale_bar = this.svg
283
- .selectAll("." + css_classes["tree-scale-bar"])
284
- .data([0]);
285
-
286
- scale_bar
287
- .enter()
288
- .append("g")
289
- .attr("class", css_classes["tree-scale-bar"])
290
- .style("font-size", this.ensure_size_is_in_px(this.scale_bar_font_size))
291
- .merge(scale_bar)
292
- .attr("transform", d => {
293
- return this.d3PhylotreeSvgTranslate([
294
- this.offsets[1] + this.options["left-offset"],
295
- this.pad_height() - 10
296
- ]);
297
- })
298
- .call(this.draw_scale_bar);
299
-
300
- scale_bar.selectAll("text").style("text-anchor", "end");
301
- } else {
302
- this.svg.selectAll("." + css_classes["tree-scale-bar"]).remove();
303
- }
304
-
305
- enclosure = this.svg
306
- .selectAll("." + css_classes["tree-container"])
307
- .data([0]);
308
-
309
- this.updateCollapsedClades(transitions);
310
-
311
- let drawn_links = enclosure
312
- .selectAll(render_edges.edgeCssSelectors(css_classes))
313
- .data(this.links.filter(render_edges.edgeVisible), d => {
314
- return d.target.id || (d.target.id = ++node_id);
315
- });
316
-
317
- if (transitions) {
318
- drawn_links.exit().remove();
319
- } else {
320
- drawn_links.exit().remove();
321
- }
322
-
323
- drawn_links = drawn_links
324
- .enter()
325
- .insert("path", ":first-child")
326
- .merge(drawn_links)
327
- .each(function(d) {
328
- self.drawEdge(this, d, transitions);
329
- });
330
-
331
- let drawn_nodes = enclosure
332
- .selectAll(render_nodes.nodeCssSelectors(css_classes))
333
- .data(
334
- this.phylotree.nodes.descendants().filter(render_nodes.nodeVisible),
335
- d => {
336
- return d.id || (d.id = ++node_id);
337
- }
338
- );
339
-
340
- drawn_nodes.exit().remove();
341
-
342
- drawn_nodes = drawn_nodes
343
- .enter()
344
- .append("g")
345
- .attr("class", this.reclassNode)
346
- .merge(drawn_nodes)
347
- .attr("transform", d => {
348
- const should_shift =
349
- this.options["layout"] == "right-to-left" && isLeafNode(d);
350
-
351
- d.screen_x = xCoord(d);
352
- d.screen_y = yCoord(d);
353
-
354
- return this.d3PhylotreeSvgTranslate([
355
- should_shift ? 0 : d.screen_x,
356
- d.screen_y
357
- ]);
358
- })
359
- .each(function(d) {
360
- self.drawNode(this, d, transitions);
361
- })
362
- .attr("transform", d => {
363
- if (!_.isUndefined(d.screen_x) && !_.isUndefined(d.screen_y)) {
364
- return "translate(" + d.screen_x + "," + d.screen_y + ")";
365
- }
366
- });
367
-
368
- if (this.options["label-nodes-with-name"]) {
369
- drawn_nodes = drawn_nodes.attr("id", d => {
370
- return "node-" + d.name;
371
- });
372
- }
373
-
374
- this.resizeSvg(this.phylotree, this.svg, transitions);
375
-
376
- if (this.options["brush"]) {
377
- var brush = enclosure
378
- .selectAll("." + css_classes["tree-selection-brush"])
379
- .data([0])
380
- .enter()
381
- .insert("g", ":first-child")
382
- .attr("class", css_classes["tree-selection-brush"]);
383
-
384
- var brush_object = d3
385
- .brush()
386
- .on("brush", (event, d) => {
387
- var extent = event.selection,
388
- shown_links = this.links.filter(render_edges.edgeVisible);
389
- var selected_links = shown_links
390
- .filter((d, i) => {
391
- return (
392
- d.source.screen_x >= extent[0][0] &&
393
- d.source.screen_x <= extent[1][0] &&
394
- d.source.screen_y >= extent[0][1] &&
395
- d.source.screen_y <= extent[1][1] &&
396
- d.target.screen_x >= extent[0][0] &&
397
- d.target.screen_x <= extent[1][0] &&
398
- d.target.screen_y >= extent[0][1] &&
399
- d.target.screen_y <= extent[1][1]
400
- );
401
- })
402
- .map(d => {
403
- return d.target;
404
- });
405
-
406
- this.modifySelection(
407
-
408
- this.phylotree.links.map(d => {
409
- return d.target;
410
- }),
411
- "tag",
412
- false,
413
- selected_links.length > 0,
414
- "false"
415
- );
416
-
417
- this.modifySelection(selected_links, "tag", false, false, "true");
418
-
419
- })
420
- .on("end", () => {
421
- //brush.call(d3.event.target.clear());
422
- });
423
-
424
- brush.call(brush_object);
425
- }
426
-
427
- this.syncEdgeLabels();
428
-
429
- if (this.options["zoom"]) {
430
- let zoom = d3
431
- .zoom()
432
- .scaleExtent([0.1, 10])
433
- .on("zoom", (event) => {
434
-
435
- d3.select("." + css_classes["tree-container"]).attr("transform", d => {
436
- let toTransform = event.transform;
437
- return toTransform;
438
- });
439
-
440
- // Give some extra room
441
- d3.select("." + css_classes["tree-scale-bar"]).attr("transform", d => {
442
- let toTransform = event.transform;
443
- toTransform.y -= 10;
444
- return toTransform;
445
- });
446
-
447
- });
448
-
449
- this.svg.call(zoom);
450
- }
451
-
452
- return this;
453
- }
454
-
455
- _handle_single_node_layout(
456
- a_node
457
- ) {
458
- let _nodeSpan = this.nodeSpan(a_node) / this.rescale_nodeSpan;
459
- // compute the relative size of nodes (0,1)
460
- // sum over all nodes is 1
461
- this.x = a_node.x =
462
- this.x +
463
- this.separation(this.last_node, a_node) +
464
- (this.last_span + _nodeSpan) * 0.5;
465
-
466
-
467
- // separation is a user-settable callback to add additional spacing on nodes
468
- this._extents[1][1] = Math.max(this._extents[1][1], a_node.y);
469
- this._extents[1][0] = Math.min(
470
- this._extents[1][0],
471
- a_node.y - _nodeSpan * 0.5
472
- );
473
-
474
-
475
- if (this.is_under_collapsed_parent) {
476
- this._extents[0][1] = Math.max(
477
- this._extents[0][1],
478
- this.save_x +
479
- (a_node.x - this.save_x) * this.options["compression"] +
480
- this.save_span +
481
- (_nodeSpan * 0.5 + this.separation(this.last_node, a_node)) *
482
- this.options["compression"]
483
- );
484
- } else {
485
- this._extents[0][1] = Math.max(
486
- this._extents[0][1],
487
- this.x + _nodeSpan * 0.5 + this.separation(this.last_node, a_node)
488
- );
489
- }
490
-
491
-
492
- this.last_node = a_node;
493
- this.last_span = _nodeSpan;
494
-
495
- }
496
-
497
- tree_layout(a_node) {
498
- /**
499
- for each node:
500
- y: the y coordinate is root to tip
501
- (left to right in cladogram layout, radius is radial layout
502
- x : the x coordinate is top-most to bottom-most
503
- (top to bottom in cladogram layout, angle in radial layout)
504
-
505
-
506
- @return the x-coordinate of a_node or undefined in the node is not displayed
507
- (hidden or under a collapsed node)
508
- */
509
-
510
-
511
- // do not layout hidden nodes
512
- if (render_nodes.nodeNotshown(a_node)) {
513
- return undefined;
514
- }
515
-
516
- let is_leaf = isLeafNode(a_node);
517
-
518
- // the next four members are radial layout options
519
- a_node.text_angle = null; // the angle at which text is being laid out
520
- a_node.text_align = null; // css alignment option for node labels
521
- a_node.radius = null; // radial layout radius
522
- a_node.angle = null; // radial layout angle (in radians)
523
-
524
- /** determine the root-to-tip location of this node;
525
-
526
- the root node receives the co-ordinate of 0
527
-
528
- if the tree has branch lengths, then the placement of each node is simply the
529
- total branch length to the root
530
-
531
- if the tree has no branch lengths, all leaves get the same depth ("number of internal nodes on the deepest path")
532
- and all internal nodes get the depth in integer units of the # of internal nodes on the path to the root + 1
533
-
534
- */
535
-
536
- let undef_BL = false;
537
-
538
- /** _extents computes a bounding box for the tree (initially NOT in screen
539
- coordinates)
540
-
541
- all account for node sizes
542
-
543
- _extents [1][0] -- the minimum x coordinate (breadth)
544
- _extents [1][1] -- the maximum y coordinate (breadth)
545
- _extents [1][0] -- the minimum y coordinate (root-to-tip, or depthwise)
546
- _extents [1][1] -- the maximum y coordinate (root-to-tip, or depthwise)
547
-
548
- */
549
-
550
-
551
- // last node laid out in the top bottom hierarchy
552
-
553
- if (a_node["parent"]) {
554
- if (this.do_scaling) {
555
- if (undef_BL) {
556
- return 0;
557
- }
558
-
559
- a_node.y = this.phylotree.branch_length_accessor(a_node);
560
-
561
- if (typeof a_node.y === "undefined") {
562
- undef_BL = true;
563
- return 0;
564
- }
565
- a_node.y += a_node.parent.y;
566
- } else {
567
- a_node.y = is_leaf ? this.max_depth : a_node.depth;
568
- }
569
- } else {
570
- this.x = 0.0;
571
- // the span of the last node laid out in the top to bottom hierarchy
572
- a_node.y = 0.0;
573
- this.last_node = null;
574
- this.last_span = 0.0;
575
- this._extents = [[0, 0], [0, 0]];
576
- }
577
-
578
- /** the next block has to do with top-to-bottom spacing of nodes **/
579
-
580
- if (is_leaf) {
581
- // displayed internal nodes are handled in `process_internal_node`
582
- this._handle_single_node_layout(
583
- a_node
584
- );
585
- }
586
-
587
- if (!is_leaf) {
588
- // for internal nodes
589
- if (
590
- render_nodes.isNodeCollapsed(a_node) &&
591
- !this.is_under_collapsed_parent
592
- ) {
593
- // collapsed node
594
- this.save_x = this.x;
595
- this.save_span = this.last_span * 0.5;
596
- this.is_under_collapsed_parent = true;
597
- this.process_internal_node(a_node);
598
- this.is_under_collapsed_parent = false;
599
-
600
- if (typeof a_node.x === "number") {
601
- a_node.x =
602
- this.save_x +
603
- (a_node.x -this.save_x) * this.options["compression"] +
604
- this.save_span;
605
-
606
- a_node.collapsed = [[a_node.x, a_node.y]];
607
-
608
- var map_me = n => {
609
- n.hidden = true;
610
-
611
- if (isLeafNode(n)) {
612
- this.x = n.x =
613
- this.save_x +
614
- (n.x - this.save_x) * this.options["compression"] +
615
- this.save_span;
616
-
617
- a_node.collapsed.push([n.x, n.y]);
618
- } else {
619
- n.children.map(map_me);
620
- }
621
- };
622
-
623
- this.x = this.save_x;
624
- map_me(a_node);
625
-
626
-
627
- a_node.collapsed.splice(1, 0, [this.save_x, a_node.y]);
628
- a_node.collapsed.push([this.x, a_node.y]);
629
- a_node.collapsed.push([a_node.x, a_node.y]);
630
- a_node.hidden = false;
631
- }
632
- } else {
633
- // normal node, or under a collapsed parent
634
- this.process_internal_node(a_node);
635
- }
636
- }
637
-
638
- return a_node.x;
639
- }
640
-
641
- process_internal_node(a_node) {
642
- /**
643
- decide if the node will be shown, and compute its top-to-bottom (breadthwise)
644
- placement
645
- */
646
-
647
- let count_undefined = 0;
648
-
649
- if (this.showInternalName(a_node)) {
650
- // do in-order traversal to allow for proper internal node spacing
651
- // (x/2) >> 0 is integer division
652
- let half_way = (a_node.children.length / 2) >> 0;
653
- let displayed_children = 0;
654
- let managed_to_display = false;
655
-
656
- for (let child_id = 0; child_id < a_node.children.length; child_id++) {
657
- let child_x = this.tree_layout(a_node.children[child_id]);//.bind(this);
658
-
659
- if (typeof child_x == "number") {
660
- displayed_children++;
661
- }
662
-
663
- if (displayed_children >= half_way && !managed_to_display) {
664
- this._handle_single_node_layout(a_node);
665
- managed_to_display = true;
666
- }
667
- }
668
-
669
- if (displayed_children == 0) {
670
- a_node.notshown = true;
671
- a_node.x = undefined;
672
- } else {
673
- if (!managed_to_display) {
674
- this._handle_single_node_layout(a_node);
675
- }
676
- }
677
- } else {
678
- // postorder layout
679
- a_node.x = a_node.children
680
- .map(this.tree_layout.bind(this))
681
- .reduce((a, b) => {
682
- if (typeof b == "number") return a + b;
683
- count_undefined += 1;
684
- return a;
685
- }, 0.0);
686
-
687
- if (count_undefined == a_node.children.length) {
688
- a_node.notshown = true;
689
- a_node.x = undefined;
690
- } else {
691
- a_node.x /= a_node.children.length - count_undefined;
692
- }
693
- }
694
- }
695
-
696
- do_lr(at_least_one_dimension_fixed) {
697
- if (this.radial() && at_least_one_dimension_fixed) {
698
- this.offsets[1] = 0;
699
- }
700
-
701
- if (this.options["left-right-spacing"] == "fixed-step") {
702
- this.size[1] = this.max_depth * this.fixed_width[1];
703
-
704
- this.scales[1] =
705
- (this.size[1] - this.offsets[1] - this.options["left-offset"]) /
706
- this._extents[1][1];
707
-
708
- this.label_width = this._label_width(this.shown_font_size);
709
-
710
- if (this.radial()) {
711
- this.label_width *= 2;
712
- }
713
- } else {
714
- this.label_width = this._label_width(this.shown_font_size);
715
-
716
- at_least_one_dimension_fixed = true;
717
-
718
- let available_width =
719
- this.size[1] - this.offsets[1] - this.options["left-offset"];
720
-
721
- if (available_width * 0.5 < this.label_width) {
722
- this.shown_font_size *= (available_width * 0.5) / this.label_width;
723
- this.label_width = available_width * 0.5;
724
- }
725
-
726
- this.scales[1] =
727
- (this.size[1] -
728
- this.offsets[1] -
729
- this.options["left-offset"] -
730
- this.label_width) /
731
- this._extents[1][1];
732
- }
733
- }
734
-
735
- /**
736
- * Place the current nodes, i.e., determine their coordinates based
737
- * on current settings.
738
- *
739
- * @returns The current ``phylotree``.
740
- */
741
- placenodes() {
742
- this._extents = [
743
- [0, 0],
744
- [0, 0]
745
- ];
746
-
747
- this.x = 0.0;
748
- this.last_span = 0.0;
749
- //let x = 0.0,
750
- // last_span = 0;
751
-
752
- this.last_node = null;
753
- this.last_span = 0.0;
754
-
755
- (this.save_x = this.x), (this.save_span = this.last_span * 0.5);
756
-
757
- this.do_scaling = this.options["scaling"];
758
- let undef_BL = false;
759
-
760
- this.is_under_collapsed_parent = false;
761
- this.max_depth = 1;
762
-
763
- // Set initial x
764
- this.phylotree.nodes.x = this.tree_layout(
765
- this.phylotree.nodes,
766
- this.do_scaling
767
- );
768
-
769
- this.max_depth = d3.max(this.phylotree.nodes.descendants(), n => {
770
- return n.depth;
771
- });
772
-
773
- if (this.do_scaling && undef_BL) {
774
- // requested scaling, but some branches had no branch lengths
775
- // redo layout without branch lengths
776
- this.do_scaling = false;
777
- this.phylotree.nodes.x = this.tree_layout(this.phylotree.nodes);
778
- }
779
-
780
- let at_least_one_dimension_fixed = false;
781
-
782
- this.draw_scale_bar = this.options["show-scale"] && this.do_scaling;
783
-
784
- // this is a hack so that phylotree.pad_height would return ruler spacing
785
- this.offsets[1] = Math.max(
786
- this.font_size,
787
- -this._extents[1][0] * this.fixed_width[0]
788
- );
789
-
790
- if (this.options["top-bottom-spacing"] == "fixed-step") {
791
- this.size[0] = this._extents[0][1] * this.fixed_width[0];
792
- this.scales[0] = this.fixed_width[0];
793
- } else {
794
- this.scales[0] = (this.size[0] - this.pad_height()) / this._extents[0][1];
795
- at_least_one_dimension_fixed = true;
796
- }
797
-
798
- this.shown_font_size = Math.min(this.font_size, this.scales[0]);
799
-
800
- if (this.radial()) {
801
- // map the nodes to polar coordinates
802
- this.draw_branch = _.partial(drawArc, this.radial_center);
803
- this.edge_placer = arcSegmentPlacer;
804
-
805
- let last_child_angle = null,
806
- last_circ_position = null,
807
- last_child_radius = null,
808
- min_radius = 0,
809
- effective_span = this._extents[0][1] * this.scales[0];
810
-
811
- let compute_distance = function(r1, r2, a1, a2, annular_shift) {
812
- annular_shift = annular_shift || 0;
813
- return Math.sqrt(
814
- (r2 - r1) * (r2 - r1) +
815
- 2 *
816
- (r1 + annular_shift) *
817
- (r2 + annular_shift) *
818
- (1 - Math.cos(a1 - a2))
819
- );
820
- };
821
-
822
- let max_r = 0;
823
-
824
- this.phylotree.nodes.each(d => {
825
- let my_circ_position = d.x * this.scales[0];
826
- d.angle = (2 * Math.PI * my_circ_position) / effective_span;
827
- d.text_angle = d.angle - Math.PI / 2;
828
- d.text_angle = d.text_angle > 0 && d.text_angle < Math.PI;
829
- d.text_align = d.text_angle ? "end" : "start";
830
- d.text_angle = (d.text_angle ? 180 : 0) + (d.angle * 180) / Math.PI;
831
- });
832
-
833
- this.do_lr(at_least_one_dimension_fixed);
834
-
835
- this.phylotree.nodes.each(d => {
836
- d.radius = (d.y * this.scales[1]) / this.size[1];
837
- max_r = Math.max(d.radius, max_r);
838
- });
839
-
840
- let annular_shift = 0;
841
-
842
- this.phylotree.nodes.each(d => {
843
- if (!d.children) {
844
- let my_circ_position = d.x * this.scales[0];
845
- if (last_child_angle !== null) {
846
- let required_spacing = my_circ_position - last_circ_position,
847
- radial_dist = compute_distance(
848
- d.radius,
849
- last_child_radius,
850
- d.angle,
851
- last_child_angle,
852
- annular_shift
853
- );
854
-
855
- let local_mr =
856
- radial_dist > 0
857
- ? required_spacing / radial_dist
858
- : 10 * this.options["max-radius"];
859
-
860
- if (local_mr > this.options["max-radius"]) {
861
- // adjust the annular shift
862
- let dd = required_spacing / this.options["max-radius"],
863
- b = d.radius + last_child_radius,
864
- c =
865
- d.radius * last_child_radius -
866
- (dd * dd -
867
- (last_child_radius - d.radius) *
868
- (last_child_radius - d.radius)) /
869
- 2 /
870
- (1 - Math.cos(last_child_angle - d.angle)),
871
- st = Math.sqrt(b * b - 4 * c);
872
-
873
- annular_shift = Math.min(
874
- this.options["annular-limit"] * max_r,
875
- (-b + st) / 2
876
- );
877
- min_radius = this.options["max-radius"];
878
- } else {
879
- min_radius = Math.max(min_radius, local_mr);
880
- }
881
- }
882
-
883
- last_child_angle = d.angle;
884
- last_circ_position = my_circ_position;
885
- last_child_radius = d.radius;
886
- }
887
- });
888
-
889
- this.radius = Math.min(
890
- this.options["max-radius"],
891
- Math.max(effective_span / 2 / Math.PI, min_radius)
892
- );
893
-
894
- if (at_least_one_dimension_fixed) {
895
- this.radius = Math.min(
896
- this.radius,
897
- (Math.min(effective_span, this._extents[1][1] * this.scales[1]) -
898
- this.label_width) *
899
- 0.5 -
900
- this.radius * annular_shift
901
- );
902
- }
903
-
904
- this.radial_center = this.radius_pad_for_bubbles = this.radius;
905
- this.draw_branch = _.partial(drawArc, this.radial_center);
906
-
907
- let scaler = 1;
908
-
909
- if (annular_shift) {
910
- scaler = max_r / (max_r + annular_shift);
911
- this.radius *= scaler;
912
- }
913
-
914
- this.phylotree.nodes.each(d => {
915
- cartesianToPolar(
916
- d,
917
- this.radius,
918
- annular_shift,
919
- this.radial_center,
920
- this.scales,
921
- this.size
922
- );
923
-
924
- max_r = Math.max(max_r, d.radius);
925
-
926
- if (this.options["draw-size-bubbles"]) {
927
- this.radius_pad_for_bubbles = Math.max(
928
- this.radius_pad_for_bubbles,
929
- d.radius + this.nodeBubbleSize(d)
930
- );
931
- } else {
932
- this.radius_pad_for_bubbles = Math.max(
933
- this.radius_pad_for_bubbles,
934
- d.radius
935
- );
936
- }
937
-
938
- if (d.collapsed) {
939
- d.collapsed = d.collapsed.map(p => {
940
- let z = {};
941
- z.x = p[0];
942
- z.y = p[1];
943
- z = cartesianToPolar(
944
- z,
945
- this.radius,
946
- annular_shift,
947
- this.radial_center,
948
- this.scales,
949
- this.size
950
- );
951
- return [z.x, z.y];
952
- });
953
-
954
- let last_point = d.collapsed[1];
955
-
956
- d.collapsed = d.collapsed.filter(function(p, i) {
957
- if (i < 3 || i > d.collapsed.length - 4) return true;
958
- if (
959
- Math.sqrt(
960
- Math.pow(p[0] - last_point[0], 2) +
961
- Math.pow(p[1] - last_point[1], 2)
962
- ) > 3
963
- ) {
964
- last_point = p;
965
- return true;
966
- }
967
- return false;
968
- });
969
- }
970
- });
971
-
972
- this.size[0] = this.radial_center + this.radius / scaler;
973
- this.size[1] = this.radial_center + this.radius / scaler;
974
- } else {
975
- this.do_lr();
976
-
977
- this.draw_branch = draw_line;
978
- this.edge_placer = lineSegmentPlacer;
979
- this.right_most_leaf = 0;
980
-
981
- this.phylotree.nodes.each(d => {
982
-
983
- d.x *= this.scales[0];
984
- d.y *= this.scales[1]*.8;
985
-
986
- if (this.options["layout"] == "right-to-left") {
987
- d.y = this._extents[1][1] * this.scales[1] - d.y;
988
- }
989
-
990
-
991
- if (isLeafNode(d)) {
992
- this.right_most_leaf = Math.max(
993
- this.right_most_leaf,
994
- d.y + this.nodeBubbleSize(d)
995
- );
996
- }
997
-
998
- if (d.collapsed) {
999
- d.collapsed.forEach(p => {
1000
- p[0] *= this.scales[0];
1001
- p[1] *= this.scales[1]*.8;
1002
- });
1003
-
1004
- let last_x = d.collapsed[1][0];
1005
-
1006
- d.collapsed = d.collapsed.filter(function(p, i) {
1007
- if (i < 3 || i > d.collapsed.length - 4) return true;
1008
- if (p[0] - last_x > 3) {
1009
- last_x = p[0];
1010
- return true;
1011
- }
1012
- return false;
1013
- });
1014
- }
1015
- });
1016
- }
1017
-
1018
- if (this.draw_scale_bar) {
1019
- let domain_limit, range_limit;
1020
-
1021
- if (this.radial()) {
1022
- range_limit = Math.min(this.radius / 5, 50);
1023
- domain_limit = Math.pow(
1024
- 10,
1025
- Math.ceil(
1026
- Math.log((this._extents[1][1] * range_limit) / this.radius) /
1027
- Math.log(10)
1028
- )
1029
- );
1030
-
1031
-
1032
- range_limit = domain_limit * (this.radius / this._extents[1][1]);
1033
-
1034
- if (range_limit < 30) {
1035
- let stretch = Math.ceil(30 / range_limit);
1036
- range_limit *= stretch;
1037
- domain_limit *= stretch;
1038
- }
1039
- } else {
1040
- domain_limit = this._extents[1][1];
1041
-
1042
- range_limit =
1043
- this.size[1] - this.offsets[1] - this.options["left-offset"] - this.shown_font_size;
1044
- }
1045
-
1046
- let scale = d3
1047
- .scaleLinear()
1048
- .domain([0, domain_limit])
1049
- .range([0, range_limit]),
1050
-
1051
- scaleTickFormatter = d3.format(".2f");
1052
-
1053
- this.draw_scale_bar = d3
1054
- .axisTop()
1055
- .scale(scale)
1056
- .tickFormat(function(d) {
1057
- if (d === 0) {
1058
- return "";
1059
- }
1060
- return scaleTickFormatter(d);
1061
- });
1062
-
1063
- if (this.radial()) {
1064
- this.draw_scale_bar.tickValues([domain_limit]);
1065
- } else {
1066
- let round = function(x, n) {
1067
- return n ? Math.round(x * (n = Math.pow(10, n))) / n : Math.round(x);
1068
- };
1069
-
1070
- let my_ticks = scale.ticks();
1071
- my_ticks = my_ticks.length > 1 ? my_ticks[1] : my_ticks[0];
1072
-
1073
- this.draw_scale_bar.ticks(
1074
- Math.min(
1075
- 10,
1076
- round(
1077
- range_limit /
1078
- (this.shown_font_size *
1079
- scaleTickFormatter(my_ticks).length *
1080
- 2),
1081
- 0
1082
- )
1083
- )
1084
- );
1085
- }
1086
- } else {
1087
- this.draw_scale_bar = null;
1088
- }
1089
-
1090
- return this;
1091
- }
1092
-
1093
- /**
1094
- * Get or set spacing in the x-direction.
1095
- *
1096
- * @param {Number} attr (Optional), the new spacing value if setting.
1097
- * @param {Boolean} skip_render (Optional), whether or not a refresh should be performed.
1098
- * @returns The current ``spacing_x`` value if getting, or the current ``phylotree`` if setting.
1099
- */
1100
- spacing_x(attr, skip_render) {
1101
- if (!arguments.length) return this.fixed_width[0];
1102
-
1103
- if (
1104
- this.fixed_width[0] != attr &&
1105
- attr >= this.options["minimum-per-node-spacing"] &&
1106
- attr <= this.options["maximum-per-node-spacing"]
1107
- ) {
1108
- this.fixed_width[0] = attr;
1109
- if (!skip_render) {
1110
- this.placenodes();
1111
- }
1112
- }
1113
-
1114
- return this;
1115
- }
1116
-
1117
- /**
1118
- * Get or set spacing in the y-direction.
1119
- *
1120
- * @param {Number} attr (Optional), the new spacing value if setting.
1121
- * @param {Boolean} skip_render (Optional), whether or not a refresh should be performed.
1122
- * @returns The current ``spacing_y`` value if getting, or the current ``phylotree`` if setting.
1123
- */
1124
- spacing_y(attr, skip_render) {
1125
- if (!arguments.length) return this.fixed_width[1];
1126
-
1127
- if (
1128
- this.fixed_width[1] != attr &&
1129
- attr >= this.options["minimum-per-level-spacing"] &&
1130
- attr <= this.options["maximum-per-level-spacing"]
1131
- ) {
1132
- this.fixed_width[1] = attr;
1133
- if (!skip_render) {
1134
- this.placenodes();
1135
- }
1136
- }
1137
- return this;
1138
- }
1139
-
1140
- _label_width(_font_size) {
1141
- _font_size = _font_size || this.shown_font_size;
1142
- let width = 0;
1143
-
1144
- this.phylotree.nodes
1145
- .descendants()
1146
- .filter(render_nodes.nodeVisible)
1147
- .forEach(node => {
1148
- let node_width = 12 + this._nodeLabel(node).length * _font_size * 0.8;
1149
-
1150
- if (node.angle !== null) {
1151
- node_width *= Math.max(
1152
- Math.abs(Math.cos(node.angle)),
1153
- Math.abs(Math.sin(node.angle))
1154
- );
1155
- }
1156
- width = Math.max(node_width, width);
1157
- });
1158
-
1159
- return width;
1160
- }
1161
-
1162
- /**
1163
- * Get or set font size.
1164
- *
1165
- * @param {Function} attr Empty if getting, or new font size if setting.
1166
- * @returns The current ``font_size`` accessor if getting, or the current ``phylotree`` if setting.
1167
- */
1168
- font_size(attr) {
1169
- if (!arguments.length) return this.font_size;
1170
- this.font_size = attr === undefined ? 12 : attr;
1171
- return this;
1172
- }
1173
-
1174
- scale_bar_font_size(attr) {
1175
- if (!arguments.length) return this.scale_bar_font_size;
1176
- this.scale_bar_font_size = attr === undefined ? 12 : attr;
1177
- return this;
1178
- }
1179
-
1180
- node_circle_size(attr, attr2) {
1181
- if (!arguments.length) return this.options["node_circle_size"];
1182
- this.options["node_circle_size"] = constant(attr === undefined ? 3 : attr);
1183
- return this;
1184
- }
1185
-
1186
- css(opt) {
1187
- if (arguments.length === 0) return this.css_classes;
1188
-
1189
- if (arguments.length > 2) {
1190
- var arg = {};
1191
- arg[opt[0]] = opt[1];
1192
- return this.css(arg);
1193
- }
1194
-
1195
- for (var key in css_classes) {
1196
- if (key in opt && opt[key] != css_classes[key]) {
1197
- css_classes[key] = opt[key];
1198
- }
1199
- }
1200
-
1201
- return this;
1202
- }
1203
-
1204
- transitions(arg) {
1205
- if (arg !== undefined) {
1206
- return arg;
1207
- }
1208
-
1209
- if (this.options["transitions"] !== null) {
1210
- return this.options["transitions"];
1211
- }
1212
-
1213
- return this.phylotree.nodes.descendants().length <= 300;
1214
- }
1215
-
1216
- /**
1217
- * Get or set CSS classes.
1218
- *
1219
- * @param {Object} opt Keys are the CSS class to toggle and values are
1220
- * the parameters for that CSS class.
1221
- * @param {Boolean} run_update (optional) Whether or not the tree should update.
1222
- * @returns The current ``phylotree``.
1223
- */
1224
- css_classes(opt, run_update) {
1225
- if (!arguments.length) return this.css_classes;
1226
-
1227
- let do_update = false;
1228
-
1229
- for (var key in css_classes) {
1230
- if (key in opt && opt[key] != this.css_classes[key]) {
1231
- do_update = true;
1232
- this.css_classes[key] = opt[key];
1233
- }
1234
- }
1235
-
1236
- if (run_update && do_update) {
1237
- this.layout();
1238
- }
1239
-
1240
- return this;
1241
- }
1242
-
1243
- /**
1244
- * Lay out the tree within the SVG.
1245
- *
1246
- * @param {Boolean} transitions Specify whether or not transitions should occur.
1247
- * @returns The current ``phylotree``.
1248
- */
1249
- layout(transitions) {
1250
- if (this.svg) {
1251
- this.svg.selectAll(
1252
- "." +
1253
- this.css_classes["tree-container"] +
1254
- ",." +
1255
- this.css_classes["tree-scale-bar"] +
1256
- ",." +
1257
- this.css_classes["tree-selection-brush"]
1258
- );
1259
-
1260
- //.remove();
1261
- this.d3PhylotreeTriggerLayout(this);
1262
- return this.update();
1263
- }
1264
-
1265
- this.d3PhylotreeTriggerLayout(this);
1266
- return this;
1267
- }
1268
-
1269
- handle_node_click(node, event) {
1270
- this.nodeDropdownMenu(node, this.container, this, this.options, event);
1271
- }
1272
-
1273
- refresh() {
1274
- if (this.svg) {
1275
- // for re-entrancy
1276
- let enclosure = this.svg.selectAll(
1277
- "." + this.css_classes["tree-container"]
1278
- );
1279
-
1280
- let edges = enclosure
1281
- .selectAll(render_edges.edgeCssSelectors(this.css_classes))
1282
- .attr("class", this.reclassEdge.bind(this));
1283
-
1284
- if (this.edge_styler) {
1285
- edges.each(d => {
1286
- this.edge_styler(d3.select(this), d);
1287
- });
1288
- }
1289
-
1290
- //let nodes = this.enclosure
1291
- // .selectAll(inspector.nodeCssSelectors(this.css_classes))
1292
- // .attr("class", this.phylotree.reclassNode);
1293
-
1294
- //if (this.node_styler) {
1295
- // nodes.each(function(d) {
1296
- // this.node_styler(d3.select(this), d);
1297
- // });
1298
- //}
1299
- }
1300
-
1301
- return this;
1302
- }
1303
-
1304
- countHandler(attr) {
1305
- if (!arguments.length) return this.count_listener_handler;
1306
- this.count_listener_handler = attr;
1307
- return this;
1308
- }
1309
-
1310
- /**
1311
- * Get or set node styler. If setting, pass a function of two arguments,
1312
- * ``element`` and ``data``. ``data`` exposes the underlying node so that
1313
- * its attributes can be referenced. These can be used to apply styles to
1314
- * ``element``, which will be a D3 selection corresponding to the SVG element
1315
- * that makes up the current node.
1316
- * ``transition`` is the third argument which indicates that there is an ongoing
1317
- * d3 transition in progress
1318
- *
1319
- * @param {Function} attr - Optional; if setting, the node styler function to be set.
1320
- * @returns The ``node_styler`` function if getting, or the current ``phylotree`` if setting.
1321
- */
1322
- style_nodes(attr) {
1323
- if (!arguments.length) return this.node_styler;
1324
- this.node_styler = attr;
1325
- return this;
1326
- }
1327
-
1328
- /**
1329
- * Get or set edge styler. If setting, pass a function of two arguments,
1330
- * ``element`` and ``data``. ``data`` exposes the underlying edge so that
1331
- * its attributes can be referenced. These can be used to apply styles to
1332
- * ``element``, which will be a D3 selection corresponding to the SVG element
1333
- * that makes up the current edge.
1334
- *
1335
- * Note that, in accordance with the D3 hierarchy layout, edges will have
1336
- * a ``source`` and ``target`` field, corresponding to the nodes that make up
1337
- * up the associated branch.
1338
- *
1339
- * @param {Function} attr - Optional; if setting, the node styler function to be set.
1340
- * @returns The ``edge_styler`` function if getting, or the current ``phylotree`` if setting.
1341
- */
1342
- style_edges(attr) {
1343
- if (!arguments.length) return this.edge_styler;
1344
- this.edge_styler = attr.bind(this);
1345
- return this;
1346
- }
1347
-
1348
- itemSelected(item, tag) {
1349
- return item[tag] || false;
1350
- }
1351
-
1352
- show() {
1353
- return this.svg.node()
1354
- }
1355
-
1356
- }
1357
-
1358
- _.extend(TreeRender.prototype, clades);
1359
- _.extend(TreeRender.prototype, render_nodes);
1360
- _.extend(TreeRender.prototype, render_edges);
1361
- _.extend(TreeRender.prototype, events);
1362
- _.extend(TreeRender.prototype, menus);
1363
- _.extend(TreeRender.prototype, opt);
1364
-
1365
- export default TreeRender;