-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.py
More file actions
268 lines (244 loc) · 7.92 KB
/
preprocess.py
File metadata and controls
268 lines (244 loc) · 7.92 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python3
from Compiler.GC.types import sbitint, sbitintvec, sbits
from Compiler.types import cint, sint, sintbit, Array, Matrix
from Compiler.library import for_range_opt, for_range_parallel, print_ln
from Compiler.util import min as secmin
sb = sbits.get_type(1)
def xor_nucleotids(N1, N2):
xor = Array(2, sb)
xor[0] = N1[0] ^ N2[0]
xor[1] = N1[1] ^ N2[1]
return xor
def or_nucleotid(N):
return N[0].bit_or(N[1])
def equal_nucleotids(N1, N2):
return or_nucleotid(xor_nucleotids(N1, N2)).bit_not()
def bitEquality(a, b, t):
p = ((t-(len(b)-len(a))) // 2)
k = -p
E = Matrix(len(a), t+4, sintbit)
E.assign_all(1)
for i in range (0, len(a)):
for j in range(max(k+i, 0), min(k+t+i, len(b))):
E[i][p+(j-i)] = sintbit(equal_nucleotids(a[i], b[j]).bit_not())
# print_ln('%s', E.reveal())
return E, 0
def equalityParallel(a, b, t: int):
'''Compute the equality matrix parallely'''
p = ((t-(len(a)-len(b))) // 2)
k = -p
E = Matrix(len(a), t, sintbit)
@for_range_opt(0, len(a))
def _(i):
@for_range_opt((k+i).max(0), (k+t+i).min(len(b)))
def _(j):
E[i][p+(j-i)] = a[i].not_equal(b[j])
return E
def equality(a, b, t: int):
'''Compute the equality matrix'''
p = ((t-(len(b)-len(a))) // 2)
k = -p
E = Matrix(len(a), t, sintbit)
E.assign_all(1)
compareCount = 0
for i in range (0, len(a)):
for j in range(max(k+i, 0), min(k+t+i, len(b))):
compareCount+=1
E[i][p+(j-i)] = a[i].not_equal(b[j])
return E, compareCount
def equality_gc(a, b, t: int):
'''Compute the equality matrix parallelized'''
p = ((t-(len(b)-len(a))) // 2)
k = -p
gc_bool = sbitint.get_type(2) # Bit length for 0/1
gc_vec_bool = sbitintvec.get_type(2)
E = [[gc_bool(0) for _ in range(t)] for _ in range(len(a))]
compareCount = 0
# Parallelized comparison
seq1 = [a[i] for i in range(len(a)) for _ in range(max(k+i, 0), min(k+t+i, len(b)))]
seq2 = [b[j] for i in range(len(a)) for j in range(max(k+i, 0), min(k+t+i, len(b)))]
comp_array = gc_vec_bool(seq1).not_equal(gc_vec_bool(seq2)).elements()
vv = 0
for i in range (0, len(a)):
for j in range(max(k+i, 0), min(k+t+i, len(b))):
compareCount+=1
# E[i][p+(j-i)] = gc_bool(a[i].not_equal(b[j])) # Regular comparison
E[i][p+(j-i)] = gc_bool(comp_array[vv])
vv+=1
return E, compareCount
def load_D_box(E, m: int, n: int, t: int, p: int, pp):
mv = max(m, n)
D = Matrix(m+1, n+1, sint)
D.assign_all(sint(mv))
for i in range(0, m+1):
D[i][0] = i
for j in range(1, n+1):
D[0][j] = j
for i in range(0, p+1):
D[i+1][1] = secmin([D[i][0]+E[i][pp-i], D[i][1]+1, D[i+1][0]+1])
for j in range(1, t-p):
D[1][j+1] = secmin([D[0][j]+E[0][pp+j], D[1][j]+1, D[0][j+1]+1])
#print_ln('%s', D.reveal())
return D
def load_D_box_with_ab(a, b, m: int, n: int, t: int, p: int):
mv = max(m, n)
D = Matrix(m+1, n+1, sint)
D.assign_all(sint(mv))
for i in range(0, m+1):
D[i][0] = i
for j in range(1, n+1):
D[0][j] = j
for i in range(0, p+1):
D[i+1][1] = (D[i][0]+a[i].not_equal(b[0])).min(D[i][1]+1).min(D[i+1][0]+1)
for j in range(1, t-p):
D[1][j+1] = (D[0][j]+a[0].not_equal(b[j])).min(D[1][j]+1).min(D[0][j+1]+1)
#print_ln('%s', D.reveal())
return D
def load_D_ukk(m: int, n: int, t):
'''Loads the initial D matrix'''
mv = max(m, n)
D = Matrix(m+1, n+1, sint)
D.assign_all(mv)
for i in range(0, t):
D[i][0] = sint(i)
for j in range(1, t):
D[0][j] = sint(j)
return D
def load_D_ukk_gc(m: int, n: int, t, bitlen):
'''Loads the initial D matrix'''
mv = max(m, n)
si32 = sbitint.get_type(bitlen)
# D = Matrix(m+1, n+1, si32)
# for i in range(m+1):
# for j in range(n+1):
# D[i][j] = si32(mv)
D = [[si32(mv) for _ in range(n+1)] for _ in range(m+1)]
for i in range(0, t):
D[i][0] = si32(i)
for j in range(1, t-1):
D[0][j] = si32(j)
return D
def iterateDiagonal(si, sj, ei, ej, m, n, k, t):
if si == m-1 or (k+si+1) > sj:
sj+=1
else:
si+=1
if ej == n-1 or (k+ei+t) <= (ej+1):
ei+=1
else:
ej+=1
return si, sj, ei, ej
def iterateDiagonal_with_p(si, sj, ei, ej, p, m, n, k, t):
if si == m-1 or (k+si+1) > sj:
sj+=1
p+=1
else:
si+=1
p-=1
if ej == n-1 or (k+ei+t) <= (ej+1):
ei+=1
else:
ej+=1
return si, sj, ei, ej, p
def iterateTauedDiagonal(si, sj, ei, ej, m, n, k, t, tau):
if si == m-1-tau or (k+si+tau) > sj:
sj+=tau
else:
si+=tau
if ej == n-1-tau or (k+ei+t) <= (ej+tau):
ei+=tau
else:
ej+=tau
return si, sj, ei, ej
def iterateTauedDiagonal_with_p(si, sj, ei, ej, m, n, k, t, tau, p):
if si == m-1-tau or (k+si+tau) > sj:
sj+=tau
p+=tau
else:
si+=tau
p-=tau
if ej == n-1-tau or (k+ei+t) <= (ej+tau):
ei+=tau
else:
ej+=tau
return si, sj, ei, ej, p
# Ideas from the past: (Not used anymore)
def load_leaks(E, t: int, tau: int, m: int, n: int):
p = ((t-(n-m)) // 2)
k = -p
leakedComparisons = Array((t//tau)*(n//tau) + m, sintbit)
v = 0
for i in range(0, m-tau, tau):
for j in range(0, n-tau, tau):
if j < k or j >= k+t:
continue
test = sintbit(0)
for tx in range(1, tau+1):
test = test | E[i+tx][j+tx-max(0, (-p+i+tx))]
leakedComparisons[v] = test
v+=1
k+=tau
return leakedComparisons
def load_leaks_multi_proc(E, t: int, tau: int, m: int, n: int):
p = ((t-(n-m)) // 2)
k = -p
kd = p % tau
leakedComparisons = Matrix(m, t+tau, sintbit)
kstart = Array(m, cint)
kend = Array(m, cint)
for i in range(0, m, tau):
kstart[i] = 0 if k+i < 0 else k+i+kd
kend[i] = min(n-tau, k+i+t)
@for_range_opt(0, m-tau, tau)
def _(i):
@for_range_opt(kstart[i], kend[i], tau)
def __(j):
test = sintbit(0)
for tx in range(1, tau+1):
test = test | E[i+tx][j+tx-((-p+i+tx).max(0))]
leakedComparisons[i][j-((-p+i).max(0))] = test
return leakedComparisons
def load_leaks_leading(a, b, tau: int, m: int, n: int):
leakedComparisons = Array(max(m, n)//tau, sintbit)
v = 0
for i in range(0, m-tau, tau):
test = sintbit(0)
for tx in range(1, tau+1):
test = test | a[i+tx].not_equal(b[i+tx])
leakedComparisons[v] = test
v+=1
return leakedComparisons
def cumulated_leaks_leading(a, b, tau: int, m: int, n: int):
leakedComparisons = Array(max(m, n)//tau, sint)
v = 0
for i in range(-1, m-tau, tau):
test = sintbit(0)
for tx in range(1, tau+1):
test = test | a[i+tx].not_equal(b[i+tx])
# Compares [-1] for first. Beware when parallelising.
leakedComparisons[v] = leakedComparisons[v-1] + test
v+=1
return leakedComparisons
def load_mx_leading(a, b, tau: int, m: int, n: int):
mx = sint(0)
for i in range(0, m-tau, tau):
test = sintbit(0)
for tx in range(1, tau+1):
test = test | a[i+tx].not_equal(b[i+tx])
mx += test
return mx
def find_opt_endonly(a, b, t: int, m: int, n: int):
p = (t-(n-m)) // 2 # Position of leading diagonal
k = -p # Mapping of first element in array
cumulative = Array(t, sint)
for i in range(m):
start = max(k+i, 0)
idx = max(p,0)
for j in range(start, min(k+t+i, len(b))):
cumulative[j+idx-start] = cumulative[j+idx-start] + a[i].not_equal(b[j])
p-=1
p = (t-(n-m)) // 2
for i in range(t):
cumulative[i] = cumulative[i]+(2*abs(p-i))
# print_ln("%s", cumulative.reveal())
return secmin(cumulative)