forked from AlphaGenes/tinyhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBurrowsWheelerLibrary.py
456 lines (369 loc) · 15.5 KB
/
BurrowsWheelerLibrary.py
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import random
import numpy as np
import numba
from numba import njit, jit, int8, int32,int64, boolean, deferred_type, optional, float32
from numba.experimental import jitclass
from collections import OrderedDict
try:
profile
except:
def profile(x):
return x
#####################################
### ###
### Burrows Wheeler ###
### ###
#####################################
class BurrowsWheelerLibrary():
def __init__(self, haplotypeList):
self.library = createBWLibrary(np.array(haplotypeList))
self.hapList = haplotypeList
self.nHaps = len(haplotypeList)
def getHaplotypeMatches(self, haplotype, start, stop):
nHaps, hapIndexes = getConsistentHaplotypes(self.library, haplotype, start, stop)
haps = [(self.hapList[hapIndexes[index, 0]], hapIndexes[index, 1]) for index in range(nHaps)]
return haps
@profile
def getBestHaplotype(self, weights, start, stop):
index = getHaplotypesPlusWeights(self.library, weights, start, stop)
return self.hapList[index][start:stop]
jit_BurrowsWheelerLibrary_spec = OrderedDict()
jit_BurrowsWheelerLibrary_spec['a'] = int64[:,:]
jit_BurrowsWheelerLibrary_spec['d'] = int64[:,:]
jit_BurrowsWheelerLibrary_spec['zeroOccPrev'] = int64[:,:]
jit_BurrowsWheelerLibrary_spec['nZeros'] = int64[:]
jit_BurrowsWheelerLibrary_spec['haps'] = int8[:,:]
@jitclass(jit_BurrowsWheelerLibrary_spec)
class jit_BurrowsWheelerLibrary():
def __init__(self, a, d, nZeros, zeroOccPrev, haps):
self.a = a
self.d = d
self.nZeros = nZeros
self.zeroOccPrev = zeroOccPrev
self.haps = haps
def getValues(self):
return (self.a, self.d, self.nZeros, self.zeroOccPrev, self.haps)
def update_state(self, state, index):
pass
def get_null_state(self, value, index):
if value == 0:
lowerR = 0
upperR = nZeros[stop-1]
if value == 1:
lowerR = nZeros[stop-1]
upperR = nHaps
pass
@njit
def createBWLibrary(haps):
#Definitions.
# haps : a list of haplotypes
# a : an ordering of haps in lexographic order.
# d : Number of loci of a[i,j+k] == a[i,-1, j+k]
nHaps = haps.shape[0]
nLoci = haps.shape[1]
a = np.full(haps.shape, 0, dtype = np.int64)
d = np.full(haps.shape, 0, dtype = np.int64)
nZerosArray = np.full(nLoci, 0, dtype = np.int64)
zeros = np.full(nHaps, 0, dtype = np.int64)
ones = np.full(nHaps, 0, dtype = np.int64)
dZeros = np.full(nHaps, 0, dtype = np.int64)
dOnes = np.full(nHaps, 0, dtype = np.int64)
nZeros = 0
nOnes = 0
for j in range(nHaps):
if haps[j, nLoci-1] == 0:
zeros[nZeros] = j
if nZeros == 0:
dZeros[nZeros] = 0
else:
dZeros[nZeros] = 1
nZeros += 1
else:
ones[nOnes] = j
if nOnes == 0:
dOnes[nOnes] = 0
else:
dOnes[nOnes] = 1
nOnes += 1
if nZeros > 0:
a[0:nZeros, nLoci-1] = zeros[0:nZeros]
d[0:nZeros, nLoci-1] = dZeros[0:nZeros]
if nOnes > 0:
a[nZeros:nHaps, nLoci-1] = ones[0:nOnes]
d[nZeros:nHaps, nLoci-1] = dOnes[0:nOnes]
nZerosArray[nLoci-1] = nZeros
for i in range(nLoci-2, -1, -1) :
zeros = np.full(nHaps, 0, dtype = np.int64)
ones = np.full(nHaps, 0, dtype = np.int64)
dZeros = np.full(nHaps, 0, dtype = np.int64)
dOnes = np.full(nHaps, 0, dtype = np.int64)
nZeros = 0
nOnes = 0
dZerosTmp = -1 #This is a hack.
dOnesTmp = -1
for j in range(nHaps) :
dZerosTmp = min(dZerosTmp, d[j,i+1])
dOnesTmp = min(dOnesTmp, d[j,i+1])
if haps[a[j, i+1], i] == 0:
zeros[nZeros] = a[j, i+1]
dZeros[nZeros] = dZerosTmp + 1
nZeros += 1
dZerosTmp = nLoci
else:
ones[nOnes] = a[j, i+1]
dOnes[nOnes] = dOnesTmp + 1
nOnes += 1
dOnesTmp = nLoci
if nZeros > 0:
a[0:nZeros, i] = zeros[0:nZeros]
d[0:nZeros, i] = dZeros[0:nZeros]
if nOnes > 0:
a[nZeros:nHaps, i] = ones[0:nOnes]
d[nZeros:nHaps, i] = dOnes[0:nOnes]
nZerosArray[i] = nZeros
#I'm going to be a wee bit sloppy in creating zeroOccPrev
#Not defined at 0 so start at 1.
zeroOccPrev = np.full(haps.shape, 0, dtype = np.int64)
for i in range(1, nLoci):
count = 0
for j in range(0, nHaps):
if haps[a[j, i], i-1] == 0:
count += 1
zeroOccPrev[j, i] = count
library = jit_BurrowsWheelerLibrary(a, d, nZerosArray, zeroOccPrev, haps)
return library
@jit(nopython=True, nogil=True)
def getConsistentHaplotypes(bwLibrary, hap, start, stop):
a, d, nZeros, zeroOccPrev, haps = bwLibrary.getValues()
nHaps = a.shape[0]
nLoci = a.shape[1]
intervals = np.full((nHaps, 2), 0, dtype = np.int64)
intervals_new = np.full((nHaps, 2), 0, dtype = np.int64)
nIntervals = 0
nIntervals_new = 0
#Haps go from 0 to nHaps-1. Loci go from start to stop-1 (inclusive).
#The first hap with one is nZeros. The last hap with zero is nZeros -1.
#Last loci is stop -1
#These are split out because they represent *distinct* haplotypes.
#Maybe could do this with tuple and list append but *shrug*.
if hap[stop-1] == 0 or hap[stop-1] == 9:
lowerR = 0
upperR = nZeros[stop-1]
if upperR >= lowerR:
intervals[nIntervals, 0] = lowerR
intervals[nIntervals, 1] = upperR
nIntervals += 1
if hap[stop-1] == 1 or hap[stop-1] == 9:
lowerR = nZeros[stop-1]
upperR = nHaps
if upperR >= lowerR:
intervals[nIntervals, 0] = lowerR
intervals[nIntervals, 1] = upperR
nIntervals += 1
#Python indexing is annoying.
#Exclude stop and stop-1, include start.
#Intervals are over haplotypes.
for i in range(stop-2, start-1, -1):
# print(intervals[0:nIntervals,:])
nIntervals_new = 0
#Doing it on interval seems to make marginally more sense.
for interval in range(nIntervals):
int_start = intervals[interval, 0]
int_end = intervals[interval, 1]
if hap[i] == 0 or hap[i] == 9:
if int_start == 0:
lowerR = 0
else:
lowerR = zeroOccPrev[int_start-1, i+1]
upperR = zeroOccPrev[int_end-1, i+1] #Number of zeros in the region.
if upperR > lowerR: #Needs to be greater than. Regions no longer inclusive.
# print("Added 0:", int_start, int_end, "->>", lowerR, upperR)
intervals_new[nIntervals_new, 0] = lowerR
intervals_new[nIntervals_new, 1] = upperR
nIntervals_new += 1
if hap[i] == 1 or hap[i] == 9:
# of ones between 0 and k (k inclusive) is k+1 - number of zeros.
if int_start == 0:
lowerR = nZeros[i]
else:
lowerR = nZeros[i] + (int_start - zeroOccPrev[int_start-1, i+1])
upperR = nZeros[i] + (int_end - zeroOccPrev[int_end-1, i+1])
if upperR > lowerR:
# print("Added 1:", int_start, int_end, "->>", lowerR, upperR)
intervals_new[nIntervals_new, 0] = lowerR
intervals_new[nIntervals_new, 1] = upperR
nIntervals_new += 1
# else:
# print(i, "interval rejected:", int_start, int_end, "->", upperR, lowerR)
#This is basically intervals = intervals_new
for j in range(nIntervals_new):
intervals[j, 0] = intervals_new[j, 0]
intervals[j, 1] = intervals_new[j, 1]
nIntervals = nIntervals_new
# print("Finished", i, "->", nIntervals)
# print(intervals[0:nIntervals,:])
hapIndexes = np.full((nHaps, 2), 0, dtype = np.int64)
nHapsAssigned = 0
for i in range(nIntervals):
int_start = intervals[i, 0]
int_end = intervals[i, 1]
hapIndexes[nHapsAssigned, 0] = a[int_start,start]
hapIndexes[nHapsAssigned, 1] = int_end - int_start
nHapsAssigned +=1
return (nHapsAssigned, hapIndexes)
# def printSortAt(loci, library):
# a, d, nZeros, zeroOccPrev, haps = library.getValues()
# vals = haps[a[:,loci],:]
# for i in range(vals.shape[0]):
# print(i, ' '.join(map(str, vals[i,:])) )
# # print(zeroOccPrev[:,:])
# hapLib = [np.array([1, 0, 0, 0, 0, 0, 1], dtype = np.int8),
# np.array([0, 1, 0, 0, 0, 1, 0], dtype = np.int8),
# np.array([0, 1, 0, 0, 0, 1, 0], dtype = np.int8),
# np.array([0, 1, 0, 0, 0, 1, 0], dtype = np.int8),
# np.array([0, 0, 1, 0, 1, 0, 0], dtype = np.int8),
# np.array([1, 1, 1, 0, 0, 0, 0], dtype = np.int8),
# np.array([0, 0, 1, 0, 1, 0, 0], dtype = np.int8),
# np.array([1, 1, 1, 0, 1, 0, 0], dtype = np.int8),
# np.array([0, 0, 0, 1, 0, 0, 0], dtype = np.int8),
# np.array([0, 1, 1, 1, 0, 0, 0], dtype = np.int8),
# np.array([0, 1, 1, 1, 0, 0, 0], dtype = np.int8),
# np.array([1, 1, 0, 1, 0, 0, 0], dtype = np.int8),
# np.array([0, 0, 1, 0, 1, 0, 0], dtype = np.int8),
# np.array([0, 0, 1, 0, 1, 0, 0], dtype = np.int8),
# np.array([0, 0, 1, 0, 1, 0, 0], dtype = np.int8),
# np.array([0, 0, 1, 0, 1, 0, 0], dtype = np.int8),
# np.array([0, 0, 1, 0, 1, 0, 0], dtype = np.int8)]
# bwlib = BurrowsWheelerLibrary(hapLib)
# # printSortAt(0, bwlib.library)
# printSortAt(6, bwlib.library); print("")
# printSortAt(5, bwlib.library); print("")
# printSortAt(4, bwlib.library); print("")
# printSortAt(3, bwlib.library); print("")
# printSortAt(2, bwlib.library); print("")
# printSortAt(1, bwlib.library); print("")
# printSortAt(0, bwlib.library); print("")
# # print(bwlib.getHaplotypeMatches(haplotype = np.array([0, 0, 0], dtype = np.int8), start = 0, stop = 3))
# tmp = (bwlib.getHaplotypeMatches(haplotype = np.array([9, 9, 9, 9, 9, 9, 9], dtype = np.int8), start = 0, stop = 7))
# for key, value in tmp:
# print(key, value)
@njit
def getConsistentHaplotypesBruteForce(bwLibrary, hap, start, stop):
hap = hap[start:stop]
a, d, nZeros, zeroOccPrev, haps = bwLibrary.getValues()
recodedHaps = haps[:, start:stop]
nHaps = recodedHaps.shape[0]
nLoci = recodedHaps.shape[1]
consistent = np.full(nHaps, 0, dtype = np.int64)
#Last correct index
lastIndex = -1
for j in range(nHaps):
#Otherwise, we have not enough information and need to search.
add = True
for i in range(nLoci):
if hap[i] != 9 :
if recodedHaps[j, i] != hap[i]:
add = False
if add:
consistent[j] = 1
hapIndexes = np.full((nHaps, 2), 0, dtype = np.int64)
nHapsAssigned = 0
for i in range(nHaps):
if consistent[i] > 0:
# hapIndexes[nHapsAssigned, 0] = a[i,start]
hapIndexes[nHapsAssigned, 0] = i
hapIndexes[nHapsAssigned, 1] = consistent[i]
nHapsAssigned +=1
return (nHapsAssigned, hapIndexes)
@njit
def getHaplotypesPlusWeights(bwLibrary, weights, start, stop):
#Weights are weights to the original haplotypes (haps)
a, d, nZeros, zeroOccPrev, haps = bwLibrary.getValues()
recodedWeights = weights[a[:, start]]
nHaps = d.shape[0]
nLoci = stop - start
bestLoci = -1
bestWeight = -1
currentLoci = 0
currentWeight = 0
for j in range(nHaps):
#Will need to double check this. This code will be slow!
if d[j, start] < nLoci:
#Process the last haplotype before moving on.
if currentWeight > bestWeight :
bestLoci = currentLoci
bestWeight = currentWeight
currentLoci = j
currentWeight = 0
currentWeight += recodedWeights[j]
#Make sure we check the last haplotype.
if currentWeight > bestWeight :
bestLoci = currentLoci
bestWeight = currentWeight
#REMEMBER TO RECODE
return a[bestLoci, start]
#Older version that doesn't use all of the meta data we have.
# @jit(nopython=True, nogil=True)
# def getConsistentHaplotypes(bwLibrary, hap, start, stop):
# a, d, nZeros, zeroOccPrev, haps = bwLibrary.getValues()
# hap = hap[start:stop]
# recodedHaps = haps[a[:, start], start:stop]
# nHaps = recodedHaps.shape[0]
# nLoci = recodedHaps.shape[1]
# consistent = np.full(nHaps, 0, dtype = np.int64)
# lastCorrect = -1
# firstError = nLoci + 1
# #Last correct index
# lastIndex = -1
# for j in range(nHaps):
# #Basically, we know how much overlap there was with the previous haplotype.
# #We can use that to get a better estimate of where this one will be correct.
# #By definition, all of 0 -> d[j, start]-1 inclusive is the same.
# #All of 0 -> lastCorrect (inclusive) is correct.
# #First error is the position of the first error. If firstError < nLoci, this is a problem. (nLoci is out of our bounds)
# lastCorrect = min(lastCorrect, d[j, start]-1)
# if firstError > d[j,start]-1: firstError = nLoci
# #Two elif statements.
# #First can we spot an error?
# #Second if no error's exist, are we sure that the entire haplotype is right.
# if firstError < nLoci: #or equal?
# consistent[j] = 0
# lastIndex = -1
# #nLoci is the last position we care about (nLoci is out of our bounds). If nLoci is correct, then we're good).
# elif lastCorrect >= nLoci-1:
# #Adding in some short circuit code to prevent duplication
# # lastIndex = -1 ###THIS LINE TOGGLES DUPLICATION PREVENTION
# if lastIndex != -1:
# consistent[lastIndex] += 1
# else:
# consistent[j] = 1
# lastIndex = j
# else:
# #Otherwise, we have not enough information and need to search.
# #Last correct is the last known correct loci.
# stopIters = False
# i = lastCorrect
# while not stopIters:
# i = i+1 #We know that the last correct loci is correct. So we're safe to start at last correct +1
# if hap[i] != 9 and recodedHaps[j, i] != hap[i]:
# lastCorrect = i-1
# firstError = i
# stopIters = True
# elif i == nLoci-1:
# stopIters = True
# lastCorrect = nLoci-1
# firstError = nLoci
# if firstError < nLoci:
# consistent[j] = 0
# lastIndex = -1
# elif lastCorrect >= nLoci-1: #This will probably be nLoci-1 since that is where our search stops.
# consistent[j] = 1
# lastIndex = j
# hapIndexes = np.full((nHaps, 2), 0, dtype = np.int64)
# nHapsAssigned = 0
# for i in range(nHaps):
# if consistent[i] > 0:
# hapIndexes[nHapsAssigned, 0] = a[i,start]
# hapIndexes[nHapsAssigned, 1] = consistent[i]
# nHapsAssigned +=1
# return (nHapsAssigned, hapIndexes)