phylotree 1.0.20 → 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 (40) hide show
  1. package/bin/phylotree-neighbor-join.js +28 -0
  2. package/bin/phylotree-validate.js +33 -0
  3. package/bin/phylotree.js +1 -0
  4. package/dist/phylotree.js +268 -22
  5. package/dist/phylotree.min.js +1 -1
  6. package/package.json +4 -3
  7. package/src/branches.js +0 -156
  8. package/src/clustering/cluster-picker.js +0 -115
  9. package/src/clustering/phylopart.js +0 -238
  10. package/src/export.js +0 -25
  11. package/src/extract-dates.js +0 -69
  12. package/src/formats/beast.js +0 -29
  13. package/src/formats/newick.js +0 -271
  14. package/src/formats/nexml.js +0 -42
  15. package/src/formats/nexus.js +0 -96
  16. package/src/formats/phyloxml.js +0 -80
  17. package/src/formats/registry.js +0 -20
  18. package/src/index.js +0 -22
  19. package/src/main.js +0 -311
  20. package/src/max-parsimony.js +0 -81
  21. package/src/metrics/center-of-tree.js +0 -138
  22. package/src/metrics/compute-midpoint.js +0 -81
  23. package/src/metrics/pairwise-distances.js +0 -119
  24. package/src/metrics/root-to-tip.js +0 -220
  25. package/src/metrics/sackins.js +0 -21
  26. package/src/nodes.js +0 -215
  27. package/src/render/cartesian.js +0 -23
  28. package/src/render/clades.js +0 -116
  29. package/src/render/coordinates.js +0 -7
  30. package/src/render/draw.js +0 -1365
  31. package/src/render/edges.js +0 -134
  32. package/src/render/events.js +0 -166
  33. package/src/render/helpers.js +0 -7
  34. package/src/render/menus.js +0 -452
  35. package/src/render/nodes.js +0 -355
  36. package/src/render/options.js +0 -141
  37. package/src/render/radial.js +0 -79
  38. package/src/rooting.js +0 -183
  39. package/src/transformations.js +0 -20
  40. package/src/traversal.js +0 -114
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require("fs"),
4
+ phylotree = require("../dist/phylotree.js"),
5
+ commander = require("commander"),
6
+ _ = require("lodash");
7
+
8
+ commander
9
+ .arguments("<fasta>", "Input fasta file")
10
+ .usage("./test/data/CD2.fas")
11
+ .on("--help", function() {
12
+ console.log("\nConstructs neighbor-joining algorithm");
13
+ })
14
+ .parse(process.argv);
15
+
16
+
17
+ fs.readFile(commander.args[0], ( err, fastaData ) => {
18
+
19
+ let fasta = phylotree.parseFasta(fastaData);
20
+ let nodeList = _.keys(fasta);
21
+ let distanceMatrix = phylotree.getDistanceMatrix(fasta);
22
+ let distanceMatrixArr = _.values(distanceMatrix);
23
+
24
+ let nj = phylotree.neighborJoining(distanceMatrixArr, _.keys(fasta).length, nodeList)
25
+ console.log(nj.getNewick());
26
+
27
+ });
28
+
@@ -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
@@ -70,34 +70,36 @@
70
70
 
71
71
  // These methods are part of the Phylotree object
72
72
 
