phylotree 2.0.1 → 2.1.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.
File without changes
File without changes
File without changes
@@ -0,0 +1,131 @@
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require("fs");
4
+ const phylotree = require("../dist/phylotree.js");
5
+ const commander = require("commander");
6
+ const _ = require("underscore");
7
+
8
+ // Parse command line arguments
9
+ commander
10
+ .arguments("<newick>", "Input newick file")
11
+ .requiredOption(
12
+ "-p --patterns <patterns>",
13
+ "Comma-separated regex patterns for tagging",
14
+ )
15
+ .requiredOption(
16
+ "-t --tags <tags>",
17
+ "Comma-separated tags that correspond to each pattern",
18
+ )
19
+ .option("-r --reroot <node>", "Node to reroot on (optional)")
20
+ .option("-i --invert", "Invert the selection", false)
21
+ .option("-l --leaf-only", "Only label leaf nodes", false)
22
+ .option(
23
+ "-s --strategy <strategy>",
24
+ "Strategy for labeling internal nodes: none, all, some, parsimony",
25
+ "all",
26
+ )
27
+ .usage("./test/data/tree.nwk -p 'regex1,regex2' -t 'tag1,tag2'")
28
+ .description(
29
+ "Tag branches in a Newick tree based on regex patterns and labeling strategy",
30
+ )
31
+ .parse(process.argv);
32
+
33
+ // Input validation
34
+ if (commander.patterns.split(",").length !== commander.tags.split(",").length) {
35
+ throw new Error(
36
+ "The number of patterns must match the number of corresponding tags.",
37
+ );
38
+ }
39
+
40
+ // Parse patterns and tags
41
+ const patterns = commander.patterns.split(",");
42
+ const tags = commander.tags.split(",");
43
+
44
+ // Function to get annotations for Newick output
45
+ function annotator(node) {
46
+ return node.data.annotation ? `{${node.data.annotation}}` : "";
47
+ }
48
+
49
+ // Main tagging function
50
+ fs.readFile(commander.args[0], (err, newick_data) => {
51
+ if (err) throw new Error("Error reading Newick file: " + err.message);
52
+
53
+ // Parse tree
54
+ const tree = new phylotree.phylotree(newick_data.toString());
55
+
56
+ // Reroot if specified
57
+ if (commander.reroot) {
58
+ const reroot_node = tree.getNodeByName(commander.reroot);
59
+ if (reroot_node) {
60
+ tree.reroot(reroot_node);
61
+ }
62
+ }
63
+
64
+ // Initial leaf node tagging
65
+ function tagLeafNodes(node) {
66
+ if (tree.isLeafNode(node)) {
67
+ for (let i = 0; i < patterns.length; i++) {
68
+ const matches = new RegExp(patterns[i]).test(node.data.name);
69
+ if (commander.invert ? !matches : matches) {
70
+ node.data.annotation = tags[i];
71
+ break;
72
+ }
73
+ }
74
+ }
75
+ }
76
+
77
+ function applyStrategy() {
78
+ switch (commander.strategy) {
79
+ case "none":
80
+ // Only leaf nodes are tagged
81
+ break;
82
+
83
+ case "all":
84
+ // Tag internal node if all descendants have the same non-empty tag
85
+ tree.traverse_and_compute((node) => {
86
+ if (!tree.isLeafNode(node)) {
87
+ const descendantTags = _.uniq(
88
+ node.descendants().map((d) => d.data.annotation), // Get all annotations, including empty ones
89
+ ).filter((tag) => tag !== ""); // Filter out empty annotations
90
+
91
+ if (descendantTags.length === 1) {
92
+ node.data.annotation = descendantTags[0];
93
+ }
94
+ }
95
+ });
96
+ break;
97
+
98
+ case "some":
99
+ // Tag internal node if any descendants have a non-empty tag
100
+ tree.traverse_and_compute((node) => {
101
+ if (!tree.isLeafNode(node)) {
102
+ const descendantTags = node
103
+ .descendants()
104
+ .map((d) => d.data.annotation) // Get all annotations
105
+ .filter((tag) => tag !== ""); // Filter out empty annotations
106
+
107
+ if (descendantTags.length > 0) {
108
+ node.data.annotation = descendantTags[0];
109
+ }
110
+ }
111
+ });
112
+ break;
113
+
114
+ case "parsimony":
115
+ // Use maxParsimony to tag internal nodes
116
+ tree.maxParsimony("annotation");
117
+ break;
118
+ }
119
+ }
120
+
121
+ // Apply tagging
122
+ if (!commander.leafOnly) {
123
+ tree.traverse_and_compute(tagLeafNodes);
124
+ applyStrategy();
125
+ } else {
126
+ tree.traverse_and_compute(tagLeafNodes);
127
+ }
128
+
129
+ // Output tagged tree
130
+ console.log(tree.getNewick(annotator));
131
+ });
@@ -1,27 +1,64 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const fs = require("fs"),
4
- phylotree = require("../dist/phylotree.js"),
5
- commander = require("commander"),
6
- _ = require("underscore");
3
+ const fs = require("fs");
4
+ const phylotree = require("../dist/phylotree.js");
5
+ const commander = require("commander");
6
+ const _ = require("underscore");
7
7
 
8
8
  commander
