Спасибо kaa76, он обратил мое внимание на то, что приведенный выше скрипт, для конвертации деревьев из tnt в newick, работает в питоне версии 2.X, но не работает в версии питона 3.X.
Привожу новую версию, которая одинаково работает и во второй версии питона, и в третьей:
#!/usr/bin/python
__author__ = 'Semargl'
import re, sys, os
def tntfile2newick(fn):
print(fn)
fh = open(fn, 'r')
treeslist = []
for line in fh:
if line.startswith("("):
pass
else:
continue
tntstr = line
tntstr2 = tntstr2newick(tntstr)
treeslist.append(tntstr2)
outfn = fn + ".tree"
outfh = open(outfn, 'w')
outfh.write('#NEXUS\nbegin trees;\n')
cnt = 1
for newickstr in treeslist:
outfh.write('\ttree tree_%s = [&R]\n%s\n' % (cnt, newickstr))
cnt += 1
outfh.write('end;\n')
outfh.close()
fh.close()
print(outfn)
def tntstr2newick(tntstr):
tntstr = re.sub(r'\s', ',', tntstr)
tntstr = re.sub(r'\)\(', '),(', tntstr)
tntstr = re.sub(r',\)', ')', tntstr)
tntstr = re.sub(r'=', '', tntstr)
tntstr = re.sub(r'__', '_', tntstr)
r2=re.compile(r'(?P<r3>\d+)/(?P<r4>\d+)')
tntstr = re.sub(r'\*', '', tntstr)
tntstr = re.sub(r',,$', ';', tntstr)
tntstr = re.sub(r',;,', ';', tntstr)
r1=re.compile(r',(?P<r1>\d+)(?P<r2>,|\))')
tntstr = r1.sub('[&!name="\g<r1>"]\g<r2>', tntstr)
r2=re.compile(r'(?P<r3>\d+\/\d+)')
return tntstr
file_name = sys.argv[1]
tntfile2newick(file_name)