-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_newick.py
More file actions
executable file
·74 lines (62 loc) · 1.94 KB
/
format_newick.py
File metadata and controls
executable file
·74 lines (62 loc) · 1.94 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
69
70
71
72
73
74
#!/usr/bin/python3
# Implemented by J.T. Brandenburg for using the script from Brandenburg et al. (2012)
import re
import sys
def parse(newick):
tokens = re.findall(r"([^:;,()\s]*)(?:\s*:\s*([\d.]+)\s*)?([,);])|(\S)", newick+";")
def recurse(nextid = 0, parentid = -1): # one node
thisid = nextid;
children = []
name, length, delim, ch = tokens.pop(0)
if ch == "(":
while ch in "(,":
node, ch, nextid = recurse(nextid+1, thisid)
children.append(node)
name, length, delim, ch = tokens.pop(0)
return {"id": thisid, "name": name, "length": float(length) if length else None,
"parentid": parentid, "children": children}, delim, nextid
return recurse()[0]
def print_newick(newick, Cmt=0, length=0):
if len(newick['children'])==0:
return newick['name']+":"+str(newick['length']+length)
if len(newick['children'])==1:
if newick['length']==None:
lng=0
else :
lng=newick['length']
return print_newick(newick['children'][0],Cmt+1, lng)
strout='('
cmtchil=0
if len(newick['children'])==1 and Cmt==1:
print(newick)
print(Cmt)
sys.exit('children is 1')
for children in newick['children'] :
if cmtchil==2 :
strout="("+strout
strout+='):0,'
cmtchil=1
strout+=print_newick(children,Cmt+1)
if cmtchil==0 :
strout+=','
cmtchil+=1
if newick['length']!=None :
return strout+'):'+str(length+newick['length'])
else :
return strout+')'
Test=False
if Test :
AAA=parse('((A:01,C:23,D:5):2,G:89)')
print(AAA)
print(print_newick(AAA))
sys.exit()
FileIn=sys.argv[1]
FileOut=sys.argv[2]
ReadIn=open(FileIn)
Writ=open(FileOut, 'w')
ListVec=ReadIn.readlines()
for lines in ListVec :
Cmt=0
treeform=parse(lines.replace('\n', ''))
treeformnet=print_newick(treeform)+';'+'\n'
Writ.write(treeformnet)