forked from combogenomics/regtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mrBayes2Newick_3.2
executable file
·93 lines (73 loc) · 2.04 KB
/
mrBayes2Newick_3.2
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/python
# IMPORTANT! we assume that MrBayes produces a *.con file
# Some newer versions produce a different tree file (this works for 3.2.x)
import sys
if len(sys.argv) < 2:
print 'USAGE: ./mrBayes2Newick_3.2 CON_FILE'
sys.exit(1)
infile = sys.argv[1]
trigger_translate = 'translate'
trigger = 'tree con_50_majrule = '
bStart = True
bTrans = False
nwk = None
d = {}
for l in open(infile):
l = l.strip()
# ID conversion
if not l.startswith(trigger_translate) and bStart:
continue
elif l.startswith(trigger_translate) and bStart:
bStart = False
bTrans = True
continue
elif bTrans:
if l.rstrip().lstrip() == ';':
bTrans = False
continue
oid, org = l.rstrip(',').split('\t')
d[oid] = org
elif l.startswith(trigger):
nwk1 = l.split(trigger)[1]
# Fix the tree
nwk2 = ''
s = nwk1.split('[')
for i in s:
if ']' in i:
nwk2 += i.split(']')[1]
else:
nwk2 += i
#
# Fix the clade names
nwk3 = ''
s = nwk2.split(',')
for i in s:
i=i.strip()
while i.lstrip().startswith('('):
nwk3 += '('
i = i.lstrip()[1:]
j = i.split(':')
nwk3 += d[j[0]] + ':'
nwk3 += ':'.join(j[1:])
nwk3 += ','
nwk3 = nwk3.rstrip(',')
#
# Fix the distance
nwk4 = ''
s = nwk3.split(':')
nwk4 += s[0]
for i in s[1:]:
if ',' in i:
j = i.split(',')
nwk4 += ':' + str(round(float(j[0]),6)) + ',' + ','.join(j[1:])
else:
j = i.split(')')
nwk4 += ':' + str(round(float(j[0]),6)) + ')' + ')'.join(j[1:])
#
nwk = nwk4
break
if nwk is None:
raise ValueError('No nwk tree!')
f = open('%s.nwk'%infile, 'w')
f.write('%s\n'%nwk)
f.close()