phylotree 2.0.1 → 2.1.1
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.
- package/bin/phylotree-distance.js +0 -0
- package/bin/phylotree-neighbor-join.js +0 -0
- package/bin/phylotree-reroot.js +0 -0
- package/bin/phylotree-tag.js +131 -0
- package/bin/phylotree-tips.js +51 -14
- package/bin/phylotree-validate.js +0 -0
- package/bin/phylotree.js +11 -10
- package/bin/root-to-tip.js +0 -0
- package/bin/tip-date-extractor.js +0 -0
- package/dist/phylotree.js +6424 -60
- package/dist/phylotree.min.js +1 -1
- package/package.json +4 -3
- package/src/formats/nexml.js +1 -1
- package/src/index.js +2 -4
- package/src/render/draw.js +2 -1
- package/src/render/options.js +6 -0
|
File without changes
|
|
File without changes
|
package/bin/phylotree-reroot.js
CHANGED
|
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
|
+
});
|
package/bin/phylotree-tips.js
CHANGED
|
@@ -1,27 +1,64 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
const fs = require("fs")
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
.
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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(
|
|
4
|
-
const phylotree = require(
|
|
5
|
-
const { program } = require(
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const phylotree = require("../dist/phylotree.js");
|
|
5
|
+
const { program } = require("commander");
|
|
6
6
|
|
|
7
7
|
program
|
|
8
|
-
.version(
|
|
9
|
-
.command(
|
|
10
|
-
.command(
|
|
11
|
-
.command(
|
|
12
|
-
.command(
|
|
13
|
-
.command(
|
|
14
|
-
.command(
|
|
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);
|
package/bin/root-to-tip.js
CHANGED
|
File without changes
|
|
File without changes
|