-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithms.go
254 lines (212 loc) · 5.42 KB
/
algorithms.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package main
import (
"fmt"
"os"
"strings"
"time"
)
func compareFiles(dataA []record, dataB []record, alg string) (runTimes runTimesArr) {
runNum := 100
runTimes = make(runTimesArr, 0)
scoresColl := make(scoresArr, 0)
if alg == "nwa" {
for x := 0; x < runNum; x++ {
for y := 0; y < runNum; y++ {
var timeRow times
var scoreRow scores
startTime := time.Now()
seqA := dataA[x].seq
seqB := dataB[y].seq
scoreRow.score = nwa(seqA, seqB)
keyA := dataA[x].name
keyB := dataB[y].name
scoreRow.key = keyA + "," + keyB
scoresColl = append(scoresColl, scoreRow)
endTime := time.Now()
timeRow.strLen = (len(seqA) * len(seqB))
timeRow.runTime = int(endTime.Sub(startTime))
runTimes = append(runTimes, timeRow)
}
}
} else if alg == "hnwa" {
for x := 0; x < runNum; x++ {
for y := 0; y < runNum; y++ {
var timeRow times
var scoreRow scores
startTime := time.Now()
seqA := dataA[x].seq
seqB := dataB[y].seq
scoreRow.score = hnwa(seqA, seqB)
keyA := dataA[x].name
keyB := dataB[y].name
scoreRow.key = keyA + "," + keyB
scoresColl = append(scoresColl, scoreRow)
endTime := time.Now()
timeRow.strLen = (len(seqA) * len(seqB))
timeRow.runTime = int(endTime.Sub(startTime))
runTimes = append(runTimes, timeRow)
}
}
} else if alg == "snwa" {
for x := 0; x < runNum; x++ {
for y := 0; y < runNum; y++ {
var timeRow times
var scoreRow scores
startTime := time.Now()
seqA := dataA[x].seq
seqB := dataB[y].seq
scoreRow.score = snwa(seqA, seqB)
keyA := dataA[x].name
keyB := dataB[y].name
scoreRow.key = keyA + "," + keyB
scoresColl = append(scoresColl, scoreRow)
endTime := time.Now()
timeRow.strLen = (len(seqA) * len(seqB))
timeRow.runTime = int(endTime.Sub(startTime))
runTimes = append(runTimes, timeRow)
}
}
} else {
fmt.Println("The algorithm you configured in the config-file does not exist")
fmt.Print("Please choose on of the following three: \n- nwa for Needleman-Wunsch-Algorithm \n- snwa for a splitted nwa where the value is not exact \n-hnwa for Hirschberg-Needleman-Wunsch (not done yet)\n")
os.Exit(1)
}
saveScore(scoresColl)
return runTimes
}
func compareFilesTest(dataA []record, dataB []record, alg string) (runTimes runTimesArr) {
runNum := 10
runTimes = make(runTimesArr, 0)
scoresColl := make(scoresArr, 0)
if alg == "nwa" {
for x := 0; x < runNum; x++ {
for y := 0; y < runNum; y++ {
var timeRow times
startTime := time.Now()
seqA := dataA[1].seq
seqB := dataB[1].seq
nwa(seqA, seqB)
endTime := time.Now()
timeRow.strLen = (len(seqA) * len(seqB))
timeRow.runTime = int(endTime.Sub(startTime))
runTimes = append(runTimes, timeRow)
}
}
} else if alg == "snwa" {
for x := 0; x < runNum; x++ {
for y := 0; y < runNum; y++ {
var timeRow times
startTime := time.Now()
seqA := dataA[1].seq
seqB := dataB[1].seq
snwa(seqA, seqB)
endTime := time.Now()
timeRow.strLen = (len(seqA) * len(seqB))
timeRow.runTime = int(endTime.Sub(startTime))
runTimes = append(runTimes, timeRow)
}
}
} else {
err := fmt.Errorf("somehow an error occured here, sorry, that shouldn't happen")
check(err)
}
saveScore(scoresColl)
return runTimes
}
func nwa(seqA, seqB string) int {
a := len(seqA) + 1
b := len(seqB) + 1
match := 1
mismatch := -1
gap := -1
numMat := make([][]int, a)
for i := range numMat {
numMat[i] = make([]int, b)
}
arrMat := make([][]int, a)
for i := range arrMat {
arrMat[i] = make([]int, b)
}
for i := 0; i < a; i++ {
numMat[i][0] = mismatch * i
}
for j := 0; j < b; j++ {
numMat[0][j] = mismatch * j
}
for i := 1; i < a; i++ {
for j := 1; j < b; j++ {
score := 0
if seqA[i-1] == seqB[j-1] {
score = match
} else {
score = mismatch
}
resMatch := numMat[i-1][j-1] + score
resDel := numMat[i-1][j] + gap
resIns := numMat[i][j-1] + gap
numMat[i][j], arrMat[i][j] = max(resMatch, resDel, resIns)
}
}
return numMat[a-1][b-1]
}
func hnwa(seqA, seqB string) int {
fmt.Println("not working at the moment, sorry")
os.Exit(1)
return 1
}
func snwa(seqA, seqB string) int {
//calculate a point where a letter is the same in both strings nearest to the middle
splitterA, splitterB := findSame(seqA, seqB)
var score int
if splitterA == 0 || splitterB == 0 {
score = nwa(seqA, seqB)
} else {
seqAA, seqAB := bisectString(seqA, splitterA)
seqBA, seqBB := bisectString(seqB, splitterB)
scoreA := nwa(seqAA, seqBA)
scoreB := nwa(seqAB, seqBB)
score = scoreA + scoreB
}
return score
}
func findSame(seqA, seqB string) (int, int) {
for i := 0; i < len(seqA); i++ {
for j := 0; j < len(seqB) && j < (i+5); j++ {
k := snwaHelper(i, len(seqA))
l := snwaHelper(j, len(seqB))
if seqA[k] == seqB[l] {
return k, l
}
}
}
return 0, 0
}
func snwaHelper(a, b int) int {
c := a
if a%2 == 0 {
c = a / 2
} else {
c = int(-(a/2 + 1))
}
d := b/2 + c
return d
}
func bisectString(a string, strLen int) (string, string) {
//make the string into a Sclice, bisect it and return it
strSli := strings.Split(a, "")
firHalf := strings.Join(strSli[:strLen], "")
secHalf := strings.Join(strSli[strLen:], "")
return firHalf, secHalf
}
func max(a, b, c int) (int, int) {
diagonal := 1
up := 2
left := 3
if c < b && b < a {
return a, diagonal
} else if a < b && b < c {
return c, up
} else {
return b, left
}
}