forked from ballesterus/Utensils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiqtree_namefix.py
More file actions
executable file
·66 lines (55 loc) · 1.68 KB
/
iqtree_namefix.py
File metadata and controls
executable file
·66 lines (55 loc) · 1.68 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
#!/usr/bin/env python
import sys
import os
import re
from sys import argv
def name_catalog(logFile):
catalog = {}
switch = 0
try:
log= open(logFile, 'r')
except:
print "There was a problem reading the file %s" %logFile
exit(1)
for line in log:
if line == "WARNING: Some sequence names are changed as follows:\n":
# print "Catalog begins"
switch = 1
continue
while switch == 1:
if re.search('->', line):
try:
original= re.split('->', line)[0]
original =re.sub('[ \n]', '', original)
iqname= re.split('->', line)[1]
iqname=re.sub('[ \n]', '', iqname)
catalog[original]=iqname
break
except:
print "ERROR IN: %s" % line
else:
#print "Done with the catalog"
#print line
switch = 0
log.close()
return catalog
def replaceNames(treefile, catalog):
with open(treefile, 'r') as infile:
tree = infile.readline()
for k in catalog.iterkeys():
fro = catalog[k]
to = k
tree=re.sub(fro, to, tree)
return tree
def main():
fname= sys.argv[1]
log = fname.replace('treefile', 'log')
out = open(fname.replace('fa.treefile', 'tre'), 'w')
cat = name_catalog(log)
out.write(replaceNames(fname, cat))
out.close
if __name__ == "__main__":
if len(argv) == 2:
main()
else:
print "Error: Need the name of iqtree tree file as argument to process."