phylotree 1.1.0 → 1.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.
Files changed (41) hide show
  1. package/bin/phylotree-validate.js +33 -0
  2. package/bin/phylotree.js +1 -0
  3. package/dist/phylotree.js +120 -114
  4. package/dist/phylotree.min.js +1 -1
  5. package/package.json +4 -4
  6. package/src/branches.js +0 -155
  7. package/src/clustering/cluster-picker.js +0 -115
  8. package/src/clustering/phylopart.js +0 -238
  9. package/src/export.js +0 -25
  10. package/src/extract-dates.js +0 -69
  11. package/src/formats/beast.js +0 -29
  12. package/src/formats/newick.js +0 -272
  13. package/src/formats/nexml.js +0 -42
  14. package/src/formats/nexus.js +0 -96
  15. package/src/formats/phyloxml.js +0 -80
  16. package/src/formats/registry.js +0 -20
  17. package/src/index.js +0 -24
  18. package/src/main.js +0 -311
  19. package/src/max-parsimony.js +0 -81
  20. package/src/metrics/center-of-tree.js +0 -138
  21. package/src/metrics/compute-midpoint.js +0 -81
  22. package/src/metrics/pairwise-distances.js +0 -119
  23. package/src/metrics/root-to-tip.js +0 -220
  24. package/src/metrics/sackins.js +0 -21
  25. package/src/msa-parsers/fasta.js +0 -18
  26. package/src/neighbor-join.js +0 -196
  27. package/src/nodes.js +0 -241
  28. package/src/render/cartesian.js +0 -23
  29. package/src/render/clades.js +0 -116
  30. package/src/render/coordinates.js +0 -7
  31. package/src/render/draw.js +0 -1365
  32. package/src/render/edges.js +0 -134
  33. package/src/render/events.js +0 -166
  34. package/src/render/helpers.js +0 -7
  35. package/src/render/menus.js +0 -452
  36. package/src/render/nodes.js +0 -355
  37. package/src/render/options.js +0 -141
  38. package/src/render/radial.js +0 -79
  39. package/src/rooting.js +0 -183
  40. package/src/transformations.js +0 -20
  41. package/src/traversal.js +0 -114
