-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathprune_tips.py
More file actions
executable file
·68 lines (49 loc) · 1.81 KB
/
prune_tips.py
File metadata and controls
executable file
·68 lines (49 loc) · 1.81 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
if __name__ == '__main__':
import newick3, phylo3, sys
#if len(sys.argv) < 4:
if len(sys.argv) < 3:
print "usage: prunetips <namestoprunefile> <treefile>" #<outfile>"
sys.exit()
badnamesfname = sys.argv[1]
badnamesfile = open(badnamesfname, "r")
badnames = [name.strip() for name in badnamesfile.readlines()]
#print badnames
treefname = sys.argv[2]
treefile = open(treefname, "r")
#outfname = sys.argv[3]
#outfile = open(outfname,"w")
logfile = open("prunetips.log","w")
for line in treefile:
tree = newick3.parse(line)
# print "in tree: " + newick3.to_string(tree) + ";"
# prune the bad tips
for tip in tree.leaves():
name_ok = False
if tip.label in badnames:
leftlabel = tip.label
# print "removing " + leftlabel
# logfile.write("removing " + leftlabel)
tip.prune(logfile)
else:
name_ok = True
# remove any leftover empty tip nodes
if not name_ok:
for n in tree.descendants():
nc = n
while (not nc.istip) and len(nc.children) == 0:
# print "pruning an empty tip"
logfile.write("pruning an empty tip")
np = nc.parent
nc.prune()
if np:
# prepare for next step back
nc = np
# if we hit the root of the tree, move on to next descendant
else:
break
print newick3.to_string(tree) + ";"
# write the pruned tree
# outfile.write(newick3.to_string(tree) + ";\n")
# outfile.flush()
#outfile.close()