phylotree 2.5.0 → 2.6.1

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.
@@ -106,9 +106,9 @@ export function getSetMembers(setName) {
106
106
  return [];
107
107
  }
108
108
 
109
- const memberNames = this._selectionSets[setName].members;
109
+ const members = this._selectionSets[setName].members;
110
110
  return this.phylotree.nodes.descendants()
111
- .filter(node => memberNames.has(node.data.name));
111
+ .filter(node => members.has(node));
112
112
  }
113
113
 
114
114
  /**
@@ -127,7 +127,8 @@ export function addToSet(node, setName) {
127
127
  return this;
128
128
  }
129
129
 
130
- const nodeName = typeof node === "string" ? node : node.data.name;
130
+ // Membership is keyed by node identity; names are resolved here so that
131
+ // unnamed or duplicate-named nodes do not share membership.
131
132
  const nodeObj = typeof node === "string"
132
133
  ? this.phylotree.getNodeByName(node)
133
134
  : node;
@@ -135,10 +136,10 @@ export function addToSet(node, setName) {
135
136
  if (nodeObj) {
136
137
  // Remove from all other sets (mutually exclusive)
137
138
  Object.keys(this._selectionSets).forEach(otherSet => {
138
- this._selectionSets[otherSet].members.delete(nodeName);
139
+ this._selectionSets[otherSet].members.delete(nodeObj);
139
140
  });
140
141
 
141
- this._selectionSets[setName].members.add(nodeName);
142
+ this._selectionSets[setName].members.add(nodeObj);
142
143
  nodeObj._selectionSet = setName;
143
144
 
144
145
  this.emit("setChange", {
@@ -169,16 +170,13 @@ export function removeFromSet(node, setName) {
169
170
  return this;
170
171
  }
171
172
 
172
- const nodeName = typeof node === "string" ? node : node.data.name;
173
173
  const nodeObj = typeof node === "string"
174
174
  ? this.phylotree.getNodeByName(node)
175
175
  : node;
176
176
 
177
- if (this._selectionSets[setName].members.has(nodeName)) {
178
- this._selectionSets[setName].members.delete(nodeName);
179
- if (nodeObj) {
180
- delete nodeObj._selectionSet;
181
- }
177
+ if (nodeObj && this._selectionSets[setName].members.has(nodeObj)) {
178
+ this._selectionSets[setName].members.delete(nodeObj);
179
+ delete nodeObj._selectionSet;
182
180
 
183
181
  this.emit("setChange", {
184
182
  action: "remove",
@@ -204,7 +202,8 @@ export function _getSetsSummary() {
204
202
  const summary = {};
205
203
  if (this._selectionSets) {
206
204
  Object.keys(this._selectionSets).forEach(setName => {
207
- summary[setName] = Array.from(this._selectionSets[setName].members);
205
+ summary[setName] = Array.from(this._selectionSets[setName].members,
206
+ node => node.data.name);
208
207
  });
209
208
  }
210
209
  return summary;
@@ -222,7 +221,6 @@ export function handleMultiSetClick(node) {
222
221
  return;
223
222
  }
224
223
 
225
- const nodeName = node.data.name;
226
224
  const currentSet = node._selectionSet;
227
225
 
228
226
  if (currentSet === this._activeSetName) {
@@ -245,7 +243,7 @@ export function isInSet(node, setName) {
245
243
  if (!this._selectionSets || !this._selectionSets[setName]) {
246
244
  return false;
247
245
  }
248
- return this._selectionSets[setName].members.has(node.data.name);
246
+ return this._selectionSets[setName].members.has(node);
249
247
  }
250
248
 
251
249
  /**
@@ -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
+ }
package/src/rooting.js CHANGED
@@ -155,6 +155,9 @@ export function reroot(node, fraction) {
155
155
  // retain selection
156
156
  let selectionName = this.display.selection_attribute_name
157
157
 
158
+ // retain class names, including any set at runtime with css()
159
+ options["css-classes"] = Object.assign({}, this.display.css_classes);
160
+
158
161
  delete this.display;
159
162
 
160
163
  let rendered_tree = this.render(options);