phylotree 2.1.7 → 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.
@@ -17,7 +17,7 @@ export function nodeDropdownMenu(node, container, phylotree, options, event) {
17
17
  .select(container)
18
18
  .append("div")
19
19
  .attr("id", d3_layout_phylotree_context_menu_id)
20
- .attr("class", "dropdown-menu")
20
+ .attr("class", "phylotree-context-menu dropdown-menu")
21
21
  .attr("role", "menu");
22
22
  }
23
23
 
@@ -40,7 +40,7 @@ export function nodeDropdownMenu(node, container, phylotree, options, event) {
40
40
  if (options["collapsible"]) {
41
41
  menu_object
42
42
  .append("a")
43
- .attr("class", "dropdown-item")
43
+ .attr("class", "phylotree-menu-item dropdown-item")
44
44
  .attr("tabindex", "-1")
45
45
  .text(isNodeCollapsed(node) ? "Expand Subtree" : "Collapse Subtree")
46
46
  .on("click", d => {
@@ -48,10 +48,10 @@ export function nodeDropdownMenu(node, container, phylotree, options, event) {
48
48
  this.toggleCollapse(node).update();
49
49
  });
50
50
  if (options["selectable"]) {
51
- menu_object.append("div").attr("class", "dropdown-divider");
51
+ menu_object.append("div").attr("class", "phylotree-menu-divider dropdown-divider");
52
52
  menu_object
53
53
  .append("h6")
54
- .attr("class", "dropdown-header")
54
+ .attr("class", "phylotree-menu-header dropdown-header")
55
55
  .text("Toggle selection");
56
56
  }
57
57
  }
@@ -59,7 +59,7 @@ export function nodeDropdownMenu(node, container, phylotree, options, event) {
59
59
  if (options["selectable"]) {
60
60
  menu_object
61
61
  .append("a")
62
- .attr("class", "dropdown-item")
62
+ .attr("class", "phylotree-menu-item dropdown-item")
63
63
  .attr("tabindex", "-1")
64
64
  .text("All descendant branches")
65
65
  .on("click", function(d) {
@@ -71,7 +71,7 @@ export function nodeDropdownMenu(node, container, phylotree, options, event) {
71
71
 
72
72
  menu_object
73
73
  .append("a")
74
- .attr("class", "dropdown-item")
74
+ .attr("class", "phylotree-menu-item dropdown-item")
75
75
  .attr("tabindex", "-1")
76
76
  .text("All terminal branches")
77
77
  .on("click", function(d) {
@@ -83,7 +83,7 @@ export function nodeDropdownMenu(node, container, phylotree, options, event) {
83
83
 
84
84
  menu_object
85
85
  .append("a")
86
- .attr("class", "dropdown-item")
86
+ .attr("class", "phylotree-menu-item dropdown-item")
87
87
  .attr("tabindex", "-1")
88
88
  .text("All internal branches")
89
89
  .on("click", function(d) {
@@ -99,7 +99,7 @@ export function nodeDropdownMenu(node, container, phylotree, options, event) {
99
99
  if (options["selectable"]) {
100
100
  menu_object
101
101
  .append("a")
102
- .attr("class", "dropdown-item")
102
+ .attr("class", "phylotree-menu-item dropdown-item")
103
103
  .attr("tabindex", "-1")
104
104
  .text("Incident branch")
105
105
  .on("click", function(d) {
@@ -109,7 +109,7 @@ export function nodeDropdownMenu(node, container, phylotree, options, event) {
109
109
 
110
110
  menu_object
111
111
  .append("a")
112
- .attr("class", "dropdown-item")
112
+ .attr("class", "phylotree-menu-item dropdown-item")
113
113
  .attr("tabindex", "-1")
114
114
  .text("Path to root")
115
115
  .on("click", d => {
@@ -118,14 +118,14 @@ export function nodeDropdownMenu(node, container, phylotree, options, event) {
118
118
  });
119
119
 
120
120
  if (options["reroot"] || options["hide"]) {
121
- menu_object.append("div").attr("class", "dropdown-divider");
121
+ menu_object.append("div").attr("class", "phylotree-menu-divider dropdown-divider");
122
122
  }
123
123
  }
124
124
 
125
125
  if (options["reroot"]) {
126
126
  menu_object
127
127
  .append("a")
128
- .attr("class", "dropdown-item")
128
+ .attr("class", "phylotree-menu-item dropdown-item")
129
129
  .attr("tabindex", "-1")
130
130
  .text("Reroot on this node")
131
131
  .on("click", d => {
@@ -138,7 +138,7 @@ export function nodeDropdownMenu(node, container, phylotree, options, event) {
138
138
  if (options["hide"]) {
139
139
  menu_object
140
140
  .append("a")
141
- .attr("class", "dropdown-item")
141
+ .attr("class", "phylotree-menu-item dropdown-item")
142
142
  .attr("tabindex", "-1")
143
143
  .text("Hide this " + (isLeafNode(node) ? "node" : "subtree"))
144
144
  .on("click", d => {
@@ -153,7 +153,7 @@ export function nodeDropdownMenu(node, container, phylotree, options, event) {
153
153
  if (hasHiddenNodes(node)) {
154
154
  menu_object
155
155
  .append("a")
156
- .attr("class", "dropdown-item")
156
+ .attr("class", "phylotree-menu-item dropdown-item")
157
157
  .attr("tabindex", "-1")
158
158
  .text("Show all descendant nodes")
159
159
  .on("click", function(d) {
@@ -192,13 +192,13 @@ export function nodeDropdownMenu(node, container, phylotree, options, event) {
192
192
  ];
193
193
 
194
194
  if (_.some(show_divider_options)) {
195
- menu_object.append("div").attr("class", "dropdown-divider");
195
+ menu_object.append("div").attr("class", "phylotree-menu-divider dropdown-divider");
196
196
  }
197
197
 
198
198
  has_user_elements.forEach(function(d) {
199
199
  menu_object
200
200
  .append("a")
201
- .attr("class", "dropdown-item")
201
+ .attr("class", "phylotree-menu-item dropdown-item")
202
202
  .attr("tabindex", "-1")
203
203
  .text((d[0])(node)) // eslint-disable-line
204
204
  .on("click", _.partial(d[1], node));
@@ -388,8 +388,14 @@ export function modifySelection(
388
388
  }
389
389
  }
390
390
 
391
- if (this._selectionCallback && attr != "tag") {
392
- this._selectionCallback(this.getSelection());
391
+ if (attr != "tag") {
392
+ const selection = this.getSelection();
393
+ // Call legacy callback for backward compatibility
394
+ if (this._selectionCallback) {
395
+ this._selectionCallback(selection);
396
+ }
397
+ // Emit event for new event system
398
+ this.emit('selectionChange', selection);
393
399
  }
394
400
 
395
401
  this.refresh();
@@ -451,3 +457,62 @@ export function selectionCallback(callback) {
451
457
  this._selectionCallback = callback;
452
458
  return this;
453
459
  }
460
+
461
+ /**
462
+ * Select nodes by their names.
463
+ *
464
+ * @param {string[]} names - Array of node names to select.
465
+ * @returns {this} For chaining.
466
+ * @example
467
+ * tree.selectNodes(['HUMAN', 'CHIMP', 'GORILLA']);
468
+ */
469
+ export function selectNodes(names) {
470
+ if (!names || !Array.isArray(names) || names.length === 0) {
471
+ return this;
472
+ }
473
+
474
+ const nodesToSelect = this.phylotree.nodes.descendants()
475
+ .filter(node => names.includes(node.data.name));
476
+
477
+ if (nodesToSelect.length > 0) {
478
+ this.modifySelection(nodesToSelect, this.selection_attribute_name, false, false, 'true');
479
+ }
480
+
481
+ return this;
482
+ }
483
+
484
+ /**
485
+ * Deselect nodes by their names.
486
+ *
487
+ * @param {string[]} names - Array of node names to deselect.
488
+ * @returns {this} For chaining.
489
+ * @example
490
+ * tree.deselectNodes(['HUMAN']);
491
+ */
492
+ export function deselectNodes(names) {
493
+ if (!names || !Array.isArray(names) || names.length === 0) {
494
+ return this;
495
+ }
496
+
497
+ const nodesToDeselect = this.phylotree.nodes.descendants()
498
+ .filter(node => names.includes(node.data.name));
499
+
500
+ if (nodesToDeselect.length > 0) {
501
+ this.modifySelection(nodesToDeselect, this.selection_attribute_name, false, false, 'false');
502
+ }
503
+
504
+ return this;
505
+ }
506
+
507
+ /**
508
+ * Clear all current selection.
509
+ *
510
+ * @returns {this} For chaining.
511
+ * @example
512
+ * tree.clearSelection();
513
+ */
514
+ export function clearSelection() {
515
+ const allNodes = this.phylotree.nodes.descendants();
516
+ this.modifySelection(allNodes, this.selection_attribute_name, false, false, 'false');
517
+ return this;
518
+ }
@@ -41,8 +41,12 @@ export function drawNode(container, node, transitions) {
41
41
  .append("text")
42
42
  .classed(this.css_classes["node_text"], true)
43
43
  .merge(labels)
44
- .on("click", d=> {
45
- this.handle_node_click(node, d);
44
+ .on("click", (event, d) => {
45
+ this.emit('nodeClick', node, event);
46
+ this.handle_node_click(node, event);
47
+ })
48
+ .on("mouseenter", (event) => {
49
+ this.emit('nodeHover', node, event);
46
50
  })
47
51
  .attr("dy", d => {
48
52
  return this.shown_font_size * 0.33;
@@ -183,8 +187,12 @@ export function drawNode(container, node, transitions) {
183
187
  .attr("r", d => {
184
188
  return Math.min(this.shown_font_size * 0.75, radius);
185
189
  })
186
- .on("click", d => {
187
- this.handle_node_click(node, d);
190
+ .on("click", (event, d) => {
191
+ this.emit('nodeClick', node, event);
192
+ this.handle_node_click(node, event);
193
+ })
194
+ .on("mouseenter", (event) => {
195
+ this.emit('nodeHover', node, event);
188
196
  });
189
197
  } else {
190
198
  circles.remove();
@@ -0,0 +1,259 @@
1
+ /**
2
+ * Multi-set selection management for phylotree
3
+ * @module render/selection-sets
4
+ */
5
+
6
+ /**
7
+ * Initialize multi-set selection mode.
8
+ * Called automatically during render when selection-mode is 'multi-set'.
9
+ *
10
+ * @param {Array<{name: string, color: string}>} sets - Array of selection set definitions.
11
+ * @private
12
+ */
13
+ export function initializeSelectionSets(sets) {
14
+ this._selectionSets = {};
15
+ this._activeSetName = null;
16
+
17
+ sets.forEach(set => {
18
+ this._selectionSets[set.name] = {
19
+ name: set.name,
20
+ color: set.color,
21
+ members: new Set()
22
+ };
23
+ });
24
+
25
+ // Set first set as active by default
26
+ if (sets.length > 0) {
27
+ this._activeSetName = sets[0].name;
28
+ }
29
+
30
+ // Inject CSS for set colors
31
+ this._injectSetStyles(sets);
32
+ }
33
+
34
+ /**
35
+ * Inject dynamic CSS styles for selection set colors.
36
+ *
37
+ * @param {Array<{name: string, color: string}>} sets - Array of selection set definitions.
38
+ * @private
39
+ */
40
+ export function _injectSetStyles(sets) {
41
+ const styleId = 'phylotree-selection-set-styles';
42
+ let styleEl = document.getElementById(styleId);
43
+
44
+ if (!styleEl) {
45
+ styleEl = document.createElement('style');
46
+ styleEl.id = styleId;
47
+ document.head.appendChild(styleEl);
48
+ }
49
+
50
+ const css = sets.map(set => {
51
+ const safeName = set.name.replace(/[^a-zA-Z0-9-_]/g, '_');
52
+ return `
53
+ .phylotree-set-${safeName} path.branch {
54
+ stroke: ${set.color} !important;
55
+ }
56
+ .phylotree-set-${safeName} .node circle {
57
+ fill: ${set.color} !important;
58
+ }
59
+ .phylotree-set-branch-${safeName} {
60
+ stroke: ${set.color} !important;
61
+ }
62
+ `;
63
+ }).join('\n');
64
+
65
+ styleEl.textContent = css;
66
+ }
67
+
68
+ /**
69
+ * Set the active selection set. Clicks will add nodes to this set.
70
+ *
71
+ * @param {string} name - Name of the set to activate.
72
+ * @returns {this} For chaining.
73
+ * @example
74
+ * tree.setActiveSet('foreground');
75
+ */
76
+ export function setActiveSet(name) {
77
+ if (this._selectionSets && this._selectionSets[name]) {
78
+ this._activeSetName = name;
79
+ } else {
80
+ console.warn(`Selection set '${name}' not found`);
81
+ }
82
+ return this;
83
+ }
84
+
85
+ /**
86
+ * Get the currently active selection set name.
87
+ *
88
+ * @returns {string|null} Active set name or null if not in multi-set mode.
89
+ * @example
90
+ * const activeSet = tree.getActiveSet(); // 'foreground'
91
+ */
92
+ export function getActiveSet() {
93
+ return this._activeSetName;
94
+ }
95
+
96
+ /**
97
+ * Get all members (nodes) of a selection set.
98
+ *
99
+ * @param {string} setName - Name of the selection set.
100
+ * @returns {Node[]} Array of nodes in the set.
101
+ * @example
102
+ * const foregroundNodes = tree.getSetMembers('foreground');
103
+ */
104
+ export function getSetMembers(setName) {
105
+ if (!this._selectionSets || !this._selectionSets[setName]) {
106
+ return [];
107
+ }
108
+
109
+ const memberNames = this._selectionSets[setName].members;
110
+ return this.phylotree.nodes.descendants()
111
+ .filter(node => memberNames.has(node.data.name));
112
+ }
113
+
114
+ /**
115
+ * Add a node to a selection set.
116
+ *
117
+ * @param {Node|string} node - Node object or node name.
118
+ * @param {string} setName - Target set name.
119
+ * @returns {this} For chaining.
120
+ * @example
121
+ * tree.addToSet('HUMAN', 'foreground');
122
+ * tree.addToSet(nodeObject, 'background');
123
+ */
124
+ export function addToSet(node, setName) {
125
+ if (!this._selectionSets || !this._selectionSets[setName]) {
126
+ console.warn(`Selection set '${setName}' not found`);
127
+ return this;
128
+ }
129
+
130
+ const nodeName = typeof node === 'string' ? node : node.data.name;
131
+ const nodeObj = typeof node === 'string'
132
+ ? this.phylotree.getNodeByName(node)
133
+ : node;
134
+
135
+ if (nodeObj) {
136
+ // Remove from all other sets (mutually exclusive)
137
+ Object.keys(this._selectionSets).forEach(otherSet => {
138
+ this._selectionSets[otherSet].members.delete(nodeName);
139
+ });
140
+
141
+ this._selectionSets[setName].members.add(nodeName);
142
+ nodeObj._selectionSet = setName;
143
+
144
+ this.emit('setChange', {
145
+ action: 'add',
146
+ set: setName,
147
+ node: nodeObj,
148
+ allSets: this._getSetsSummary()
149
+ });
150
+
151
+ this.refresh();
152
+ this.update();
153
+ }
154
+
155
+ return this;
156
+ }
157
+
158
+ /**
159
+ * Remove a node from a selection set.
160
+ *
161
+ * @param {Node|string} node - Node object or node name.
162
+ * @param {string} setName - Target set name.
163
+ * @returns {this} For chaining.
164
+ * @example
165
+ * tree.removeFromSet('HUMAN', 'foreground');
166
+ */
167
+ export function removeFromSet(node, setName) {
168
+ if (!this._selectionSets || !this._selectionSets[setName]) {
169
+ return this;
170
+ }
171
+
172
+ const nodeName = typeof node === 'string' ? node : node.data.name;
173
+ const nodeObj = typeof node === 'string'
174
+ ? this.phylotree.getNodeByName(node)
175
+ : node;
176
+
177
+ if (this._selectionSets[setName].members.has(nodeName)) {
178
+ this._selectionSets[setName].members.delete(nodeName);
179
+ if (nodeObj) {
180
+ delete nodeObj._selectionSet;
181
+ }
182
+
183
+ this.emit('setChange', {
184
+ action: 'remove',
185
+ set: setName,
186
+ node: nodeObj,
187
+ allSets: this._getSetsSummary()
188
+ });
189
+
190
+ this.refresh();
191
+ this.update();
192
+ }
193
+
194
+ return this;
195
+ }
196
+
197
+ /**
198
+ * Get a summary of all sets and their members.
199
+ *
200
+ * @returns {Object} Object with set names as keys and arrays of node names as values.
201
+ * @private
202
+ */
203
+ export function _getSetsSummary() {
204
+ const summary = {};
205
+ if (this._selectionSets) {
206
+ Object.keys(this._selectionSets).forEach(setName => {
207
+ summary[setName] = Array.from(this._selectionSets[setName].members);
208
+ });
209
+ }
210
+ return summary;
211
+ }
212
+
213
+ /**
214
+ * Handle click in multi-set mode.
215
+ * Adds the clicked node to the active set.
216
+ *
217
+ * @param {Node} node - The clicked node.
218
+ * @private
219
+ */
220
+ export function handleMultiSetClick(node) {
221
+ if (!this._activeSetName || !this._selectionSets) {
222
+ return;
223
+ }
224
+
225
+ const nodeName = node.data.name;
226
+ const currentSet = node._selectionSet;
227
+
228
+ if (currentSet === this._activeSetName) {
229
+ // Already in active set, remove it
230
+ this.removeFromSet(node, this._activeSetName);
231
+ } else {
232
+ // Add to active set (removes from other sets automatically)
233
+ this.addToSet(node, this._activeSetName);
234
+ }
235
+ }
236
+
237
+ /**
238
+ * Check if a node belongs to a specific set.
239
+ *
240
+ * @param {Node} node - The node to check.
241
+ * @param {string} setName - The set name to check.
242
+ * @returns {boolean} True if node is in the set.
243
+ */
244
+ export function isInSet(node, setName) {
245
+ if (!this._selectionSets || !this._selectionSets[setName]) {
246
+ return false;
247
+ }
248
+ return this._selectionSets[setName].members.has(node.data.name);
249
+ }
250
+
251
+ /**
252
+ * Get the set name a node belongs to.
253
+ *
254
+ * @param {Node} node - The node to check.
255
+ * @returns {string|null} The set name or null if not in any set.
256
+ */
257
+ export function getNodeSet(node) {
258
+ return node._selectionSet || null;
259
+ }
@@ -0,0 +1,93 @@
1
+ /**
2
+ * phylotree-menus.css - Standalone menu styles (no Bootstrap dependency)
3
+ *
4
+ * This file provides native styling for phylotree context menus.
5
+ * Include this CSS file to use menus without Bootstrap.
6
+ */
7
+
8
+ .phylotree-context-menu {
9
+ position: absolute;
10
+ display: none;
11
+ min-width: 160px;
12
+ padding: 8px 0;
13
+ margin: 0;
14
+ background-color: #ffffff;
15
+ border: 1px solid rgba(0, 0, 0, 0.15);
16
+ border-radius: 4px;
17
+ box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
18
+ z-index: 1000;
19
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
20
+ font-size: 14px;
21
+ color: #212529;
22
+ list-style: none;
23
+ }
24
+
25
+ .phylotree-menu-item {
26
+ display: block;
27
+ padding: 6px 20px;
28
+ color: #212529;
29
+ text-decoration: none;
30
+ cursor: pointer;
31
+ white-space: nowrap;
32
+ background-color: transparent;
33
+ border: none;
34
+ width: 100%;
35
+ text-align: left;
36
+ box-sizing: border-box;
37
+ }
38
+
39
+ .phylotree-menu-item:hover,
40
+ .phylotree-menu-item:focus {
41
+ background-color: #f8f9fa;
42
+ color: #16181b;
43
+ outline: none;
44
+ }
45
+
46
+ .phylotree-menu-item:active {
47
+ background-color: #e9ecef;
48
+ }
49
+
50
+ .phylotree-menu-divider {
51
+ height: 0;
52
+ margin: 8px 0;
53
+ overflow: hidden;
54
+ border-top: 1px solid #e9ecef;
55
+ }
56
+
57
+ .phylotree-menu-header {
58
+ display: block;
59
+ padding: 6px 20px;
60
+ margin-bottom: 0;
61
+ font-size: 12px;
62
+ font-weight: 600;
63
+ color: #6c757d;
64
+ white-space: nowrap;
65
+ text-transform: uppercase;
66
+ letter-spacing: 0.5px;
67
+ }
68
+
69
+ /* Selection set color indicators */
70
+ .phylotree-set-indicator {
71
+ display: inline-block;
72
+ width: 10px;
73
+ height: 10px;
74
+ border-radius: 50%;
75
+ margin-right: 8px;
76
+ vertical-align: middle;
77
+ }
78
+
79
+ /* Animation for menu appearance */
80
+ .phylotree-context-menu[style*="display: block"] {
81
+ animation: phylotree-menu-fade-in 0.15s ease-out;
82
+ }
83
+
84
+ @keyframes phylotree-menu-fade-in {
85
+ from {
86
+ opacity: 0;
87
+ transform: translateY(-4px);
88
+ }
89
+ to {
90
+ opacity: 1;
91
+ transform: translateY(0);
92
+ }
93
+ }
package/src/rooting.js CHANGED
@@ -9,6 +9,23 @@ import * as _ from "underscore";
9
9
  the specified fraction
10
10
 
11
11
  * @returns {Phylotree} The current ``phylotree``.
12
+ * @example
13
+ * // Reroot tree on a specific node
14
+ * const tree = new Phylotree("((A:0.1,B:0.2):0.05,(C:0.3,D:0.1):0.08);");
15
+ * const nodeC = tree.getNodeByName("C");
16
+ * tree.reroot(nodeC);
17
+ * // Tree is now rooted on the branch leading to C
18
+ *
19
+ * @example
20
+ * // Reroot with custom branch length partitioning
21
+ * const nodeA = tree.getNodeByName("A");
22
+ * tree.reroot(nodeA, 0.3); // 30% of branch length goes to new root
23
+ *
24
+ * @example
25
+ * // Reroot on midpoint of tree (balance tree)
26
+ * const tips = tree.getTips();
27
+ * const midpoint = tree.computeMidpoint();
28
+ * tree.reroot(midpoint);
12
29
  */
13
30
  export function reroot(node, fraction) {
14
31
 
@@ -146,6 +163,11 @@ export function reroot(node, fraction) {
146
163
  d3.select(rendered_tree.container).node().appendChild(rendered_tree.show());
147
164
  d3.select(this.display.container).dispatch('reroot');
148
165
 
166
+ // Emit rerooted event via new event system
167
+ if (rendered_tree.emit) {
168
+ rendered_tree.emit('rerooted', this.nodes);
169
+ }
170
+
149
171
  }
150
172
 
151
173
  return this;