phylotree 2.6.1 → 2.7.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.
- package/README.md +48 -4
- package/bin/phylotree-neighbor-join.js +4 -5
- package/bin/root-to-tip.js +1 -1
- package/bin/tip-date-extractor.js +26 -18
- package/dist/phylotree.d.ts +42 -6
- package/dist/phylotree.js +3236 -730
- package/dist/phylotree.min.js +1 -1
- package/package.json +14 -25
- package/src/branches.js +58 -34
- package/src/clustering/cluster-picker.js +18 -2
- package/src/clustering/phylopart.js +14 -6
- package/src/export.js +1 -1
- package/src/extract-dates.js +3 -3
- package/src/formats/beast.js +5 -1
- package/src/formats/newick.js +43 -7
- package/src/formats/nexml.js +87 -30
- package/src/formats/nexus.js +112 -32
- package/src/formats/phyloxml.js +74 -15
- package/src/formats/registry.js +5 -1
- package/src/index.js +1 -0
- package/src/main.js +76 -25
- package/src/metrics/center-of-tree.js +4 -2
- package/src/metrics/compute-midpoint.js +7 -1
- package/src/metrics/pairwise-distances.js +4 -2
- package/src/metrics/root-to-tip.js +43 -10
- package/src/metrics/sackins.js +7 -1
- package/src/msa-parsers/fasta.js +9 -12
- package/src/neighbor-join.js +107 -75
- package/src/nodes.js +43 -31
- package/src/render/clades.js +12 -3
- package/src/render/draw.js +93 -53
- package/src/render/edges.js +12 -3
- package/src/render/menus.js +83 -6
- package/src/render/nodes.js +15 -9
- package/src/render/selection-sets.js +18 -0
- package/src/render/unrooted.js +90 -12
- package/src/rooting.js +9 -3
- package/src/traversal.js +4 -3
package/src/render/unrooted.js
CHANGED
|
@@ -1,12 +1,21 @@
|
|
|
1
1
|
import { xCoord, yCoord } from "./coordinates";
|
|
2
|
+
import {
|
|
3
|
+
isNodeCollapsed,
|
|
4
|
+
nodeNotshown,
|
|
5
|
+
nodeOrAncestorNotshown
|
|
6
|
+
} from "./nodes";
|
|
2
7
|
|
|
3
8
|
/**
|
|
4
|
-
* Count the number of leaf nodes in a subtree.
|
|
9
|
+
* Count the number of leaf nodes in a subtree. A hidden (notshown) subtree
|
|
10
|
+
* has no leaves and a collapsed clade counts as one.
|
|
5
11
|
* @param {Object} node - A D3 hierarchy node
|
|
6
12
|
* @returns {Number} The number of leaves
|
|
7
13
|
*/
|
|
8
14
|
function countLeaves(node) {
|
|
9
|
-
if (
|
|
15
|
+
if (nodeNotshown(node)) {
|
|
16
|
+
return 0;
|
|
17
|
+
}
|
|
18
|
+
if (!node.children || node.children.length === 0 || isNodeCollapsed(node)) {
|
|
10
19
|
return 1;
|
|
11
20
|
}
|
|
12
21
|
return node.children.reduce((sum, child) => sum + countLeaves(child), 0);
|
|
@@ -36,19 +45,35 @@ export function computeUnrootedLayout(root, scales, width, height, labelPad) {
|
|
|
36
45
|
root._unrooted_x = 0;
|
|
37
46
|
root._unrooted_y = 0;
|
|
38
47
|
|
|
48
|
+
root._unrooted_collapsed = null;
|
|
39
49
|
equalAngle(root, 0, 2 * Math.PI);
|
|
40
50
|
|
|
41
|
-
//
|
|
51
|
+
// Hidden nodes and clades are not laid out: they must not take up space
|
|
52
|
+
let isSkipped = d => nodeOrAncestorNotshown(d) || d.hidden;
|
|
53
|
+
|
|
54
|
+
// Find bounding box of all raw positions (and collapsed clade outlines)
|
|
42
55
|
let minX = Infinity, maxX = -Infinity;
|
|
43
56
|
let minY = Infinity, maxY = -Infinity;
|
|
44
57
|
|
|
58
|
+
let extend = (x, y) => {
|
|
59
|
+
if (x < minX) minX = x;
|
|
60
|
+
if (x > maxX) maxX = x;
|
|
61
|
+
if (y < minY) minY = y;
|
|
62
|
+
if (y > maxY) maxY = y;
|
|
63
|
+
};
|
|
64
|
+
|
|
45
65
|
root.each(d => {
|
|
46
|
-
if (d
|
|
47
|
-
|
|
48
|
-
if (d.
|
|
49
|
-
|
|
66
|
+
if (isSkipped(d)) return;
|
|
67
|
+
extend(d._unrooted_x, d._unrooted_y);
|
|
68
|
+
if (d._unrooted_collapsed) {
|
|
69
|
+
d._unrooted_collapsed.forEach(p => extend(p[0], p[1]));
|
|
70
|
+
}
|
|
50
71
|
});
|
|
51
72
|
|
|
73
|
+
if (minX === Infinity) {
|
|
74
|
+
minX = maxX = minY = maxY = 0;
|
|
75
|
+
}
|
|
76
|
+
|
|
52
77
|
let rawWidth = maxX - minX || 1;
|
|
53
78
|
let rawHeight = maxY - minY || 1;
|
|
54
79
|
|
|
@@ -69,9 +94,23 @@ export function computeUnrootedLayout(root, scales, width, height, labelPad) {
|
|
|
69
94
|
// Note: the coordinate system uses d.y for screen-x and d.x for screen-y
|
|
70
95
|
// (xCoord returns d.y, yCoord returns d.x)
|
|
71
96
|
root.each(d => {
|
|
97
|
+
if (isSkipped(d)) return;
|
|
98
|
+
|
|
72
99
|
d.y = d._unrooted_x * scale + offsetX;
|
|
73
100
|
d.x = d._unrooted_y * scale + offsetY;
|
|
74
101
|
|
|
102
|
+
// Collapsed clade outline, as [vertical, horizontal] = [d.x, d.y] points
|
|
103
|
+
// starting at the node (draw.js stores layout-space points otherwise)
|
|
104
|
+
if (d._unrooted_collapsed) {
|
|
105
|
+
d.collapsed = d._unrooted_collapsed.map(p => [
|
|
106
|
+
p[1] * scale + offsetY,
|
|
107
|
+
p[0] * scale + offsetX
|
|
108
|
+
]);
|
|
109
|
+
} else if (Array.isArray(d.collapsed)) {
|
|
110
|
+
// no outline to draw (the root): drop the layout-space points
|
|
111
|
+
d.collapsed = [];
|
|
112
|
+
}
|
|
113
|
+
|
|
75
114
|
// Compute text angle for labels (angle from parent to this node in degrees)
|
|
76
115
|
if (d.parent) {
|
|
77
116
|
let dx = d._unrooted_x - d.parent._unrooted_x;
|
|
@@ -96,6 +135,24 @@ export function computeUnrootedLayout(root, scales, width, height, labelPad) {
|
|
|
96
135
|
});
|
|
97
136
|
}
|
|
98
137
|
|
|
138
|
+
/**
|
|
139
|
+
* Largest distance from a node to any of its shown descendants.
|
|
140
|
+
*
|
|
141
|
+
* @param {Object} node - The clade root
|
|
142
|
+
* @param {Number} baseY - Cumulative branch length at the clade root
|
|
143
|
+
* @returns {Number} The distance, never negative
|
|
144
|
+
*/
|
|
145
|
+
function maxDescendantDistance(node, baseY) {
|
|
146
|
+
let dist = 0;
|
|
147
|
+
(node.children || []).forEach(child => {
|
|
148
|
+
if (nodeNotshown(child)) return;
|
|
149
|
+
let len = child.y - baseY;
|
|
150
|
+
if (isFinite(len)) dist = Math.max(dist, len);
|
|
151
|
+
dist = Math.max(dist, maxDescendantDistance(child, baseY));
|
|
152
|
+
});
|
|
153
|
+
return dist;
|
|
154
|
+
}
|
|
155
|
+
|
|
99
156
|
/**
|
|
100
157
|
* Recursive equal-angle layout.
|
|
101
158
|
*
|
|
@@ -106,7 +163,7 @@ export function computeUnrootedLayout(root, scales, width, height, labelPad) {
|
|
|
106
163
|
function equalAngle(node, startAngle, arcSize) {
|
|
107
164
|
let totalLeaves = countLeaves(node);
|
|
108
165
|
|
|
109
|
-
if (!node.children || node.children.length === 0) {
|
|
166
|
+
if (!node.children || node.children.length === 0 || totalLeaves === 0) {
|
|
110
167
|
return;
|
|
111
168
|
}
|
|
112
169
|
|
|
@@ -114,17 +171,38 @@ function equalAngle(node, startAngle, arcSize) {
|
|
|
114
171
|
|
|
115
172
|
node.children.forEach(child => {
|
|
116
173
|
let childLeaves = countLeaves(child);
|
|
174
|
+
if (childLeaves === 0) {
|
|
175
|
+
// hidden subtree: no angle, no position
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
117
178
|
let childArc = arcSize * (childLeaves / totalLeaves);
|
|
118
179
|
let midAngle = currentAngle + childArc / 2;
|
|
119
180
|
|
|
120
181
|
// Use the node's y value (cumulative branch length / depth) for distance
|
|
121
|
-
let branchLen = child.y
|
|
122
|
-
|
|
182
|
+
let branchLen = child.y - node.y;
|
|
183
|
+
branchLen = isFinite(branchLen) ? Math.max(0, branchLen) : 0;
|
|
123
184
|
|
|
124
185
|
child._unrooted_x = node._unrooted_x + branchLen * Math.cos(midAngle);
|
|
125
186
|
child._unrooted_y = node._unrooted_y + branchLen * Math.sin(midAngle);
|
|
126
|
-
|
|
127
|
-
|
|
187
|
+
child._unrooted_collapsed = null;
|
|
188
|
+
|
|
189
|
+
if (isNodeCollapsed(child)) {
|
|
190
|
+
// a collapsed clade is one wedge; its descendants are not placed
|
|
191
|
+
if (child.children && child.children.length) {
|
|
192
|
+
let r = maxDescendantDistance(child, child.y);
|
|
193
|
+
let cx = child._unrooted_x, cy = child._unrooted_y;
|
|
194
|
+
let at = angle => [cx + r * Math.cos(angle), cy + r * Math.sin(angle)];
|
|
195
|
+
child._unrooted_collapsed = [
|
|
196
|
+
[cx, cy],
|
|
197
|
+
at(currentAngle),
|
|
198
|
+
at(midAngle),
|
|
199
|
+
at(currentAngle + childArc),
|
|
200
|
+
[cx, cy]
|
|
201
|
+
];
|
|
202
|
+
}
|
|
203
|
+
} else {
|
|
204
|
+
equalAngle(child, currentAngle, childArc);
|
|
205
|
+
}
|
|
128
206
|
currentAngle += childArc;
|
|
129
207
|
});
|
|
130
208
|
}
|
package/src/rooting.js
CHANGED
|
@@ -34,6 +34,11 @@ export function reroot(node, fraction) {
|
|
|
34
34
|
throw new Error('node needs to be an instance of a d3.hierarchy node!');
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
// rerooting on the root is a no-op
|
|
38
|
+
if (!node.parent) {
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
|
|
37
42
|
let nodes = this.nodes.copy();
|
|
38
43
|
|
|
39
44
|
fraction = fraction !== undefined ? fraction : 0.5;
|
|
@@ -57,7 +62,7 @@ export function reroot(node, fraction) {
|
|
|
57
62
|
|
|
58
63
|
let remove_me = node,
|
|
59
64
|
current_node = node.parent,
|
|
60
|
-
stashed_bl
|
|
65
|
+
stashed_bl;
|
|
61
66
|
|
|
62
67
|
let apportioned_bl =
|
|
63
68
|
node.data.__mapped_bl === undefined ? undefined : node.data.__mapped_bl * fraction;
|
|
@@ -114,8 +119,9 @@ export function reroot(node, fraction) {
|
|
|
114
119
|
// the tree through
|
|
115
120
|
if (current_node.children.length == 1) {
|
|
116
121
|
|
|
117
|
-
if (stashed_bl) {
|
|
118
|
-
current_node.children[0].data.__mapped_bl
|
|
122
|
+
if (Number.isFinite(stashed_bl)) {
|
|
123
|
+
current_node.children[0].data.__mapped_bl =
|
|
124
|
+
(current_node.children[0].data.__mapped_bl || 0) + stashed_bl;
|
|
119
125
|
}
|
|
120
126
|
|
|
121
127
|
remove_me.children = remove_me.children.concat(current_node.children);
|
package/src/traversal.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import * as _ from "lodash";
|
|
2
1
|
|
|
3
2
|
export function postOrder(node, callback, backtrack) {
|
|
4
3
|
|
|
@@ -82,7 +81,9 @@ export function leftChildRightSibling(root) {
|
|
|
82
81
|
n.children[0].data.multiway_parent = n;
|
|
83
82
|
|
|
84
83
|
// right child is the sibling
|
|
85
|
-
n.children[1]
|
|
84
|
+
if (n.children[1]) {
|
|
85
|
+
n.children[1].data.multiway_parent = n.parent;
|
|
86
|
+
}
|
|
86
87
|
}
|
|
87
88
|
|
|
88
89
|
}
|
|
@@ -91,7 +92,7 @@ export function leftChildRightSibling(root) {
|
|
|
91
92
|
postOrder(root, declareTrueParent);
|
|
92
93
|
|
|
93
94
|
// return edge list
|
|
94
|
-
let edge_list =
|
|
95
|
+
let edge_list = root.descendants().map(n => {
|
|
95
96
|
|
|
96
97
|
let source = n.data.multiway_parent;
|
|
97
98
|
let name = "unknown";
|