phylotree 2.1.6 → 2.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.
- package/README.md +18 -0
- package/dist/phylotree.js +833 -35
- package/dist/phylotree.min.js +1 -1
- package/package.json +4 -1
- package/src/branches.js +52 -3
- package/src/formats/newick.js +90 -0
- package/src/main.js +86 -3
- package/src/nodes.js +53 -0
- package/src/render/draw.js +62 -5
- package/src/render/edges.js +15 -3
- package/src/render/event-emitter.js +64 -0
- package/src/render/events.js +11 -1
- package/src/render/menus.js +82 -17
- package/src/render/nodes.js +12 -4
- package/src/render/selection-sets.js +259 -0
- package/src/render/styles/phylotree-menus.css +93 -0
- package/src/rooting.js +22 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "phylotree",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"main": "dist/phylotree.js",
|
|
5
5
|
"unpkg": "dist/phylotree.js",
|
|
6
6
|
"jsdelivr": "dist/phylotree.js",
|
|
@@ -58,6 +58,9 @@
|
|
|
58
58
|
"build": "rollup -c rollup.config.js",
|
|
59
59
|
"watch": "tape-watch -r source-map-support/register test/*-test.js",
|
|
60
60
|
"test": "tape -r source-map-support/register 'test/*-test.js'",
|
|
61
|
+
"docs:generate": "jsdoc2md 'src/**/*.js' > API.md",
|
|
62
|
+
"docs:validate": "jsdoc2md 'src/**/*.js' --no-cache --partial 'partials/**/*.hbs'",
|
|
63
|
+
"docs:watch": "jsdoc2md 'src/**/*.js' -w > API.md",
|
|
61
64
|
"postpublish": "zip -j dist/phylotree.js.zip -- LICENSE README.md dist/phylotree.js dist/phylotree.min.js dist/phylotree.css"
|
|
62
65
|
},
|
|
63
66
|
"files": [
|
package/src/branches.js
CHANGED
|
@@ -80,6 +80,23 @@ export function defBranchLengthAccessor(_node, new_length) {
|
|
|
80
80
|
*
|
|
81
81
|
* @param {Function} attr Empty if getting, or new branch length accessor if setting.
|
|
82
82
|
* @returns {Object} The branch length accessor if getting, or the current this if setting.
|
|
83
|
+
* @example
|
|
84
|
+
* // Set a custom branch length accessor
|
|
85
|
+
* const tree = new Phylotree(newick);
|
|
86
|
+
* tree.setBranchLength(function(node) {
|
|
87
|
+
* // Use a transformed branch length
|
|
88
|
+
* return Math.log(node.data.attribute + 1);
|
|
89
|
+
* });
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* // Set all branch lengths to 1 (equal branch lengths)
|
|
93
|
+
* tree.setBranchLength(function(node) {
|
|
94
|
+
* return 1;
|
|
95
|
+
* });
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* // Get the current branch length accessor
|
|
99
|
+
* const currentAccessor = tree.setBranchLength();
|
|
83
100
|
*/
|
|
84
101
|
export function setBranchLength(attr) {
|
|
85
102
|
if (!arguments.length) return this.branch_length_accessor;
|
|
@@ -88,7 +105,19 @@ export function setBranchLength(attr) {
|
|
|
88
105
|
}
|
|
89
106
|
|
|
90
107
|
/**
|
|
91
|
-
* Normalizes branch lengths
|
|
108
|
+
* Normalizes branch lengths to range [0,1]
|
|
109
|
+
* @returns {Phylotree} The current phylotree instance
|
|
110
|
+
* @example
|
|
111
|
+
* // Normalize all branch lengths to 0-1 range
|
|
112
|
+
* const tree = new Phylotree(newick);
|
|
113
|
+
* tree.normalizeBranchLengths();
|
|
114
|
+
* // Now all branch lengths are between 0 and 1
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* // Useful for comparing trees with different scales
|
|
118
|
+
* const tree1 = new Phylotree(newick1).normalizeBranchLengths();
|
|
119
|
+
* const tree2 = new Phylotree(newick2).normalizeBranchLengths();
|
|
120
|
+
* // Both trees now have comparable branch length scales
|
|
92
121
|
*/
|
|
93
122
|
export function normalize(attr) {
|
|
94
123
|
|
|
@@ -122,9 +151,29 @@ export function normalize(attr) {
|
|
|
122
151
|
|
|
123
152
|
|
|
124
153
|
/**
|
|
125
|
-
* Scales branch lengths
|
|
154
|
+
* Scales branch lengths using a custom transformation function
|
|
126
155
|
*
|
|
127
|
-
* @param {Function}
|
|
156
|
+
* @param {Function} scale_by Function that transforms each branch length
|
|
157
|
+
* @returns {Phylotree} The current phylotree instance
|
|
158
|
+
* @example
|
|
159
|
+
* // Scale all branch lengths by a constant factor
|
|
160
|
+
* const tree = new Phylotree(newick);
|
|
161
|
+
* tree.scaleBranchLengths(function(length) {
|
|
162
|
+
* return length * 2; // Double all branch lengths
|
|
163
|
+
* });
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* // Apply logarithmic transformation
|
|
167
|
+
* tree.scaleBranchLengths(function(length) {
|
|
168
|
+
* return Math.log10(length + 1);
|
|
169
|
+
* });
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* // Convert to time units (assuming substitutions per site)
|
|
173
|
+
* const mutationRate = 1e-8;
|
|
174
|
+
* tree.scaleBranchLengths(function(length) {
|
|
175
|
+
* return length / mutationRate; // Convert to years
|
|
176
|
+
* });
|
|
128
177
|
*/
|
|
129
178
|
export function scale(scale_by) {
|
|
130
179
|
|
package/src/formats/newick.js
CHANGED
|
@@ -11,6 +11,29 @@ import { isLeafNode } from "../nodes";
|
|
|
11
11
|
* @param {String} nwk_str - A string representing a phylogenetic tree in Newick format.
|
|
12
12
|
* @param {Object} bootstrap_values.
|
|
13
13
|
* @returns {Object} An object with keys ``json`` and ``error``.
|
|
14
|
+
* @example
|
|
15
|
+
* // Parse a simple Newick tree with branch lengths
|
|
16
|
+
* const newick = "((A:0.1,B:0.2):0.05,C:0.3);";
|
|
17
|
+
* const result = newickParser(newick);
|
|
18
|
+
* if (result.error) {
|
|
19
|
+
* console.error("Parse error:", result.error);
|
|
20
|
+
* } else {
|
|
21
|
+
* console.log("Parsed tree:", result.json);
|
|
22
|
+
* }
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* // Parse tree with tagged branches
|
|
26
|
+
* const taggedNewick = "((A:0.1,B{Foreground}:0.2){Foreground}:0.05,C:0.3);";
|
|
27
|
+
* const result = newickParser(taggedNewick);
|
|
28
|
+
* // Tagged branches will have annotation properties
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* // Parse tree with custom delimiters
|
|
32
|
+
* const nhxNewick = "((A:0.1,B:0.2[&&NHX:S=species1:D=Y]):0.05,C:0.3);";
|
|
33
|
+
* const result = newickParser(nhxNewick, {
|
|
34
|
+
* left_delimiter: '[',
|
|
35
|
+
* right_delimiter: ']'
|
|
36
|
+
* });
|
|
14
37
|
*/
|
|
15
38
|
|
|
16
39
|
function newickParser(nwk_str, options={}) {
|
|
@@ -228,6 +251,25 @@ function newickParser(nwk_str, options={}) {
|
|
|
228
251
|
* what label is written (optional).
|
|
229
252
|
* @param {Node} node - start at this node (default == root)
|
|
230
253
|
* @returns {String} newick - Phylogenetic tree serialized as a Newick string.
|
|
254
|
+
* @example
|
|
255
|
+
* // Export tree to basic Newick format
|
|
256
|
+
* const tree = new Phylotree(newick);
|
|
257
|
+
* const exportedNewick = tree.getNewick();
|
|
258
|
+
* console.log(exportedNewick); // "((A:0.1,B:0.2):0.05,C:0.3);"
|
|
259
|
+
*
|
|
260
|
+
* @example
|
|
261
|
+
* // Export with custom node annotations
|
|
262
|
+
* const annotatedNewick = tree.getNewick(function(node) {
|
|
263
|
+
* if (node.data.selected) {
|
|
264
|
+
* return "{SELECTED}";
|
|
265
|
+
* }
|
|
266
|
+
* return "";
|
|
267
|
+
* });
|
|
268
|
+
*
|
|
269
|
+
* @example
|
|
270
|
+
* // Export a subtree starting from a specific node
|
|
271
|
+
* const nodeOfInterest = tree.getNodeByName("A");
|
|
272
|
+
* const subtreeNewick = tree.getNewick(null, nodeOfInterest);
|
|
231
273
|
*/
|
|
232
274
|
|
|
233
275
|
|
|
@@ -278,4 +320,52 @@ export function getNewick(annotator, root) {
|
|
|
278
320
|
return element_array.join("")+";";
|
|
279
321
|
}
|
|
280
322
|
|
|
323
|
+
/**
|
|
324
|
+
* Generate Newick string with selection tags.
|
|
325
|
+
*
|
|
326
|
+
* @param {Object} options - Configuration options.
|
|
327
|
+
* @param {string} [options.tag='Foreground'] - Tag name for selected nodes (single selection mode).
|
|
328
|
+
* @param {boolean} [options.multiSet=false] - Use multi-set mode tags (uses set names as tags).
|
|
329
|
+
* @returns {string} Tagged Newick string.
|
|
330
|
+
* @example
|
|
331
|
+
* // Basic tagged output (uses selection state)
|
|
332
|
+
* const tagged = tree.getTaggedNewick();
|
|
333
|
+
* // Returns: "((A{Foreground}:0.1,B:0.2):0.05,C{Foreground}:0.3);"
|
|
334
|
+
*
|
|
335
|
+
* @example
|
|
336
|
+
* // Custom tag name
|
|
337
|
+
* const tagged = tree.getTaggedNewick({ tag: 'TEST' });
|
|
338
|
+
* // Returns: "((A{TEST}:0.1,B:0.2):0.05,C{TEST}:0.3);"
|
|
339
|
+
*
|
|
340
|
+
* @example
|
|
341
|
+
* // Multi-set mode (uses set names as tags)
|
|
342
|
+
* const tagged = tree.getTaggedNewick({ multiSet: true });
|
|
343
|
+
* // Returns: "((A{TEST}:0.1,B{REFERENCE}):0.2,C{TEST}:0.3);"
|
|
344
|
+
*/
|
|
345
|
+
export function getTaggedNewick(options = {}) {
|
|
346
|
+
const tag = options.tag || 'Foreground';
|
|
347
|
+
const multiSet = options.multiSet || false;
|
|
348
|
+
|
|
349
|
+
// Get selection attribute name from display if available
|
|
350
|
+
const selectionAttr = this.display
|
|
351
|
+
? this.display.selection_attribute_name
|
|
352
|
+
: 'selected';
|
|
353
|
+
|
|
354
|
+
// Get selection sets from display if in multi-set mode
|
|
355
|
+
const selectionSets = this.display ? this.display._selectionSets : null;
|
|
356
|
+
|
|
357
|
+
const annotator = (node) => {
|
|
358
|
+
if (multiSet && node._selectionSet) {
|
|
359
|
+
// Multi-set mode: use set name as tag
|
|
360
|
+
return `{${node._selectionSet}}`;
|
|
361
|
+
} else if (node[selectionAttr]) {
|
|
362
|
+
// Single selection mode: use configured tag
|
|
363
|
+
return `{${tag}}`;
|
|
364
|
+
}
|
|
365
|
+
return '';
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
return this.getNewick(annotator);
|
|
369
|
+
}
|
|
370
|
+
|
|
281
371
|
export default newickParser;
|
package/src/main.js
CHANGED
|
@@ -2,7 +2,7 @@ import * as d3 from "d3";
|
|
|
2
2
|
import * as _ from "underscore";
|
|
3
3
|
|
|
4
4
|
import { default as parser_registry } from "./formats/registry";
|
|
5
|
-
import { default as newickParser, getNewick } from "./formats/newick";
|
|
5
|
+
import { default as newickParser, getNewick, getTaggedNewick } from "./formats/newick";
|
|
6
6
|
import { default as getTipLengths } from "./export";
|
|
7
7
|
import * as nexus from "./formats/nexus";
|
|
8
8
|
import { default as phyloxml_parser } from "./formats/phyloxml";
|
|
@@ -85,6 +85,28 @@ function mrca(mrca_nodes) {
|
|
|
85
85
|
* - boostrap_values
|
|
86
86
|
* - type - format type
|
|
87
87
|
* @returns {Phylotree} phylotree - itself, following the builder pattern.
|
|
88
|
+
* @example
|
|
89
|
+
* // Create a simple phylotree from a Newick string
|
|
90
|
+
* const newick = "((A:0.1,B:0.2):0.05,C:0.3)";
|
|
91
|
+
* const tree = new Phylotree(newick);
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* // Create a phylotree with options
|
|
95
|
+
* const tree = new Phylotree(newick, {
|
|
96
|
+
* bootstrap_values: true,
|
|
97
|
+
* type: "newick"
|
|
98
|
+
* });
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* // Create from hierarchical JSON
|
|
102
|
+
* const jsonTree = {
|
|
103
|
+
* name: "root",
|
|
104
|
+
* children: [
|
|
105
|
+
* { name: "A", length: 0.1 },
|
|
106
|
+
* { name: "B", length: 0.2 }
|
|
107
|
+
* ]
|
|
108
|
+
* };
|
|
109
|
+
* const tree = new Phylotree(jsonTree);
|
|
88
110
|
*/
|
|
89
111
|
let Phylotree = class {
|
|
90
112
|
constructor(nwk, options = {}) {
|
|
@@ -214,7 +236,7 @@ let Phylotree = class {
|
|
|
214
236
|
return JSON.stringify(node_array);
|
|
215
237
|
}
|
|
216
238
|
|
|
217
|
-
|
|
239
|
+
/**
|
|
218
240
|
* Traverse the tree in a prescribed order, and compute a value at each node.
|
|
219
241
|
*
|
|
220
242
|
* @param {Function} callback A function to be called on each node.
|
|
@@ -223,6 +245,31 @@ let Phylotree = class {
|
|
|
223
245
|
* @param {Function} backtrack ; if provided, then at each node n, backtrack (n) will be called,
|
|
224
246
|
and if it returns TRUE, traversal will NOT continue past into this
|
|
225
247
|
node and its children
|
|
248
|
+
* @example
|
|
249
|
+
* // Count all nodes in the tree
|
|
250
|
+
* let nodeCount = 0;
|
|
251
|
+
* tree.traverse_and_compute(function(node) {
|
|
252
|
+
* nodeCount++;
|
|
253
|
+
* });
|
|
254
|
+
* console.log(`Tree has ${nodeCount} nodes`);
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* // Find maximum depth in post-order traversal
|
|
258
|
+
* tree.traverse_and_compute(function(node) {
|
|
259
|
+
* if (!node.children) {
|
|
260
|
+
* node.depth = 0;
|
|
261
|
+
* } else {
|
|
262
|
+
* node.depth = Math.max(...node.children.map(c => c.depth)) + 1;
|
|
263
|
+
* }
|
|
264
|
+
* }, "post-order");
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* // Add custom attributes to leaf nodes only
|
|
268
|
+
* tree.traverse_and_compute(function(node) {
|
|
269
|
+
* if (!node.children) {
|
|
270
|
+
* node.data.isLeaf = true;
|
|
271
|
+
* }
|
|
272
|
+
* });
|
|
226
273
|
*/
|
|
227
274
|
traverse_and_compute(callback, traversal_type, root_node, backtrack) {
|
|
228
275
|
traversal_type = traversal_type || "post-order";
|
|
@@ -267,7 +314,42 @@ let Phylotree = class {
|
|
|
267
314
|
this.nodes = json;
|
|
268
315
|
}
|
|
269
316
|
|
|
270
|
-
|
|
317
|
+
/**
|
|
318
|
+
* Render the phylotree to a DOM element. Warning: Requires DOM!
|
|
319
|
+
*
|
|
320
|
+
* @param {Object} options - Rendering options including container, dimensions, and styling
|
|
321
|
+
* @returns {TreeRender} The display object for further customization
|
|
322
|
+
* @example
|
|
323
|
+
* // Basic rendering to a div element
|
|
324
|
+
* const tree = new Phylotree(newick);
|
|
325
|
+
* tree.render({
|
|
326
|
+
* container: "#tree-container",
|
|
327
|
+
* width: 800,
|
|
328
|
+
* height: 600
|
|
329
|
+
* });
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* // Render with custom styling options
|
|
333
|
+
* const display = tree.render({
|
|
334
|
+
* container: d3.select("#my-tree"),
|
|
335
|
+
* width: 1000,
|
|
336
|
+
* height: 800,
|
|
337
|
+
* 'left-right-spacing': 'fit-to-size',
|
|
338
|
+
* 'top-bottom-spacing': 'fit-to-size',
|
|
339
|
+
* 'show-scale': true,
|
|
340
|
+
* selectable: true,
|
|
341
|
+
* collapsible: true
|
|
342
|
+
* });
|
|
343
|
+
*
|
|
344
|
+
* @example
|
|
345
|
+
* // Render radial tree layout
|
|
346
|
+
* tree.render({
|
|
347
|
+
* container: "#radial-tree",
|
|
348
|
+
* layout: "radial",
|
|
349
|
+
* width: 600,
|
|
350
|
+
* height: 600
|
|
351
|
+
* });
|
|
352
|
+
*/
|
|
271
353
|
render(options) {
|
|
272
354
|
this.display = new TreeRender(this, options);
|
|
273
355
|
return this.display;
|
|
@@ -283,6 +365,7 @@ Phylotree.prototype.branchName = branchName;
|
|
|
283
365
|
Phylotree.prototype.normalizeBranchLengths = normalize;
|
|
284
366
|
Phylotree.prototype.scaleBranchLengths = scale;
|
|
285
367
|
Phylotree.prototype.getNewick = getNewick;
|
|
368
|
+
Phylotree.prototype.getTaggedNewick = getTaggedNewick;
|
|
286
369
|
Phylotree.prototype.resortChildren = resortChildren;
|
|
287
370
|
Phylotree.prototype.setBranchLength = setBranchLength;
|
|
288
371
|
Phylotree.prototype.maxParsimony = maxParsimony;
|
package/src/nodes.js
CHANGED
|
@@ -124,6 +124,16 @@ export function deleteANode(index) {
|
|
|
124
124
|
/**
|
|
125
125
|
* Get the tips of the tree
|
|
126
126
|
* @returns {Array} Nodes in the current ``phylotree``.
|
|
127
|
+
* @example
|
|
128
|
+
* // Get all leaf nodes from a tree
|
|
129
|
+
* const tree = new Phylotree("((A:0.1,B:0.2):0.05,C:0.3);");
|
|
130
|
+
* const tips = tree.getTips();
|
|
131
|
+
* console.log(tips.map(tip => tip.data.name)); // ["A", "B", "C"]
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* // Count the number of taxa in the tree
|
|
135
|
+
* const numTaxa = tree.getTips().length;
|
|
136
|
+
* console.log(`Tree has ${numTaxa} taxa`);
|
|
127
137
|
*/
|
|
128
138
|
export function getTips() {
|
|
129
139
|
// get all nodes that have no nodes
|
|
@@ -166,6 +176,18 @@ export function getNodes() {
|
|
|
166
176
|
*
|
|
167
177
|
* @param {String} name Name of the desired node.
|
|
168
178
|
* @returns {Node} Desired node.
|
|
179
|
+
* @example
|
|
180
|
+
* // Find a specific node by name
|
|
181
|
+
* const tree = new Phylotree("((A:0.1,B:0.2):0.05,C:0.3);");
|
|
182
|
+
* const nodeA = tree.getNodeByName("A");
|
|
183
|
+
* console.log(nodeA.data.name); // "A"
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* // Check if a node exists and get its branch length
|
|
187
|
+
* const nodeB = tree.getNodeByName("B");
|
|
188
|
+
* if (nodeB) {
|
|
189
|
+
* console.log(`Node B has branch length: ${nodeB.data.attribute}`);
|
|
190
|
+
* }
|
|
169
191
|
*/
|
|
170
192
|
export function getNodeByName(name) {
|
|
171
193
|
return _.filter(this.nodes.descendants(), d => {
|
|
@@ -179,6 +201,21 @@ export function getNodeByName(name) {
|
|
|
179
201
|
*
|
|
180
202
|
* @param {Object} attributes An object whose keys are the names of nodes
|
|
181
203
|
* to modify, and whose values are the new attributes to add.
|
|
204
|
+
* @example
|
|
205
|
+
* // Add species information to nodes
|
|
206
|
+
* const tree = new Phylotree(newick);
|
|
207
|
+
* tree.assignAttributes({
|
|
208
|
+
* "Human": { species: "Homo sapiens", host: "human" },
|
|
209
|
+
* "Chimp": { species: "Pan troglodytes", host: "chimpanzee" }
|
|
210
|
+
* });
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* // Add selection categories for visualization
|
|
214
|
+
* tree.assignAttributes({
|
|
215
|
+
* "foreground_1": { category: "foreground", color: "red" },
|
|
216
|
+
* "foreground_2": { category: "foreground", color: "red" },
|
|
217
|
+
* "background_1": { category: "background", color: "blue" }
|
|
218
|
+
* });
|
|
182
219
|
*/
|
|
183
220
|
export function assignAttributes(attributes) {
|
|
184
221
|
//return nodes;
|
|
@@ -246,6 +283,22 @@ export function clearInternalNodes(respect) {
|
|
|
246
283
|
* @param {Boolean} terminal Whether to include terminal nodes.
|
|
247
284
|
* @param {Boolean} internal Whther to include internal nodes.
|
|
248
285
|
* @returns {Array} An array of selected nodes.
|
|
286
|
+
* @example
|
|
287
|
+
* // Select all descendants of an internal node
|
|
288
|
+
* const tree = new Phylotree("(((A,B)AB,(C,D)CD)ABCD,E);");
|
|
289
|
+
* const internalNode = tree.getNodeByName("ABCD");
|
|
290
|
+
* const allDescendants = tree.selectAllDescendants(internalNode, true, true);
|
|
291
|
+
* console.log(allDescendants.map(n => n.data.name)); // ["AB", "A", "B", "CD", "C", "D"]
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* // Select only terminal descendants (tips in a clade)
|
|
295
|
+
* const cladeTips = tree.selectAllDescendants(internalNode, true, false);
|
|
296
|
+
* console.log(cladeTips.map(n => n.data.name)); // ["A", "B", "C", "D"]
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* // Select only internal descendants
|
|
300
|
+
* const cladeInternals = tree.selectAllDescendants(internalNode, false, true);
|
|
301
|
+
* console.log(cladeInternals.map(n => n.data.name)); // ["AB", "CD"]
|
|
249
302
|
*/
|
|
250
303
|
export function selectAllDescendants(node, terminal, internal) {
|
|
251
304
|
|
package/src/render/draw.js
CHANGED
|
@@ -12,6 +12,8 @@ import * as events from "./events";
|
|
|
12
12
|
import { css_classes, initializeCssClasses } from "./options";
|
|
13
13
|
import * as opt from "./options";
|
|
14
14
|
import * as menus from "./menus";
|
|
15
|
+
import * as eventEmitter from "./event-emitter";
|
|
16
|
+
import * as selectionSets from "./selection-sets";
|
|
15
17
|
|
|
16
18
|
// replacement for d3.functor
|
|
17
19
|
function constant(x) {
|
|
@@ -33,6 +35,7 @@ class TreeRender {
|
|
|
33
35
|
this._nodeLabel = this.defNodeLabel;
|
|
34
36
|
this.svg = null;
|
|
35
37
|
this._selectionCallback = null;
|
|
38
|
+
this._eventListeners = {};
|
|
36
39
|
this.scales = [1, 1];
|
|
37
40
|
this.size = [1, 1];
|
|
38
41
|
this.fixed_width = [14, 30];
|
|
@@ -101,7 +104,13 @@ class TreeRender {
|
|
|
101
104
|
"show-labels": true,
|
|
102
105
|
"node-styler": null,
|
|
103
106
|
"edge-styler": null,
|
|
104
|
-
"node-span": null
|
|
107
|
+
"node-span": null,
|
|
108
|
+
"responsive": false,
|
|
109
|
+
"preserve-aspect-ratio": "xMidYMid meet",
|
|
110
|
+
"selection-mode": "single",
|
|
111
|
+
"selection-sets": [],
|
|
112
|
+
"initial-selection": [],
|
|
113
|
+
"initial-sets": {}
|
|
105
114
|
};
|
|
106
115
|
|
|
107
116
|
this.ensure_size_is_in_px = function(value) {
|
|
@@ -143,6 +152,37 @@ class TreeRender {
|
|
|
143
152
|
this.initializeEdgeLabels();
|
|
144
153
|
this.update();
|
|
145
154
|
events.d3PhylotreeAddEventListener();
|
|
155
|
+
|
|
156
|
+
// Initialize multi-set selection if configured
|
|
157
|
+
if (this.options["selection-mode"] === "multi-set" &&
|
|
158
|
+
this.options["selection-sets"].length > 0) {
|
|
159
|
+
this.initializeSelectionSets(this.options["selection-sets"]);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Apply initial selection if configured
|
|
163
|
+
this.applyInitialSelection();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Apply initial selection from render options.
|
|
168
|
+
* @private
|
|
169
|
+
*/
|
|
170
|
+
applyInitialSelection() {
|
|
171
|
+
// Single selection mode
|
|
172
|
+
if (this.options["initial-selection"] &&
|
|
173
|
+
this.options["initial-selection"].length > 0) {
|
|
174
|
+
this.selectNodes(this.options["initial-selection"]);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Multi-set mode
|
|
178
|
+
if (this.options["selection-mode"] === "multi-set" &&
|
|
179
|
+
this.options["initial-sets"]) {
|
|
180
|
+
Object.entries(this.options["initial-sets"]).forEach(([setName, nodeNames]) => {
|
|
181
|
+
nodeNames.forEach(nodeName => {
|
|
182
|
+
this.addToSet(nodeName, setName);
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
}
|
|
146
186
|
}
|
|
147
187
|
|
|
148
188
|
pad_height() {
|
|
@@ -210,10 +250,22 @@ class TreeRender {
|
|
|
210
250
|
.select("svg")
|
|
211
251
|
.remove();
|
|
212
252
|
|
|
213
|
-
this.svg = d3
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
253
|
+
this.svg = d3.create("svg");
|
|
254
|
+
|
|
255
|
+
if (this.options["responsive"]) {
|
|
256
|
+
// Responsive mode: use viewBox for scaling
|
|
257
|
+
this.svg
|
|
258
|
+
.attr("viewBox", `0 0 ${this.width} ${this.height}`)
|
|
259
|
+
.attr("preserveAspectRatio", this.options["preserve-aspect-ratio"])
|
|
260
|
+
.style("width", "100%")
|
|
261
|
+
.style("height", "auto")
|
|
262
|
+
.style("max-width", "100%");
|
|
263
|
+
} else {
|
|
264
|
+
// Fixed mode: explicit width/height
|
|
265
|
+
this.svg
|
|
266
|
+
.attr("width", this.width)
|
|
267
|
+
.attr("height", this.height);
|
|
268
|
+
}
|
|
217
269
|
|
|
218
270
|
this.set_size([this.height, this.width]);
|
|
219
271
|
|
|
@@ -452,6 +504,9 @@ class TreeRender {
|
|
|
452
504
|
this.svg.call(zoom);
|
|
453
505
|
}
|
|
454
506
|
|
|
507
|
+
// Emit rendered event
|
|
508
|
+
this.emit('rendered');
|
|
509
|
+
|
|
455
510
|
return this;
|
|
456
511
|
}
|
|
457
512
|
|
|
@@ -1364,5 +1419,7 @@ _.extend(TreeRender.prototype, render_edges);
|
|
|
1364
1419
|
_.extend(TreeRender.prototype, events);
|
|
1365
1420
|
_.extend(TreeRender.prototype, menus);
|
|
1366
1421
|
_.extend(TreeRender.prototype, opt);
|
|
1422
|
+
_.extend(TreeRender.prototype, eventEmitter);
|
|
1423
|
+
_.extend(TreeRender.prototype, selectionSets);
|
|
1367
1424
|
|
|
1368
1425
|
export default TreeRender;
|
package/src/render/edges.js
CHANGED
|
@@ -10,9 +10,15 @@ export function drawEdge(container, edge, transition) {
|
|
|
10
10
|
.attr("class", d => {
|
|
11
11
|
return this.reclassEdge(d);
|
|
12
12
|
})
|
|
13
|
-
.on("click", d => {
|
|
14
|
-
this.
|
|
15
|
-
|
|
13
|
+
.on("click", (event, d) => {
|
|
14
|
+
this.emit('branchClick', edge, event);
|
|
15
|
+
// Check for multi-set selection mode
|
|
16
|
+
if (this.options["selection-mode"] === "multi-set" && this._activeSetName) {
|
|
17
|
+
this.handleMultiSetClick(edge.target);
|
|
18
|
+
} else {
|
|
19
|
+
this.modifySelection([edge.target], this.selection_attribute_name);
|
|
20
|
+
this.update();
|
|
21
|
+
}
|
|
16
22
|
});
|
|
17
23
|
|
|
18
24
|
let new_branch_path = this.draw_branch([edge.source, edge.target]);
|
|
@@ -66,6 +72,12 @@ export function reclassEdge(edge) {
|
|
|
66
72
|
class_var += " " + css_classes["selected-branch"];
|
|
67
73
|
}
|
|
68
74
|
|
|
75
|
+
// Multi-set selection mode: add set-specific class
|
|
76
|
+
if (edge.target && edge.target._selectionSet) {
|
|
77
|
+
const safeName = edge.target._selectionSet.replace(/[^a-zA-Z0-9-_]/g, '_');
|
|
78
|
+
class_var += " phylotree-set-branch-" + safeName;
|
|
79
|
+
}
|
|
80
|
+
|
|
69
81
|
return class_var;
|
|
70
82
|
|
|
71
83
|
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event emitter mixin for phylotree TreeRender
|
|
3
|
+
* Provides on/off/emit pattern for tree events
|
|
4
|
+
* @module render/event-emitter
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Register an event listener
|
|
9
|
+
* @param {string} eventName - Event name (nodeClick, branchClick, nodeHover,
|
|
10
|
+
* contextMenu, rendered, selectionChange, collapsed, rerooted)
|
|
11
|
+
* @param {Function} callback - Handler function
|
|
12
|
+
* @returns {this} For chaining
|
|
13
|
+
* @example
|
|
14
|
+
* tree.on('nodeClick', (node, event) => console.log(node.data.name));
|
|
15
|
+
* tree.on('selectionChange', (selectedNodes) => updatePanel(selectedNodes));
|
|
16
|
+
*/
|
|
17
|
+
export function on(eventName, callback) {
|
|
18
|
+
if (!this._eventListeners) {
|
|
19
|
+
this._eventListeners = {};
|
|
20
|
+
}
|
|
21
|
+
if (!this._eventListeners[eventName]) {
|
|
22
|
+
this._eventListeners[eventName] = [];
|
|
23
|
+
}
|
|
24
|
+
this._eventListeners[eventName].push(callback);
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Remove an event listener
|
|
30
|
+
* @param {string} eventName - Event name
|
|
31
|
+
* @param {Function} callback - Handler to remove (must be same reference)
|
|
32
|
+
* @returns {this} For chaining
|
|
33
|
+
* @example
|
|
34
|
+
* const handler = (node) => console.log(node);
|
|
35
|
+
* tree.on('nodeClick', handler);
|
|
36
|
+
* tree.off('nodeClick', handler);
|
|
37
|
+
*/
|
|
38
|
+
export function off(eventName, callback) {
|
|
39
|
+
if (this._eventListeners && this._eventListeners[eventName]) {
|
|
40
|
+
this._eventListeners[eventName] = this._eventListeners[eventName]
|
|
41
|
+
.filter(cb => cb !== callback);
|
|
42
|
+
}
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Emit an event to all registered listeners
|
|
48
|
+
* @param {string} eventName - Event name
|
|
49
|
+
* @param {...*} args - Arguments to pass to listeners
|
|
50
|
+
* @returns {this} For chaining
|
|
51
|
+
* @private
|
|
52
|
+
*/
|
|
53
|
+
export function emit(eventName, ...args) {
|
|
54
|
+
if (this._eventListeners && this._eventListeners[eventName]) {
|
|
55
|
+
this._eventListeners[eventName].forEach(callback => {
|
|
56
|
+
try {
|
|
57
|
+
callback.apply(this, args);
|
|
58
|
+
} catch (e) {
|
|
59
|
+
console.error(`Error in ${eventName} event handler:`, e);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return this;
|
|
64
|
+
}
|
package/src/render/events.js
CHANGED
|
@@ -30,6 +30,10 @@ export function toggleCollapse(node) {
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
this.placenodes();
|
|
33
|
+
|
|
34
|
+
// Emit collapsed event with node and current collapsed state
|
|
35
|
+
this.emit('collapsed', node, node.collapsed);
|
|
36
|
+
|
|
33
37
|
return this;
|
|
34
38
|
}
|
|
35
39
|
|
|
@@ -82,7 +86,13 @@ export function resizeSvg(tree, svg, tr) {
|
|
|
82
86
|
svg = svg.transition(100);
|
|
83
87
|
}
|
|
84
88
|
|
|
85
|
-
|
|
89
|
+
if (this.options["responsive"]) {
|
|
90
|
+
// Responsive mode: update viewBox
|
|
91
|
+
svg.attr("viewBox", `0 0 ${sizes[1]} ${sizes[0]}`);
|
|
92
|
+
} else {
|
|
93
|
+
// Fixed mode: update width/height
|
|
94
|
+
svg.attr("height", sizes[0]).attr("width", sizes[1]);
|
|
95
|
+
}
|
|
86
96
|
|
|
87
97
|
}
|
|
88
98
|
|