-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_utils.go
62 lines (58 loc) · 1.1 KB
/
tree_utils.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
package inducedates
import "math"
// GetMrca get the mrca
func GetMrca(nds []*Node, root *Node) (mrca *Node) {
mrca = nil
traceback := make([]*Node, 0)
first := nds[0]
for first != root {
first = first.Par
traceback = append(traceback, first)
if first.Par == nil {
break
}
}
mrca = nds[0].Par
for _, i := range nds[1:] {
mrca = mrcaRecurse(mrca, i, traceback)
}
return
}
func mrcaRecurse(nd1 *Node, nd2 *Node, path1 []*Node) (mrca *Node) {
mrca = nil
path := path1[NodeSlicePosition(path1, nd1):]
parent := nd2
for parent != nil {
if NodeSlicePosition(path, parent) != -1 {
mrca = parent
return
}
parent = parent.Par
}
return
}
// SetHeights set tree height
func SetHeights(tree *Tree) {
for _, i := range tree.Tips {
cur := i
h := 0.0
going := true
for going {
h += cur.Len
cur = cur.Par
if cur == nil {
going = false
break
} else {
if h > cur.Height {
cur.Height = h
}
}
}
}
for _, i := range tree.Pre {
if i != tree.Rt {
i.Height = math.Abs(Round(i.Par.Height-i.Len, 0.5, 5)) //weird rounding thing on some machines
}
}
}