@@ -1,196 +0,0 @@
1
- import * as _ from "lodash";
2
- import { default as phylotree } from "./main";
3
-
4
- function remove(i, D) {
5
- let dNew = [];
6
-
7
- for (let j of _.range(D.length)) {
8
- if (j != i) {
9
- let dNewRow = [];
10
- for (let k of _.range(D[j].length)) {
11
- if (k != i) {
12
- dNewRow.push(D[j][k]);
13
- }
14
- }
15
- dNew.push(dNewRow);
16
- }
17
- }
18
-
19
- return dNew;
20
- }
21
-
22
- function getDPrime(distanceMatrix, totalDistances, N) {
23
- let DPrime = _.chunk(_.fill(Array(N * N), 0), N);
24
- for (let i of _.range(N)) {
25
- for (let j of _.range(_.parseInt(i) + 1, N)) {
26
- DPrime[i][j] =
27
- (N - 2) * distanceMatrix[i][j] - totalDistances[i] - totalDistances[j];
28
- DPrime[j][i] = DPrime[i][j];
29
- }
30
- }
31
- return DPrime;
32
- }
33
-
34
- function ijMinDPrime(dPrime, N) {
35
- let i = -1;
36
- let j = -1;
37
- let minD = Infinity;
38
- for (let ii of _.range(N)) {
39
- for (let jj of _.range(i, N)) {
40
- if (dPrime[ii][jj] < minD) {
41
- i = ii;
42
- j = jj;
43
- minD = dPrime[i][j];
44
- }
45
- }
46
- }
47
- return [i, j, minD];
48
- }
49
-
50
- function createDelta(totalDistances, N) {
51
- let deltaMatrix = _.chunk(Array(N * N), N);
52
-
53
- for (let i of _.range(N)) {
54
- for (let j of _.range(parseInt(i) + 1, N)) {
55
- deltaMatrix[i][j] = (totalDistances[i] - totalDistances[j]) / (N - 2);
56
- deltaMatrix[j][i] = deltaMatrix[i][j];
57
- }
58
- }
59
-
60
- return deltaMatrix;
61
- }
62
-
63
- export function getDistanceMatrix(seqs) {
64
- // Gaps are masked
65
-
66
- let initKey = _.keys(seqs)[0];
67
- let seqLength = seqs[initKey].length;
68
-
69
- return _.mapValues(seqs, (seq) =>
70
- _.map(seqs, (seq2) =>
71
- _.sum(
72
- _.map(
73
- _.range(seqLength),
74
- (i) => seq[i] != seq2[i] && seq[i] != "-" && seq2[i] != "-"
75
- )
76
- )
77
- )
78
- );
79
- }
80
-
81
- function getTotalDistances(distanceMatrix) {
82
- return _.map(distanceMatrix, _.sum);
83
- }
84
-
85
- /**
86
- * Create a neighbor joining tree from a distance matrix
87
- * See test/neighbor-join-test.js for a working example
88
- *
89
- * @param {Array} distanceMatrixArr The NxN distance matrix.
90
- * const D = [
91
- * [0, 5, 9, 9, 8],
92
- * [5, 0, 10, 10, 9],
93
- * [9, 10, 0, 8, 7],
94
- * [9, 10, 8, 0, 3],
95
- * [8, 9, 7, 3, 0]
96
- * ];
97
- *
98
- * @param {Number} n The dimension of the distanceMatrixArr.
99
- * @param {Array} nodeList The names of each row in the distanceMatrix
100
- * @returns The neighbor joining new tree.
101
- */
102
- export default function neighborJoining(distanceMatrixArr, n, nodeList) {
103
- if (n <= 2) {
104
- let tree = new phylotree("");
105
-
106
- //T.link(nodeList[0],nodeList[1],D[0][1])
107
-
108
- let newNode = tree.getNodes();
109
- // Get root
110
- let distance = distanceMatrixArr[0][1] / 2;
111
-
112
- let nodeA = tree.createNode(nodeList[0], [null, [distance]]);
113
- let nodeB = tree.createNode(nodeList[1], [null, [distance]]);
114
-
115
- //// Add the children to the newly created node
116
- tree.addChild(newNode, nodeA);
117
- tree.addChild(newNode, nodeB);
118
-
119
- return tree;
120
- } else {
121
- let N = n;
122
- let totalDistances = getTotalDistances(distanceMatrixArr);
123
- let dPrime = getDPrime(distanceMatrixArr, totalDistances, N);
124
- let [i, j, minD] = ijMinDPrime(dPrime, N);
125
- let deltaMatrix = createDelta(totalDistances, N);
126
- let limbLengthI = (distanceMatrixArr[i][j] + deltaMatrix[i][j]) / 2;
127
- let limbLengthJ = (distanceMatrixArr[i][j] - deltaMatrix[i][j]) / 2;
128
- let newRow = _.concat(
129
- 0,
130
- _.filter(
131
- _.map(_.range(n), (k) => {
132
- if (k != i && k != j) {
133
- return (
134
- 0.5 *
135
- (distanceMatrixArr[k][i] +
136
- distanceMatrixArr[k][j] -
137
- distanceMatrixArr[i][j])
138
- );
139
- }
140
- }),
141
- _.isNumber
142
- )
143
- );
144
-
145
- let nodeI = nodeList[i];
146
- let nodeJ = nodeList[j];
147
-
148
- // Get all nodes of type InternalNode{x} and increment number.
149
- // If there are none, start with InternalNode0
150
- let m = "InternalNode0";
151
- let internalNodes = _.filter(nodeList, (x) =>
152
- _.includes(x, "InternalNode")
153
- );
154
-
155
- if (internalNodes.length) {
156
- let highestNum = _.max(
157
- _.map(internalNodes, (label) => _.split(label, "InternalNode")[1])
158
- );
159
- m = "InternalNode" + ++highestNum;
160
- }
161
-
162
- nodeList.unshift(m);
163
-
164
- distanceMatrixArr = remove(_.max([i, j]), distanceMatrixArr);
165
- distanceMatrixArr = remove(_.min([i, j]), distanceMatrixArr);
166
-
167
- distanceMatrixArr.unshift(newRow);
168
-
169
- _.each(_.range(1, n - 1), (l) => distanceMatrixArr[l].unshift(newRow[l]));
170
-
171
- // Remove from nodeList
172
- _.remove(nodeList, (n) => n == nodeI || n == nodeJ);
173
- let tree = neighborJoining(distanceMatrixArr, N - 1, nodeList);
174
-
175
- let treeNodeI = tree.createNode(nodeI, [null, [limbLengthI]]);
176
- let treeNodeJ = tree.createNode(nodeJ, [null, [limbLengthJ]]);
177
-
178
- // If the node doesn't exist, create. Otherwise, reassign the length
179
- if (tree.getNodeByName(m)) {
180
- let internalNode = tree.getNodeByName(m);
181
- tree.addChild(internalNode, treeNodeI);
182
- tree.addChild(internalNode, treeNodeJ);
183
- } else {
184
- let newNode = tree.createNode(m, [null, [0]]);
185
- tree.addChild(tree.getNodes(), newNode);
186
- // Add the children to the newly created node
187
- tree.addChild(newNode, treeNodeI);
188
- tree.addChild(newNode, treeNodeJ);
189
- }
190
-
191
- // Set negative to 0 and add distance to other limblength
192
- return tree;
193
- }
194
-
195
- return null;
196
- }
package/src/nodes.js DELETED
@@ -1,241 +0,0 @@
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
-
@@ -1,23 +0,0 @@
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;
@@ -1,116 +0,0 @@
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
- }
@@ -1,7 +0,0 @@
1
- export function xCoord(d) {
2
- return d.y;
3
- }
4
-
5
- export function yCoord(d) {
6
- return d.x;
7
- }