phylotree 2.6.0 → 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 +49 -5
- 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-menus.css +93 -0
- package/dist/phylotree.d.ts +509 -241
- package/dist/phylotree.js +5023 -1515
- package/dist/phylotree.min.js +1 -1
- package/package.json +25 -27
- 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 +16 -4
- package/src/render/draw.js +128 -86
- package/src/render/edges.js +17 -8
- package/src/render/events.js +2 -3
- package/src/render/menus.js +110 -10
- package/src/render/nodes.js +42 -16
- package/src/render/options.js +3 -1
- package/src/render/selection-sets.js +30 -14
- package/src/render/unrooted.js +90 -12
- package/src/rooting.js +12 -3
- package/src/traversal.js +4 -3
package/src/render/menus.js
CHANGED
|
@@ -7,6 +7,41 @@ import { predefined_selecters } from "./options";
|
|
|
7
7
|
|
|
8
8
|
let d3_layout_phylotree_context_menu_id = "d3_layout_phylotree_context_menu";
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* Whether the node menu opens on events of `type` ("click" or
|
|
12
|
+
* "contextmenu"), per the `menu-trigger` option.
|
|
13
|
+
* @private
|
|
14
|
+
*/
|
|
15
|
+
export function menuOpensOn(type) {
|
|
16
|
+
const trigger = this.options["menu-trigger"] || "click";
|
|
17
|
+
return trigger === "both" || trigger === type;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Open the menu for `node` at the position of `event`.
|
|
22
|
+
*
|
|
23
|
+
* Emits `contextMenu` with `(node, event)` first; a listener that calls
|
|
24
|
+
* `event.preventDefault()` replaces the built-in menu with its own. For a
|
|
25
|
+
* right-click, the browser's own context menu is suppressed.
|
|
26
|
+
*
|
|
27
|
+
* @param {Object} node The node whose menu to open.
|
|
28
|
+
* @param {Event} event The triggering mouse event; its `clientX`/`clientY`
|
|
29
|
+
* position the menu.
|
|
30
|
+
* @returns The current ``this``.
|
|
31
|
+
* @example
|
|
32
|
+
* tree.display.on("contextMenu", (node, event) => {
|
|
33
|
+
* event.preventDefault(); // skip the built-in menu
|
|
34
|
+
* showMyMenu(node, event.clientX, event.clientY);
|
|
35
|
+
* });
|
|
36
|
+
*/
|
|
37
|
+
export function openNodeMenu(node, event) {
|
|
38
|
+
this.emit("contextMenu", node, event);
|
|
39
|
+
if (event.defaultPrevented) return this;
|
|
40
|
+
if (event.type === "contextmenu") event.preventDefault();
|
|
41
|
+
this.handle_node_click(node, event);
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
|
|
10
45
|
export function nodeDropdownMenu(node, container, phylotree, options, event) {
|
|
11
46
|
let menu_object = d3
|
|
12
47
|
.select(container)
|
|
@@ -166,7 +201,14 @@ export function nodeDropdownMenu(node, container, phylotree, options, event) {
|
|
|
166
201
|
.text("Hide this " + (isLeafNode(node) ? "node" : "subtree"))
|
|
167
202
|
.on("click", d => {
|
|
168
203
|
menu_object.style("display", "none");
|
|
169
|
-
|
|
204
|
+
// hide the whole subtree, mirroring "Show all descendant nodes"
|
|
205
|
+
this.modifySelection(
|
|
206
|
+
[node].concat(selectAllDescendants(node, true, true)),
|
|
207
|
+
"notshown",
|
|
208
|
+
true,
|
|
209
|
+
true,
|
|
210
|
+
"true"
|
|
211
|
+
)
|
|
170
212
|
.updateHasHiddenNodes()
|
|
171
213
|
.update();
|
|
172
214
|
});
|
|
@@ -223,12 +265,12 @@ export function nodeDropdownMenu(node, container, phylotree, options, event) {
|
|
|
223
265
|
.append("a")
|
|
224
266
|
.attr("class", "phylotree-menu-item dropdown-item")
|
|
225
267
|
.attr("tabindex", "-1")
|
|
226
|
-
.text((d[0])(node))
|
|
268
|
+
.text((d[0])(node))
|
|
227
269
|
.on("click", _.partial(d[1], node));
|
|
228
270
|
});
|
|
229
271
|
}
|
|
230
272
|
|
|
231
|
-
let tree_container =
|
|
273
|
+
let tree_container = d3.select(container).node();
|
|
232
274
|
let rect = tree_container.getBoundingClientRect();
|
|
233
275
|
|
|
234
276
|
menu_object
|
|
@@ -242,6 +284,38 @@ export function nodeDropdownMenu(node, container, phylotree, options, event) {
|
|
|
242
284
|
|
|
243
285
|
}
|
|
244
286
|
|
|
287
|
+
/**
|
|
288
|
+
* Add a custom menu item to a node's menu.
|
|
289
|
+
*
|
|
290
|
+
* It is easy to add the menu items inside the `node-styler` function in tree.render(), which gets (element, node) as
|
|
291
|
+
* its two arguments -- that `node` can be passed to addCustomMenu() as its first argument. You may want to set
|
|
292
|
+
* `node.menu_items = []` to avoid adding the same menu item multiple times.
|
|
293
|
+
*
|
|
294
|
+
* The custom item will be added in addition to the default items: "Collapse Subtree", the "Toggle selection" submenu,
|
|
295
|
+
* "Reroot on this node" and "Hide this subtree". It will be added at the end of the menu after a menu divider.
|
|
296
|
+
*
|
|
297
|
+
* Available both as a package export and as a method on the rendered tree.
|
|
298
|
+
*
|
|
299
|
+
* @param {Object} node The node to add the custom menu item to.
|
|
300
|
+
* @param {Function} name Given a node, returns the name of the menu item.
|
|
301
|
+
* @param {Function} callback Called as `callback(node, event)` when the menu item is clicked.
|
|
302
|
+
* @param {Function} [condition] Given a node, returns whether the menu item should be shown. Always shown if omitted.
|
|
303
|
+
* @example
|
|
304
|
+
* import { phylotree, addCustomMenu } from "phylotree";
|
|
305
|
+
*
|
|
306
|
+
* const tree = new phylotree(newick);
|
|
307
|
+
* tree.render({
|
|
308
|
+
* container: "#tree",
|
|
309
|
+
* "node-styler": (element, node) => {
|
|
310
|
+
* addCustomMenu(
|
|
311
|
+
* node,
|
|
312
|
+
* n => "Show details for " + n.data.name,
|
|
313
|
+
* n => showDetails(n),
|
|
314
|
+
* n => !n.children
|
|
315
|
+
* );
|
|
316
|
+
* }
|
|
317
|
+
* });
|
|
318
|
+
*/
|
|
245
319
|
export function addCustomMenu(node, name, callback, condition) {
|
|
246
320
|
if (!("menu_items" in node)) {
|
|
247
321
|
node["menu_items"] = [];
|
|
@@ -283,17 +357,27 @@ export function modifySelection(
|
|
|
283
357
|
|
|
284
358
|
// check if node_selecter is a value of pre-defined selecters
|
|
285
359
|
|
|
286
|
-
|
|
360
|
+
// Hiding ("notshown") and brushing ("tag") are not selections, so they are
|
|
361
|
+
// not subject to the selectable / restricted-selectable options.
|
|
362
|
+
const is_selection = attr != "notshown" && attr != "tag";
|
|
363
|
+
|
|
364
|
+
if (
|
|
365
|
+
is_selection &&
|
|
366
|
+
Array.isArray(this.options["restricted-selectable"]) &&
|
|
367
|
+
this.options["restricted-selectable"].length
|
|
368
|
+
) {
|
|
287
369
|
// the selection must be from a list of pre-determined selections
|
|
288
370
|
if (_.contains(_.keys(predefined_selecters), node_selecter)) {
|
|
289
371
|
node_selecter = predefined_selecters[node_selecter];
|
|
290
372
|
} else {
|
|
291
|
-
return;
|
|
373
|
+
return this;
|
|
292
374
|
}
|
|
293
375
|
}
|
|
294
376
|
|
|
295
377
|
if (
|
|
296
|
-
(
|
|
378
|
+
(!is_selection ||
|
|
379
|
+
this.options["restricted-selectable"] ||
|
|
380
|
+
this.options["selectable"]) &&
|
|
297
381
|
!this.options["binary-selectable"]
|
|
298
382
|
) {
|
|
299
383
|
var do_refresh = false;
|
|
@@ -353,8 +437,12 @@ export function modifySelection(
|
|
|
353
437
|
}
|
|
354
438
|
}
|
|
355
439
|
} else if (this.options["binary-selectable"]) {
|
|
440
|
+
// Only selection attributes are mutually exclusive; hiding ("notshown")
|
|
441
|
+
// or brushing ("tag") must not clear the attributes in attribute-list.
|
|
442
|
+
const exclusive = attr != "notshown" && attr != "tag";
|
|
443
|
+
|
|
356
444
|
if (typeof node_selecter === "function") {
|
|
357
|
-
this.links.forEach(
|
|
445
|
+
this.links.forEach(d => {
|
|
358
446
|
var select_me = node_selecter(d);
|
|
359
447
|
d[attr] = d[attr] || false;
|
|
360
448
|
|
|
@@ -364,6 +452,7 @@ export function modifySelection(
|
|
|
364
452
|
d.target[attr] = select_me;
|
|
365
453
|
}
|
|
366
454
|
|
|
455
|
+
if (!exclusive) return;
|
|
367
456
|
this.options["attribute-list"].forEach(function(type) {
|
|
368
457
|
if (type != attr && d[attr] === true) {
|
|
369
458
|
d[type] = false;
|
|
@@ -374,7 +463,17 @@ export function modifySelection(
|
|
|
374
463
|
} else {
|
|
375
464
|
node_selecter.forEach(function(d) {
|
|
376
465
|
var new_value;
|
|
377
|
-
|
|
466
|
+
switch (mode) {
|
|
467
|
+
case "true":
|
|
468
|
+
new_value = true;
|
|
469
|
+
break;
|
|
470
|
+
case "false":
|
|
471
|
+
new_value = false;
|
|
472
|
+
break;
|
|
473
|
+
default:
|
|
474
|
+
new_value = !d[attr];
|
|
475
|
+
break;
|
|
476
|
+
}
|
|
378
477
|
|
|
379
478
|
if (d[attr] != new_value) {
|
|
380
479
|
d[attr] = new_value;
|
|
@@ -382,10 +481,11 @@ export function modifySelection(
|
|
|
382
481
|
}
|
|
383
482
|
});
|
|
384
483
|
|
|
385
|
-
this.links.forEach(
|
|
484
|
+
this.links.forEach(d => {
|
|
386
485
|
d[attr] = d.target[attr];
|
|
486
|
+
if (!exclusive) return;
|
|
387
487
|
this.options["attribute-list"].forEach(function(type) {
|
|
388
|
-
if (type != attr && d[attr]
|
|
488
|
+
if (type != attr && d[attr] === true) {
|
|
389
489
|
d[type] = false;
|
|
390
490
|
d.target[type] = false;
|
|
391
491
|
}
|
package/src/render/nodes.js
CHANGED
|
@@ -2,7 +2,6 @@ import * as d3 from "d3";
|
|
|
2
2
|
|
|
3
3
|
import { itemTagged, itemSelected } from "./helpers";
|
|
4
4
|
import { isLeafNode } from "../nodes";
|
|
5
|
-
import { css_classes } from "./options";
|
|
6
5
|
|
|
7
6
|
export function shiftTip(d) {
|
|
8
7
|
|
|
@@ -50,7 +49,10 @@ export function drawNode(container, node, transitions) {
|
|
|
50
49
|
.merge(labels)
|
|
51
50
|
.on("click", (event, d) => {
|
|
52
51
|
this.emit('nodeClick', node, event);
|
|
53
|
-
this.
|
|
52
|
+
if (this.menuOpensOn("click")) this.openNodeMenu(node, event);
|
|
53
|
+
})
|
|
54
|
+
.on("contextmenu", (event) => {
|
|
55
|
+
if (this.menuOpensOn("contextmenu")) this.openNodeMenu(node, event);
|
|
54
56
|
})
|
|
55
57
|
.on("mouseenter", (event) => {
|
|
56
58
|
this.emit('nodeHover', node, event);
|
|
@@ -110,7 +112,7 @@ export function drawNode(container, node, transitions) {
|
|
|
110
112
|
};
|
|
111
113
|
|
|
112
114
|
if (transitions) {
|
|
113
|
-
tracers
|
|
115
|
+
tracers
|
|
114
116
|
.enter()
|
|
115
117
|
.append("line")
|
|
116
118
|
.classed(this.css_classes["branch-tracer"], true)
|
|
@@ -165,7 +167,7 @@ export function drawNode(container, node, transitions) {
|
|
|
165
167
|
|
|
166
168
|
if (this.shown_font_size >= 5) {
|
|
167
169
|
const isRTL = this.options["layout"] == "right-to-left";
|
|
168
|
-
labels
|
|
170
|
+
labels.attr("dx", d => {
|
|
169
171
|
const direction = (this.radial() || this.unrooted())
|
|
170
172
|
? (d.text_align == "end" ? -1 : 1)
|
|
171
173
|
: (isRTL ? -1 : 1);
|
|
@@ -201,7 +203,10 @@ export function drawNode(container, node, transitions) {
|
|
|
201
203
|
})
|
|
202
204
|
.on("click", (event, d) => {
|
|
203
205
|
this.emit('nodeClick', node, event);
|
|
204
|
-
this.
|
|
206
|
+
if (this.menuOpensOn("click")) this.openNodeMenu(node, event);
|
|
207
|
+
})
|
|
208
|
+
.on("contextmenu", (event) => {
|
|
209
|
+
if (this.menuOpensOn("contextmenu")) this.openNodeMenu(node, event);
|
|
205
210
|
})
|
|
206
211
|
.on("mouseenter", (event) => {
|
|
207
212
|
this.emit('nodeHover', node, event);
|
|
@@ -271,35 +276,56 @@ export function nodeSpan(attr) {
|
|
|
271
276
|
|
|
272
277
|
export function reclassNode(node) {
|
|
273
278
|
|
|
274
|
-
let class_var = css_classes[isLeafNode(node) ? "node" : "internal-node"];
|
|
279
|
+
let class_var = this.css_classes[isLeafNode(node) ? "node" : "internal-node"];
|
|
275
280
|
|
|
276
281
|
if (itemTagged(node)) {
|
|
277
|
-
class_var += " " + css_classes["tagged-node"];
|
|
282
|
+
class_var += " " + this.css_classes["tagged-node"];
|
|
278
283
|
}
|
|
279
284
|
|
|
280
285
|
if (itemSelected(node, this.selection_attribute_name)) {
|
|
281
|
-
class_var += " " + css_classes["selected-node"];
|
|
286
|
+
class_var += " " + this.css_classes["selected-node"];
|
|
282
287
|
}
|
|
283
288
|
|
|
284
289
|
if (!node["parent"]) {
|
|
285
|
-
class_var += " " + css_classes["root-node"];
|
|
290
|
+
class_var += " " + this.css_classes["root-node"];
|
|
286
291
|
}
|
|
287
292
|
|
|
288
293
|
if (isNodeCollapsed(node) || hasHiddenNodes(node)) {
|
|
289
|
-
class_var += " " + css_classes["collapsed-node"];
|
|
294
|
+
class_var += " " + this.css_classes["collapsed-node"];
|
|
290
295
|
}
|
|
291
296
|
|
|
292
297
|
return class_var;
|
|
293
298
|
}
|
|
294
299
|
|
|
295
300
|
export function nodeVisible(node) {
|
|
296
|
-
return !(node.hidden || node
|
|
301
|
+
return !(node.hidden || nodeOrAncestorNotshown(node));
|
|
297
302
|
}
|
|
298
303
|
|
|
299
304
|
export function nodeNotshown(node) {
|
|
300
305
|
return node.notshown;
|
|
301
306
|
}
|
|
302
307
|
|
|
308
|
+
// true if the node or any of its ancestors is hidden (notshown); the layout
|
|
309
|
+
// skips a hidden node's whole subtree, so its descendants are hidden too.
|
|
310
|
+
// Reads the flag cached by markNotshownSubtrees() (run by placenodes) so the
|
|
311
|
+
// check stays O(1) on deep trees; walks the ancestors if it is not set yet.
|
|
312
|
+
export function nodeOrAncestorNotshown(node) {
|
|
313
|
+
if (node.notshown) return true;
|
|
314
|
+
if (node.notshown_ancestor !== undefined) return node.notshown_ancestor;
|
|
315
|
+
for (let n = node.parent; n; n = n.parent) {
|
|
316
|
+
if (n.notshown) return true;
|
|
317
|
+
}
|
|
318
|
+
return false;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// cache, on every node, whether one of its ancestors is notshown
|
|
322
|
+
export function markNotshownSubtrees(root) {
|
|
323
|
+
root.eachBefore(d => {
|
|
324
|
+
d.notshown_ancestor =
|
|
325
|
+
!!d.parent && (!!d.parent.notshown || d.parent.notshown_ancestor);
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
|
|
303
329
|
export function hasHiddenNodes(node) {
|
|
304
330
|
return node.hasHiddenNodes || false;
|
|
305
331
|
}
|
|
@@ -316,7 +342,7 @@ export function nodeCssSelectors(css_classes) {
|
|
|
316
342
|
css_classes["tagged-node"],
|
|
317
343
|
css_classes["root-node"]
|
|
318
344
|
].reduce(function(p, c, i, a) {
|
|
319
|
-
return
|
|
345
|
+
return p + "g." + c + (i < a.length - 1 ? "," : "");
|
|
320
346
|
}, "");
|
|
321
347
|
}
|
|
322
348
|
|
|
@@ -344,14 +370,14 @@ export function internalLabel(callback, respect_existing) {
|
|
|
344
370
|
|
|
345
371
|
export function defNodeLabel(_node) {
|
|
346
372
|
|
|
347
|
-
|
|
373
|
+
const data = _node.data;
|
|
348
374
|
|
|
349
|
-
if (isLeafNode(
|
|
350
|
-
return
|
|
375
|
+
if (isLeafNode(data)) {
|
|
376
|
+
return data.name || "";
|
|
351
377
|
}
|
|
352
378
|
|
|
353
379
|
if (this.showInternalName(_node)) {
|
|
354
|
-
return
|
|
380
|
+
return data.name || "";
|
|
355
381
|
}
|
|
356
382
|
|
|
357
383
|
return "";
|
package/src/render/options.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { isLeafNode } from "../nodes";
|
|
2
2
|
|
|
3
|
+
// Default class names. Each TreeRender works on its own copy
|
|
4
|
+
// (this.css_classes), so this object is never mutated.
|
|
3
5
|
export const css_classes = {
|
|
4
6
|
"tree-container": "phylotree-container",
|
|
5
7
|
"tree-scale-bar": "tree-scale-bar",
|
|
@@ -20,7 +22,7 @@ export const css_classes = {
|
|
|
20
22
|
|
|
21
23
|
export function initializeCssClasses(classes = {}) {
|
|
22
24
|
Object.keys(classes).forEach(key => {
|
|
23
|
-
css_classes[key] = classes[key]
|
|
25
|
+
this.css_classes[key] = classes[key]
|
|
24
26
|
})
|
|
25
27
|
}
|
|
26
28
|
|
|
@@ -14,6 +14,9 @@ export function initializeSelectionSets(sets) {
|
|
|
14
14
|
this._selectionSets = {};
|
|
15
15
|
this._activeSetName = null;
|
|
16
16
|
|
|
17
|
+
// ignore malformed entries such as null or {} rather than throwing
|
|
18
|
+
sets = sets.filter(set => set && typeof set.name === "string");
|
|
19
|
+
|
|
17
20
|
sets.forEach(set => {
|
|
18
21
|
this._selectionSets[set.name] = {
|
|
19
22
|
name: set.name,
|
|
@@ -27,6 +30,20 @@ export function initializeSelectionSets(sets) {
|
|
|
27
30
|
this._activeSetName = sets[0].name;
|
|
28
31
|
}
|
|
29
32
|
|
|
33
|
+
// On re-render (e.g. after reroot) restore the active set and rebuild the
|
|
34
|
+
// members from the flags that persist on the nodes.
|
|
35
|
+
const remembered = this.phylotree.__activeSelectionSet;
|
|
36
|
+
if (remembered && this._selectionSets[remembered]) {
|
|
37
|
+
this._activeSetName = remembered;
|
|
38
|
+
}
|
|
39
|
+
this.phylotree.__activeSelectionSet = this._activeSetName;
|
|
40
|
+
|
|
41
|
+
this.phylotree.nodes.descendants().forEach(node => {
|
|
42
|
+
if (node._selectionSet && this._selectionSets[node._selectionSet]) {
|
|
43
|
+
this._selectionSets[node._selectionSet].members.add(node);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
30
47
|
// Inject CSS for set colors
|
|
31
48
|
this._injectSetStyles(sets);
|
|
32
49
|
}
|
|
@@ -76,6 +93,7 @@ export function _injectSetStyles(sets) {
|
|
|
76
93
|
export function setActiveSet(name) {
|
|
77
94
|
if (this._selectionSets && this._selectionSets[name]) {
|
|
78
95
|
this._activeSetName = name;
|
|
96
|
+
this.phylotree.__activeSelectionSet = name;
|
|
79
97
|
} else {
|
|
80
98
|
console.warn(`Selection set '${name}' not found`);
|
|
81
99
|
}
|
|
@@ -106,9 +124,9 @@ export function getSetMembers(setName) {
|
|
|
106
124
|
return [];
|
|
107
125
|
}
|
|
108
126
|
|
|
109
|
-
const
|
|
127
|
+
const members = this._selectionSets[setName].members;
|
|
110
128
|
return this.phylotree.nodes.descendants()
|
|
111
|
-
.filter(node =>
|
|
129
|
+
.filter(node => members.has(node));
|
|
112
130
|
}
|
|
113
131
|
|
|
114
132
|
/**
|
|
@@ -127,7 +145,8 @@ export function addToSet(node, setName) {
|
|
|
127
145
|
return this;
|
|
128
146
|
}
|
|
129
147
|
|
|
130
|
-
|
|
148
|
+
// Membership is keyed by node identity; names are resolved here so that
|
|
149
|
+
// unnamed or duplicate-named nodes do not share membership.
|
|
131
150
|
const nodeObj = typeof node === "string"
|
|
132
151
|
? this.phylotree.getNodeByName(node)
|
|
133
152
|
: node;
|
|
@@ -135,10 +154,10 @@ export function addToSet(node, setName) {
|
|
|
135
154
|
if (nodeObj) {
|
|
136
155
|
// Remove from all other sets (mutually exclusive)
|
|
137
156
|
Object.keys(this._selectionSets).forEach(otherSet => {
|
|
138
|
-
this._selectionSets[otherSet].members.delete(
|
|
157
|
+
this._selectionSets[otherSet].members.delete(nodeObj);
|
|
139
158
|
});
|
|
140
159
|
|
|
141
|
-
this._selectionSets[setName].members.add(
|
|
160
|
+
this._selectionSets[setName].members.add(nodeObj);
|
|
142
161
|
nodeObj._selectionSet = setName;
|
|
143
162
|
|
|
144
163
|
this.emit("setChange", {
|
|
@@ -169,16 +188,13 @@ export function removeFromSet(node, setName) {
|
|
|
169
188
|
return this;
|
|
170
189
|
}
|
|
171
190
|
|
|
172
|
-
const nodeName = typeof node === "string" ? node : node.data.name;
|
|
173
191
|
const nodeObj = typeof node === "string"
|
|
174
192
|
? this.phylotree.getNodeByName(node)
|
|
175
193
|
: node;
|
|
176
194
|
|
|
177
|
-
if (this._selectionSets[setName].members.has(
|
|
178
|
-
this._selectionSets[setName].members.delete(
|
|
179
|
-
|
|
180
|
-
delete nodeObj._selectionSet;
|
|
181
|
-
}
|
|
195
|
+
if (nodeObj && this._selectionSets[setName].members.has(nodeObj)) {
|
|
196
|
+
this._selectionSets[setName].members.delete(nodeObj);
|
|
197
|
+
delete nodeObj._selectionSet;
|
|
182
198
|
|
|
183
199
|
this.emit("setChange", {
|
|
184
200
|
action: "remove",
|
|
@@ -204,7 +220,8 @@ export function _getSetsSummary() {
|
|
|
204
220
|
const summary = {};
|
|
205
221
|
if (this._selectionSets) {
|
|
206
222
|
Object.keys(this._selectionSets).forEach(setName => {
|
|
207
|
-
summary[setName] = Array.from(this._selectionSets[setName].members
|
|
223
|
+
summary[setName] = Array.from(this._selectionSets[setName].members,
|
|
224
|
+
node => node.data.name);
|
|
208
225
|
});
|
|
209
226
|
}
|
|
210
227
|
return summary;
|
|
@@ -222,7 +239,6 @@ export function handleMultiSetClick(node) {
|
|
|
222
239
|
return;
|
|
223
240
|
}
|
|
224
241
|
|
|
225
|
-
const nodeName = node.data.name;
|
|
226
242
|
const currentSet = node._selectionSet;
|
|
227
243
|
|
|
228
244
|
if (currentSet === this._activeSetName) {
|
|
@@ -245,7 +261,7 @@ export function isInSet(node, setName) {
|
|
|
245
261
|
if (!this._selectionSets || !this._selectionSets[setName]) {
|
|
246
262
|
return false;
|
|
247
263
|
}
|
|
248
|
-
return this._selectionSets[setName].members.has(node
|
|
264
|
+
return this._selectionSets[setName].members.has(node);
|
|
249
265
|
}
|
|
250
266
|
|
|
251
267
|
/**
|
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);
|
|
@@ -155,6 +161,9 @@ export function reroot(node, fraction) {
|
|
|
155
161
|
// retain selection
|
|
156
162
|
let selectionName = this.display.selection_attribute_name
|
|
157
163
|
|
|
164
|
+
// retain class names, including any set at runtime with css()
|
|
165
|
+
options["css-classes"] = Object.assign({}, this.display.css_classes);
|
|
166
|
+
|
|
158
167
|
delete this.display;
|
|
159
168
|
|
|
160
169
|
let rendered_tree = this.render(options);
|