-
Notifications
You must be signed in to change notification settings - Fork 2
/
root_tree.py
executable file
·78 lines (48 loc) · 1.93 KB
/
root_tree.py
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
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
import os, sys
from Bio.Nexus import Trees, Nodes
from optparse import OptionParser
import modules.Si_nexus as Si_nexus
from modules.Si_general import *
##########################################
# Function to Get command line arguments #
##########################################
def main():
usage = "usage: %prog [options]"
parser = OptionParser(usage=usage)
parser.add_option("-r", "--root", action="store", dest="outgroup", help="taxon to root on (if not specified, will midpoint root)", default="")
parser.add_option("-t", "--tree", action="store", dest="tree", help="tree file", default="")
parser.add_option("-l", "--ladderise", action="store", dest="ladderise", help="ladderise tree (choose from left and right)", type="choice", choices=["right", "left"], default=None)
parser.add_option("-o", "--output", action="store", dest="outputfile", help="output file name", default="")
return parser.parse_args()
################
# Main program #
################
if __name__ == "__main__":
#Get command line arguments
(options, args) = main()
#Do some checking of the input files
if options.tree=="":
DoError("No tree file specified")
print "Reading tree file"
sys.stdout.flush()
try:
tree_strings = open(options.tree).readlines()
except IOError:
DoError("Cannot open tree file "+options.tree)
handle = open(options.outputfile, "w")
for tree_string in tree_strings:
tree = Trees.Tree(tree_string, rooted=True)
if options.outgroup!="":
print "Rooting tree on", options.outgroup
sys.stdout.flush()
tree.root_with_outgroup(outgroup=options.outgroup)
else:
print "Midpoint rooting tree"
tree=Si_nexus.midpoint_root(tree)
#Print tree
treestring=Si_nexus.tree_to_string(tree,plain=False,ladderize=options.ladderise)#tree with branch lengths and support
if treestring[-1]!=";":
treestring=treestring+";"
print >> handle, treestring
handle.close()