phylotree 1.3.0 → 1.4.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.
@@ -10,42 +10,42 @@ commander
10
10
  .requiredOption("-s --source <node>", "Source Node")
11
11
  .requiredOption("-t --target <node>", "Target Node")
12
12
  .usage("./test/data/CD2-relax.new -s Cow -t Cat")
13
- .on("--help", function() {
14
- console.log("\nComputes the distance from the source tip to target tip. Does *not* include the length of the source tip by default.");
13
+ .on("--help", function () {
14
+ console.log(
15
+ "\nComputes the distance from the source tip to target tip. Does *not* include the length of the source tip by default."
16
+ );
15
17
  })
16
18
  .parse(process.argv);
17
19
 
18
20
  fs.readFile(commander.args[0], (err, newick_data) => {
19
-
20
21
  const tree = new phylotree.phylotree(newick_data.toString());
21
22
 
22
23
  const targetNode = tree.getNodeByName(commander.target);
23
24
 
24
25
  // GET SOURCE NODE
25
- const sourceNode = tree.getNodeByName(commander.source)
26
+ const sourceNode = tree.getNodeByName(commander.source);
26
27
 
27
- if(_.isUndefined(sourceNode)) {
28
- throw new Error('Could not find source node with name ' + commander.source)
28
+ if (_.isUndefined(sourceNode)) {
29
+ throw new Error("Could not find source node with name " + commander.source);
29
30
  }
30
31
 
31
- if(_.isUndefined(targetNode)) {
32
- throw new Error('Could not find target node with name ' + commander.target)
32
+ if (_.isUndefined(targetNode)) {
33
+ throw new Error("Could not find target node with name " + commander.target);
33
34
  }
34
35
 
35
36
  let mrca = tree.mrca([sourceNode, targetNode]);
36
37
 
37
38
  // Set all nodeLabel for all descendants of the MRCA
38
- let descendants = tree.selectAllDescendants(mrca, true, true)
39
+ let descendants = tree.selectAllDescendants(mrca, true, true);
39
40
  descendants.push(mrca);
40
41
 
41
- let annotator = function(node) {
42
- if(descendants.includes(node)) {
42
+ let annotator = function (node) {
43
+ if (descendants.includes(node)) {
43
44
  return "{hi}";
44
45
  } else {
45
46
  return "";
46
47
  }
47
- }
48
+ };
48
49
 
49
50
  console.log(tree.getNewick(annotator));
50
-
51
51
  });
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require("fs"),
4
+ phylotree = require("../dist/phylotree.js"),
5
+ commander = require("commander"),
6
+ _ = require("underscore");
7
+
8
+ commander
9
+ .arguments("<newick>", "Input newick file")
10
+ .on("--help", function() {
11
+ console.log("");
12
+ console.log("Examples:");
13
+ console.log(
14
+ 'phylotree shuffle test/data/MERS.txt'
15
+ );
16
+ })
17
+ .parse(process.argv);
18
+
19
+ fs.readFile(commander.args[0], (err, newickData) => {
20
+
21
+ const tree = new phylotree.phylotree(newickData.toString());
22
+ let tips = tree.getTips();
23
+
24
+ // Get all the names and randomly assign them
25
+ let names = _.map(tips, d => d.data.name);
26
+
27
+ // Shuffle names
28
+ let shuffledNames = _.shuffle(names);
29
+
30
+ _.each(tips, (d,i) => d.data.name = shuffledNames[i])
31
+
32
+ console.log(tree.getNewick());
33
+
34
+ });
package/bin/phylotree.js CHANGED
@@ -11,5 +11,6 @@ program
11
11
  .command('reroot', 'reroot newick tree based on node')
12
12
  .command('distance', 'find distance between two nodes')
13
13
  .command('mrca', 'tag all descendants of mrca')
14
+ .command('shuffle', 'shuffle tips in a tree')
14
15
 
15
16
  program.parse(process.argv);
package/dist/phylotree.js CHANGED
@@ -4259,7 +4259,7 @@
4259
4259
  function resortChildren(comparator, start_node, filter) {
4260
4260
  // ascending
4261
4261
  this.nodes
4262
- .sum(function(d) {
4262
+ .sum(function (d) {
4263
4263
  return d.value;
4264
4264
  })
4265
4265
  .sort(comparator);
@@ -4278,14 +4278,13 @@
4278
4278
  * @returns An array of strings, comprising each tag that was read.
4279
4279
  */
4280
4280
  function mrca(mrca_nodes) {
4281
-
4282
4281
  var mrca;
4283
4282
 
4284
- mrca_nodes = mrca_nodes.map(function(mrca_node) {
4283
+ mrca_nodes = mrca_nodes.map(function (mrca_node) {
4285
4284
  return typeof mrca_node == "string" ? mrca_node : mrca_node.data.name;
4286
4285
  });
4287
4286
 
4288
- this.traverse_and_compute(function(node) {
4287
+ this.traverse_and_compute(function (node) {
4289
4288
  if (!node.children) {
4290
4289
  node.data.mrca = ___namespace.intersection([node.data.name], mrca_nodes);
4291
4290
  } else if (!node.parent) {
@@ -4293,7 +4292,9 @@
4293
4292
  mrca = node;
4294
4293
  }
4295
4294
  } else {
4296
- node.data.mrca = ___namespace.union(...node.descendants().map(child => child.data.mrca));
4295
+ node.data.mrca = ___namespace.union(
4296
+ ...node.descendants().map((child) => child.data.mrca)
4297
+ );
4297
4298
  if (!mrca && node.data.mrca.length == mrca_nodes.length) {
4298
4299
  mrca = node;
4299
4300
  }
@@ -4301,7 +4302,6 @@
4301
4302
  });
4302
4303
 
4303
4304
  return mrca;
4304
-
4305
4305
  }
4306
4306
 
4307
4307
  /**
@@ -4315,9 +4315,7 @@
4315
4315
  * @returns {Phylotree} phylotree - itself, following the builder pattern.
4316
4316
  */
4317
4317
  let Phylotree = class {
4318
-
4319
4318
  constructor(nwk, options = {}) {
4320
-
4321
4319
  this.newick_string = "";
4322
4320
 
4323
4321
  this.nodes = [];
@@ -4371,40 +4369,36 @@
4371
4369
  this.newick_string = nwk;
4372
4370
  _node_data = newickParser(nwk, options);
4373
4371
  }
4374
-
4375
4372
  }
4376
4373
 
4377
4374
  if (!_node_data["json"]) {
4378
-
4379
4375
  self.nodes = [];
4380
-
4381
4376
  } else {
4382
-
4383
4377
  self.nodes = d3__namespace.hierarchy(_node_data.json);
4384
4378
 
4385
4379
  // Parse tags
4386
4380
  let _parsed_tags = {};
4387
4381
 
4388
- self.nodes.each(node => {
4382
+ self.nodes.each((node) => {
4389
4383
  if (node.data.annotation) {
4390
4384
  _parsed_tags[node.data.annotation] = true;
4391
4385
  }
4392
4386
  });
4393
4387
 
4394
4388
  self.parsed_tags = Object.keys(_parsed_tags);
4395
-
4396
4389
  }
4397
4390
 
4398
4391
  self.links = self.nodes.links();
4399
4392
 
4400
4393
  // If no branch lengths are supplied, set all to 1
4401
- if(!this.hasBranchLengths()) {
4402
- console.warn("Phylotree User Warning : NO BRANCH LENGTHS DETECTED, SETTING ALL LENGTHS TO 1");
4403
- this.setBranchLength(x => 1);
4394
+ if (!this.hasBranchLengths()) {
4395
+ console.warn(
4396
+ "Phylotree User Warning : NO BRANCH LENGTHS DETECTED, SETTING ALL LENGTHS TO 1"
4397
+ );
4398
+ this.setBranchLength((x) => 1);
4404
4399
  }
4405
4400
 
4406
4401
  return self;
4407
-
4408
4402
  }
4409
4403
 
4410
4404
  /*
@@ -4415,10 +4409,9 @@
4415
4409
 
4416
4410
  */
4417
4411
  json(traversal_type) {
4418
-
4419
4412
  var index = 0;
4420
4413
 
4421
- this.traverse_and_compute(function(n) {
4414
+ this.traverse_and_compute(function (n) {
4422
4415
  n.json_export_index = index++;
4423
4416
  }, traversal_type);
4424
4417
 
@@ -4426,7 +4419,7 @@
4426
4419
 
4427
4420
  index = 0;
4428
4421
 
4429
- this.traverse_and_compute(function(n) {
4422
+ this.traverse_and_compute(function (n) {
4430
4423
  let node_copy = ___namespace.clone(n);
4431
4424
  delete node_copy.json_export_index;
4432
4425
 
@@ -4435,14 +4428,14 @@
4435
4428
  }
4436
4429
 
4437
4430
  if (n.children) {
4438
- node_copy.children = ___namespace.map(n.children, function(c) {
4431
+ node_copy.children = ___namespace.map(n.children, function (c) {
4439
4432
  return c.json_export_index;
4440
4433
  });
4441
4434
  }
4442
4435
  node_array[index++] = node_copy;
4443
4436
  }, traversal_type);
4444
4437
 
4445
- this.traverse_and_compute(function(n) {
4438
+ this.traverse_and_compute(function (n) {
4446
4439
  delete n.json_export_index;
4447
4440
  }, traversal_type);
4448
4441
 
@@ -4468,7 +4461,6 @@
4468
4461
  }
4469
4462
 
4470
4463
  postOrder(node, callback, backtrack);
4471
-
4472
4464
  }
4473
4465
 
4474
4466
  function pre_order(node) {
@@ -4492,7 +4484,6 @@
4492
4484
  traversal_type(root_node ? root_node : this.nodes);
4493
4485
 
4494
4486
  return this;
4495
-
4496
4487
  }
4497
4488
 
4498
4489
  get_parsed_tags() {
@@ -4509,7 +4500,6 @@
4509
4500
  this.display = new TreeRender(this, options);
4510
4501
  return this.display;
4511
4502
  }
4512
-
4513
4503
  };
4514
4504
 
4515
4505
  Phylotree.prototype.isLeafNode = isLeafNode;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phylotree",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "main": "dist/phylotree.js",
5
5
  "unpkg": "dist/phylotree.js",
6
6
  "jsdelivr": "dist/phylotree.js",
package/src/main.js CHANGED
@@ -7,7 +7,12 @@ import { default as getTipLengths } from "./export";
7
7
  import * as nexus from "./formats/nexus";
8
8
  import { default as phyloxml_parser } from "./formats/phyloxml";
9
9
  import { default as maxParsimony } from "./max-parsimony";
10
- import { leftChildRightSibling, postOrder, preOrder, default as inOrder } from "./traversal";
10
+ import {
11
+ leftChildRightSibling,
12
+ postOrder,
13
+ preOrder,
14
+ default as inOrder,
15
+ } from "./traversal";
11
16
 
12
17
  import {
13
18
  default as hasBranchLengths,
@@ -16,7 +21,7 @@ import {
16
21
  setBranchLength,
17
22
  branchName,
18
23
  normalize,
19
- scale
24
+ scale,
20
25
  } from "./branches";
21
26
 
22
27
  import * as node_operations from "./nodes";
@@ -26,7 +31,7 @@ import { default as TreeRender } from "./render/draw";
26
31
  function resortChildren(comparator, start_node, filter) {
27
32
  // ascending
28
33
  this.nodes
29
- .sum(function(d) {
34
+ .sum(function (d) {
30
35
  return d.value;
31
36
  })
32
37
  .sort(comparator);
@@ -45,14 +50,13 @@ function resortChildren(comparator, start_node, filter) {
45
50
  * @returns An array of strings, comprising each tag that was read.
46
51
  */
47
52
  function mrca(mrca_nodes) {
48
-
49
53
  var mrca;
50
54
 
51
- mrca_nodes = mrca_nodes.map(function(mrca_node) {
55
+ mrca_nodes = mrca_nodes.map(function (mrca_node) {
52
56
  return typeof mrca_node == "string" ? mrca_node : mrca_node.data.name;
53
57
  });
54
58
 
55
- this.traverse_and_compute(function(node) {
59
+ this.traverse_and_compute(function (node) {
56
60
  if (!node.children) {
57
61
  node.data.mrca = _.intersection([node.data.name], mrca_nodes);
58
62
  } else if (!node.parent) {
@@ -60,7 +64,9 @@ function mrca(mrca_nodes) {
60
64
  mrca = node;
61
65
  }
62
66
  } else {
63
- node.data.mrca = _.union(...node.descendants().map(child => child.data.mrca));
67
+ node.data.mrca = _.union(
68
+ ...node.descendants().map((child) => child.data.mrca)
69
+ );
64
70
  if (!mrca && node.data.mrca.length == mrca_nodes.length) {
65
71
  mrca = node;
66
72
  }
@@ -68,7 +74,6 @@ function mrca(mrca_nodes) {
68
74
  });
69
75
 
70
76
  return mrca;
71
-
72
77
  }
73
78
 
74
79
  /**
@@ -82,9 +87,7 @@ function mrca(mrca_nodes) {
82
87
  * @returns {Phylotree} phylotree - itself, following the builder pattern.
83
88
  */
84
89
  let Phylotree = class {
85
-
86
90
  constructor(nwk, options = {}) {
87
-
88
91
  this.newick_string = "";
89
92
 
90
93
  this.nodes = [];
@@ -138,40 +141,36 @@ let Phylotree = class {
138
141
  this.newick_string = nwk;
139
142
  _node_data = newickParser(nwk, options);
140
143
  }
141
-
142
144
  }
143
145
 
144
146
  if (!_node_data["json"]) {
145
-
146
147
  self.nodes = [];
147
-
148
148
  } else {
149
-
150
149
  self.nodes = d3.hierarchy(_node_data.json);
151
150
 
152
151
  // Parse tags
153
152
  let _parsed_tags = {};
154
153
 
155
- self.nodes.each(node => {
154
+ self.nodes.each((node) => {
156
155
  if (node.data.annotation) {
157
156
  _parsed_tags[node.data.annotation] = true;
158
157
  }
159
158
  });
160
159
 
161
160
  self.parsed_tags = Object.keys(_parsed_tags);
162
-
163
161
  }
164
162
 
165
163
  self.links = self.nodes.links();
166
164
 
167
165
  // If no branch lengths are supplied, set all to 1
168
- if(!this.hasBranchLengths()) {
169
- console.warn("Phylotree User Warning : NO BRANCH LENGTHS DETECTED, SETTING ALL LENGTHS TO 1");
170
- this.setBranchLength(x => 1)
166
+ if (!this.hasBranchLengths()) {
167
+ console.warn(
168
+ "Phylotree User Warning : NO BRANCH LENGTHS DETECTED, SETTING ALL LENGTHS TO 1"
169
+ );
170
+ this.setBranchLength((x) => 1);
171
171
  }
172
172
 
173
173
  return self;
174
-
175
174
  }
176
175
 
177
176
  /*
@@ -182,10 +181,9 @@ let Phylotree = class {
182
181
 
183
182
  */
184
183
  json(traversal_type) {
185
-
186
184
  var index = 0;
187
185
 
188
- this.traverse_and_compute(function(n) {
186
+ this.traverse_and_compute(function (n) {
189
187
  n.json_export_index = index++;
190
188
  }, traversal_type);
191
189
 
@@ -193,7 +191,7 @@ let Phylotree = class {
193
191
 
194
192
  index = 0;
195
193
 
196
- this.traverse_and_compute(function(n) {
194
+ this.traverse_and_compute(function (n) {
197
195
  let node_copy = _.clone(n);
198
196
  delete node_copy.json_export_index;
199
197
 
@@ -202,14 +200,14 @@ let Phylotree = class {
202
200
  }
203
201
 
204
202
  if (n.children) {
205
- node_copy.children = _.map(n.children, function(c) {
203
+ node_copy.children = _.map(n.children, function (c) {
206
204
  return c.json_export_index;
207
205
  });
208
206
  }
209
207
  node_array[index++] = node_copy;
210
208
  }, traversal_type);
211
209
 
212
- this.traverse_and_compute(function(n) {
210
+ this.traverse_and_compute(function (n) {
213
211
  delete n.json_export_index;
214
212
  }, traversal_type);
215
213
 
@@ -235,7 +233,6 @@ let Phylotree = class {
235
233
  }
236
234
 
237
235
  postOrder(node, callback, backtrack);
238
-
239
236
  }
240
237
 
241
238
  function pre_order(node) {
@@ -259,7 +256,6 @@ let Phylotree = class {
259
256
  traversal_type(root_node ? root_node : this.nodes);
260
257
 
261
258
  return this;
262
-
263
259
  }
264
260
 
265
261
  get_parsed_tags() {
@@ -276,7 +272,6 @@ let Phylotree = class {
276
272
  this.display = new TreeRender(this, options);
277
273
  return this.display;
278
274
  }
279
-
280
275
  };
281
276
 
282
277
  Phylotree.prototype.isLeafNode = node_operations.isLeafNode;