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
@@ -0,0 +1,33 @@
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
+
9
+ commander
10
+ .arguments("<newick>", "Input newick file")
11
+ .on("--help", function() {
12
+ console.log("");
13
+ console.log("Examples:");
14
+ console.log(
15
+ 'phylotree validate test/data/MERS.txt'
16
+ );
17
+ })
18
+ .parse(process.argv);
19
+
20
+ fs.readFile(commander.args[0], (err, newickData) => {
21
+
22
+ const tree = new phylotree.newickParser(newickData.toString());
23
+
24
+ if(!tree.error) {
25
+ console.log("Valid Newick string ");
26
+ } else {
27
+ console.warn("Newick string is not valid!")
28
+ console.log(tree.error);
29
+
30
+ }
31
+
32
+
33
+ });
package/bin/phylotree.js CHANGED
@@ -7,6 +7,7 @@ const { program } = require('commander');
7
7
  program
8
8
  .version('1.0.7')
9
9
  .command('tips', 'get tips from newick tree')
10
+ .command('validate', 'validate newick string')
10
11
  .command('reroot', 'reroot newick tree based on node')
11
12
  .command('distance', 'find distance between two nodes')
12
13
 
package/dist/phylotree.js CHANGED
@@ -251,9 +251,9 @@
251
251
  function assignAttributes(attributes) {
252
252
  //return nodes;
253
253
  // add annotations to each matching node
254
- ___namespace.each(this.nodes.descendants(), function(d) {
255
- if (d.data && (d.data.name in attributes)) {
256
- d["annotations"] = attributes[d.data.name];
254
+ ___namespace.each(this.nodes, function(d) {
255
+ if (___namespace.indexOf(___namespace.keys(attributes), d.name) >= 0) {
256
+ d["annotations"] = attributes[d.name];
257
257
  }
258
258
  });
259
259
  }
@@ -548,12 +548,11 @@
548
548
  *
549
549
  * @param {Function} annotator - Function to apply to each node, determining
550
550
  * what label is written (optional).
551
- * @param {Node} node - start at this node (default == root)
552
551
  * @returns {String} newick - Phylogenetic tree serialized as a Newick string.
553
552
  */
554
553
 
555
554
  // TODO: break this out into two seperate functions
556
- function getNewick(annotator, root) {
555
+ function getNewick(annotator) {
557
556
 
558
557
  let self = this;
559
558
 
@@ -585,7 +584,7 @@
585
584
 
586
585
  let element_array = [];
587
586
  annotator = annotator || "";
588
- nodeDisplay(root || this.nodes);
587
+ nodeDisplay(this.nodes);
589
588
 
590
589
  return element_array.join("")+";";
591
590
 
@@ -2614,7 +2613,7 @@
2614
2613
  .append("a")
2615
2614
  .attr("class", "dropdown-item")
2616
2615
  .attr("tabindex", "-1")
2617
- .text((d[0])(node)) // eslint-disable-line
2616
+ .text(constant(d[0])(node)) // eslint-disable-line
2618
2617
  .on("click", ___namespace.partial(d[1], node));
2619
2618
  });
2620
2619
  }
@@ -2876,7 +2875,7 @@
2876
2875
  });
2877
2876
 
2878
2877
  // replacement for d3.functor
