-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_reader.go
88 lines (86 loc) · 1.74 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
package inducedates
import (
"bytes"
"fmt"
"os"
"strconv"
)
// ReadNewickString given a string it will return a pointer to the root node
func ReadNewickString(ts string) (root *Node) {
rt := Node{nil, nil, "root", map[string]string{}, 0., nil, false, 0., map[float64]bool{}}
x := 0
nc := string(ts[x : x+1])
start := true
cn := new(Node)
for {
if nc == "(" {
if start == true {
cn = &rt
start = false
} else {
nn := Node{cn, nil, "", map[string]string{}, 0., nil, false, 0., map[float64]bool{}}
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.Nam = 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.Fprintf(os.Stderr, "There is an error in branch length processing\n")
}
cn.Len = b
x--
} else {
nn := Node{cn, nil, "", map[string]string{}, 0., nil, false, 0., map[float64]bool{}}
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.Nam = nm.String()
}
x++
nc = ts[x : x+1]
}
root = &rt
return
}