73
- function graftANode(graft_at, new_child, new_parent, lengths) {
73
+ function graftANode(graftAt, newChild, newParent, lengths) {
74
74
 
75
75
  let nodes = this.nodes.descendants();
76
76
 
77
- if (graft_at.parent) {
78
- let node_index = nodes.indexOf(graft_at);
77
+ if (graftAt.parent) {
79
78
 
80
- if (node_index >= 0) {
81
- let parent_index = graft_at.parent.children.indexOf(graft_at);
79
+ let nodeIndex = nodes.indexOf(graftAt);
82
80
 
83
- let new_split = {
84
- name: new_parent,
85
- parent: graft_at.parent,
81
+ if (nodeIndex >= 0) {
82
+
83
+ let parentIndex = graftAt.parent.children.indexOf(graftAt);
84
+
85
+ let newSplit = {
86
+ name: newParent,
87
+ parent: graftAt.parent,
86
88
  attribute: lengths ? lengths[2] : null,
87
- original_child_order: graft_at["original_child_order"]
89
+ original_child_order: graftAt["original_child_order"]
88
90
  },
89
- new_node = {
90
- name: new_child,
91
- parent: new_split,
91
+ newNode = {
92
+ name: newChild,
93
+ parent: newSplit,
92
94
  attribute: lengths ? lengths[1] : null,
93
95
  original_child_order: 2
94
96
  };
95
97
 
96
- new_split["children"] = [graft_at, new_node];
97
- graft_at["parent"].children[parent_index] = new_split;
98
- graft_at.parent = new_split;
99
- graft_at["attribute"] = lengths ? lengths[0] : null;
100
- graft_at["original_child_order"] = 1;
98
+ newSplit["children"] = [graftAt, newNode];
99
+ graftAt["parent"].children[parentIndex] = newSplit;
100
+ graftAt.parent = newSplit;
101
+ graftAt["attribute"] = lengths ? lengths[0] : null;
102
+ graftAt["original_child_order"] = 1;
101
103
  }
102
104
  }
103
105
 
@@ -105,6 +107,30 @@
105
107
 
106
108
  }
107
109
 
110
+ function addChild(parent, child) {
111
+
112
+ if(parent.children) {
113
+ parent.children.push(child);
114
+ } else {
115
+ parent["children"] = [child];
116
+ }
117
+
118
+ return parent;
119
+
120
+ }
121
+
122
+ function createNode(name, lengths) {
123
+
124
+ return {
125
+ data: {
126
+ name: name,
127
+ attribute: lengths ? lengths[1] : null
128
+ },
129
+ parent: '',
130
+ };
131
+
132
+ }
133
+
108
134
  /**
109
135
  * Delete a given node.
110
136
  *
@@ -283,6 +309,8 @@
283
309
  var node_operations = /*#__PURE__*/Object.freeze({
284
310
  __proto__: null,
285
311
  graftANode: graftANode,
312
+ addChild: addChild,
313
+ createNode: createNode,
286
314
  deleteANode: deleteANode,
287
315
  getTips: getTips,
288
316
  getInternals: getInternals,
@@ -1030,7 +1058,6 @@
1030
1058
  function defBranchLengthAccessor(_node, new_length) {
1031
1059
 
1032
1060
  let _node_data = _node.data;
1033
- //let _node_data = _node;
1034
1061
 
1035
1062
  if (
1036
1063
  "attribute" in _node_data &&
@@ -3217,8 +3244,8 @@
3217
3244
 
3218
3245
  var brush_object = d3__namespace
3219
3246
  .brush()
3220
- .on("brush", (event, d) => {
3221
- var extent = event.selection,
3247
+ .on("brush", d => {
3248
+ var extent = d3__namespace.event.selection,
3222
3249
  shown_links = this.links.filter(edgeVisible);
3223
3250
  var selected_links = shown_links
3224
3251
  .filter((d, i) => {
@@ -5021,9 +5048,9 @@
5021
5048
  const default_regexp = /([0-9]{4}).?([0-9]{2}).?([0-9]{2})$/g;
5022
5049
 
5023
5050
  const default_date_getter = function(node) {
5024
- if (isLeafNode(node)) {
5051
+ if (d3__namespace.layout.phylotree.isLeafNode(node)) {
5025
5052
  if ("name" in node) {
5026
- let location = default_regexp.exec(node.name);
5053
+ var location = default_regexp.exec(node.name);
5027
5054
  if (location) {
5028
5055
  return location[1] + location[2] + location[3];
5029
5056
  }
@@ -5397,16 +5424,235 @@
5397
5424
  return clusters;
5398
5425
  }
5399
5426
 
5427
+ function parseFasta(fastaData) {
5428
+
5429
+
5430
+ let sfasta = ___namespace$1.split(fastaData, "\n");
5431
+
5432
+ let seqs = ___namespace$1.chain(sfasta)
5433
+ .map((d, i) => (d.startsWith(">") ? i : -1))
5434
+ .filter((d) => d != -1)
5435
+ .map((d, i, c) => ___namespace$1.slice(sfasta, c[i], c[i + 1]))
5436
+ .keyBy((d) => ___namespace$1.trim(d[0], ">"))
5437
+ .mapValues((d) => ___namespace$1.tail(d).join(""))
5438
+ .value();
5439
+
5440
+ return seqs;
5441
+
5442
+ }
5443
+
5444
+ function remove(i,D) {
5445
+
5446
+
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
+
5463
+ }
5464
+
5465
+ function getDPrime(distanceMatrix, totalDistances, N) {
5466
+
5467
+ let DPrime = ___namespace$1.chunk(___namespace$1.fill(Array(N * N), 0), N);
5468
+ for (let i of ___namespace$1.range(N)) {
5469
+ for (let j of ___namespace$1.range(___namespace$1.parseInt(i) + 1, N)) {
5470
+ DPrime[i][j] =
5471
+ (N - 2) * distanceMatrix[i][j] -
5472
+ totalDistances[i] -
5473
+ totalDistances[j];
5474
+ DPrime[j][i] = DPrime[i][j];
5475
+ }
5476
+ }
5477
+ return DPrime;
5478
+ }
5479
+
5480
+ function ijMinDPrime(dPrime, N) {
5481
+ let i = -1;
5482
+ let j = -1;
5483
+ let minD=Infinity;
5484
+ for (let ii of ___namespace$1.range(N)) {
5485
+ for (let jj of ___namespace$1.range(i, N)) {
5486
+ if (dPrime[ii][jj] < minD) {
5487
+ i = ii;
5488
+ j = jj;
5489
+ minD = dPrime[i][j];
5490
+ }
5491
+ }
5492
+ }
5493
+ return [i, j, minD]
5494
+ }
5495
+
5496
+ function createDelta(totalDistances, N) {
5497
+
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
+ }
5506
+
5507
+ return deltaMatrix;
5508
+
5509
+ }
5510
+
5511
+
5512
+ function getDistanceMatrix(seqs) {
5513
+
5514
+ // Gaps are masked
5515
+
5516
+ let initKey = ___namespace$1.keys(seqs)[0];
5517
+ let seqLength = seqs[initKey].length;
5518
+
5519
+ return ___namespace$1.mapValues(seqs, (seq) =>
5520
+ ___namespace$1.map(seqs, (seq2) =>
5521
+ ___namespace$1.sum(
5522
+ ___namespace$1.map(
5523
+ ___namespace$1.range(seqLength),
5524
+ (i) => seq[i] != seq2[i] && seq[i] != "-" && seq2[i] != "-"
5525
+ )
5526
+ )
5527
+ )
5528
+ );
5529
+
5530
+ }
5531
+
5532
+ function getTotalDistances(distanceMatrix) {
5533
+ return ___namespace$1.map(distanceMatrix, ___namespace$1.sum)
5534
+ }
5535
+
5536
+
5537
+ /**
5538
+ * Create a neighbor joining tree from a distance matrix
5539
+ * See test/neighbor-join-test.js for a working example
5540
+ *
5541
+ * @param {Array} distanceMatrixArr The NxN distance matrix.
5542
+ * const D = [
5543
+ * [0, 5, 9, 9, 8],
5544
+ * [5, 0, 10, 10, 9],
5545
+ * [9, 10, 0, 8, 7],
5546
+ * [9, 10, 8, 0, 3],
5547
+ * [8, 9, 7, 3, 0]
5548
+ * ];
5549
+ *
5550
+ * @param {Number} n The dimension of the distanceMatrixArr.
5551
+ * @param {Array} nodeList The names of each row in the distanceMatrix
5552
+ * @returns The neighbor joining new tree.
5553
+ */
5554
+ function neighborJoining(distanceMatrixArr, n, nodeList) {
5555
+
5556
+ if(n<=2) {
5557
+
5558
+ let tree = new Phylotree("");
5559
+
5560
+ //T.link(nodeList[0],nodeList[1],D[0][1])
5561
+
5562
+ let newNode = tree.getNodes();
5563
+ // Get root
5564
+ let distance = distanceMatrixArr[0][1]/2;
5565
+
5566
+ let nodeA = tree.createNode(nodeList[0], [null, [distance]]);
5567
+ let nodeB = tree.createNode(nodeList[1], [null, [distance]]);
5568
+
5569
+ //// Add the children to the newly created node
5570
+ tree.addChild(newNode, nodeA);
5571
+ tree.addChild(newNode, nodeB);
5572
+
5573
+ return tree;
5574
+
5575
+ } else {
5576
+
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));
5589
+
5590
+ let nodeI = nodeList[i];
5591
+ let nodeJ = nodeList[j];
5592
+
5593
+
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'));
5598
+
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);
5604
+
5605
+ distanceMatrixArr = remove(___namespace$1.max([i,j]), distanceMatrixArr );
5606
+ distanceMatrixArr = remove(___namespace$1.min([i,j]), distanceMatrixArr );
5607
+
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
+ }
5638
+
5639
+ }
5640
+
5400
5641
  exports.centerOfTree = centerOfTree;
5401
5642
  exports.clusterPicker = clusterPicker;
5402
5643
  exports.computeMidpoint = computeMidpoint;
5403
5644
  exports.extract_dates = extract_dates;
5404
5645
  exports.fitRootToTip = fitRootToTip;
5646
+ exports.getDistanceMatrix = getDistanceMatrix;
5647
+ exports.getNewick = getNewick;
5405
5648
  exports.inOrder = inOrder;
5406
5649
  exports.leftChildRightSibling = leftChildRightSibling;
5407
5650
  exports.loadAnnotations = loadAnnotations;
5651
+ exports.neighborJoining = neighborJoining;
5652
+ exports.newickParser = newickParser;
5408
5653
  exports.pairwise_distances = computePairwiseDistances;
5409
5654
  exports.parseAnnotations = parseAnnotations;
5655
+ exports.parseFasta = parseFasta;
5410
5656
  exports.phylopart = phylopart;
5411
5657
  exports.phylotree = Phylotree;
5412
5658
  exports.postOrder = postOrder;