2879
- function constant(x) {
2878
+ function constant$1(x) {
2880
2879
  return function() {
2881
2880
  return x;
2882
2881
  };
@@ -2953,7 +2952,7 @@
2953
2952
  "minimum-per-node-spacing": 2,
2954
2953
  "maximum-per-level-spacing": 100,
2955
2954
  "minimum-per-level-spacing": 10,
2956
- node_circle_size: constant(3),
2955
+ node_circle_size: constant$1(3),
2957
2956
  transitions: null,
2958
2957
  brush: true,
2959
2958
  reroot: true,
@@ -3245,8 +3244,8 @@
3245
3244
 
3246
3245
  var brush_object = d3__namespace
3247
3246
  .brush()
3248
- .on("brush", (event, d) => {
3249
- var extent = event.selection,
3247
+ .on("brush", d => {
3248
+ var extent = d3__namespace.event.selection,
3250
3249
  shown_links = this.links.filter(edgeVisible);
3251
3250
  var selected_links = shown_links
3252
3251
  .filter((d, i) => {
@@ -4041,7 +4040,7 @@
4041
4040
 
4042
4041
  node_circle_size(attr, attr2) {
4043
4042
  if (!arguments.length) return this.options["node_circle_size"];
4044
- this.options["node_circle_size"] = constant(attr === undefined ? 3 : attr);
4043
+ this.options["node_circle_size"] = constant$1(attr === undefined ? 3 : attr);
4045
4044
  return this;
4046
4045
  }
4047
4046
 
@@ -5049,9 +5048,9 @@
5049
5048
  const default_regexp = /([0-9]{4}).?([0-9]{2}).?([0-9]{2})$/g;
5050
5049
 
5051
5050
  const default_date_getter = function(node) {
5052
- if (isLeafNode(node)) {
5051
+ if (d3__namespace.layout.phylotree.isLeafNode(node)) {
5053
5052
  if ("name" in node) {
5054
- let location = default_regexp.exec(node.name);
5053
+ var location = default_regexp.exec(node.name);
5055
5054
  if (location) {
5056
5055
  return location[1] + location[2] + location[3];
5057
5056
  }
@@ -5442,30 +5441,36 @@
5442
5441
 
5443
5442
  }
5444
5443
 
5445
- function remove(i, D) {
5446
- let dNew = [];
5444
+ function remove(i,D) {
5447
5445
 
5448
- for (let j of ___namespace$1.range(D.length)) {
5449
- if (j != i) {
5450
- let dNewRow = [];
5451
- for (let k of ___namespace$1.range(D[j].length)) {
5452
- if (k != i) {
5453
- dNewRow.push(D[j][k]);
5454
- }
5455
- }
5456
- dNew.push(dNewRow);
5457
- }
5458
- }
5459
5446
 
5460
- return dNew;
5447
+ let dNew = [];
5448
+
5449
+ for (let j of ___namespace$1.range(D.length)) {
5450
+ if (j!=i) {
5451
+ let dNewRow = [];
5452
+ for (let k of ___namespace$1.range(D[j].length)) {
5453
+ if( k != i ) {
5454
+ dNewRow.push(D[j][k]);
5455
+ }
5456
+ }
5457
+ dNew.push(dNewRow);
5458
+ }
5459
+ }
5460
+
5461
+ return dNew
5462
+
5461
5463
  }
5462
5464
 
5463
5465
  function getDPrime(distanceMatrix, totalDistances, N) {
5466
+
5464
5467
  let DPrime = ___namespace$1.chunk(___namespace$1.fill(Array(N * N), 0), N);
5465
5468
  for (let i of ___namespace$1.range(N)) {
5466
5469
  for (let j of ___namespace$1.range(___namespace$1.parseInt(i) + 1, N)) {
5467
5470
  DPrime[i][j] =
5468
- (N - 2) * distanceMatrix[i][j] - totalDistances[i] - totalDistances[j];
5471
+ (N - 2) * distanceMatrix[i][j] -
5472
+ totalDistances[i] -
5473
+ totalDistances[j];
5469
5474
  DPrime[j][i] = DPrime[i][j];
5470
5475
  }
5471
5476
  }
@@ -5475,7 +5480,7 @@
5475
5480
  function ijMinDPrime(dPrime, N) {
5476
5481
  let i = -1;
5477
5482
  let j = -1;
5478
- let minD = Infinity;
5483
+ let minD=Infinity;
5479
5484
  for (let ii of ___namespace$1.range(N)) {
5480
5485
  for (let jj of ___namespace$1.range(i, N)) {
5481
5486
  if (dPrime[ii][jj] < minD) {
@@ -5485,23 +5490,27 @@
5485
5490
  }
5486
5491
  }
5487
5492
  }
5488
- return [i, j, minD];
5493
+ return [i, j, minD]
5489
5494
  }
5490
5495
 
5491
5496
  function createDelta(totalDistances, N) {
5492
- let deltaMatrix = ___namespace$1.chunk(Array(N * N), N);
5493
5497
 
5494
- for (let i of ___namespace$1.range(N)) {
5495
- for (let j of ___namespace$1.range(parseInt(i) + 1, N)) {
5496
- deltaMatrix[i][j] = (totalDistances[i] - totalDistances[j]) / (N - 2);
5497
- deltaMatrix[j][i] = deltaMatrix[i][j];
5498
- }
5499
- }
5498
+ let deltaMatrix = ___namespace$1.chunk(Array(N*N), N);
5499
+
5500
+ for (let i of ___namespace$1.range(N)) {
5501
+ for (let j of ___namespace$1.range(parseInt(i)+1,N)) {
5502
+ deltaMatrix[i][j] = (totalDistances[i]-totalDistances[j])/(N-2);
5503
+ deltaMatrix[j][i] = deltaMatrix[i][j];
5504
+ }
5505
+ }
5500
5506
 
5501
5507
  return deltaMatrix;
5508
+
5502
5509
  }
5503
5510
 
5511
+
5504
5512
  function getDistanceMatrix(seqs) {
5513
+
5505
5514
  // Gaps are masked
5506
5515
 
5507
5516
  let initKey = ___namespace$1.keys(seqs)[0];
@@ -5517,12 +5526,14 @@
5517
5526
  )
5518
5527
  )
5519
5528
  );
5529
+
5520
5530
  }
5521
5531
 
5522
5532
  function getTotalDistances(distanceMatrix) {
5523
- return ___namespace$1.map(distanceMatrix, ___namespace$1.sum);
5533
+ return ___namespace$1.map(distanceMatrix, ___namespace$1.sum)
5524
5534
  }
5525
5535
 
5536
+
5526
5537
  /**
5527
5538
  * Create a neighbor joining tree from a distance matrix
5528
5539
  * See test/neighbor-join-test.js for a working example
@@ -5541,97 +5552,90 @@
5541
5552
  * @returns The neighbor joining new tree.
5542
5553
  */
5543
5554
  function neighborJoining(distanceMatrixArr, n, nodeList) {
5544
- if (n <= 2) {
5545
- let tree = new Phylotree("");
5546
5555
 
5547
- //T.link(nodeList[0],nodeList[1],D[0][1])
5556
+ if(n<=2) {
5548
5557
 
5549
- let newNode = tree.getNodes();
5550
- // Get root
5551
- let distance = distanceMatrixArr[0][1] / 2;
5558
+ let tree = new Phylotree("");
5552
5559
 
5553
- let nodeA = tree.createNode(nodeList[0], [null, [distance]]);
5554
- let nodeB = tree.createNode(nodeList[1], [null, [distance]]);
5560
+ //T.link(nodeList[0],nodeList[1],D[0][1])
5555
5561
 
5556
- //// Add the children to the newly created node
5557
- tree.addChild(newNode, nodeA);
5558
- tree.addChild(newNode, nodeB);
5562
+ let newNode = tree.getNodes();
5563
+ // Get root
5564
+ let distance = distanceMatrixArr[0][1]/2;
5559
5565
 
5560
- return tree;
5561
- } else {
5562
- let N = n;
5563
- let totalDistances = getTotalDistances(distanceMatrixArr);
5564
- let dPrime = getDPrime(distanceMatrixArr, totalDistances, N);
5565
- let [i, j, minD] = ijMinDPrime(dPrime, N);
5566
- let deltaMatrix = createDelta(totalDistances, N);
5567
- let limbLengthI = (distanceMatrixArr[i][j] + deltaMatrix[i][j]) / 2;
5568
- let limbLengthJ = (distanceMatrixArr[i][j] - deltaMatrix[i][j]) / 2;
5569
- let newRow = ___namespace$1.concat(
5570
- 0,
5571
- ___namespace$1.filter(
5572
- ___namespace$1.map(___namespace$1.range(n), (k) => {
5573
- if (k != i && k != j) {
5574
- return (
5575
- 0.5 *
5576
- (distanceMatrixArr[k][i] +
5577
- distanceMatrixArr[k][j] -
5578
- distanceMatrixArr[i][j])
5579
- );
5580
- }
5581
- }),
5582
- ___namespace$1.isNumber
5583
- )
5584
- );
5566
+ let nodeA = tree.createNode(nodeList[0], [null, [distance]]);
5567
+ let nodeB = tree.createNode(nodeList[1], [null, [distance]]);
5585
5568
 
5586
- let nodeI = nodeList[i];
5587
- let nodeJ = nodeList[j];
5569
+ //// Add the children to the newly created node
5570
+ tree.addChild(newNode, nodeA);
5571
+ tree.addChild(newNode, nodeB);
5588
5572
 
5589
- // Get all nodes of type InternalNode{x} and increment number.
5590
- // If there are none, start with InternalNode0
5591
- let m = "InternalNode0";
5592
- let internalNodes = ___namespace$1.filter(nodeList, (x) =>
5593
- ___namespace$1.includes(x, "InternalNode")
5594
- );
5573
+ return tree;
5595
5574
 
5596
- if (internalNodes.length) {
5597
- let highestNum = ___namespace$1.max(
5598
- ___namespace$1.map(internalNodes, (label) => ___namespace$1.split(label, "InternalNode")[1])
5599
- );
5600
- m = "InternalNode" + ++highestNum;
5601
- }
5575
+ } else {
5602
5576
 
5603
- nodeList.unshift(m);
5577
+ let N = n;
5578
+ let totalDistances = getTotalDistances(distanceMatrixArr);
5579
+ let dPrime = getDPrime(distanceMatrixArr, totalDistances, N);
5580
+ let [i,j,minD] = ijMinDPrime(dPrime, N);
5581
+ let deltaMatrix = createDelta(totalDistances, N);
5582
+ let limbLengthI = (distanceMatrixArr[i][j] + deltaMatrix[i][j])/2;
5583
+ let limbLengthJ = (distanceMatrixArr[i][j] - deltaMatrix[i][j])/2;
5584
+ let newRow = ___namespace$1.concat(0,___namespace$1.filter(___namespace$1.map(___namespace$1.range(n), k => {
5585
+ if(k != i && k !=j) {
5586
+ return 0.5*(distanceMatrixArr[k][i]+distanceMatrixArr[k][j]-distanceMatrixArr[i][j])
5587
+ }
5588
+ }), ___namespace$1.isNumber));
5604
5589
 
5605
- distanceMatrixArr = remove(___namespace$1.max([i, j]), distanceMatrixArr);
5606
- distanceMatrixArr = remove(___namespace$1.min([i, j]), distanceMatrixArr);
5590
+ let nodeI = nodeList[i];
5591
+ let nodeJ = nodeList[j];
5607
5592
 
5608
- distanceMatrixArr.unshift(newRow);
5609
5593
 
5610
- ___namespace$1.each(___namespace$1.range(1, n - 1), (l) => distanceMatrixArr[l].unshift(newRow[l]));
5594
+ // Get all nodes of type InternalNode{x} and increment number.
5595
+ // If there are none, start with InternalNode0
5596
+ let m = "InternalNode0";
5597
+ let internalNodes = ___namespace$1.filter(nodeList, x => ___namespace$1.includes(x, 'InternalNode'));
5611
5598
 
5612
- // Remove from nodeList
5613
- ___namespace$1.remove(nodeList, (n) => n == nodeI || n == nodeJ);
5614
- let tree = neighborJoining(distanceMatrixArr, N - 1, nodeList);
5599
+ if(internalNodes.length) {
5600
+ let highestNum = ___namespace$1.max(___namespace$1.map(internalNodes, label => ___namespace$1.split(label, 'InternalNode')[1]));
5601
+ m = "InternalNode" + ++highestNum;
5602
+ }
5603
+ nodeList.unshift(m);
5615
5604
 
5616
- let treeNodeI = tree.createNode(nodeI, [null, [limbLengthI]]);
5617
- let treeNodeJ = tree.createNode(nodeJ, [null, [limbLengthJ]]);
5605
+ distanceMatrixArr = remove(___namespace$1.max([i,j]), distanceMatrixArr );
5606
+ distanceMatrixArr = remove(___namespace$1.min([i,j]), distanceMatrixArr );
5618
5607
 
5619
- // If the node doesn't exist, create. Otherwise, reassign the length
5620
- if (tree.getNodeByName(m)) {
5621
- let internalNode = tree.getNodeByName(m);
5622
- tree.addChild(internalNode, treeNodeI);
5623
- tree.addChild(internalNode, treeNodeJ);
5624
- } else {
5625
- let newNode = tree.createNode(m, [null, [0]]);
5626
- tree.addChild(tree.getNodes(), newNode);
5627
- // Add the children to the newly created node
5628
- tree.addChild(newNode, treeNodeI);
5629
- tree.addChild(newNode, treeNodeJ);
5630
- }
5608
+ distanceMatrixArr.unshift(newRow);
5609
+
5610
+ ___namespace$1.each(___namespace$1.range(1,n-1), l => distanceMatrixArr[l].unshift(newRow[l]));
5611
+
5612
+ // Remove from nodeList
5613
+ ___namespace$1.remove(nodeList, n => n == nodeI || n == nodeJ );
5614
+ let tree = neighborJoining(distanceMatrixArr, N-1, nodeList);
5615
+
5616
+ let treeNodeI = tree.createNode(nodeI, [null, [limbLengthI]]);
5617
+ let treeNodeJ = tree.createNode(nodeJ, [null, [limbLengthJ]]);
5618
+
5619
+ // If the node doesn't exist, create. Otherwise, reassign the length
5620
+ if(tree.getNodeByName(m)) {
5621
+ let internalNode = tree.getNodeByName(m);
5622
+ tree.addChild(internalNode, treeNodeI);
5623
+ tree.addChild(internalNode, treeNodeJ);
5624
+
5625
+ } else {
5626
+ let newNode = tree.createNode(m, [null, [0]]);
5627
+ tree.addChild(tree.getNodes(), newNode);
5628
+ // Add the children to the newly created node
5629
+ tree.addChild(newNode, treeNodeI);
5630
+ tree.addChild(newNode, treeNodeJ);
5631
+
5632
+ }
5633
+
5634
+ // Set negative to 0 and add distance to other limblength
5635
+ return tree;
5636
+
5637
+ }
5631
5638
 
5632
- // Set negative to 0 and add distance to other limblength
5633
- return tree;
5634
- }
5635
5639
  }
5636
5640
 
5637
5641
  exports.centerOfTree = centerOfTree;
@@ -5640,10 +5644,12 @@
5640
5644
  exports.extract_dates = extract_dates;
5641
5645
  exports.fitRootToTip = fitRootToTip;
5642
5646
  exports.getDistanceMatrix = getDistanceMatrix;
5647
+ exports.getNewick = getNewick;
5643
5648
  exports.inOrder = inOrder;
5644
5649
  exports.leftChildRightSibling = leftChildRightSibling;
5645
5650
  exports.loadAnnotations = loadAnnotations;
5646
5651
  exports.neighborJoining = neighborJoining;
5652
+ exports.newickParser = newickParser;
5647
5653
  exports.pairwise_distances = computePairwiseDistances;
5648
5654
  exports.parseAnnotations = parseAnnotations;
5649
5655
  exports.parseFasta = parseFasta;