9
9
  .arguments("<newick>", "Input newick file")
10
- .on("--help", function() {
11
- console.log("");
12
- console.log("Examples:");
10
+ .requiredOption(
11
+ "-p --patterns <patterns>",
12
+ "Comma-separated regex patterns for tagging"
13
+ )
14
+ .requiredOption(
15
+ "-t --tags <tags>",
16
+ "Comma-separated tags that correspond to each pattern"
17
+ )
18
+ .usage("./test/data/CD2-relax.new -p 'regex1,regex2' -t 'tag1,tag2'")
19
+ .description("Tag branches in a Newick tree based on regex patterns.")
20
+ .on("--help", function () {
13
21
  console.log(
14
- 'phylotree tips test/data/MERS.txt'
22
+ "\nThis command tags the branches in a Newick tree based on regex patterns and outputs the new tree."
15
23
  );
16
24
  })
17
25
  .parse(process.argv);
18
26
 
19
- fs.readFile(commander.args[0], (err, newickData) => {
27
+ if (commander.patterns.split(",").length !== commander.tags.split(",").length) {
28
+ throw new Error(
29
+ "The number of patterns must match the number of corresponding tags."
30
+ );
31
+ }
20
32
 
21
- const tree = new phylotree.phylotree(newickData.toString());
22
- console.log('name', 'length', 'annotation')
23
- _.each(tree.getTips(), d => {
24
- console.log(d.data.name, d.data.attribute, d.data.annotation)
25
- })
33
+ const patterns = commander.patterns.split(",");
34
+ const tags = commander.tags.split(",");
35
+
36
+ fs.readFile(commander.args[0], (err, newick_data) => {
37
+ if (err) {
38
+ throw new Error("Error reading the Newick file: " + err.message);
39
+ }
40
+
41
+ const tree = new phylotree.phylotree(newick_data.toString());
42
+
43
+ // Function to apply tags based on patterns
44
+ function applyTags(node) {
45
+ for (let i = 0; i < patterns.length; i++) {
46
+ // Use the regex pattern to check against the node's name
47
+ if (new RegExp(patterns[i]).test(node.data.name)) {
48
+ node.data.annotation =
49
+ (node.data.annotation || "") + "{" + tags[i] + "}";
50
+ }
51
+ }
52
+ }
53
+
54
+ // Set default branch lengths if not already set
55
+ if (!tree.hasBranchLengths()) {
56
+ tree.setBranchLength(() => 1); // Set default branch length to 1
57
+ }
58
+
59
+ // Traverse the tree and tag nodes
60
+ tree.traverse_and_compute(applyTags);
26
61
 
62
+ // Output the new Newick string
63
+ console.log(tree.getNewick());
27
64
  });
File without changes
package/bin/phylotree.js CHANGED
@@ -1,16 +1,17 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const fs = require('fs');
4
- const phylotree = require('../dist/phylotree.js');
5
- const { program } = require('commander');
3
+ const fs = require("fs");
4
+ const phylotree = require("../dist/phylotree.js");
5
+ const { program } = require("commander");
6
6
 
7
7
  program
8
- .version('1.2.3')
9
- .command('tips', 'get tips from newick tree')
10
- .command('validate', 'validate newick string')
11
- .command('reroot', 'reroot newick tree based on node')
12
- .command('distance', 'find distance between two nodes')
13
- .command('mrca', 'tag all descendants of mrca')
14
- .command('shuffle', 'shuffle tips in a tree')
8
+ .version("1.2.3")
9
+ .command("tips", "get tips from newick tree")
10
+ .command("tag", "tag branches in newick tree")
11
+ .command("validate", "validate newick string")
12
+ .command("reroot", "reroot newick tree based on node")
13
+ .command("distance", "find distance between two nodes")
14
+ .command("mrca", "tag all descendants of mrca")
15
+ .command("shuffle", "shuffle tips in a tree");
15
16
 
16
17
  program.parse(process.argv);
File without changes
File without changes
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "phylotree",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "main": "dist/phylotree.js",
5
5
  "unpkg": "dist/phylotree.js",
6
6
  "jsdelivr": "dist/phylotree.js",
7
7
  "module": "src/index.js",
8
8
  "bin": {
9
9
  "root-to-tip": "./bin/root-to-tip.js",
10
+ "tag-branches": "./bin/phylotree-tag.js",
10
11
  "phylotree": "./bin/phylotree.js",
11
12
  "tip-date-extractor": "./bin/tip-date-extractor.js"
12
13
  },
package/src/index.js CHANGED
@@ -12,15 +12,13 @@ export { default as phylopart } from "./clustering/phylopart";
12
12
 
13
13
  export { default as parseFasta } from "./msa-parsers/fasta";
14
14
  export { default as neighborJoining, getDistanceMatrix } from "./neighbor-join";
15
- export { parseAnnotations, loadAnnotations } from "./formats/nexus";
15
+ export { loadAnnotations, parseAnnotations } from "./formats/nexus";
16
16
 
17
17
  export { default as newickParser, getNewick } from "./formats/newick";
18
18
 
19
19
  export {
20
+ default as inOrder,
20
21
  leftChildRightSibling,
21
22
  postOrder,
22
23
  preOrder,
23
- default as inOrder
24
24
  } from "./traversal.js";
25
-
26
-