-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
213 lines (196 loc) · 4.23 KB
/
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
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
package inducedates
import (
"math"
"strconv"
)
// CalcSliceIntDifferenceInt calculate the size of the difference (set) between two int slices
func CalcSliceIntDifferenceInt(a, b []int) int {
mb := map[int]bool{}
for _, x := range b {
mb[x] = true
}
ab := 0
for _, x := range a {
if _, ok := mb[x]; !ok {
ab++
}
}
return ab
}
// CalcSliceIntDifference calculate the difference (set) between two int slices
func CalcSliceIntDifference(a, b []int) []int {
mb := map[int]bool{}
for _, x := range b {
mb[x] = true
}
ab := []int{}
for _, x := range a {
if _, ok := mb[x]; !ok {
ab = append(ab, x)
}
}
return ab
}
// PCalcSliceIntDifferenceInt calculate the size of the difference (set) between two int slices in parallel
// used for: RF distance where the bpts is the tree index -> bipart index list map
func PCalcSliceIntDifferenceInt(bpts map[int][]int, jobs <-chan []int, results chan<- []int) {
for j := range jobs {
in1, in2 := j[0], j[1]
mb := map[int]bool{}
for _, x := range bpts[in2] {
mb[x] = true
}
ab := 0
for _, x := range bpts[in1] {
if _, ok := mb[x]; !ok {
ab++
}
}
results <- []int{in1, in2, ab}
}
}
//PCalcRFDistancesPartial calculates the partial rf, bpts is the tree index, bipart list,
// bps is the list of biparts
func PCalcRFDistancesPartial(bpts map[int][]int, bps []Bipart, jobs <-chan []int, results chan<- []int) {
for j := range jobs {
in1, in2 := j[0], j[1]
mb := map[string]bool{}
for _, x := range bpts[in1] {
for _, y := range bpts[in2] {
if bps[x].ConflictsWith(bps[y]) {
mb["t1"+string(x)] = true
mb["t2"+string(y)] = true
}
}
}
ab := 0
for _, x := range mb {
if x == true {
ab++
}
}
results <- []int{in1, in2, ab}
}
}
// IntSliceIntersects checks to see whether two int slices intersect
func IntSliceIntersects(a, b []int) (rb bool) {
rb = false
for _, k := range a {
for _, l := range b {
if k == l {
rb = true
return
}
}
}
return
}
// IntSliceContains checks to see if the int slice contains an int and returns the bool
func IntSliceContains(is []int, s int) (rb bool) {
rb = false
for _, a := range is {
if a == s {
rb = true
return
}
}
return
}
// IntMapIntersects checks to see if the two map[int]bool intersect (in the set sense)
func IntMapIntersects(s1 map[int]bool, s2 map[int]bool) (in bool) {
in = false
for k := range s1 {
if s2[k] {
in = true
return
}
}
return
}
// IntMapIntersects2 checks to see if the two map[int]bool intersect (in the set sense)
// with at least 2 matches
func IntMapIntersects2(s1 map[int]bool, s2 map[int]bool) (in bool) {
in = false
count := 0
for k := range s1 {
if s2[k] {
count++
if count >= 2 {
in = true
return
}
}
}
return
}
// IntMapSetString get a string for printing off a set
func IntMapSetString(intmap map[int]bool) (s string) {
s = ""
for m := range intmap {
s += strconv.Itoa(m) + " "
}
return
}
// StringSliceContains tells you whether the e string is in the slice
func StringSliceContains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
// NodeSliceContains tells you whether the e string is in the slice
func NodeSliceContains(s []*Node, e *Node) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
// SumFloatVec sum the float vectors
func SumFloatVec(x []float64) (s float64) {
for _, a := range x {
s += a
}
return
}
// SumLogExp sum log of exps
func SumLogExp(a, b float64) float64 {
return a + log1exp(b-a)
}
func log1exp(x float64) float64 {
if x > 35 {
return x
}
if x < -10 {
return math.Exp(x)
}
return math.Log1p(math.Exp(x))
}
// NodeSlicePosition take a *[]Node slice and teh get the index of the element node
func NodeSlicePosition(sl []*Node, nd *Node) (x int) {
x = -1
for p, v := range sl {
if v == nd {
x = p
return
}
}
return
}
// Round to the nearest place probably val=num, roundOn = 0.5 places = 5
func Round(val float64, roundOn float64, places int) (newVal float64) {
var round float64
pow := math.Pow(10, float64(places))
digit := pow * val
_, div := math.Modf(digit)
if div >= roundOn {
round = math.Ceil(digit)
} else {
round = math.Floor(digit)
}
newVal = round / pow
return
}