phylotree 1.2.0 → 1.2.2

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/src/nodes.js ADDED
@@ -0,0 +1,241 @@
1
+ import * as _ from "underscore";
2
+
3
+ // These methods are part of the Phylotree object
4
+
5
+ export function graftANode(graftAt, newChild, newParent, lengths) {
6
+
7
+ let nodes = this.nodes.descendants();
8
+
9
+ if (graftAt.parent) {
10
+
11
+ let nodeIndex = nodes.indexOf(graftAt);
12
+
13
+ if (nodeIndex >= 0) {
14
+
15
+ let parentIndex = graftAt.parent.children.indexOf(graftAt);
16
+
17
+ let newSplit = {
18
+ name: newParent,
19
+ parent: graftAt.parent,
20
+ attribute: lengths ? lengths[2] : null,
21
+ original_child_order: graftAt["original_child_order"]
22
+ },
23
+ newNode = {
24
+ name: newChild,
25
+ parent: newSplit,
26
+ attribute: lengths ? lengths[1] : null,
27
+ original_child_order: 2
28
+ };
29
+
30
+ newSplit["children"] = [graftAt, newNode];
31
+ graftAt["parent"].children[parentIndex] = newSplit;
32
+ graftAt.parent = newSplit;
33
+ graftAt["attribute"] = lengths ? lengths[0] : null;
34
+ graftAt["original_child_order"] = 1;
35
+ }
36
+ }
37
+
38
+ return this;
39
+
40
+ }
41
+
42
+ export function addChild(parent, child) {
43
+
44
+ if(parent.children) {
45
+ parent.children.push(child);
46
+ } else {
47
+ parent["children"] = [child];
48
+ }
49
+
50
+ return parent;
51
+
52
+ }
53
+
54
+ export function createNode(name, lengths) {
55
+
56
+ return {
57
+ data: {
58
+ name: name,
59
+ attribute: lengths ? lengths[1] : null
60
+ },
61
+ parent: '',
62
+ };
63
+
64
+ }
65
+
66
+ /**
67
+ * Delete a given node.
68
+ *
69
+ * @param {Node} The node to be deleted, or the index of such a node.
70
+ * @returns The current ``phylotree``.
71
+ */
72
+ export function deleteANode(index) {
73
+ let nodes = this.nodes.descendants();
74
+
75
+ if (typeof index != "number") {
76
+ return this.deleteANode(nodes.indexOf(index));
77
+ }
78
+
79
+ if (index > 0 && index < nodes.length) {
80
+ let node = nodes[index];
81
+
82
+ if (node.parent) {
83
+ // can only delete nodes that are not the root
84
+ let delete_me_idx = node.parent.children.indexOf(node);
85
+
86
+ if (delete_me_idx >= 0) {
87
+ nodes.splice(index, 1);
88
+
89
+ if (node.children) {
90
+ node.children.forEach(function(d) {
91
+ d["original_child_order"] = node.parent.children.length;
92
+ node.parent.children.push(d);
93
+ d.parent = node.parent;
94
+ });
95
+ }
96
+
97
+ if (node.parent.children.length > 2) {
98
+ node.parent.children.splice(delete_me_idx, 1);
99
+ } else {
100
+ if (node.parent.parent) {
101
+ node.parent.parent.children[
102
+ node.parent.parent.children.indexOf(node.parent)
103
+ ] =
104
+ node.parent.children[1 - delete_me_idx];
105
+ node.parent.children[1 - delete_me_idx].parent = node.parent.parent;
106
+ nodes.splice(nodes.indexOf(node.parent), 1);
107
+ } else {
108
+ nodes.splice(0, 1);
109
+ nodes.parent = null;
110
+ delete nodes.data["attribute"];
111
+ delete nodes.data["annotation"];
112
+ delete nodes.data["original_child_order"];
113
+ nodes.name = "root";
114
+ nodes.data.name = "root";
115
+ }
116
+ }
117
+ }
118
+ }
119
+ }
120
+
121
+ return this;
122
+ }
123
+
124
+ /**
125
+ * Get the tips of the tree
126
+ * @returns {Array} Nodes in the current ``phylotree``.
127
+ */
128
+ export function getTips() {
129
+ // get all nodes that have no nodes
130
+ return _.filter(this.nodes.descendants(), n => {
131
+ return !_.has(n, "children");
132
+ });
133
+ }
134
+
135
+ /**
136
+ * Get the internal nodes of the tree
137
+ * @returns {Array} Nodes in the current ``phylotree``.
138
+ */
139
+ export function getInternals() {
140
+ // get all nodes that have no nodes
141
+ return _.filter(this.nodes.descendants(), n => {
142
+ return _.has(n, "children");
143
+ });
144
+ }
145
+
146
+
147
+ /**
148
+ * Get the root node.
149
+ *
150
+ * @returns the current root node of the ``phylotree``.
151
+ */
152
+ export function getRootNode() {
153
+ return this.nodes;
154
+ }
155
+
156
+ /**
157
+ * Get an array of all nodes.
158
+ * @returns {Array} Nodes in the current ``phylotree``.
159
+ */
160
+ export function getNodes() {
161
+ return this.nodes;
162
+ }
163
+
164
+ /**
165
+ * Get a node by name.
166
+ *
167
+ * @param {String} name Name of the desired node.
168
+ * @returns {Node} Desired node.
169
+ */
170
+ export function getNodeByName(name) {
171
+ return _.filter(this.nodes.descendants(), d => {
172
+ return d.data.name == name;
173
+ })[0];
174
+ }
175
+
176
+ /**
177
+ * Add attributes to nodes. New attributes will be placed in the
178
+ * ``annotations`` key of any nodes that are matched.
179
+ *
180
+ * @param {Object} attributes An object whose keys are the names of nodes
181
+ * to modify, and whose values are the new attributes to add.
182
+ */
183
+ export function assignAttributes(attributes) {
184
+ //return nodes;
185
+ // add annotations to each matching node
186
+ _.each(this.nodes.descendants(), function(d) {
187
+ if (d.data && (d.data.name in attributes)) {
188
+ d["annotations"] = attributes[d.data.name];
189
+ }
190
+ });
191
+ }
192
+
193
+ /**
194
+ * Determine if a given node is a leaf node.
195
+ *
196
+ * @param {Node} A node in a tree.
197
+ * @returns {Bool} Whether or not the node is a leaf node.
198
+ */
199
+ export function isLeafNode(node) {
200
+ return !_.has(node, "children")
201
+ }
202
+
203
+ /**
204
+ * Update a given key name in each node.
205
+ *
206
+ * @param {String} old_key The old key name.
207
+ * @param {String} new_key The new key name.
208
+ * @returns The current ``this``.
209
+ */
210
+ export function updateKeyName(old_key, new_key) {
211
+ this.nodes.each(function(n) {
212
+ if (old_key in n) {
213
+ if (new_key) {
214
+ n[new_key] = n[old_key];
215
+ }
216
+ delete n[old_key];
217
+ }
218
+ });
219
+
220
+ return this;
221
+ }
222
+
223
+ export function clearInternalNodes(respect) {
224
+ if (!respect) {
225
+ this.nodes.each(d => {
226
+ if (!isLeafNode(d)) {
227
+
228
+ // TODO: Move away from storing attribute data as root (BREAKS occasionally with d3>3)
229
+ d[this.selection_attribute_name] = false;
230
+
231
+ if(!d.data.traits) {
232
+ d.data.traits = {};
233
+ }
234
+ d.data.traits[this.selection_attribute_name] = d[this.selection_attribute_name];
235
+
236
+ }
237
+ });
238
+ }
239
+ }
240
+
241
+
@@ -0,0 +1,23 @@
1
+ import * as d3 from "d3";
2
+ import { xCoord, yCoord } from "./coordinates";
3
+
4
+ export var draw_line = d3
5
+ .line()
6
+ .x(function(d) {
7
+ return xCoord(d);
8
+ })
9
+ .y(function(d) {
10
+ return yCoord(d);
11
+ })
12
+ .curve(d3.curveStepBefore);
13
+
14
+ export function lineSegmentPlacer(edge, where) {
15
+ return {
16
+ x:
17
+ xCoord(edge.target) +
18
+ (xCoord(edge.source) - xCoord(edge.target)) * where,
19
+ y: yCoord(edge.target)
20
+ };
21
+ }
22
+
23
+ export default draw_line;
@@ -0,0 +1,116 @@
1
+ import * as d3 from "d3";
2
+ import * as _ from "underscore";
3
+ import { isNodeCollapsed } from "./nodes";
4
+
5
+ export function cladeCssSelectors(css_classes) {
6
+ return [css_classes["clade"]].reduce(function(p, c, i, a) {
7
+ return (p += "path." + c + (i < a.length - 1 ? "," : ""));
8
+ }, "");
9
+ }
10
+
11
+ export function updateCollapsedClades(transitions) {
12
+
13
+ let enclosure = this.svg.selectAll("." + this.css_classes["tree-container"]);
14
+ var node_id = 0;
15
+
16
+ let collapsed_clades = enclosure
17
+ .selectAll(cladeCssSelectors(this.css_classes))
18
+ .data(
19
+ this.phylotree.nodes.descendants().filter(isNodeCollapsed),
20
+ function(d) {
21
+ return d.id || (d.id = ++node_id);
22
+ }
23
+ );
24
+
25
+ let spline = function() {};
26
+ let spline_f = _.noop();
27
+
28
+ // Collapse radial differently
29
+ if (this.radial()) {
30
+ spline = d3
31
+ .line()
32
+ .curve(d3.curveBasis)
33
+ .y(function(d) {
34
+ return d[0];
35
+ })
36
+ .x(function(d) {
37
+ return d[1];
38
+ });
39
+
40
+ spline_f = function(coord, i, d, init_0, init_1) {
41
+ if (i) {
42
+ return [
43
+ d.screen_y + (coord[0] - init_0) / 50,
44
+ d.screen_x + (coord[1] - init_1) / 50
45
+ ];
46
+ } else {
47
+ return [d.screen_y, d.screen_x];
48
+ }
49
+ };
50
+ } else {
51
+ spline = d3
52
+ .line()
53
+ .y(function(d) {
54
+ return d[0];
55
+ })
56
+ .x(function(d) {
57
+ return d[1];
58
+ }).curve(d3.curveBasis);
59
+
60
+ spline_f = function(coord, i, d, init_0, init_1) {
61
+ if (i) {
62
+ return [
63
+ d.screen_y + (coord[0] - init_0) / 50 ,
64
+ d.screen_x + (coord[1] - init_1) / 50,
65
+ ];
66
+ } else {
67
+ return [d.screen_y, d.screen_x];
68
+ }
69
+ };
70
+ }
71
+
72
+ collapsed_clades
73
+ .exit()
74
+ .each(function(d) {
75
+ d.collapsed_clade = null;
76
+ })
77
+ .remove();
78
+
79
+ if (transitions) {
80
+ collapsed_clades
81
+ .enter()
82
+ .insert("path", ":first-child")
83
+ .attr("class", this.css_classes["clade"])
84
+ .merge(collapsed_clades)
85
+ .attr("d", function(d) {
86
+ if (d.collapsed_clade) {
87
+ return d.collapsed_clade;
88
+ }
89
+
90
+ //console.log (d.collapsed);
91
+ let init_0 = d.collapsed[0][0];
92
+ let init_1 = d.collapsed[0][1];
93
+
94
+
95
+
96
+ // #1 return spline(d.collapsed.map(spline_f, d, init_0, init_1));
97
+ return spline(
98
+ d.collapsed.map(function(coord, i) {
99
+ return spline_f(coord, i, d, init_0, init_1);
100
+ })
101
+ );
102
+ })
103
+ .attr("d", function(d) {
104
+ return (d.collapsed_clade = spline(d.collapsed));
105
+ });
106
+ } else {
107
+ collapsed_clades
108
+ .enter()
109
+ .insert("path", ":first-child")
110
+ .attr("class", this.css_classes["clade"])
111
+ .merge(collapsed_clades)
112
+ .attr("d", function(d) {
113
+ return (d.collapsed_clade ? d.collapsed_clade : d.collapsed_clade = spline(d.collapsed));
114
+ });
115
+ }
116
+ }
@@ -0,0 +1,7 @@
1
+ export function xCoord(d) {
2
+ return d.y;
3
+ }
4
+
5
+ export function yCoord(d) {
6
+ return d.x;
7
+ }