/
radial.go
195 lines (175 loc) · 5.54 KB
/
radial.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
package draw
import (
"math"
"github.com/evolbioinfo/gotree/tree"
)
type radialLayout struct {
drawer TreeDrawer
spread float64
hasBranchLengths bool
hasTipLabels bool
hasTipSymbols bool
hasInternalNodeLabels bool
hasInternalNodeSymbols bool
hasNodeComments bool
hasSupport bool
supportCutoff float64
cache *layoutCache
tipColors map[string][]uint8
}
func NewRadialLayout(td TreeDrawer, withBranchLengths, withTipLabels, withInternalNodeLabels, withSuppportCircles bool) TreeLayout {
return &radialLayout{
td,
0.0,
withBranchLengths,
withTipLabels,
false,
withInternalNodeLabels,
false,
false,
withSuppportCircles,
0.7,
newLayoutCache(),
make(map[string][]uint8),
}
}
func (layout *radialLayout) SetSupportCutoff(c float64) {
layout.supportCutoff = c
}
func (layout *radialLayout) SetDisplayInternalNodes(s bool) {
layout.hasInternalNodeSymbols = s
}
func (layout *radialLayout) SetDisplayNodeComments(s bool) {
layout.hasNodeComments = s
}
func (layout *radialLayout) SetTipColors(colors map[string][]uint8) {
layout.hasTipSymbols = true
layout.tipColors = colors
}
/*
Draw the tree on the specific drawer. Does not close the file. The caller must do it.
This layout is an adaptation in Go of the figtree radial layout : figtree/treeviewer/treelayouts/RadialTreeLayout.java
( https://github.com/rambaut/figtree/ )
Tree indexes must have been set with t.ReinitIndexes()
*/
func (layout *radialLayout) DrawTree(t *tree.Tree) error {
root := t.Root()
layout.spread = 0.0
layout.constructNode(t, root, nil, 0.0, 0.0, math.Pi*2, 0.0, 0.0, 0.0)
_, maxNameLength := maxLength(t, layout.hasBranchLengths, layout.hasTipLabels, layout.hasNodeComments)
layout.drawTree(maxNameLength)
layout.drawer.Write()
return nil
}
func (layout *radialLayout) constructNode(t *tree.Tree, node *tree.Node, prev *tree.Node, support, angleStart, angleFinish, xPosition, yPosition, length float64) *layoutPoint {
branchAngle := (angleStart + angleFinish) / 2.0
directionX := math.Cos(branchAngle)
directionY := math.Sin(branchAngle)
nodePoint := &layoutPoint{xPosition + (length * directionX), yPosition + (length * directionY), branchAngle, node.Name(), node.CommentsString()}
if !node.Tip() {
leafCounts := make([]int, 0)
sumLeafCount := 0
i := 0
for num, child := range node.Neigh() {
if child != prev {
numT := node.Edges()[num].NumTipsRight()
leafCounts = append(leafCounts, numT)
sumLeafCount += numT
i++
}
}
span := (angleFinish - angleStart)
if node != t.Root() {
span *= 1.0 + (layout.spread / 10.0)
angleStart = branchAngle - (span / 2.0)
angleFinish = branchAngle + (span / 2.0)
}
a2 := angleStart
rotate := false
i = 0
for num, child := range node.Neigh() {
if child != prev {
index := i
if rotate {
index = len(node.Neigh()) - i - 1
}
brLen := node.Edges()[num].Length()
supp := node.Edges()[num].Support()
if !layout.hasBranchLengths || brLen == tree.NIL_LENGTH {
brLen = 1.0
}
a1 := a2
a2 = a1 + (span * float64(leafCounts[index]) / float64(sumLeafCount))
childPoint := layout.constructNode(t, child, node, supp, a1, a2, nodePoint.x, nodePoint.y, brLen)
branchLine := &layoutLine{childPoint, nodePoint, supp}
//add the branchLine to the map of branch paths
layout.cache.branchPaths = append(layout.cache.branchPaths, branchLine)
i++
}
}
layout.cache.nodePoints = append(layout.cache.nodePoints, nodePoint)
} else {
layout.cache.tipLabelPoints = append(layout.cache.tipLabelPoints, nodePoint)
}
return nodePoint
}
func (layout *radialLayout) drawTree(maxNameLength int) {
xmin, ymin, xmax, ymax := layout.cache.borders()
xoffset := 0.0
if xmin < 0 {
xoffset = -xmin
}
yoffset := 0.0
if ymin < 0 {
yoffset = -ymin
}
layout.drawer.SetMaxValues(xmax+xoffset, ymax+yoffset, maxNameLength, maxNameLength)
for _, l := range layout.cache.branchPaths {
layout.drawer.DrawLine(l.p1.x+xoffset, l.p1.y+yoffset, l.p2.x+xoffset, l.p2.y+yoffset)
}
if layout.hasTipLabels {
for _, p := range layout.cache.tipLabelPoints {
// Add space to label so it's not hidden by node symbol
// There is probably a better way to do this
spc := ""
if layout.hasTipSymbols {
if _, ok := layout.tipColors[p.name]; ok {
spc = " "
}
}
if layout.hasNodeComments {
layout.drawer.DrawName(p.x+xoffset, p.y+yoffset, spc+p.name+p.comment+spc, p.brAngle)
} else {
layout.drawer.DrawName(p.x+xoffset, p.y+yoffset, spc+p.name+spc, p.brAngle)
}
}
}
if layout.hasInternalNodeLabels {
for _, p := range layout.cache.nodePoints {
layout.drawer.DrawName(p.x+xoffset, p.y+yoffset, p.name, p.brAngle)
}
} else if layout.hasNodeComments {
for _, p := range layout.cache.nodePoints {
layout.drawer.DrawName(p.x+xoffset, p.y+yoffset, p.comment, p.brAngle)
}
}
if layout.hasInternalNodeSymbols {
for _, p := range layout.cache.nodePoints {
layout.drawer.DrawCircle(p.x+xoffset, p.y+yoffset)
}
}
if layout.hasTipSymbols {
for _, p := range layout.cache.tipLabelPoints {
if col, ok := layout.tipColors[p.name]; ok {
layout.drawer.DrawColoredCircle(p.x+xoffset, p.y+yoffset, col[0], col[1], col[2], 0xff)
}
}
}
for _, l := range layout.cache.branchPaths {
middlex := (l.p1.x + l.p2.x + 2*xoffset) / 2.0
middley := (l.p1.y + l.p2.y + 2*yoffset) / 2.0
if layout.hasSupport && l.support != tree.NIL_SUPPORT && l.support >= layout.supportCutoff {
layout.drawer.DrawCircle(middlex, middley)
}
}
}