phylotree 2.6.1 → 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.
@@ -102,6 +102,7 @@ class TreeRender {
102
102
  "label-nodes-with-name": false,
103
103
  zoom: false,
104
104
  "show-menu": true,
105
+ "menu-trigger": "click",
105
106
  "show-labels": true,
106
107
  "node-styler": null,
107
108
  "edge-styler": null,
@@ -142,16 +143,14 @@ class TreeRender {
142
143
  };
143
144
  }
144
145
 
145
- this.rescale_nodeSpan =
146
- this.phylotree.nodes.children
147
- .map(d => {
148
- if (isLeafNode(d) || this.showInternalName(d))
149
- return this.nodeSpan(d);
150
- })
151
- .reduce(function(p, c) {
152
- return Math.min(c, p || 1e200);
153
- }, null) || 1;
146
+ const spans = this.phylotree.nodes
147
+ .descendants()
148
+ .filter(d => isLeafNode(d) || this.showInternalName(d))
149
+ .map(d => this.nodeSpan(d))
150
+ .filter(v => typeof v === "number" && isFinite(v) && v > 0);
151
+ this.rescale_nodeSpan = spans.length ? Math.min(...spans) : 1;
154
152
 
153
+ this.undef_BL = false;
155
154
  this.initialize_svg(this.container);
156
155
  this.links = this.phylotree.nodes.links();
157
156
  this.initializeEdgeLabels();
