phylotree 1.3.1 → 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.
@@ -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.1",
3
+ "version": "1.4.0",
4
4
  "main": "dist/phylotree.js",
5
5
  "unpkg": "dist/phylotree.js",
6
6
  "jsdelivr": "dist/phylotree.js",