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,355 +0,0 @@
1
- import * as d3 from "d3";
2
-
3
- import { itemTagged, itemSelected } from "./helpers";
4
- import { isLeafNode } from "../nodes";
5
- import { css_classes } from "./options";
6
-
7
- export function shiftTip(d) {
8
-
9
- if (this.radial()) {
10
- return [
11
- (d.text_align == "end" ? -1 : 1) *
12
- (this.radius_pad_for_bubbles - d.radius),
13
- 0
14
- ];
15
- }
16
-
17
- if (this.options["right-to-left"]) {
18
- return [this.right_most_leaf - d.screen_x, 0];
19
- }
20
-
21
- return [this.right_most_leaf - d.screen_x, 0];
22
-
23
- }
24
-
25
- export function drawNode(container, node, transitions) {
26
-
27
- container = d3.select(container);
28
- var is_leaf = isLeafNode(node);
29
-
30
- if (is_leaf) {
31
- container = container.attr("data-node-name", node.data.name);
32
- }
33
-
34
- var labels = container.selectAll("text").data([node]),
35
- tracers = container.selectAll("line");
36
-
37
- if (is_leaf || (this.showInternalName(node) && !isNodeCollapsed(node))) {
38
-
39
- labels = labels
40
- .enter()
41
- .append("text")
42
- .classed(this.css_classes["node_text"], true)
43
- .merge(labels)
44
- .on("click", d=> {
45
- this.handle_node_click(node, d);
46
- })
47
- .attr("dy", d => {
48
- return this.shown_font_size * 0.33;
49
- })
50
- .text(d => {
51
- return this.options["show-labels"] ? this._nodeLabel(d) : "";
52
- })
53
- .style("font-size", d => {
54
- return this.ensure_size_is_in_px(this.shown_font_size);
55
- });
56
-
57
- if (this.radial()) {
58
- labels = labels
59
- .attr("transform", d => {
60
- return (
61
- this.d3PhylotreeSvgRotate(d.text_angle) +
62
- this.d3PhylotreeSvgTranslate(
63
- this.alignTips() ? this.shiftTip(d) : null
64
- )
65
- );
66
- })
67
- .attr("text-anchor", d => {
68
- return d.text_align;
69
- });
70
- } else {
71
- labels = labels.attr("text-anchor", "start").attr("transform", d => {
72
- if (this.options["layout"] == "right-to-left") {
73
- return this.d3PhylotreeSvgTranslate([-20, 0]);
74
- }
75
- return this.d3PhylotreeSvgTranslate(
76
- this.alignTips() ? this.shiftTip(d) : null
77
- );
78
- });
79
- }
80
-
81
- if (this.alignTips()) {
82
- tracers = tracers.data([node]);
83
-
84
- if (transitions) {
85
- tracers = tracers
86
- .enter()
87
- .append("line")
88
- .classed(this.css_classes["branch-tracer"], true)
89
- .merge(tracers)
90
- .attr("x1", d => {
91
- return (
92
- (d.text_align == "end" ? -1 : 1) * this.nodeBubbleSize(node)
93
- );
94
- })
95
- .attr("x2", 0)
96
- .attr("y1", 0)
97
- .attr("y2", 0)
98
- .attr("x2", d => {
99
- if (this.options["layout"] == "right-to-left") {
100
- return d.screen_x;
101
- }
102
-
103
- return this.shiftTip(d)[0];
104
- })
105
- .attr("transform", d => {
106
- return this.d3PhylotreeSvgRotate(d.text_angle);
107
- })
108
- .attr("x2", d => {
109
- if (this.options["layout"] == "right-to-left") {
110
- return d.screen_x;
111
- }
112
- return this.shiftTip(d)[0];
113
- })
114
- .attr("transform", d => {
115
- return this.d3PhylotreeSvgRotate(d.text_angle);
116
- });
117
- } else {
118
- tracers = tracers
119
- .enter()
120
- .append("line")
121
- .classed(this.css_classes["branch-tracer"], true)
122
- .merge(tracers)
123
- .attr("x1", d => {
124
- return (
125
- (d.text_align == "end" ? -1 : 1) * this.nodeBubbleSize(node)
126
- );
127
- })
128
- .attr("y2", 0)
129
- .attr("y1", 0)
130
- .attr("x2", d => {
131
- return this.shiftTip(d)[0];
132
- });
133
- tracers.attr("transform", d => {
134
- return this.d3PhylotreeSvgRotate(d.text_angle);
135
- });
136
- }
137
- } else {
138
- tracers.remove();
139
- }
140
-
141
- if (this.options["draw-size-bubbles"]) {
142
-
143
- var shift = this.nodeBubbleSize(node);
144
-
145
- let circles = container
146
- .selectAll("circle")
147
- .data([shift])
148
- .enter()
149
- .append("circle");
150
-
151
- circles.attr("r", function(d) {
152
- return d;
153
- });
154
-
155
- if (this.shown_font_size >= 5) {
156
- labels = labels.attr("dx", d => {
157
- return (
158
- (d.text_align == "end" ? -1 : 1) *
159
- ((this.alignTips() ? 0 : shift) + this.shown_font_size * 0.33)
160
- );
161
- });
162
- }
163
- } else {
164
- if (this.shown_font_size >= 5) {
165
- labels = labels.attr("dx", d => { // eslint-disable-line
166
- return (d.text_align == "end" ? -1 : 1) * this.shown_font_size * 0.33;
167
- });
168
- }
169
- }
170
- }
171
-
172
- if (!is_leaf) {
173
- let circles = container
174
- .selectAll("circle")
175
- .data([node])
176
- .enter()
177
- .append("circle"),
178
- radius = this.node_circle_size()(node);
179
-
180
- if (radius > 0) {
181
- circles
182
- .merge(circles)
183
- .attr("r", d => {
184
- return Math.min(this.shown_font_size * 0.75, radius);
185
- })
186
- .on("click", d => {
187
- this.handle_node_click(node, d);
188
- });
189
- } else {
190
- circles.remove();
191
- }
192
- }
193
-
194
- if (this.node_styler) {
195
- this.node_styler(container, node);
196
- }
197
-
198
- return node;
199
- }
200
-
201
- export function updateHasHiddenNodes() {
202
- let nodes = this.phylotree.nodes.descendants();
203
-
204
- for (let k = nodes.length - 1; k >= 0; k -= 1) {
205
- if (isLeafNode(nodes[k])) {
206
- nodes[k].hasHiddenNodes = nodes[k].notshown;
207
- } else {
208
- nodes[k].hasHiddenNodes = nodes[k].children.reduce(function(p, c) {
209
- return c.notshown || p;
210
- }, false);
211
- }
212
- }
213
-
214
- return this;
215
- }
216
-
217
- export function showInternalName(node) {
218
-
219
- const i_names = this.internalNames();
220
-
221
- if (i_names) {
222
- if (typeof i_names === "function") {
223
- return i_names(node);
224
- }
225
- return i_names;
226
- }
227
-
228
- return false;
229
- }
230
-
231
- /**
232
- * Get or set the current node span. If setting, the argument should
233
- * be a function of a node which returns a number, so that node spans
234
- * can be determined dynamically. Alternatively, the argument can be the
235
- * string ``"equal"``, to give all nodes an equal span.
236
- *
237
- * @param {Function} attr Optional; if setting, the nodeSpan function.
238
- * @returns The ``nodeSpan`` if getting, or the current ``phylotree`` if setting.
239
- */
240
- export function nodeSpan(attr) {
241
- if (!arguments.length) return this.nodeSpan;
242
- if (typeof attr == "string" && attr == "equal") {
243
- this.nodeSpan = function(d) {
244
- return 1;
245
- };
246
- } else {
247
- this.nodeSpan = attr;
248
- }
249
- return this;
250
- }
251
-
252
- export function reclassNode(node) {
253
-
254
- let class_var = css_classes[isLeafNode(node) ? "node" : "internal-node"];
255
-
256
- if (itemTagged(node)) {
257
- class_var += " " + css_classes["tagged-node"];
258
- }
259
-
260
- if (itemSelected(node, this.selection_attribute_name)) {
261
- class_var += " " + css_classes["selected-node"];
262
- }
263
-
264
- if (!node["parent"]) {
265
- class_var += " " + css_classes["root-node"];
266
- }
267
-
268
- if (isNodeCollapsed(node) || hasHiddenNodes(node)) {
269
- class_var += " " + css_classes["collapsed-node"];
270
- }
271
-
272
- return class_var;
273
- }
274
-
275
- export function nodeVisible(node) {
276
- return !(node.hidden || node.notshown || false);
277
- }
278
-
279
- export function nodeNotshown(node) {
280
- return node.notshown;
281
- }
282
-
283
- export function hasHiddenNodes(node) {
284
- return node.hasHiddenNodes || false;
285
- }
286
-
287
- export function isNodeCollapsed(node) {
288
- return node.collapsed || false;
289
- }
290
-
291
- export function nodeCssSelectors(css_classes) {
292
- return [
293
- css_classes["node"],
294
- css_classes["internal-node"],
295
- css_classes["collapsed-node"],
296
- css_classes["tagged-node"],
297
- css_classes["root-node"]
298
- ].reduce(function(p, c, i, a) {
299
- return (p += "g." + c + (i < a.length - 1 ? "," : ""));
300
- }, "");
301
- }
302
-
303
- export function internalLabel(callback, respect_existing) {
304
-
305
- this.phylotree.clearInternalNodes(respect_existing);
306
-
307
- for (var i = this.phylotree.nodes.descendants().length - 1; i >= 0; i--) {
308
-
309
- var d = this.phylotree.nodes.descendants()[i];
310
-
311
- if (!(isLeafNode(d) || itemSelected(d, this.selection_attribute_name))) {
312
- d[this.selection_attribute_name] = callback(d.children);
313
- }
314
-
315
- }
316
-
317
- this.modifySelection((d, callback) => {
318
- if (isLeafNode(d.target)) {
319
- return d.target[this.selection_attribute_name];
320
- }
321
- return d.target[this.selection_attribute_name];
322
- });
323
- }
324
-
325
- export function defNodeLabel(_node) {
326
-
327
- _node = _node.data;
328
-
329
- if (isLeafNode(_node)) {
330
- return _node.name || "";
331
- }
332
-
333
- if (this.showInternalName(_node)) {
334
- return _node.name;
335
- }
336
-
337
- return "";
338
-
339
- }
340
-
341
- /**
342
- * Get or set nodeLabel accessor.
343
- *
344
- * @param {Function} attr (Optional) If setting, a function that accesses a branch name
345
- * from a node.
346
- * @returns The ``nodeLabel`` accessor if getting, or the current ``this`` if setting.
347
- */
348
- export function nodeLabel(attr) {
349
- if (!arguments.length) return this._nodeLabel;
350
- this._nodeLabel = attr ? attr : defNodeLabel;
351
- this.update();
352
- return this;
353
- }
354
-
355
-
@@ -1,141 +0,0 @@
1
- import { isLeafNode } from "../nodes";
2
-
3
- export const css_classes = {
4
- "tree-container": "phylotree-container",
5
- "tree-scale-bar": "tree-scale-bar",
6
- node: "node",
7
- "internal-node": "internal-node",
8
- "tagged-node": "node-tagged",
9
- "selected-node": "node-selected",
10
- "collapsed-node": "node-collapsed",
11
- "root-node": "root-node",
12
- branch: "branch",
13
- "selected-branch": "branch-selected",
14
- "tagged-branch": "branch-tagged",
15
- "tree-selection-brush": "tree-selection-brush",
16
- "branch-tracer": "branch-tracer",
17
- clade: "clade",
18
- node_text: "phylotree-node-text"
19
- };
20
-
21
- export function internalNames(attr) {
22
- if (!arguments.length) return this.options["internal-names"];
23
- this.options["internal-names"] = attr;
24
- return this;
25
- }
26
-
27
- export function radial(attr) {
28
- if (!arguments.length) return this.options["is-radial"];
29
- this.options["is-radial"] = attr;
30
- return this;
31
- }
32
-
33
- export function alignTips(attr) {
34
- if (!arguments.length) return this.options["align-tips"];
35
- this.options["align-tips"] = attr;
36
- return this;
37
- }
38
-
39
- /**
40
- * Return the bubble size of the current node.
41
- *
42
- * @param {Node} A node in the phylotree.
43
- * @returns {Float} The size of the bubble associated to this node.
44
- */
45
- export function nodeBubbleSize(node) {
46
-
47
- // if a custom bubble styler, use that instead
48
-
49
- if(this.options["draw-size-bubbles"] && this.options["bubble-styler"]) {
50
- return this.options["bubble-styler"](node);
51
- } else {
52
- return this.options["draw-size-bubbles"]
53
- ? this.relative_nodeSpan(node) * this.scales[0] * 0.25
54
- : 0;
55
- }
56
- }
57
-
58
- export function shiftTip(d) {
59
- if (this.options["is-radial"]) {
60
- return [
61
- (d.text_align == "end" ? -1 : 1) *
62
- (this.radius_pad_for_bubbles - d.radius),
63
- 0
64
- ];
65
- }
66
- if (this.options["right-to-left"]) {
67
- return [this.right_most_leaf - d.screen_x, 0];
68
- }
69
- return [this.right_most_leaf - d.screen_x, 0];
70
- }
71
-
72
- export function layoutHandler(attr) {
73
- if (!arguments.length) return this.layout_listener_handler;
74
- this.layout_listener_handler = attr;
75
- return this;
76
- }
77
-
78
- /**
79
- * Getter/setter for the selection label. Useful when allowing
80
- * users to make multiple selections.
81
- *
82
- * @param {String} attr (Optional) If setting, the new selection label.
83
- * @returns The current selection label if getting, or the current ``phylotree`` if setting.
84
- */
85
- export function selectionLabel(attr) {
86
- if (!arguments.length) return this.selection_attribute_name;
87
- this.selection_attribute_name = attr;
88
- this.syncEdgeLabels();
89
- return this;
90
- }
91
-
92
- /**
93
- * Get or set the current node span. If setting, the argument should
94
- * be a function of a node which returns a number, so that node spans
95
- * can be determined dynamically. Alternatively, the argument can be the
96
- * string ``"equal"``, to give all nodes an equal span.
97
- *
98
- * @param {Function} attr Optional; if setting, the nodeSpan function.
99
- * @returns The ``nodeSpan`` if getting, or the current ``phylotree`` if setting.
100
- */
101
- export function nodeSpan(attr) {
102
- if (!arguments.length) return nodeSpan;
103
- if (typeof attr == "string" && attr == "equal") {
104
- nodeSpan = function(d) { // eslint-disable-line
105
- return 1;
106
- };
107
- } else {
108
- nodeSpan = attr; // eslint-disable-line
109
- }
110
- return this;
111
- }
112
-
113
- // List of all selecters that can be used with the restricted-selectable option
114
- export var predefined_selecters = {
115
- all: d => {
116
- return true;
117
- },
118
- none: d => {
119
- return false;
120
- },
121
- "all-leaf-nodes": d => {
122
- return isLeafNode(d.target);
123
- },
124
- "all-internal-nodes": d => {
125
- return !isLeafNode(d.target);
126
- }
127
- };
128
-
129
- /**
130
- * Getter/setter for the selection callback. This function is called
131
- * every time the current selection is modified, and its argument is
132
- * an array of nodes that make up the current selection.
133
- *
134
- * @param {Function} callback (Optional) The selection callback function.
135
- * @returns The current ``selectionCallback`` if getting, or the current ``this`` if setting.
136
- */
137
- export function selectionCallback(callback) {
138
- if (!callback) return this.selectionCallback;
139
- this.selectionCallback = callback;
140
- return this;
141
- }
@@ -1,79 +0,0 @@
1
- import { xCoord, yCoord } from "./coordinates";
2
-
3
- function radialMapper(r, a, radial_center) {
4
- return {
5
- x: radial_center + r * Math.sin(a),
6
- y: radial_center + r * Math.cos(a)
7
- };
8
- }
9
-
10
- function cartesianMapper(x, y, radial_center) { // eslint-disable-line
11
- return polarToCartesian(x - radial_center, y - radial_center);
12
- }
13
-
14
- function polarToCartesian(x, y) {
15
- let r = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
16
- let a = Math.atan2(y, x);
17
- return [r, a];
18
- }
19
-
20
- export function cartesianToPolar(
21
- node,
22
- radius,
23
- radial_root_offset,
24
- radial_center,
25
- scales,
26
- size
27
- ) {
28
-
29
- node.radius = radius * (node.radius + radial_root_offset);
30
-
31
- //if (!node.angle) {
32
- node.angle = 2 * Math.PI * node.x * scales[0] / size[0];
33
- //}
34
-
35
- let radial = radialMapper(node.radius, node.angle, radial_center);
36
-
37
- node.x = radial.x;
38
- node.y = radial.y;
39
-
40
- return node;
41
-
42
- }
43
-
44
- export function drawArc(radial_center, points) {
45
-
46
-
47
- var start = radialMapper(points[0].radius, points[0].angle, radial_center),
48
- end = radialMapper(points[0].radius, points[1].angle, radial_center);
49
-
50
- return (
51
- "M " +
52
- xCoord(start) +
53
- "," +
54
- yCoord(start) +
55
- " A " +
56
- points[0].radius +
57
- "," +
58
- points[0].radius +
59
- " 0,0, " +
60
- (points[1].angle > points[0].angle ? 1 : 0) +
61
- " " +
62
- xCoord(end) +
63
- "," +
64
- yCoord(end) +
65
- " L " +
66
- xCoord(points[1]) +
67
- "," +
68
- yCoord(points[1])
69
- );
70
- }
71
-
72
- export function arcSegmentPlacer(edge, where, radial_center) {
73
- var r = radialMapper(
74
- edge.target.radius + (edge.source.radius - edge.target.radius) * where,
75
- edge.target.angle,
76
- radial_center
77
- );
78
- return { x: xCoord(r), y: yCoord(r) };
79
- }