-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpars.go
239 lines (230 loc) · 5.13 KB
/
pars.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
package gophy
import (
"math"
"strconv"
)
// ParsResult gives the parsimony score (value) for a site
type ParsResult struct {
value float64
site int
}
// PCalcSankParsPatterns parallel caclulation of parsimony costs with patterns
func PCalcSankParsPatterns(t *Tree, numstates int, patternval []float64, wks int) (fl float64) {
fl = 0.0
nsites := len(patternval)
jobs := make(chan int, nsites)
//results := make(chan float64, nsites)
results := make(chan ParsResult, nsites)
for i := 0; i < wks; i++ {
go CalcSankParsWork(t, numstates, jobs, results)
}
for i := 0; i < nsites; i++ {
jobs <- i
}
close(jobs)
rr := ParsResult{}
for i := 0; i < nsites; i++ {
rr = <-results
fl += (rr.value * patternval[rr.site])
}
return
}
// CalcSankParsWork ...
func CalcSankParsWork(t *Tree, numstates int, jobs <-chan int, results chan<- ParsResult) { //results chan<- float64) {
for j := range jobs {
sl := 0.0
for _, n := range t.Post {
if len(n.Chs) > 0 {
CalcSankParsNode(n, numstates, j)
}
if t.Rt == n {
sl = math.MaxFloat64
for i := 0; i < numstates; i++ {
if n.Data[j][i] < sl {
sl = n.Data[j][i]
}
}
}
}
results <- ParsResult{value: sl, site: j}
}
}
// CalcSankParsNode ...
func CalcSankParsNode(nd *Node, numstates int, site int) {
for i := 0; i < numstates; i++ {
nd.Data[site][i] = 0.
}
for _, c := range nd.Chs {
// tip , assume that the data are 1.0 and not data are 0.0
if len(c.Chs) == 0 {
for i := 0; i < numstates; i++ {
minh := math.MaxFloat64
for j := 0; j < numstates; j++ {
tempv := 0.0
if c.Data[site][j] == 0.0 {
tempv += math.MaxFloat64
} else {
if i == j {
tempv = 0.0
} else {
tempv = 1.0
}
}
if tempv < minh {
minh = tempv
}
}
nd.Data[site][i] += minh
}
} else {
for i := 0; i < numstates; i++ {
minh := math.MaxFloat64
for j := 0; j < numstates; j++ {
tempv := 0.0
if i != j {
tempv += 1.0
}
tempv += c.Data[site][j]
if tempv < minh {
minh = tempv
}
}
nd.Data[site][i] += minh
}
}
}
}
// CalcSankParsAncStateSingleSite ....
// assumes bifurcating for now
func CalcSankParsAncStateSingleSite(t *Tree, numstates int, site int) {
ancmaps := make(map[*Node][]int) // ancestral states are stored here
for _, n := range t.Pre {
if len(n.Chs) == 0 {
continue
}
if t.Rt == n {
sl := MinF(n.Data[site])
states := make([]int, 0)
//things are stored in n.Data[j][i]
for i := 0; i < numstates; i++ {
if n.Data[site][i] == sl {
states = append(states, i)
}
}
ancmaps[n] = states
}
//
c1v := make(map[int]bool)
c2v := make(map[int]bool)
for m := range ancmaps[n] {
v := n.Data[site][m]
for i, j := range n.Chs[0].Data[site] {
c1 := 0.
if m != i {
c1 = 1.
}
if len(n.Chs[0].Chs) == 0 {
if j != 1 {
c1 += math.MaxFloat64
}
} else {
c1 += j
}
for k, l := range n.Chs[1].Data[site] {
c2 := 0.
if m != k {
c2 = 1.
}
if len(n.Chs[1].Chs) == 0 {
if l != 1 {
c2 += math.MaxFloat64
}
} else {
c2 += l
}
if c1+c2 == v {
c1v[i] = true
c2v[k] = true
}
}
}
}
c1vv := make([]int, 0)
for i := range c1v {
c1vv = append(c1vv, i)
}
c2vv := make([]int, 0)
for i := range c2v {
c2vv = append(c2vv, i)
}
ancmaps[n.Chs[0]] = c1vv
ancmaps[n.Chs[1]] = c2vv
if len(n.Chs) > 0 {
tnam := ""
for i, j := range ancmaps[n] {
tnam += strconv.Itoa(j)
if i+1 != len(ancmaps[n]) {
tnam += ","
}
}
n.Nam = "[&values={" + tnam + "}]"
}
}
}
// EstParsBL estimate the parsimony branch lengths
func EstParsBL(t *Tree, numstates int, patternval []float64, totalsites int) {
nsites := len(patternval)
for _, n := range t.Post {
n.FData["parsbl"] = 0.0
}
for i := 0; i < nsites; i++ {
for _, n := range t.Pre {
if t.Rt == n {
minj := 0
minv := n.Data[i][0]
for j := 1; j < numstates; j++ {
if n.Data[i][j] < minv {
minj = j
minv = n.Data[i][j]
}
}
n.IData["anc"] = minj
} else {
from := n.Par.IData["anc"]
if len(n.Chs) > 0 {
minj := math.MaxInt64
minv := math.MaxFloat64
for j := 0; j < numstates; j++ {
add := 0.
if j != from {
add += 1.
}
if (n.Data[i][j] + add) < minv {
minj = j
minv = (n.Data[i][j] + add)
}
}
if minj != from {
n.FData["parsbl"] += 1. * patternval[i]
}
n.IData["anc"] = minj
} else {
minj := 0
for j := 0; j < numstates; j++ {
if n.Data[i][j] == 1.0 {
minj = j
break
}
}
if minj != from {
n.FData["parsbl"] += 1. * patternval[i]
}
}
}
}
}
for _, n := range t.Post {
n.Len = math.Max(10e-10, n.FData["parsbl"]/(float64(totalsites)))
//n.Len = math.Max(0.0, n.FData["parsbl"])
}
}