/
tree_reader.go
98 lines (94 loc) · 1.88 KB
/
tree_reader.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
package cophycollapse
import (
"bytes"
"fmt"
"strconv"
)
//func initNode(n *Node) (inode *Node) {
// n.PAR = nil
// n.CHLDS =
//}
//ReadTree will parse a newick string into a node struct
func ReadTree(ts string) (root *Node) {
rt := Node{nil, nil, "root", 0., 0., nil, false, nil, nil, nil, nil, nil}
x := 0
nc := string(ts[x : x+1])
start := true
//var(
// chslice []*Node
// contrt []float64
//)
cn := new(Node)
//cn := &Node{PAR: nil,CHLD:chslice,NAME:"",LEN: 0.0,PRNLEN:0.0,CONTRT: contrt,MRK:false}
for {
if nc == "(" {
if start == true {
cn = &rt
start = false
} else {
nn := Node{cn, nil, "", 0., 0.0, nil, false, nil, nil, nil, nil, nil}
cn.AddChild(&nn)
cn = &nn
}
} else if nc == "," {
cn = cn.PAR
} else if nc == ")" {
cn = cn.PAR
x++
nc = ts[x : x+1]
if nc == "," || nc == ")" || nc == ":" || nc == "[" || nc == ";" {
continue
}
var nm bytes.Buffer
for {
nm.WriteString(nc)
x++
nc = ts[x : x+1]
if nc == "," || nc == ")" || nc == ":" || nc == "[" || nc == ";" {
break
}
}
cn.NAME = nm.String()
x--
} else if nc == ";" {
break
} else if nc == ":" {
x++
nc = ts[x : x+1]
var bl bytes.Buffer
for {
bl.WriteString(nc)
x++
nc = ts[x : x+1]
if nc == "," || nc == ")" || nc == ":" || nc == "[" || nc == ";" {
break
}
}
b, err := strconv.ParseFloat(bl.String(), 64)
if err != nil {
fmt.Printf("There is an error in branch length processing\n")
}
cn.LEN = b
x--
} else {
nn := Node{cn, nil, "", 0., 0., nil, false, nil, nil, nil, nil, nil}
cn.AddChild(&nn)
cn = &nn
var nm bytes.Buffer
for {
nm.WriteString(nc)
x++
nc = ts[x : x+1]
if nc == "," || nc == ")" || nc == ":" || nc == "[" {
break
}
}
x--
nn.NAME = nm.String()
}
x++
nc = ts[x : x+1]
}
root = &rt
return
}