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,96 +0,0 @@
1
- import * as _ from "underscore";
2
- import {default as newickParser} from "./newick";
3
-
4
- export function parseAnnotations (buf) {
5
-
6
- let str = buf;
7
- let index = str.toUpperCase().indexOf('BEGIN DATA;');
8
- let data = str.slice(index);
9
-
10
- if(data.length < 2) {
11
- return '';
12
- }
13
-
14
- index = data.toUpperCase().indexOf('END;');
15
- let data_str = data.slice(0, index);
16
-
17
- // split on semicolon
18
- data = _.map(data_str.split(';'), d => { return d.trim() } );
19
-
20
- // get dimensions
21
- let dimensions = _.filter(data, d => {return d.toUpperCase().startsWith('DIMENSION')});
22
- dimensions = dimensions[0].split(' ');
23
- dimensions = _.object(_.map(_.rest(dimensions), d => { return d.split('=') }));
24
-
25
- // get formats
26
- let format = _.filter(data, d => {return d.toUpperCase().startsWith('FORMAT')});
27
- format = format[0].split(' ');
28
- format = _.object(_.map(_.rest(format), d => { return d.split('=') }));
29
- format.symbols = _.reject(format.symbols.split(""), d => d=='"');
30
-
31
- // get character matrix
32
- let matrix = _.filter(data, d => {return d.toUpperCase().startsWith('MATRIX')});
33
- matrix = matrix[0].split('\n')
34
- matrix = _.object(_.map(_.rest(matrix), d=> { return _.compact(d.split(' ')) }));
35
-
36
- // create all possible states for matrix
37
- matrix = _.mapObject(matrix, (v,k) => {
38
-
39
- if(v == '?') {
40
- return format.symbols
41
- }
42
- else {
43
- return Array(v)
44
- }
45
-
46
- });
47
-
48
- return { 'dimensions' : dimensions, 'format' : format, 'matrix' : matrix }
49
-
50
- }
51
-
52
- /**
53
- * Loads annotations from a nexus-formatted buffer and annotates existing tree
54
- * nodes appropriately.
55
- *
56
- * @param {Object} tree - Instatiated phylotree
57
- * @param {String} NEXUS string
58
- * @returns {Object} Annotations
59
- */
60
- export function loadAnnotations(tree, label, annotations) {
61
-
62
- // if filename, then load from filesystem
63
- _.each(tree.getTips(), d => { d.data["test"] = annotations.matrix[d.data.name] });
64
-
65
- // decorate nodes with annotations
66
-
67
- }
68
-
69
- export default function loadTree(buf) {
70
-
71
- // if filename, then load from filesystem
72
- // Parse first tree from NEXUS file and send to newickParser
73
-
74
- // Make all upper case
75
- let str = buf;
76
-
77
- // Get TREE block
78
- let index = str.toUpperCase().indexOf('BEGIN TREES;');
79
- let split = str.slice(index);
80
-
81
- if(split.length < 2) {
82
- return '';
83
- }
84
-
85
- index = split.toUpperCase().indexOf('END;');
86
- let tree_str = split.slice(0, index);
87
-
88
- // Filter lines that start with TREE
89
- let trees = tree_str.split('\n');
90
- trees = _.filter(trees, d => { return d.trim().toUpperCase().startsWith('TREE') });
91
-
92
- // Identify start of newick string
93
- return newickParser(trees[0]);
94
-
95
- }
96
-
@@ -1,80 +0,0 @@
1
- // Changes XML to JSON
2
- // Modified version from here: http://davidwalsh.name/convert-xml-json
3
- function xmlToJson(xml) {
4
-
5
- // Create the return object
6
- var obj = {};
7
-
8
- if (xml.nodeType == 1) { // element
9
- // do attributes
10
- if (xml.attributes.length > 0) {
11
- obj["@attributes"] = {};
12
- for (var j = 0; j < xml.attributes.length; j++) {
13
- var attribute = xml.attributes.item(j);
14
- obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
15
- }
16
- }
17
- } else if (xml.nodeType == 3) { // text
18
- obj = xml.nodeValue;
19
- }
20
-
21
- // do children
22
- // If just one text node inside
23
- if (xml.hasChildNodes() && xml.childNodes.length === 1 && xml.childNodes[0].nodeType === 3) {
24
- obj = xml.childNodes[0].nodeValue;
25
- }
26
- else if (xml.hasChildNodes()) {
27
- for(var i = 0; i < xml.childNodes.length; i++) {
28
- var item = xml.childNodes.item(i);
29
- var nodeName = item.nodeName;
30
- if (typeof(obj[nodeName]) == "undefined") {
31
- obj[nodeName] = xmlToJson(item);
32
- } else {
33
- if (typeof(obj[nodeName].push) == "undefined") {
34
- var old = obj[nodeName];
35
- obj[nodeName] = [];
36
- obj[nodeName].push(old);
37
- }
38
- obj[nodeName].push(xmlToJson(item));
39
- }
40
- }
41
- }
42
- return obj;
43
- }
44
-
45
- var phyloxml_parser = function(xml, options) {
46
-
47
- function parsePhyloxml(node, index) {
48
- if (node.clade) {
49
- node.clade.forEach(parsePhyloxml);
50
- node.children = node.clade;
51
- delete node.clade;
52
- }
53
-
54
- node.annotation = 1;
55
- node.attribute = "0.01";
56
- if (node.branch_length) {
57
- node.attribute = node.branch_length;
58
- }
59
- if (node.taxonomy) {
60
- node.name = node.taxonomy.scientific_name;
61
- }
62
-
63
- node.annotation = "";
64
-
65
- }
66
-
67
- var tree_json;
68
-
69
- xml = xmlToJson(xml);
70
- tree_json = xml.phyloxml.phylogeny.clade;
71
- tree_json.name = "root";
72
- parsePhyloxml(tree_json, 0);
73
-
74
- return {
75
- json: tree_json,
76
- error: null
77
- };
78
- };
79
-
80
- export default phyloxml_parser;
@@ -1,20 +0,0 @@
1
- import { default as nexml_parser } from "./nexml";
2
- import { default as newickParser } from "./newick";
3
- import { default as nexus_parser } from "./nexus";
4
- import { default as phyloxml_parser } from "./phyloxml";
5
- import { default as beast_parser } from "./beast";
6
-
7
- /*
8
- * A parser must have two fields, the object and
9
- * options
10
- */
11
- var format_registry = {
12
- nexml: nexml_parser,
13
- phyloxml: phyloxml_parser,
14
- nexus : nexus_parser,
15
- nwk: newickParser,
16
- nhx: newickParser,
17
- beast: beast_parser
18
- };
19
-
20
- export default format_registry;
package/src/index.js DELETED
@@ -1,24 +0,0 @@
1
- export { default as phylotree } from "./main.js";
2
-
3
- export { default as pairwise_distances } from "./metrics/pairwise-distances";
4
- export { default as sackin } from "./metrics/sackins";
5
- export { centerOfTree } from "./metrics/center-of-tree";
6
- export { computeMidpoint } from "./metrics/compute-midpoint";
7
- export { default as rootToTip, fitRootToTip } from "./metrics/root-to-tip";
8
-
9
- export { default as extract_dates } from "./extract-dates";
10
- export { default as clusterPicker } from "./clustering/cluster-picker";
11
- export { default as phylopart } from "./clustering/phylopart";
12
-
13
- export { default as parseFasta } from "./msa-parsers/fasta";
14
- export { default as neighborJoining, getDistanceMatrix } from "./neighbor-join";
15
- export { parseAnnotations, loadAnnotations } from "./formats/nexus";
16
-
17
- export {
18
- leftChildRightSibling,
19
- postOrder,
20
- preOrder,
21
- default as inOrder
22
- } from "./traversal.js";
23
-
24
-
package/src/main.js DELETED
@@ -1,311 +0,0 @@
1
- import * as d3 from "d3";
2
- import * as _ from "underscore";
3
-
4
- import { default as parser_registry } from "./formats/registry";
5
- import { default as newickParser, getNewick } from "./formats/newick";
6
- import { default as getTipLengths } from "./export";
7
- import * as nexus from "./formats/nexus";
8
- import { default as phyloxml_parser } from "./formats/phyloxml";
9
- import { default as maxParsimony } from "./max-parsimony";
10
- import { leftChildRightSibling, postOrder, preOrder, default as inOrder } from "./traversal";
11
-
12
- import {
13
- default as hasBranchLengths,
14
- getBranchLengths,
15
- defBranchLengthAccessor,
16
- setBranchLength,
17
- branchName,
18
- normalize,
19
- scale
20
- } from "./branches";
21
-
22
- import * as node_operations from "./nodes";
23
- import * as rooting from "./rooting";
24
- import { default as TreeRender } from "./render/draw";
25
-
26
- function resortChildren(comparator, start_node, filter) {
27
- // ascending
28
- this.nodes
29
- .sum(function(d) {
30
- return d.value;
31
- })
32
- .sort(comparator);
33
-
34
- // if a tree is rendered in the DOM
35
- if (this.display) {
36
- this.display.update_layout(this.nodes);
37
- this.display.update();
38
- }
39
-
40
- return this;
41
- }
42
-
43
- /**
44
- * Return most recent common ancestor of a pair of nodes.
45
- * @returns An array of strings, comprising each tag that was read.
46
- */
47
- function mrca() {
48
-
49
- var mrca_nodes, mrca;
50
-
51
- if (arguments.length == 1) {
52
- mrca_nodes = arguments[0];
53
- } else {
54
- mrca_nodes = Array.from(arguments);
55
- }
56
-
57
- mrca_nodes = mrca_nodes.map(function(mrca_node) {
58
- return typeof mrca_node == "string" ? mrca_node : mrca_node.name;
59
- });
60
-
61
- this.traverse_and_compute(function(node) {
62
- if (!node.children) {
63
- node.mrca = _.intersection([node.name], mrca_nodes);
64
- } else if (!node.parent) {
65
- if (!mrca) {
66
- mrca = node;
67
- }
68
- } else {
69
- node.mrca = _.union(...node.descendants().map(child => child.mrca));
70
- if (!mrca && node.mrca.length == mrca_nodes.length) {
71
- mrca = node;
72
- }
73
- }
74
- });
75
-
76
- return mrca;
77
-
78
- }
79
-
80
- /**
81
- * An instance of a phylotree. Sets event listeners, parses tags, and creates links
82
- * that represent branches.
83
- *
84
- * @param {Object} nwk - A Newick string, PhyloXML string, or hierarchical JSON representation of a phylogenetic tree.
85
- * @param {Object} options
86
- * - boostrap_values
87
- * - type - format type
88
- * @returns {Phylotree} phylotree - itself, following the builder pattern.
89
- */
90
- let Phylotree = class {
91
-
92
- constructor(nwk, options = {}) {
93
-
94
- this.newick_string = "";
95
-
96
- this.nodes = [];
97
- this.links = [];
98
- this.parsed_tags = [];
99
- this.partitions = [];
100
- this.branch_length_accessor = defBranchLengthAccessor;
101
- this.branch_length = defBranchLengthAccessor;
102
- this.logger = options.logger || console;
103
- this.selection_attribute_name = "selected";
104
-
105
- // initialization
106
- var type = options.type || undefined,
107
- _node_data = [],
108
- self = this;
109
-
110
- // If the type is a string, check the parser_registry
111
- if (_.isString(type)) {
112
- if (type in parser_registry) {
113
- _node_data = parser_registry[type](nwk, options);
114
- } else {
115
- // Hard failure
116
- self.logger.error(
117
- "type " +
118
- type +
119
- " not in registry! Available types are " +
120
- _.keys(parser_registry)
121
- );
122
- }
123
- } else if (_.isFunction(type)) {
124
- // If the type is a function, try executing the function
125
- try {
126
- _node_data = type(nwk, options);
127
- } catch (e) {
128
- // Hard failure
129
- self.logger.error("Could not parse custom format!");
130
- }
131
- } else {
132
- // this builds children and links;
133
- if (nwk.name == "root") {
134
- // already parsed by phylotree.js
135
- _node_data = { json: nwk, error: null };
136
- } else if (typeof nwk != "string") {
137
- // old default
138
- _node_data = nwk;
139
- } else if (nwk.contentType == "application/xml") {
140
- // xml
141
- _node_data = phyloxml_parser(nwk);
142
- } else {
143
- // newick string
144
- this.newick_string = nwk;
145
- _node_data = newickParser(nwk, options);
146
- }
147
-
148
- }
149
-
150
- if (!_node_data["json"]) {
151
-
152
- self.nodes = [];
153
-
154
- } else {
155
-
156
- self.nodes = d3.hierarchy(_node_data.json);
157
-
158
- // Parse tags
159
- let _parsed_tags = {};
160
-
161
- self.nodes.each(node => {
162
- if (node.data.annotation) {
163
- _parsed_tags[node.data.annotation] = true;
164
- }
165
- });
166
-
167
- self.parsed_tags = Object.keys(_parsed_tags);
168
-
169
- }
170
-
171
- self.links = self.nodes.links();
172
-
173
- // If no branch lengths are supplied, set all to 1
174
- if(!this.hasBranchLengths()) {
175
- console.warn("Phylotree User Warning : NO BRANCH LENGTHS DETECTED, SETTING ALL LENGTHS TO 1");
176
- this.setBranchLength(x => 1)
177
- }
178
-
179
- return self;
180
-
181
- }
182
-
183
- /*
184
- Export the nodes of the tree with all local keys to JSON
185
- The return will be an array of nodes in the specified traversal_type
186
- ('post-order' : default, 'pre-order', or 'in-order')
187
- with parents and children referring to indices in that array
188
-
189
- */
190
- json(traversal_type) {
191
-
192
- var index = 0;
193
-
194
- this.traverse_and_compute(function(n) {
195
- n.json_export_index = index++;
196
- }, traversal_type);
197
-
198
- var node_array = new Array(index);
199
-
200
- index = 0;
201
-
202
- this.traverse_and_compute(function(n) {
203
- let node_copy = _.clone(n);
204
- delete node_copy.json_export_index;
205
-
206
- if (n.parent) {
207
- node_copy.parent = n.parent.json_export_index;
208
- }
209
-
210
- if (n.children) {
211
- node_copy.children = _.map(n.children, function(c) {
212
- return c.json_export_index;
213
- });
214
- }
215
- node_array[index++] = node_copy;
216
- }, traversal_type);
217
-
218
- this.traverse_and_compute(function(n) {
219
- delete n.json_export_index;
220
- }, traversal_type);
221
-
222
- return JSON.stringify(node_array);
223
- }
224
-
225
- /*
226
- * Traverse the tree in a prescribed order, and compute a value at each node.
227
- *
228
- * @param {Function} callback A function to be called on each node.
229
- * @param {String} traversal_type Either ``"pre-order"`` or ``"post-order"`` or ``"in-order"``.
230
- * @param {Node} root_node start traversal here, if provided, otherwise start at root
231
- * @param {Function} backtrack ; if provided, then at each node n, backtrack (n) will be called,
232
- and if it returns TRUE, traversal will NOT continue past into this
233
- node and its children
234
- */
235
- traverse_and_compute(callback, traversal_type, root_node, backtrack) {
236
- traversal_type = traversal_type || "post-order";
237
-
238
- function post_order(node) {
239
- if (_.isUndefined(node)) {
240
- return;
241
- }
242
-
243
- postOrder(node, callback, backtrack);
244
-
245
- }
246
-
247
- function pre_order(node) {
248
- preOrder(node, callback, backtrack);
249
- }
250
-
251
- function in_order(node) {
252
- inOrder(node, callback, backtrack);
253
- }
254
-
255
- if (traversal_type == "pre-order") {
256
- traversal_type = pre_order;
257
- } else {
258
- if (traversal_type == "in-order") {
259
- traversal_type = in_order;
260
- } else {
261
- traversal_type = post_order;
262
- }
263
- }
264
-
265
- traversal_type(root_node ? root_node : this.nodes);
266
-
267
- return this;
268
-
269
- }
270
-
271
- get_parsed_tags() {
272
- return this.parsed_tags;
273
- }
274
-
275
- update(json) {
276
- // update with new hiearchy layout
277
- this.nodes = json;
278
- }
279
-
280
- // Warning : Requires DOM!
281
- render(options) {
282
- this.display = new TreeRender(this, options);
283
- return this.display;
284
- }
285
-
286
- };
287
-
288
- Phylotree.prototype.isLeafNode = node_operations.isLeafNode;
289
- Phylotree.prototype.mrca = mrca;
290
- Phylotree.prototype.hasBranchLengths = hasBranchLengths;
291
- Phylotree.prototype.getBranchLengths = getBranchLengths;
292
- Phylotree.prototype.branchName = branchName;
293
- Phylotree.prototype.normalizeBranchLengths = normalize;
294
- Phylotree.prototype.scaleBranchLengths = scale;
295
- Phylotree.prototype.getNewick = getNewick;
296
- Phylotree.prototype.resortChildren = resortChildren;
297
- Phylotree.prototype.setBranchLength = setBranchLength;
298
- Phylotree.prototype.maxParsimony = maxParsimony;
299
-
300
- Phylotree.prototype.getTipLengths = getTipLengths;
301
- Phylotree.prototype.leftChildRightSibling = leftChildRightSibling;
302
-
303
- _.extend(Phylotree.prototype, node_operations);
304
- _.extend(Phylotree.prototype, rooting);
305
- _.extend(Phylotree.prototype, nexus);
306
-
307
- export function itemTagged(item) {
308
- return item.tag || false;
309
- }
310
-
311
- export default Phylotree;
@@ -1,81 +0,0 @@
1
- import * as _ from "underscore";
2
- import { isLeafNode } from "./nodes";
3
-
4
- export default function maxParsimony(respect_existing, attr_name) {
5
-
6
- function populateMpMatrix(attr_name, d) {
7
-
8
- d.mp = [
9
- [0, 0], // score for parent selected / not selected
10
- [false, false]
11
- ]; // selected or not
12
-
13
- if (isLeafNode(d)) {
14
-
15
- d.mp[1][0] = d.mp[1][1] = d[attr_name] || false;
16
- d.mp[0][0] = d.mp[1][0] ? 1 : 0;
17
- d.mp[0][1] = 1 - d.mp[0][0];
18
-
19
- } else {
20
-
21
- d.children.forEach(pop_mp_mat);
22
-
23
- var s0 = d.children.reduce(function(p, n) {
24
- return n.mp[0][0] + p;
25
- }, 0);
26
-
27
- // cumulative children score if this node is 0
28
- var s1 = d.children.reduce(function(p, n) {
29
- return n.mp[0][1] + p;
30
- }, 0);
31
-
32
- // cumulative children score if this node is 1
33
- // parent = 0
34
-
35
- if (d[attr_name]) {
36
- // respect selected
37
- d.mp[0][0] = s1 + 1;
38
- d.mp[1][0] = true;
39
- d.mp[0][1] = s1;
40
- d.mp[1][1] = true;
41
- } else {
42
- if (s0 < s1 + 1) {
43
- d.mp[0][0] = s0;
44
- d.mp[1][0] = false;
45
- } else {
46
- d.mp[0][0] = s1 + 1;
47
- d.mp[1][0] = true;
48
- }
49
-
50
- // parent = 1
51
-
52
- if (s1 < s0 + 1) {
53
- d.mp[0][1] = s1;
54
- d.mp[1][1] = true;
55
- } else {
56
- d.mp[0][1] = s0 + 1;
57
- d.mp[1][1] = false;
58
- }
59
- }
60
- }
61
- }
62
-
63
- const pop_mp_mat = _.partial(populateMpMatrix, attr_name);
64
- pop_mp_mat(this.nodes);
65
-
66
- this.nodes.each(d => {
67
- if (d.parent) {
68
- d.mp = d.mp[1][d.parent.mp ? 1 : 0];
69
- } else {
70
- d.mp = d.mp[1][d.mp[0][0] < d.mp[0][1] ? 0 : 1];
71
- }
72
- });
73
-
74
- this.display.modifySelection((d, callback) => {
75
- if (isLeafNode(d.target)) {
76
- return d.target[attr_name];
77
- }
78
- return d.target.mp;
79
- });
80
-
81
- }