-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexcise_knuckles.py
More file actions
executable file
·47 lines (32 loc) · 1.34 KB
/
excise_knuckles.py
File metadata and controls
executable file
·47 lines (32 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
if __name__ == '__main__':
import newick3, phylo3, sys
if len(sys.argv) < 2:
print "usage: excise_knuckles.py <treefile>"
sys.exit(0)
treefname = sys.argv[1]
treefile = open(treefname, "r")
logfile = open("excise_knuckles.log","w")
for line in treefile:
tree = newick3.parse(line)
while len(tree.children) < 2:
# prune knuckles at the root of the tree if necessary
only_child = tree.children[0]
only_child.parent = None
only_child.isroot = True
tree = only_child
# cannot edit tree while traversing, so just record knuckles as we go
knuckles = []
# first find the knuckles
for parent in tree.iternodes(phylo3.PREORDER):
if parent.istip:
continue
logfile.write("node '" + (str(parent.label) if parent.label != None else str(parent)) + "' has " + str(len(parent.children)) + " children\n")
if len(parent.children) == 1:
logfile.write("\tthis node will be pruned\n")
knuckles.append(parent)
# now graft them out
for k in knuckles:
k.parent.add_child(k.children[0])
k.parent.remove_child(k)
print newick3.to_string(tree) + ";"