/
tree_parser.go
418 lines (307 loc) · 8.78 KB
/
tree_parser.go
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
* Copyright (C) 2009 Simon A. Berger
*
* This program is free software; you may redistribute it and/or modify its
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
/**
* A go implementation of a reader for phylogenetic trees in the newick format
* (http://evolution.genetics.washington.edu/phylip/newicktree.html)
*
* For now it should be possible to read *unrooted* standard newick files (= the
* top level node must have three children!). It can also read branch-labels and
* node labels which will be interpreted as branch support values if possible
*
* There is also code for a basic tree writer.
* See func main for usage information.
*/
package main
import (
"fmt"
"strconv"
"io"
"os"
"io/ioutil"
"tree"
"bytes"
"gob"
)
type ParserInput interface {
// Get the character at an arbitrary position in the
// input source.
CharAt(i int) uint8
// Get the number of characters in the input source.
Size() int
Substring(start, end int) string
}
func IsDigit(c uint8) bool { return c >= '0' && c <= '9' }
func IsSpace(c uint8) bool { return c == 32 || c == 9 }
func skipWhitespace(pi ParserInput, pos int) int {
for ; ; pos++ {
if !IsSpace(pi.CharAt(pos)) {
break
}
}
return pos
}
func findEndOfBranch(pi ParserInput, pos int) int {
termchar := ":,)"
for ; ; pos++ {
for c := range termchar {
if termchar[c] == pi.CharAt(pos) {
return pos
}
}
}
panic("unreachable")
}
func isFloatChar(c uint8) bool {
return (c >= '0' && c <= '9') || c == '.' || c == 'e' || c == 'E' || c == '-'
}
func findFloat(pi ParserInput, pos int) int {
for isFloatChar(pi.CharAt(pos)) {
pos++
}
//fmt.Printf( "float len: %d\n", len );
return pos
}
func parseBranchLength(pi ParserInput, pos int) (float, int) {
pos = skipWhitespace(pi, pos)
// expect + consume ':'
if pi.CharAt(pos) != ':' {
//throw new RuntimeException("parse error: parseBranchLength expects ':' at " + ptr);
return 0.0, 0
} else {
pos++
pos = skipWhitespace(pi, pos)
flen := findFloat(pi, pos)
if flen == pos {
fmt.Printf("error: missing float number at %d\n", pos)
}
blen, _ := strconv.Atof(pi.Substring(pos, flen))
pos = flen
return blen, pos
}
panic("unreachable")
}
func findNext(pi ParserInput, pos int, c uint8) int {
for ; ; pos++ {
if pi.CharAt(pos) == c {
break
}
}
return pos
}
func parseBranchLabel(pi ParserInput, pos int) (string, int) {
var label string
if pi.CharAt(pos) == '[' {
lstart := pos
pos++
lend := findNext(pi, pos, ']')
pos = lend + 1
// labels have the form [blablabla], so the label content starts at lstart + 1
if lend-(lstart+1) <= 0 {
//printLocation();
fmt.Printf("bad branch label: %s\n", pi.Substring(lstart, lend))
}
label = pi.Substring(lstart+1, lend)
} else {
label = ""
}
return label, pos
}
func twiddle(n1 *tree.LN, n2 *tree.LN, len float, label string, support float) {
n1.Back = n2
n2.Back = n1
n1.BackLen = len
n2.BackLen = len
n1.BackLabel = label
n2.BackLabel = label
n1.BackSupport = support
n2.BackSupport = support
}
func parseInnerNode(pi ParserInput, pos int) (*tree.LN, int) {
//fmt.Printf( "parse inner node\n" );
pos = skipWhitespace(pi, pos)
if pi.CharAt(pos) != '(' {
fmt.Printf("error: expected '('\n")
}
// consume opening '('
pos++
var nl *tree.LN
nl, pos = parseNode(pi, pos)
var bll float
bll, pos = parseBranchLength(pi, pos)
// fmt.Printf( "parsing next node %d\n", pos );
var labell string
labell, pos = parseBranchLabel(pi, pos)
pos = skipWhitespace(pi, pos)
// expect + consume ','
if pi.CharAt(pos) != ',' {
fmt.Printf("parse error: parseInnerNode expects ',' at %d\n", pos)
}
pos++
// parse right node + branch length
var nr *tree.LN
nr, pos = parseNode(pi, pos)
var blr float
blr, pos = parseBranchLength(pi, pos)
var labelr string
labelr, pos = parseBranchLabel(pi, pos)
pos = skipWhitespace(pi, pos)
if pi.CharAt(pos) == ',' {
// second comma found: three child nodes == pseudo root
pos++
var nx *tree.LN
nx, pos = parseNode(pi, pos)
var blx float
blx, pos = parseBranchLength(pi, pos)
var labelx string
labelx, pos = parseBranchLabel(pi, pos)
pos = skipWhitespace(pi, pos)
if pi.CharAt(pos) != ')' {
// printLocation();
fmt.Printf("parse error: parseInnerNode (at root) expects ') at %d\n", pos)
}
pos++
pos = skipWhitespace(pi, pos)
n := tree.CreateLN()
twiddle(nl, n.Next, bll, labell, n.Data.Support)
twiddle(nr, n.Next.Next, blr, labelr, n.Data.Support)
twiddle(nx, n, blx, labelx, n.Data.Support)
// fmt.Printf( "twiddle root\n" );
// System.out.printf( "root: %f %f %f\n", nl.data.getSupport(), nr.data.getSupport(), nx.data.getSupport() );
// System.exit(0);
return n, pos
} else if pi.CharAt(pos) == ')' {
// the stuff between the closing '(' and the ':' of the branch length
// is interpreted as node-label. If the node label corresponds to a float value
// it is interpreted as branch support (or node support as a rooted-trees-only-please biologist would say)
pos++
lend := findEndOfBranch(pi, pos)
nodeLabel := pi.Substring(pos, lend)
pos = lend
// fmt.Printf( "pos: %d %s\n", pos, nodeLabel );
isDigit := true
for i := range nodeLabel {
isDigit = isDigit && IsDigit(nodeLabel[i])
if i == 0 {
isDigit = isDigit && (nodeLabel[i] != '0')
}
}
var support float
if isDigit {
support, _ = strconv.Atof(nodeLabel)
} else {
support = -1
}
n := tree.CreateLN()
n.Data.Support = support
//n.data.setNodeLabel(nodeLabel);
twiddle(nl, n.Next, bll, labell, support)
twiddle(nr, n.Next.Next, blr, labelr, support)
return n, pos
} else {
fmt.Printf("parse error: parseInnerNode expects ')'or ',' at %d got: %c\n", pos, pi.CharAt(pos))
return nil, pos
}
panic("unreachable")
}
func parseLeaf(pi ParserInput, pos int) (*tree.LN, int) {
pos = skipWhitespace(pi, pos)
brLen := findEndOfBranch(pi, pos)
ld := pi.Substring(pos, brLen)
pos = brLen
node := tree.CreateLeaf(ld)
//fmt.Printf( "leaf parsed: %s\n", ld );
return node, pos
}
func parseNode(pi ParserInput, pos int) (*tree.LN, int) {
pos = skipWhitespace(pi, pos)
var n *tree.LN
if pi.CharAt(pos) == '(' {
n, pos = parseInnerNode(pi, pos)
} else {
n, pos = parseLeaf(pi, pos)
}
return n, pos
}
func Parse(pi ParserInput, pos int) (*tree.LN, int) {
pos = skipWhitespace(pi, pos)
var n *tree.LN
n, pos = parseNode(pi, pos)
return n, pos
}
type StringPI string
func (self StringPI) CharAt(i int) uint8 {
// fmt.Printf( "CharAt: %d: %d %c\n", i, self[i], self[i]);
return (self)[i]
}
func (self StringPI) Size() int { return len(self) }
func (self StringPI) Substring(start, end int) string {
return string(self)[start:end]
}
func printTreeInner(node *tree.LN, w io.Writer, root bool) {
if node.Data.IsTip {
//s.printf("%s:%G", node.data.getTipName(), node.backLen);
io.WriteString(w, fmt.Sprintf("%s:%8.20f", node.Data.Label, node.BackLen))
} else {
w.Write([]byte{'('})
printTreeInner(node.Next.Back, w, false)
w.Write([]byte{','})
printTreeInner(node.Next.Next.Back, w, false)
if root {
w.Write([]byte{','})
printTreeInner(node.Back, w, false)
w.Write([]byte{')', ';'})
} else {
//s.printf("):%G", node.backLen );
io.WriteString( w, fmt.Sprintf("):%8.20f", node.BackLen))
}
}
}
func PrintTree(node *tree.LN, w io.Writer) {
//node = LN.getTowardsTree(node);
if node.Data.IsTip {
if node.Back != nil {
node = node.Back
} else if node.Next.Back != nil {
node = node.Next.Back
} else if node.Next.Next.Back != nil {
node = node.Next.Next.Back
} else {
panic("can not print single unlinked node")
}
if node.Data.IsTip {
panic("could not find non-tip node for writing the three (this is a braindead limitation of this tree printer!)")
}
}
printTreeInner(node, w, true)
}
func main() {
data, _ := ioutil.ReadFile("/space/raxml/VINCENT/RAxML_bipartitions.1604.BEST.WITH")
n, _ := Parse(StringPI(string(data)), 0)
//n,_ := Parse( StringPI(" (bla:.1,bla2:2.1,(bla32:1.2,bla34:0.7)100:3.4);"), 0 );
PrintTree(n, os.Stdout)
fmt.Println()
PrintTree(n.Back, os.Stdout)
fmt.Println()
i := make([]int, 4)
i[0] = 72
i[1] = 105
i[2] = 32
i[3] = 9731 // Unicode snowman
var s string = string(i)
fmt.Printf("%s has length %d bytes.\n", s, len(s))
buf := bytes.NewBuffer(nil)
enc := gob.NewEncoder( buf )
enc.Encode( n )
os.Stdout.Write( buf.Bytes() )
}