phylotree 2.4.0 → 2.6.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/dist/phylotree.d.ts +118 -0
- package/dist/phylotree.js +1783 -450
- package/dist/phylotree.min.js +1 -1
- package/package.json +4 -4
- package/src/render/draw.js +38 -1
- package/src/render/events.js +19 -1
- package/src/render/nodes.js +16 -5
- package/src/render/options.js +6 -0
- package/src/render/unrooted.js +157 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "phylotree",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.0",
|
|
4
4
|
"main": "dist/phylotree.js",
|
|
5
5
|
"types": "dist/phylotree.d.ts",
|
|
6
6
|
"unpkg": "dist/phylotree.js",
|
|
@@ -26,20 +26,20 @@
|
|
|
26
26
|
"commander": "^14.0.0",
|
|
27
27
|
"csv-stringify": "^6.6.0",
|
|
28
28
|
"d3": "^7.9.0",
|
|
29
|
-
"fast-xml-parser": "^5.
|
|
29
|
+
"fast-xml-parser": "^5.3.8",
|
|
30
30
|
"jquery": "^3.7.1",
|
|
31
|
-
"lodash": "^4.17.
|
|
31
|
+
"lodash": "^4.17.23",
|
|
32
32
|
"moment": "^2.30.1",
|
|
33
33
|
"pretty-data": "^0.40.0",
|
|
34
34
|
"underscore": "^1.13.7",
|
|
35
35
|
"winston": "^3.17.0"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
+
"@eslint/js": "^9.39.0",
|
|
38
39
|
"@rollup/plugin-commonjs": "^29.0.0",
|
|
39
40
|
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
40
41
|
"@rollup/plugin-terser": "^0.4.4",
|
|
41
42
|
"concurrently": "^9.2.0",
|
|
42
|
-
"@eslint/js": "^9.39.0",
|
|
43
43
|
"eslint": "^9.39.0",
|
|
44
44
|
"globals": "^16.2.0",
|
|
45
45
|
"http-server": "^14.1.1",
|
package/src/render/draw.js
CHANGED
|
@@ -3,6 +3,7 @@ import * as _ from "underscore";
|
|
|
3
3
|
|
|
4
4
|
import { drawArc, cartesianToPolar, arcSegmentPlacer } from "./radial";
|
|
5
5
|
import { default as draw_line, lineSegmentPlacer } from "./cartesian";
|
|
6
|
+
import { computeUnrootedLayout, drawUnrootedEdge, unrootedSegmentPlacer } from "./unrooted";
|
|
6
7
|
import { isLeafNode } from "../nodes";
|
|
7
8
|
import { xCoord, yCoord } from "./coordinates";
|
|
8
9
|
import * as clades from "./clades";
|
|
@@ -84,6 +85,7 @@ class TreeRender {
|
|
|
84
85
|
"bubble-styler": this.radius_pad_for_bubbles,
|
|
85
86
|
"binary-selectable": false,
|
|
86
87
|
"is-radial": false,
|
|
88
|
+
"is-unrooted": false,
|
|
87
89
|
"attribute-list": [],
|
|
88
90
|
"max-radius": 768,
|
|
89
91
|
"annular-limit": 0.38196601125010515,
|
|
@@ -884,7 +886,42 @@ class TreeRender {
|
|
|
884
886
|
|
|
885
887
|
this.shown_font_size = Math.min(this.font_size, this.scales[0]);
|
|
886
888
|
|
|
887
|
-
if (this.
|
|
889
|
+
if (this.unrooted()) {
|
|
890
|
+
// Unrooted (equal-angle) layout
|
|
891
|
+
//
|
|
892
|
+
// For unrooted, there's no meaningful horizontal vs vertical axis.
|
|
893
|
+
// Both spacing buttons grow the canvas uniformly. Each click beyond
|
|
894
|
+
// the default adds pixels in both dimensions.
|
|
895
|
+
let pixelsPerClick = 40;
|
|
896
|
+
let deltaV = this.fixed_width[0] - 14; // clicks from default vertical
|
|
897
|
+
let deltaH = this.fixed_width[1] - 30; // clicks from default horizontal
|
|
898
|
+
let extraPx = (deltaV + deltaH) * pixelsPerClick;
|
|
899
|
+
|
|
900
|
+
let availWidth = this.width + extraPx;
|
|
901
|
+
let availHeight = this.height + extraPx;
|
|
902
|
+
if (availWidth < 100) availWidth = 100;
|
|
903
|
+
if (availHeight < 100) availHeight = 100;
|
|
904
|
+
|
|
905
|
+
// Reserve space for labels on all sides
|
|
906
|
+
this.shown_font_size = this.font_size;
|
|
907
|
+
this.label_width = this._label_width(this.shown_font_size);
|
|
908
|
+
let labelPad = this.label_width + this.shown_font_size;
|
|
909
|
+
|
|
910
|
+
computeUnrootedLayout(
|
|
911
|
+
this.phylotree.nodes,
|
|
912
|
+
this.scales,
|
|
913
|
+
availWidth,
|
|
914
|
+
availHeight,
|
|
915
|
+
labelPad
|
|
916
|
+
);
|
|
917
|
+
|
|
918
|
+
// Set size so resizeSvg sizes the SVG to fit the tree
|
|
919
|
+
this.size = [availHeight, availWidth];
|
|
920
|
+
|
|
921
|
+
this.draw_branch = drawUnrootedEdge;
|
|
922
|
+
this.edge_placer = unrootedSegmentPlacer;
|
|
923
|
+
this.draw_scale_bar = null;
|
|
924
|
+
} else if (this.radial()) {
|
|
888
925
|
// map the nodes to polar coordinates
|
|
889
926
|
this.draw_branch = _.partial(drawArc, this.radial_center);
|
|
890
927
|
this.edge_placer = arcSegmentPlacer;
|
package/src/render/events.js
CHANGED
|
@@ -41,7 +41,25 @@ export function resizeSvg(tree, svg, tr) {
|
|
|
41
41
|
|
|
42
42
|
let sizes = this.size;
|
|
43
43
|
|
|
44
|
-
if (this.
|
|
44
|
+
if (this.unrooted()) {
|
|
45
|
+
// Unrooted layout: use the computed size (may differ from initial width/height
|
|
46
|
+
// due to spacing button scaling)
|
|
47
|
+
this.baseTransform = { x: 0, y: 0 };
|
|
48
|
+
|
|
49
|
+
if (svg) {
|
|
50
|
+
let transform;
|
|
51
|
+
if (this.currentZoomTransform && this.options["zoom"]) {
|
|
52
|
+
const zt = this.currentZoomTransform;
|
|
53
|
+
transform = `translate(${zt.x}, ${zt.y}) scale(${zt.k})`;
|
|
54
|
+
} else {
|
|
55
|
+
transform = "translate(0,0)";
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
svg
|
|
59
|
+
.selectAll("." + css_classes["tree-container"])
|
|
60
|
+
.attr("transform", transform);
|
|
61
|
+
}
|
|
62
|
+
} else if (this.radial()) {
|
|
45
63
|
let pad_radius = this.pad_width(),
|
|
46
64
|
vertical_offset =
|
|
47
65
|
this.options["top-bottom-spacing"] != "fit-to-size"
|
package/src/render/nodes.js
CHANGED
|
@@ -6,6 +6,11 @@ import { css_classes } from "./options";
|
|
|
6
6
|
|
|
7
7
|
export function shiftTip(d) {
|
|
8
8
|
|
|
9
|
+
if (this.unrooted()) {
|
|
10
|
+
// No tip alignment for unrooted layout
|
|
11
|
+
return [0, 0];
|
|
12
|
+
}
|
|
13
|
+
|
|
9
14
|
if (this.radial()) {
|
|
10
15
|
return [
|
|
11
16
|
(d.text_align == "end" ? -1 : 1) *
|
|
@@ -60,7 +65,15 @@ export function drawNode(container, node, transitions) {
|
|
|
60
65
|
return this.ensure_size_is_in_px(this.shown_font_size);
|
|
61
66
|
});
|
|
62
67
|
|
|
63
|
-
if (this.
|
|
68
|
+
if (this.unrooted()) {
|
|
69
|
+
labels = labels
|
|
70
|
+
.attr("transform", d => {
|
|
71
|
+
return this.d3PhylotreeSvgRotate(d.text_angle);
|
|
72
|
+
})
|
|
73
|
+
.attr("text-anchor", d => {
|
|
74
|
+
return d.text_align;
|
|
75
|
+
});
|
|
76
|
+
} else if (this.radial()) {
|
|
64
77
|
labels = labels
|
|
65
78
|
.attr("transform", d => {
|
|
66
79
|
return (
|
|
@@ -153,8 +166,7 @@ export function drawNode(container, node, transitions) {
|
|
|
153
166
|
if (this.shown_font_size >= 5) {
|
|
154
167
|
const isRTL = this.options["layout"] == "right-to-left";
|
|
155
168
|
labels = labels.attr("dx", d => {
|
|
156
|
-
|
|
157
|
-
const direction = this.radial()
|
|
169
|
+
const direction = (this.radial() || this.unrooted())
|
|
158
170
|
? (d.text_align == "end" ? -1 : 1)
|
|
159
171
|
: (isRTL ? -1 : 1);
|
|
160
172
|
return direction * ((this.alignTips() ? 0 : shift) + this.shown_font_size * 0.33);
|
|
@@ -164,8 +176,7 @@ export function drawNode(container, node, transitions) {
|
|
|
164
176
|
if (this.shown_font_size >= 5) {
|
|
165
177
|
const isRTL = this.options["layout"] == "right-to-left";
|
|
166
178
|
labels = labels.attr("dx", d => { // eslint-disable-line
|
|
167
|
-
|
|
168
|
-
const direction = this.radial()
|
|
179
|
+
const direction = (this.radial() || this.unrooted())
|
|
169
180
|
? (d.text_align == "end" ? -1 : 1)
|
|
170
181
|
: (isRTL ? -1 : 1);
|
|
171
182
|
return direction * this.shown_font_size * 0.33;
|
package/src/render/options.js
CHANGED
|
@@ -36,6 +36,12 @@ export function radial(attr) {
|
|
|
36
36
|
return this;
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
+
export function unrooted(attr) {
|
|
40
|
+
if (!arguments.length) return this.options["is-unrooted"];
|
|
41
|
+
this.options["is-unrooted"] = attr;
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
|
|
39
45
|
export function alignTips(attr) {
|
|
40
46
|
if (!arguments.length) return this.options["align-tips"];
|
|
41
47
|
this.options["align-tips"] = attr;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { xCoord, yCoord } from "./coordinates";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Count the number of leaf nodes in a subtree.
|
|
5
|
+
* @param {Object} node - A D3 hierarchy node
|
|
6
|
+
* @returns {Number} The number of leaves
|
|
7
|
+
*/
|
|
8
|
+
function countLeaves(node) {
|
|
9
|
+
if (!node.children || node.children.length === 0) {
|
|
10
|
+
return 1;
|
|
11
|
+
}
|
|
12
|
+
return node.children.reduce((sum, child) => sum + countLeaves(child), 0);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Compute unrooted (equal-angle) layout coordinates for all nodes.
|
|
17
|
+
*
|
|
18
|
+
* The algorithm allocates 2π of angular space at the root, then recursively
|
|
19
|
+
* assigns each child subtree angular space proportional to its leaf count.
|
|
20
|
+
* Each node is placed at parent_position + branch_length * (cos(angle), sin(angle)).
|
|
21
|
+
*
|
|
22
|
+
* After computing raw positions, coordinates are normalized to fit within
|
|
23
|
+
* the given width × height.
|
|
24
|
+
*
|
|
25
|
+
* @param {Object} root - The root node of the D3 hierarchy
|
|
26
|
+
* @param {Array} scales - [scale_x, scale_y] from placenodes
|
|
27
|
+
* @param {Number} width - Available width in pixels
|
|
28
|
+
* @param {Number} height - Available height in pixels
|
|
29
|
+
* @param {Number} labelPad - Pixel padding to reserve for labels on all sides
|
|
30
|
+
*/
|
|
31
|
+
export function computeUnrootedLayout(root, scales, width, height, labelPad) {
|
|
32
|
+
labelPad = labelPad || 0;
|
|
33
|
+
|
|
34
|
+
// Assign raw (x, y) using equal-angle algorithm
|
|
35
|
+
// Root is at the origin
|
|
36
|
+
root._unrooted_x = 0;
|
|
37
|
+
root._unrooted_y = 0;
|
|
38
|
+
|
|
39
|
+
equalAngle(root, 0, 2 * Math.PI);
|
|
40
|
+
|
|
41
|
+
// Find bounding box of all raw positions
|
|
42
|
+
let minX = Infinity, maxX = -Infinity;
|
|
43
|
+
let minY = Infinity, maxY = -Infinity;
|
|
44
|
+
|
|
45
|
+
root.each(d => {
|
|
46
|
+
if (d._unrooted_x < minX) minX = d._unrooted_x;
|
|
47
|
+
if (d._unrooted_x > maxX) maxX = d._unrooted_x;
|
|
48
|
+
if (d._unrooted_y < minY) minY = d._unrooted_y;
|
|
49
|
+
if (d._unrooted_y > maxY) maxY = d._unrooted_y;
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
let rawWidth = maxX - minX || 1;
|
|
53
|
+
let rawHeight = maxY - minY || 1;
|
|
54
|
+
|
|
55
|
+
// Reserve pixel-based padding for labels on all sides
|
|
56
|
+
let drawWidth = width - 2 * labelPad;
|
|
57
|
+
let drawHeight = height - 2 * labelPad;
|
|
58
|
+
if (drawWidth < 100) drawWidth = 100;
|
|
59
|
+
if (drawHeight < 100) drawHeight = 100;
|
|
60
|
+
|
|
61
|
+
let scaleX = drawWidth / rawWidth;
|
|
62
|
+
let scaleY = drawHeight / rawHeight;
|
|
63
|
+
let scale = Math.min(scaleX, scaleY);
|
|
64
|
+
|
|
65
|
+
let offsetX = labelPad + drawWidth / 2 - (minX + maxX) / 2 * scale;
|
|
66
|
+
let offsetY = labelPad + drawHeight / 2 - (minY + maxY) / 2 * scale;
|
|
67
|
+
|
|
68
|
+
// Map to screen coordinates
|
|
69
|
+
// Note: the coordinate system uses d.y for screen-x and d.x for screen-y
|
|
70
|
+
// (xCoord returns d.y, yCoord returns d.x)
|
|
71
|
+
root.each(d => {
|
|
72
|
+
d.y = d._unrooted_x * scale + offsetX;
|
|
73
|
+
d.x = d._unrooted_y * scale + offsetY;
|
|
74
|
+
|
|
75
|
+
// Compute text angle for labels (angle from parent to this node in degrees)
|
|
76
|
+
if (d.parent) {
|
|
77
|
+
let dx = d._unrooted_x - d.parent._unrooted_x;
|
|
78
|
+
let dy = d._unrooted_y - d.parent._unrooted_y;
|
|
79
|
+
let angleDeg = Math.atan2(dy, dx) * 180 / Math.PI;
|
|
80
|
+
|
|
81
|
+
// Flip text on the left hemisphere so it reads left-to-right
|
|
82
|
+
let isLeftHalf = angleDeg > 90 || angleDeg < -90;
|
|
83
|
+
d.text_angle = isLeftHalf ? angleDeg + 180 : angleDeg;
|
|
84
|
+
d.text_align = isLeftHalf ? "end" : "start";
|
|
85
|
+
} else {
|
|
86
|
+
d.text_angle = 0;
|
|
87
|
+
d.text_align = "start";
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Store the angle (in radians) for compatibility with radial code paths
|
|
91
|
+
d.angle = d.parent
|
|
92
|
+
? Math.atan2(d._unrooted_y - d.parent._unrooted_y,
|
|
93
|
+
d._unrooted_x - d.parent._unrooted_x)
|
|
94
|
+
: 0;
|
|
95
|
+
d.radius = null;
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Recursive equal-angle layout.
|
|
101
|
+
*
|
|
102
|
+
* @param {Object} node - Current node
|
|
103
|
+
* @param {Number} startAngle - Start of the angular arc allocated to this node
|
|
104
|
+
* @param {Number} arcSize - Total angular arc allocated to this node
|
|
105
|
+
*/
|
|
106
|
+
function equalAngle(node, startAngle, arcSize) {
|
|
107
|
+
let totalLeaves = countLeaves(node);
|
|
108
|
+
|
|
109
|
+
if (!node.children || node.children.length === 0) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
let currentAngle = startAngle;
|
|
114
|
+
|
|
115
|
+
node.children.forEach(child => {
|
|
116
|
+
let childLeaves = countLeaves(child);
|
|
117
|
+
let childArc = arcSize * (childLeaves / totalLeaves);
|
|
118
|
+
let midAngle = currentAngle + childArc / 2;
|
|
119
|
+
|
|
120
|
+
// Use the node's y value (cumulative branch length / depth) for distance
|
|
121
|
+
let branchLen = child.y !== undefined ? child.y - (node.y || 0) : 1;
|
|
122
|
+
if (branchLen <= 0) branchLen = 1;
|
|
123
|
+
|
|
124
|
+
child._unrooted_x = node._unrooted_x + branchLen * Math.cos(midAngle);
|
|
125
|
+
child._unrooted_y = node._unrooted_y + branchLen * Math.sin(midAngle);
|
|
126
|
+
|
|
127
|
+
equalAngle(child, currentAngle, childArc);
|
|
128
|
+
currentAngle += childArc;
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Draw a straight line edge between two nodes (for unrooted layout).
|
|
134
|
+
*
|
|
135
|
+
* @param {Array} points - Array of [source, target] node objects
|
|
136
|
+
* @returns {String} SVG path string
|
|
137
|
+
*/
|
|
138
|
+
export function drawUnrootedEdge(points) {
|
|
139
|
+
return (
|
|
140
|
+
"M " + xCoord(points[0]) + "," + yCoord(points[0]) +
|
|
141
|
+
" L " + xCoord(points[1]) + "," + yCoord(points[1])
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Place a label along an unrooted edge via linear interpolation.
|
|
147
|
+
*
|
|
148
|
+
* @param {Object} edge - Edge object with source and target
|
|
149
|
+
* @param {Number} where - Interpolation factor (0 = target, 1 = source)
|
|
150
|
+
* @returns {Object} {x, y} screen coordinates
|
|
151
|
+
*/
|
|
152
|
+
export function unrootedSegmentPlacer(edge, where) {
|
|
153
|
+
return {
|
|
154
|
+
x: xCoord(edge.target) + (xCoord(edge.source) - xCoord(edge.target)) * where,
|
|
155
|
+
y: yCoord(edge.target) + (yCoord(edge.source) - yCoord(edge.target)) * where
|
|
156
|
+
};
|
|
157
|
+
}
|