@@ -160,6 +159,7 @@ class TreeRender {
160
159
 
161
160
  // Initialize multi-set selection if configured
162
161
  if (this.options["selection-mode"] === "multi-set" &&
162
+ Array.isArray(this.options["selection-sets"]) &&
163
163
  this.options["selection-sets"].length > 0) {
164
164
  this.initializeSelectionSets(this.options["selection-sets"]);
165
165
  }
@@ -174,15 +174,22 @@ class TreeRender {
174
174
  */
175
175
  applyInitialSelection() {
176
176
  // Single selection mode
177
- if (this.options["initial-selection"] &&
177
+ if (Array.isArray(this.options["initial-selection"]) &&
178
178
  this.options["initial-selection"].length > 0) {
179
179
  this.selectNodes(this.options["initial-selection"]);
180
180
  }
181
181
 
182
182
  // Multi-set mode
183
183
  if (this.options["selection-mode"] === "multi-set" &&
184
- this.options["initial-sets"]) {
184
+ this.options["initial-sets"] &&
185
+ typeof this.options["initial-sets"] === "object" &&
186
+ !Array.isArray(this.options["initial-sets"]) &&
187
+ this._selectionSets &&
188
+ !this.phylotree.__initialSetsApplied) {
189
+ // applied once per tree, and only once a requested set is defined
185
190
  Object.entries(this.options["initial-sets"]).forEach(([setName, nodeNames]) => {
191
+ if (!Array.isArray(nodeNames) || !this._selectionSets[setName]) return;
192
+ this.phylotree.__initialSetsApplied = true;
186
193
  nodeNames.forEach(nodeName => {
187
194
  this.addToSet(nodeName, setName);
188
195
  });
@@ -279,13 +286,21 @@ class TreeRender {
279
286
  this.svg.append("defs");
280
287
  }
281
288
 
282
- d3.select(this.container).on(
283
- "click",
284
- d => {
285
- this.handle_node_click(null);
286
- },
287
- true
288
- );
289
+ d3.select(this.container)
290
+ .on(
291
+ "click",
292
+ d => {
293
+ this.handle_node_click(null);
294
+ },
295
+ true
296
+ )
297
+ .on(
298
+ "contextmenu",
299
+ d => {
300
+ if (this.menuOpensOn("contextmenu")) this.handle_node_click(null);
301
+ },
302
+ true
303
+ );
289
304
  }
290
305
 
291
306
  return this;
@@ -316,6 +331,9 @@ class TreeRender {
316
331
 
317
332
  //if (!this.svg) return this;
318
333
 
334
+ // the topology may have changed since the last update
335
+ this.links = this.phylotree.nodes.links();
336
+
319
337
  this.placenodes();
320
338
 
321
339
  // Sync edge labels BEFORE drawing edges so that edge.selected reflects node.selected
@@ -336,7 +354,7 @@ class TreeRender {
336
354
  y: this.pad_height()
337
355
  };
338
356
 
339
- enclosure = enclosure
357
+ enclosure
340
358
  .enter()
341
359
  .append("g")
342
360
  .attr("class", this.css_classes["tree-container"])
@@ -395,7 +413,7 @@ class TreeRender {
395
413
  drawn_links.exit().remove();
396
414
  }
397
415
 
398
- drawn_links = drawn_links
416
+ drawn_links
399
417
  .enter()
400
418
  .insert("path", ":first-child")
401
419
  .merge(drawn_links)
@@ -441,7 +459,7 @@ class TreeRender {
441
459
  });
442
460
 
443
461
  if (this.options["label-nodes-with-name"]) {
444
- drawn_nodes = drawn_nodes.attr("id", d => {
462
+ drawn_nodes.attr("id", d => {
445
463
  return "node-" + d.name;
446
464
  });
447
465
  }
@@ -480,7 +498,7 @@ class TreeRender {
480
498
 
481
499
  this.modifySelection(
482
500
 
483
- this.phylotree.links.map(d => {
501
+ this.links.map(d => {
484
502
  return d.target;
485
503
  }),
486
504
  "tag",
@@ -621,8 +639,6 @@ class TreeRender {
621
639
 
622
640
  */
623
641
 
624
- let undef_BL = false;
625
-
626
642
  /** _extents computes a bounding box for the tree (initially NOT in screen
627
643
  coordinates)
628
644
 
@@ -640,14 +656,15 @@ class TreeRender {
640
656
 
641
657
  if (a_node["parent"]) {
642
658
  if (this.do_scaling) {
643
- if (undef_BL) {
659
+ if (this.undef_BL) {
644
660
  return 0;
645
661
  }
646
662
 
647
663
  a_node.y = this.phylotree.branch_length_accessor(a_node);
648
664
 
649
- if (typeof a_node.y === "undefined") {
650
- undef_BL = true;
665
+ if (typeof a_node.y !== "number" || !isFinite(a_node.y)) {
666
+ // placenodes() falls back to an unscaled layout
667
+ this.undef_BL = true;
651
668
  return 0;
652
669
  }
653
670
  a_node.y += a_node.parent.y;
@@ -694,15 +711,20 @@ class TreeRender {
694
711
  a_node.collapsed = [[a_node.x, a_node.y]];
695
712
 
696
713
  var map_me = n => {
714
+ // hidden tips have no (or stale) coordinates
715
+ if (render_nodes.nodeNotshown(n)) return;
716
+
697
717
  n.hidden = true;
698
718
 
699
- if (isLeafNode(n)) {
719
+ if (isLeafNode(n)) {
720
+ if (typeof n.x !== "number" || typeof n.y !== "number") return;
721
+
700
722
  this.x = n.x =
701
723
  this.save_x +
702
724
  (n.x - this.save_x) * this.options["compression"] +
703
725
  this.save_span;
704
726
 
705
- a_node.collapsed.push([n.x, n.y]);
727
+ a_node.collapsed.push([n.x, n.y]);
706
728
  } else {
707
729
  n.children.map(map_me);
708
730
  }
@@ -748,7 +770,7 @@ class TreeRender {
748
770
  displayed_children++;
749
771
  }
750
772
 
751
- if (displayed_children >= half_way && !managed_to_display) {
773
+ if (displayed_children > 0 && displayed_children >= half_way && !managed_to_display) {
752
774
  this._handle_single_node_layout(a_node);
753
775
  managed_to_display = true;
754
776
  }
@@ -791,7 +813,7 @@ class TreeRender {
791
813
 
792
814
  this.scales[1] =
793
815
  (this.size[1] - this.offsets[1] - this.options["left-offset"]) /
794
- this._extents[1][1];
816
+ (this._extents[1][1] || 1);
795
817
 
796
818
  this.label_width = this._label_width(this.shown_font_size);
797
819
 
@@ -801,6 +823,8 @@ class TreeRender {
801
823
  } else {
802
824
  this.label_width = this._label_width(this.shown_font_size);
803
825
 
826
+ // Only updates the local parameter; the caller's flag is unchanged.
827
+ // eslint-disable-next-line no-useless-assignment
804
828
  at_least_one_dimension_fixed = true;
805
829
 
806
830
  let available_width =
@@ -816,7 +840,7 @@ class TreeRender {
816
840
  this.offsets[1] -
817
841
  this.options["left-offset"] -
818
842
  this.label_width) /
819
- this._extents[1][1];
843
+ (this._extents[1][1] || 1);
820
844
  }
821
845
  }
822
846
 
@@ -845,22 +869,21 @@ class TreeRender {
845
869
  (this.save_x = this.x), (this.save_span = this.last_span * 0.5);
846
870
 
847
871
  this.do_scaling = this.options["scaling"];
848
- let undef_BL = false;
872
+ this.undef_BL = false;
849
873
 
850
874
  this.is_under_collapsed_parent = false;
851
- this.max_depth = 1;
852
-
875
+ // tree_layout() places the nodes by depth when scaling is off
876
+ this.max_depth = d3.max(this.phylotree.nodes.descendants(), n => {
877
+ return n.depth;
878
+ });
879
+
853
880
  // Set initial x
854
881
  this.phylotree.nodes.x = this.tree_layout(
855
882
  this.phylotree.nodes,
856
883
  this.do_scaling
857
884
  );
858
885
 
859
- this.max_depth = d3.max(this.phylotree.nodes.descendants(), n => {
860
- return n.depth;
861
- });
862
-
863
- if (this.do_scaling && undef_BL) {
886
+ if (this.do_scaling && this.undef_BL) {
864
887
  // requested scaling, but some branches had no branch lengths
865
888
  // redo layout without branch lengths
866
889
  this.do_scaling = false;
@@ -881,7 +904,7 @@ class TreeRender {
881
904
  this.size[0] = this._extents[0][1] * this.fixed_width[0];
882
905
  this.scales[0] = this.fixed_width[0];
883
906
  } else {
884
- this.scales[0] = (this.size[0] - this.pad_height()) / this._extents[0][1];
907
+ this.scales[0] = (this.size[0] - this.pad_height()) / (this._extents[0][1] || 1);
885
908
  at_least_one_dimension_fixed = true;
886
909
  }
887
910
 
@@ -931,7 +954,7 @@ class TreeRender {
931
954
  last_circ_position = null,
932
955
  last_child_radius = null,
933
956
  min_radius = 0,
934
- effective_span = this._extents[0][1] * this.scales[0];
957
+ effective_span = (this._extents[0][1] || 1) * this.scales[0];
935
958
 
936
959
  let compute_distance = function(r1, r2, a1, a2, annular_shift) {
937
960
  annular_shift = annular_shift || 0;
@@ -1067,11 +1090,15 @@ class TreeRender {
1067
1090
  );
1068
1091
  }
1069
1092
 
1070
- if (d.collapsed) {
1093
+ // a clade nested in a collapsed clade is not laid out: its flag is
1094
+ // `true` or an outline left from an earlier layout
1095
+ if (Array.isArray(d.collapsed) && !d.hidden) {
1071
1096
  d.collapsed = d.collapsed.map(p => {
1072
1097
  let z = {};
1073
1098
  z.x = p[0];
1074
1099
  z.y = p[1];
1100
+ // same radius the nodes get above: d.radius = d.y * scales[1] / size[1]
1101
+ z.radius = (p[1] * this.scales[1]) / this.size[1];
1075
1102
  z = cartesianToPolar(
1076
1103
  z,
1077
1104
  this.radius,
@@ -1132,10 +1159,15 @@ class TreeRender {
1132
1159
  );
1133
1160
  }
1134
1161
 
1135
- if (d.collapsed) {
1162
+ if (Array.isArray(d.collapsed) && !d.hidden) {
1136
1163
  d.collapsed.forEach(p => {
1137
1164
  p[0] *= this.scales[0];
1138
1165
  p[1] *= this.scales[1]*.8;
1166
+
1167
+ if (this.options["layout"] == "right-to-left") {
1168
+ // same mirroring as d.y above
1169
+ p[1] = this._extents[1][1] * this.scales[1] - p[1] + this.label_width;
1170
+ }
1139
1171
  });
1140
1172
 
1141
1173
  let last_x = d.collapsed[1][0];
@@ -1160,13 +1192,13 @@ class TreeRender {
1160
1192
  domain_limit = Math.pow(
1161
1193
  10,
1162
1194
  Math.ceil(
1163
- Math.log((this._extents[1][1] * range_limit) / this.radius) /
1195
+ Math.log(((this._extents[1][1] || 1) * range_limit) / this.radius) /
1164
1196
  Math.log(10)
1165
1197
  )
1166
1198
  );
1167
1199
 
1168
1200
 
1169
- range_limit = domain_limit * (this.radius / this._extents[1][1]);
1201
+ range_limit = domain_limit * (this.radius / (this._extents[1][1] || 1));
1170
1202
 
1171
1203
  if (range_limit < 30) {
1172
1204
  let stretch = Math.ceil(30 / range_limit);
@@ -1174,7 +1206,7 @@ class TreeRender {
1174
1206
  domain_limit *= stretch;
1175
1207
  }
1176
1208
  } else {
1177
- domain_limit = this._extents[1][1];
1209
+ domain_limit = this._extents[1][1] || 1;
1178
1210
 
1179
1211
  range_limit =
1180
1212
  this.size[1] - this.offsets[1] - this.options["left-offset"] - this.shown_font_size;
@@ -1297,18 +1329,26 @@ class TreeRender {
1297
1329
  }
1298
1330
 
1299
1331
  /**
1300
- * Get or set font size.
1332
+ * Get or set the label font size.
1301
1333
  *
1302
- * @param {Function} attr Empty if getting, or new font size if setting.
1303
- * @returns The current ``font_size`` accessor if getting, or the current ``phylotree`` if setting.
1334
+ * @param {Number} attr (Optional) The new font size; ``undefined`` resets it to 12.
1335
+ * @returns The current ``font_size`` if getting, or the current ``phylotree`` if setting.
1304
1336
  */
1305
- font_size(attr) {
1337
+ fontSize(attr) {
1306
1338
  if (!arguments.length) return this.font_size;
1307
1339
  this.font_size = attr === undefined ? 12 : attr;
1340
+ this.shown_font_size = this.font_size;
1341
+ this.offsets = [0, this.font_size / 2];
1308
1342
  return this;
1309
1343
  }
1310
1344
 
1311
- scale_bar_font_size(attr) {
1345
+ /**
1346
+ * Get or set the scale bar font size.
1347
+ *
1348
+ * @param {Number} attr (Optional) The new font size; ``undefined`` resets it to 12.
1349
+ * @returns The current ``scale_bar_font_size`` if getting, or the current ``phylotree`` if setting.
1350
+ */
1351
+ scaleBarFontSize(attr) {
1312
1352
  if (!arguments.length) return this.scale_bar_font_size;
1313
1353
  this.scale_bar_font_size = attr === undefined ? 12 : attr;
1314
1354
  return this;
@@ -1469,7 +1509,7 @@ class TreeRender {
1469
1509
  */
1470
1510
  style_edges(attr) {
1471
1511
  if (!arguments.length) return this.edge_styler;
1472
- this.edge_styler = attr.bind(this);
1512
+ this.edge_styler = typeof attr === "function" ? attr.bind(this) : attr;
1473
1513
  return this;
1474
1514
  }
1475
1515
 
@@ -82,13 +82,22 @@ export function reclassEdge(edge) {
82
82
 
83
83
  }
84
84
 
85
+ const reserved_annotations = [
86
+ "children", "data", "parent", "depth", "height", "x", "y", "collapsed",
87
+ "hidden", "notshown", "id", "selected", "text_angle", "text_align",
88
+ "radius", "angle"
89
+ ];
90
+
85
91
  export function initializeEdgeLabels() {
86
92
 
87
93
  this.links.forEach(d => {
88
94
 
89
95
  // TODO: Move away from storing attribute data as root (BREAKS occasionally with d3>3)
90
- if(d.target.data.annotation) {
91
- d.target[d.target.data.annotation] = d.target.data.annotation;
96
+ // Annotations named like internal hierarchy fields must not overwrite them.
97
+ const annotation = d.target.data.annotation;
98
+ if(annotation && !reserved_annotations.includes(annotation) &&
99
+ !(annotation in d.target && d.target[annotation] !== annotation)) {
100
+ d.target[annotation] = annotation;
92
101
  }
93
102
 
94
103
  });
@@ -137,7 +146,7 @@ export function edgeCssSelectors(css_classes) {
137
146
  css_classes["selected-branch"],
138
147
  css_classes["tagged-branch"]
139
148
  ].reduce(function(p, c, i, a) {
140
- return (p += "path." + c + (i < a.length - 1 ? "," : ""));
149
+ return p + "path." + c + (i < a.length - 1 ? "," : "");
141
150
  }, "");
142
151
  }
143
152
 
@@ -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)
@@ -230,12 +265,12 @@ export function nodeDropdownMenu(node, container, phylotree, options, event) {
230
265
  .append("a")
231
266
  .attr("class", "phylotree-menu-item dropdown-item")
232
267
  .attr("tabindex", "-1")
233
- .text((d[0])(node)) // eslint-disable-line
268
+ .text((d[0])(node))
234
269
  .on("click", _.partial(d[1], node));
235
270
  });
236
271
  }
237
272
 
238
- let tree_container = document.querySelector(container); // eslint-disable-line
273
+ let tree_container = d3.select(container).node();
239
274
  let rect = tree_container.getBoundingClientRect();
240
275
 
241
276
  menu_object
@@ -249,6 +284,38 @@ export function nodeDropdownMenu(node, container, phylotree, options, event) {
249
284
 
250
285
  }
251
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
+ */
252
319
  export function addCustomMenu(node, name, callback, condition) {
253
320
  if (!("menu_items" in node)) {
254
321
  node["menu_items"] = [];
@@ -290,17 +357,27 @@ export function modifySelection(
290
357
 
291
358
  // check if node_selecter is a value of pre-defined selecters
292
359
 
293
- if (this.options["restricted-selectable"].length) {
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
+ ) {
294
369
  // the selection must be from a list of pre-determined selections
295
370
  if (_.contains(_.keys(predefined_selecters), node_selecter)) {
296
371
  node_selecter = predefined_selecters[node_selecter];
297
372
  } else {
298
- return;
373
+ return this;
299
374
  }
300
375
  }
301
376
 
302
377
  if (
303
- (this.options["restricted-selectable"] || this.options["selectable"]) &&
378
+ (!is_selection ||
379
+ this.options["restricted-selectable"] ||
380
+ this.options["selectable"]) &&
304
381
  !this.options["binary-selectable"]
305
382
  ) {
306
383
  var do_refresh = false;
@@ -408,7 +485,7 @@ export function modifySelection(
408
485
  d[attr] = d.target[attr];
409
486
  if (!exclusive) return;
410
487
  this.options["attribute-list"].forEach(function(type) {
411
- if (type != attr && d[attr] !== true) {
488
+ if (type != attr && d[attr] === true) {
412
489
  d[type] = false;
413
490
  d.target[type] = false;
414
491
  }
@@ -49,7 +49,10 @@ export function drawNode(container, node, transitions) {
49
49
  .merge(labels)
50
50
  .on("click", (event, d) => {
51
51
  this.emit('nodeClick', node, event);
52
- this.handle_node_click(node, event);
52
+ if (this.menuOpensOn("click")) this.openNodeMenu(node, event);
53
+ })
54
+ .on("contextmenu", (event) => {
55
+ if (this.menuOpensOn("contextmenu")) this.openNodeMenu(node, event);
53
56
  })
54
57
  .on("mouseenter", (event) => {
55
58
  this.emit('nodeHover', node, event);
@@ -109,7 +112,7 @@ export function drawNode(container, node, transitions) {
109
112
  };
110
113
 
111
114
  if (transitions) {
112
- tracers = tracers
115
+ tracers
113
116
  .enter()
114
117
  .append("line")
115
118
  .classed(this.css_classes["branch-tracer"], true)
@@ -164,7 +167,7 @@ export function drawNode(container, node, transitions) {
164
167
 
165
168
  if (this.shown_font_size >= 5) {
166
169
  const isRTL = this.options["layout"] == "right-to-left";
167
- labels = labels.attr("dx", d => {
170
+ labels.attr("dx", d => {
168
171
  const direction = (this.radial() || this.unrooted())
169
172
  ? (d.text_align == "end" ? -1 : 1)
170
173
  : (isRTL ? -1 : 1);
@@ -200,7 +203,10 @@ export function drawNode(container, node, transitions) {
200
203
  })
201
204
  .on("click", (event, d) => {
202
205
  this.emit('nodeClick', node, event);
203
- this.handle_node_click(node, event);
206
+ if (this.menuOpensOn("click")) this.openNodeMenu(node, event);
207
+ })
208
+ .on("contextmenu", (event) => {
209
+ if (this.menuOpensOn("contextmenu")) this.openNodeMenu(node, event);
204
210
  })
205
211
  .on("mouseenter", (event) => {
206
212
  this.emit('nodeHover', node, event);
@@ -336,7 +342,7 @@ export function nodeCssSelectors(css_classes) {
336
342
  css_classes["tagged-node"],
337
343
  css_classes["root-node"]
338
344
  ].reduce(function(p, c, i, a) {
339
- return (p += "g." + c + (i < a.length - 1 ? "," : ""));
345
+ return p + "g." + c + (i < a.length - 1 ? "," : "");
340
346
  }, "");
341
347
  }
342
348
 
@@ -364,14 +370,14 @@ export function internalLabel(callback, respect_existing) {
364
370
 
365
371
  export function defNodeLabel(_node) {
366
372
 
367
- _node = _node.data;
373
+ const data = _node.data;
368
374
 
369
- if (isLeafNode(_node)) {
370
- return _node.name || "";
375
+ if (isLeafNode(data)) {
376
+ return data.name || "";
371
377
  }
372
378
 
373
379
  if (this.showInternalName(_node)) {
374
- return _node.name;
380
+ return data.name || "";
375
381
  }
376
382
 
377
383
  return "";
@@ -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
  }