-
Notifications
You must be signed in to change notification settings - Fork 12
/
StrobeMap
executable file
·1148 lines (952 loc) · 59.5 KB
/
StrobeMap
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env python
from __future__ import print_function
import os,sys
import argparse
import copy
# import errno
# from time import time
# import re
# import random
# import parasail
# import pysam
from collections import defaultdict, deque
from sys import stdout
from array import array
from itertools import zip_longest
from modules import help_functions
import operator
MAX = sys.maxsize
# def argmin(values):
# min_index, min_value = min(enumerate(values), key=operator.itemgetter(1))
# return min_index, min_value
def argmin(array):
min_index = array.index(min(array))
min_val = array[min_index]
return min_index, min_val
def rc(string):
rev_nuc = {'A':'T', 'C':'G', 'G':'C', 'T':'A', 'a':'t', 'c':'g', 'g':'c', 't':'a', 'N':'N', 'X':'X', 'n':'n', 'Y':'R', 'R':'Y', 'K':'M', 'M':'K', 'S':'S', 'W':'W', 'B':'V', 'V':'B', 'H':'D', 'D':'H', 'y':'r', 'r':'y', 'k':'m', 'm':'k', 's':'s', 'w':'w', 'b':'v', 'v':'b', 'h':'d', 'd':'h'}
rev_comp = ''.join([rev_nuc[nucl] for nucl in reversed(string)])
return(rev_comp)
def thinner(hash_list, w):
"""
Input: a list with hash values
Output: A list with tuples: (pos in original list, minimim hash value) for each window of w hashes
"""
window_hashes = deque(hash_list[:w])
min_index, curr_min_hash = argmin(window_hashes)
thinned_hash_list = [ (min_index, curr_min_hash) ]
for i in range(w, len(hash_list) + w-1):
if i >= len(hash_list):
new_hash = MAX
else:
new_hash = hash_list[i]
# updating window
discarded_hash = window_hashes.popleft()
window_hashes.append(new_hash)
# we have discarded previous windows minimizer, look for new minimizer brute force
if curr_min_hash == discarded_hash:
min_index, curr_min_hash = argmin(window_hashes)
thinned_hash_list.append( (min_index + i + 1 - w, curr_min_hash) )
# Previous minimizer still in window, we only need to compare with the recently added kmer
elif new_hash < curr_min_hash:
curr_min_hash = new_hash
thinned_hash_list.append( (i, curr_min_hash) )
return thinned_hash_list
def update_queue(q, curr_min, min_index, new_hash, i, start_offset, end_offset):
old_h = q.popleft()
q.append(new_hash)
# we have discarded previous windows minimizer, look for new minimizer brute force
if curr_min == old_h:
min_index, curr_min = argmin(q)
min_index = i + start_offset + min_index
# Previous minimizer still in window, we only need to compare with the recently added kmer
elif new_hash < curr_min:
curr_min = new_hash
min_index = i + end_offset
return min_index, curr_min
def seq_to_hybridstrobes2_iter(seq, k_size, w_min, w_max, prime, w):
if len(seq) < 2*w_max:
return -1, -1, None
hash_list = [ hash(seq[i:i+k_size]) for i in range(len(seq) - k_size +1) ]
n_partition = 3
w_p = (w_max - w_min ) // n_partition
win1 = deque(hash_list[w_min : w_min + w_p])
min_index1, min_w1 = argmin(win1)
min_index1 = min_index1 + w_min
win2 = deque(hash_list[w_min+w_p : w_min + 2*w_p])
min_index2, min_w2 = argmin(win2)
min_index2 = min_index2 + w_min + w_p
win3 = deque(hash_list[w_min+2*w_p : w_min + 3*w_p])
min_index3, min_w3 = argmin(win3)
min_index3 = min_index3 + w_min+2*w_p
# win4 = deque(hash_list[w_min+3*w_p : w_min + 4*w_p])
# min_index4, min_w4 = argmin(win4)
# min_index4 = min_index4 + w_min+2*w_p
for i in range(len(hash_list) - w_min - n_partition*w_p): # temporary iteration
m1 = hash_list[i]
# updating windows
new_w1 = hash_list[i + w_min + w_p]
min_index1, min_w1 = update_queue(win1, min_w1, min_index1, new_w1, i, w_min, w_min + w_p)
# print(len(win1), win1)
new_w2 = hash_list[i + w_min + 2*w_p]
min_index2, min_w2 = update_queue(win2, min_w2, min_index2, new_w2, i, w_min + w_p, w_min + 2*w_p)
new_w3 = hash_list[i+ w_min + 3*w_p]
min_index3, min_w3 = update_queue(win3, min_w3, min_index3, new_w3, i, w_min + 2*w_p, w_min + 3*w_p)
# new_w4 = hash_list[i+ w_min + 4*w_p]
# min_index4, min_w4 = update_queue(win4, min_w4, min_index4, new_w4, i, w_min + 3*w_p, w_min + 4*w_p)
# print(i, min_index1, min_w1, min_w2, min_w3)
r = m1 % n_partition
if r == 0:
# print(i, 1,m1 - min_w1)
yield i, min_index1, m1 - min_w1
elif r == 1:
# print(i, 2,m1 - min_w2)
yield i, min_index2, m1 - min_w2
elif r == 2:
# print(i, 3, m1 - min_w3)
yield i, min_index3, m1 - min_w3
# else:
# # print(i, 3, m1 - min_w3)
# yield i, min_index4, m1 - min_w4
def seq_to_hybridstrobes3_iter(seq, k_size, w_min, w_max, prime, w):
if len(seq) < 2*w_max:
return -1 , -1, -1, None
hash_list = [ hash(seq[i:i+k_size]) for i in range(len(seq) - k_size +1)]
n_partition = 3
w_p = (w_max - w_min ) // n_partition
s1_win1 = deque(hash_list[w_min : w_min + w_p])
min_index1, min_w1 = argmin(s1_win1)
min_index1 = min_index1 + w_min
s1_win2 = deque(hash_list[w_min+w_p : w_min+2*w_p])
min_index2, min_w2 = argmin(s1_win2)
min_index2 = min_index2 + w_min + w_p
s1_win3 = deque(hash_list[w_min+2*w_p : w_max])
min_index3, min_w3 = argmin(s1_win3)
min_index3 = min_index3 + w_min+2*w_p
s2_win1 = deque(hash_list[w_min + w_max : w_min + w_max + w_p])
min_index4, min_w4 = argmin(s2_win1)
min_index4 = min_index4 + w_min + w_max
s2_win2 = deque(hash_list[w_min + w_max + w_p: w_min + w_max + 2*w_p])
min_index5, min_w5 = argmin(s2_win2)
min_index5 = min_index5 + w_min + w_max + w_p
s2_win3 = deque(hash_list[w_min + w_max + 2*w_p: 2*w_max])
min_index6, min_w6 = argmin(s2_win3)
min_index6 = min_index6 + w_min + w_max + 2*w_p
for i in range(len(hash_list) - n_partition*w_max): # temporary iteration
m1 = hash_list[i]
# updating windows
new_w1 = hash_list[i + w_min + w_p]
min_index1, min_w1 = update_queue(s1_win1, min_w1, min_index1, new_w1, i, w_min, w_min + w_p)
# print(len(win1), win1)
new_w2 = hash_list[i + w_min + 2*w_p]
min_index2, min_w2 = update_queue(s1_win2, min_w2, min_index2, new_w2, i, w_min + w_p, w_min + 2*w_p)
new_w3 = hash_list[i + w_max]
min_index3, min_w3 = update_queue(s1_win3, min_w3, min_index3, new_w3, i, w_min + 2*w_p, w_max)
new_w4 = hash_list[i+ w_min + w_max + w_p]
min_index4, min_w4 = update_queue(s2_win1, min_w4, min_index4, new_w4, i, w_min + w_max, w_min + w_max + w_p )
new_w5 = hash_list[i+ w_min + w_max + 2*w_p]
min_index5, min_w5 = update_queue(s2_win2, min_w5, min_index5, new_w5, i, w_min + w_max + w_p, w_min + w_max + 2*w_p )
new_w6 = hash_list[i+ 2*w_max]
min_index6, min_w6 = update_queue(s2_win3, min_w6, min_index6, new_w6, i, w_min + w_max + 2*w_p, 2*w_max)
# print(i, min_index1, min_w1, min_w2, min_w3)
r = m1 % n_partition
if r == 0:
# print(i, 1,m1 - min_w1)
i2 = min_index1
m2 = min_w1
# yield i, min_index1, m1 - min_w1
elif r == 1:
# print(i, 2,m1 - min_w2)
i2 = min_index2
m2 = min_w2
# yield i, min_index2, m1 - min_w2
elif r == 2:
# print(i, 2,m1 - min_w2)
i2 = min_index3
m2 = min_w3
# yield i, min_index2, m1 - min_w2
r2 = (m1 - m2) % n_partition
if r2 == 0:
# print(i, 3, m1 - min_w3)
# print(i, m1, m2, min_w3, m1 - m2 + 2*min_w3)
yield i, i2, min_index4, m1 - m2 + 2*min_w4
elif r2 == 1:
yield i, i2, min_index5, m1 - m2 + 2*min_w5
elif r2 == 2:
# print(i, m1, m2, min_w4, m1 - m2 + 2*min_w4)
yield i, i2, min_index6, m1 - m2 + 2*min_w6
def randstrobe_order3(hash_seq_list, start1, stop1, start2, stop2, hash_m1, k_size, prime):
min_index1, min_value = argmin([ (hash_m1 - hash_seq_list[i][1]) % prime for i in range(start1, stop1)])
min_hash_val = hash_m1 - hash_seq_list[start1 + min_index1][1]
min_index2, min_value = argmin([ (min_hash_val - hash_seq_list[i][1]) % prime for i in range(start2, stop2)])
min_hash_val = min_hash_val + 2*hash_seq_list[start2 + min_index2][1]
return min_index1, min_index2, min_hash_val
def seq_to_randstrobes3_iter(seq, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w):
hash_seq_list = [(i, hash(seq[i:i+k_size])) for i in range(len(seq) - k_size +1) if "N" not in seq[i:i+k_size]]
if w > 1:
hash_seq_list_thinned = thinner([h for i,h in hash_seq_list], w) # produce a subset of positions, still with samme index as in full sequence
else:
hash_seq_list_thinned = hash_seq_list
# assert len(hash_seq_list[:-k_size]) == len(hash_seq_list) - k_size
for (p, hash_m1) in hash_seq_list_thinned: #[:-k_size]:
if p >= len(hash_seq_list) - 2*k_size:
break
# hash_m1 = hash_seq_list[p]
window_p2_start = p + k_size + strobe_w_max_offset + strobe_w_min_offset if p + 2*strobe_w_max_offset <= len(hash_seq_list) else max( (p + k_size + strobe_w_max_offset + strobe_w_min_offset) - (p+k_size+2*strobe_w_max_offset - len(hash_seq_list)), p + 2*k_size )
window_p2_end = min(p + 2*strobe_w_max_offset, len(hash_seq_list))
window_p1_start = p + k_size + strobe_w_min_offset if p + 2*strobe_w_max_offset <= len(hash_seq_list) else max(p+ k_size, len(hash_seq_list) + 2*(strobe_w_min_offset - strobe_w_max_offset))
window_p1_end = min(p + strobe_w_max_offset, len(hash_seq_list)- k_size)
# print(window_p1_start, window_p1_end, window_p2_start, window_p2_end, len(seq))
# assert window_p1_start < window_p1_end
# print(window_p1_start, window_p1_end)
min_index_s1, min_index_s2, hash_value = randstrobe_order3(hash_seq_list, window_p1_start, window_p1_end, window_p2_start, window_p2_end, hash_m1, k_size, prime)
p2 = window_p1_start + min_index_s1
p3 = window_p2_start + min_index_s2
yield p, p2, p3, hash_value
def randstrobe_order2(hash_seq_list, start, stop, hash_m1, k_size, prime):
min_index, min_value = argmin([ (hash_m1 - hash_seq_list[i][1]) % prime for i in range(start, stop)])
min_hash_val = hash_m1 - hash_seq_list[start + min_index][1]
return min_index, min_hash_val
def seq_to_randstrobes2_iter(seq, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w):
hash_seq_list = [(i, hash(seq[i:i+k_size])) for i in range(len(seq) - k_size +1) if "N" not in seq[i:i+k_size]]
if w > 1:
hash_seq_list_thinned = thinner([h for i,h in hash_seq_list], w) # produce a subset of positions, still with samme index as in full sequence
else:
hash_seq_list_thinned = hash_seq_list
# assert len(hash_seq_list[:-k_size]) == len(hash_seq_list) - k_size
for (p, hash_m1) in hash_seq_list_thinned: #[:-k_size]:
if p >= len(hash_seq_list) - k_size:
break
# hash_m1 = hash_seq_list[p]
window_p_start = p + k_size + strobe_w_min_offset if p + strobe_w_max_offset <= len(hash_seq_list) else max( (p + k_size + strobe_w_min_offset) - (p+k_size+strobe_w_max_offset - len(hash_seq_list)), p+ k_size )
window_p_end = min(p + strobe_w_max_offset, len(hash_seq_list))
# print(window_p_start, window_p_end)
min_index, hash_value = randstrobe_order2(hash_seq_list, window_p_start, window_p_end, hash_m1, k_size, prime)
p2 = window_p_start + min_index
yield p, p2, hash_value
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
def build_randstrobe3_index(refs, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w):
idx = defaultdict(lambda :array("L"))
ref_id_to_accession = {}
cntr = 0
for r_id, (ref_acc, (seq, _)) in enumerate(help_functions.readfq(refs)):
ref_id_to_accession[r_id] = ref_acc
for p1, p2, p3, hash_val in seq_to_randstrobes3_iter(seq, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w):
idx[hash_val].append(r_id)
idx[hash_val].append(p1)
idx[hash_val].append(p2)
idx[hash_val].append(p3)
cntr += 1
if cntr % 1000000 == 0:
print("{0} randstrobes created from references".format(cntr))
# print(hash_val, r_id, pos)
return idx, ref_id_to_accession, cntr
def build_randstrobe2_index(refs, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w):
idx = defaultdict(lambda :array("L"))
ref_id_to_accession = {}
cntr = 0
for r_id, (ref_acc, (seq, _)) in enumerate(help_functions.readfq(refs)):
ref_id_to_accession[r_id] = ref_acc
for p1, p2, hash_val in seq_to_randstrobes2_iter(seq, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w):
idx[hash_val].append(r_id)
idx[hash_val].append(p1)
idx[hash_val].append(p2)
cntr += 1
if cntr % 1000000 == 0:
print("{0} randstrobes created from references".format(cntr))
# print(hash_val, r_id, pos)
return idx, ref_id_to_accession, cntr
def build_hybridstrobe3_index(refs, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w):
idx = defaultdict(lambda :array("L"))
ref_id_to_accession = {}
cntr = 0
if w == 1:
for r_id, (ref_acc, (seq, _)) in enumerate(help_functions.readfq(refs)):
ref_id_to_accession[r_id] = ref_acc
for p1, p2, p3, hash_val in seq_to_hybridstrobes3_iter(seq, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w):
idx[hash_val].append(r_id)
idx[hash_val].append(p1)
idx[hash_val].append(p2)
idx[hash_val].append(p3)
cntr += 1
if cntr % 1000000 == 0:
print("{0} hybridstrobes created from references".format(cntr))
# print(hash_val, r_id, pos)
else:
for r_id, (ref_acc, (seq, _)) in enumerate(help_functions.readfq(refs)):
thinner_window = deque([hash(seq[i:i+k_size]) for i in range(w)])
min_index, curr_min_hash = argmin(thinner_window)
sampled_positions = set([min_index])
ref_id_to_accession[r_id] = ref_acc
info_buffer = deque([])
for p1, p2, p3, hash_val in seq_to_hybridstrobes3_iter(seq, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w):
if p1 in sampled_positions:
idx[hash_val].append(r_id)
idx[hash_val].append(p1)
idx[hash_val].append(p2)
idx[hash_val].append(p3)
sampled_positions.remove(p1)
assert len(sampled_positions) == 0
cntr += 1
if p1 < w:
info_buffer.append( (p1, p2, p3, hash_val) )
continue # already in queue
else:
# updating window
discarded_hash = thinner_window.popleft()
thinner_window.append(hash_val)
info_buffer.popleft()
info_buffer.append( (p1, p2, p3, hash_val) )
# we have discarded previous windows minimizer, look for new minimizer brute force
if curr_min_hash == discarded_hash:
min_index, curr_min_hash = argmin(thinner_window)
(p1_, p2_, p3_, hash_val_) = info_buffer[min_index]
idx[hash_val].append(r_id)
idx[hash_val].append(p1_ )
idx[hash_val].append(p2_ )
idx[hash_val].append(p3_ )
cntr += 1
# Previous minimizer still in window, we only need to compare with the recently added kmer
elif hash_val < curr_min_hash:
curr_min_hash = hash_val
idx[hash_val].append(r_id)
idx[hash_val].append(p1)
idx[hash_val].append(p2)
idx[hash_val].append(p3)
cntr += 1
if cntr % 1000000 == 0:
print("{0} hybridstrobes created from references, currently at position: {1}".format(cntr, p1))
return idx, ref_id_to_accession, cntr
def build_hybridstrobe2_index(refs, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w):
idx = defaultdict(lambda :array("L"))
ref_id_to_accession = {}
cntr = 0
if w == 1:
for r_id, (ref_acc, (seq, _)) in enumerate(help_functions.readfq(refs)):
ref_id_to_accession[r_id] = ref_acc
for p1, p2, hash_val in seq_to_hybridstrobes2_iter(seq, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w):
idx[hash_val].append(r_id)
idx[hash_val].append(p1)
idx[hash_val].append(p2)
cntr += 1
if cntr % 1000000 == 0:
print("{0} hybridstrobes created from references".format(cntr))
# print(hash_val, r_id, pos)
else:
for r_id, (ref_acc, (seq, _)) in enumerate(help_functions.readfq(refs)):
thinner_window = deque([hash(seq[i:i+k_size]) for i in range(w)])
min_index, curr_min_hash = argmin(thinner_window)
sampled_positions = set([min_index])
ref_id_to_accession[r_id] = ref_acc
info_buffer = deque([])
for p1, p2, hash_val in seq_to_hybridstrobes2_iter(seq, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w):
if p1 in sampled_positions:
idx[hash_val].append(r_id)
idx[hash_val].append(p1)
idx[hash_val].append(p2)
sampled_positions.remove(p1)
assert len(sampled_positions) == 0
cntr += 1
if p1 < w:
info_buffer.append( (p1, p2, hash_val) )
continue # already in queue
else:
# updating window
discarded_hash = thinner_window.popleft()
thinner_window.append(hash_val)
info_buffer.popleft()
info_buffer.append( (p1, p2, hash_val) )
# we have discarded previous windows minimizer, look for new minimizer brute force
if curr_min_hash == discarded_hash:
min_index, curr_min_hash = argmin(thinner_window)
(p1_, p2_, hash_val_) = info_buffer[min_index]
idx[hash_val].append(r_id)
idx[hash_val].append(p1_ )
idx[hash_val].append(p2_ )
cntr += 1
# Previous minimizer still in window, we only need to compare with the recently added kmer
elif hash_val < curr_min_hash:
curr_min_hash = hash_val
idx[hash_val].append(r_id)
idx[hash_val].append(p1)
idx[hash_val].append(p2)
cntr += 1
if cntr % 1000000 == 0:
print("{0} hybridstrobes created from references, currently at position: {1}".format(cntr, p1))
return idx, ref_id_to_accession, cntr
def seq_to_minstrobes2_iter(seq, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w):
hash_seq_list = [(i, hash(seq[i:i+k_size])) for i in range(len(seq) - k_size +1) if "N" not in seq[i:i+k_size]]
strobes = deque(thinner([h for i,h in hash_seq_list], strobe_w_max_offset - strobe_w_min_offset)) # produce a subset of positions, still with samme index as in full sequence
if w > 1:
hash_seq_list_thinned = thinner([h for i,h in hash_seq_list], w) # produce a subset of positions, still with same index as in full sequence
else:
hash_seq_list_thinned = hash_seq_list
# assert len(hash_seq_list[:-k_size]) == len(hash_seq_list) - k_size
for (p, hash_m1) in hash_seq_list_thinned: #[:-k_size]:
if p >= len(hash_seq_list) - k_size:
break
# print(p,len(hash_seq_list) - k_size, len(hash_seq_list), len(seq), len(strobes), strobes)
if p + k_size + strobe_w_min_offset < len(seq):
while strobes[0][0] < min(p + k_size + strobe_w_min_offset, len(hash_seq_list)-1):
l = strobes.popleft()
# print(p, len(hash_seq_list) - k_size, len(hash_seq_list), len(seq), len(strobes), strobes[0])
p2, hash_val = strobes[0]
hash_value = hash_m1 - hash_val
yield p, p2, hash_value
def build_minstrobe2_index(refs, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w):
idx = defaultdict(lambda :array("L"))
ref_id_to_accession = {}
cntr = 0
for r_id, (ref_acc, (seq, _)) in enumerate(help_functions.readfq(refs)):
ref_id_to_accession[r_id] = ref_acc
for p1, p2, hash_val in seq_to_minstrobes2_iter(seq, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w):
idx[hash_val].append(r_id)
idx[hash_val].append(p1)
idx[hash_val].append(p2)
cntr += 1
if cntr % 1000000 == 0:
print("{0} minstrobes created from references".format(cntr))
# print(hash_val, r_id, pos)
return idx, ref_id_to_accession, cntr
def seq_to_minstrobes3_iter(seq, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w):
hash_seq_list = [(i, hash(seq[i:i+k_size])) for i in range(len(seq) - k_size +1) if "N" not in seq[i:i+k_size]]
strobes = deque(thinner([h for i,h in hash_seq_list], strobe_w_max_offset - strobe_w_min_offset)) # produce a subset of positions, still with samme index as in full sequence
strobes2 = copy.deepcopy(strobes)
if w > 1:
hash_seq_list_thinned = thinner([h for i,h in hash_seq_list], w) # produce a subset of positions, still with same index as in full sequence
else:
hash_seq_list_thinned = hash_seq_list
# assert len(hash_seq_list[:-k_size]) == len(hash_seq_list) - k_size
for (p, hash_m1) in hash_seq_list_thinned: #[:-k_size]:
if p >= len(hash_seq_list) - 2*k_size:
break
# print(p,len(hash_seq_list) - k_size, len(hash_seq_list), len(seq), len(strobes), strobes)
if p + strobe_w_max_offset + strobe_w_min_offset < len(seq):
while strobes2[0][0] < min(p + strobe_w_min_offset + strobe_w_max_offset, len(hash_seq_list)-1):
l = strobes2.popleft()
if p + k_size + strobe_w_min_offset < len(seq):
while strobes[0][0] < min(p + k_size + strobe_w_min_offset, len(hash_seq_list)-1):
l = strobes.popleft()
# print(p, len(hash_seq_list) - k_size, len(hash_seq_list), len(seq), len(strobes), strobes[0], len(strobes2))
p2, hash_val2 = strobes[0]
p3, hash_val3 = strobes2[0]
hash_value = hash_m1 - hash_val2 + 2*hash_val3
yield p, p2, p3, hash_value
def build_minstrobe3_index(refs, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w):
idx = defaultdict(lambda :array("L"))
ref_id_to_accession = {}
cntr = 0
for r_id, (ref_acc, (seq, _)) in enumerate(help_functions.readfq(refs)):
ref_id_to_accession[r_id] = ref_acc
for p1, p2, p3, hash_val in seq_to_minstrobes3_iter(seq, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w):
idx[hash_val].append(r_id)
idx[hash_val].append(p1)
idx[hash_val].append(p2)
idx[hash_val].append(p3)
cntr += 1
if cntr % 1000000 == 0:
print("{0} minstrobes created from references".format(cntr))
# print(hash_val, r_id, pos)
return idx, ref_id_to_accession, cntr
# def sort_merge(sorted_list):
# sort_merged_list = []
# curr_merge = sorted_list[0]
# for i, t1 in enumerate( sorted_list[:-1] ):
# r_id, r_pos, q_pos, length = t1
# r2_id, r2_pos, q2_pos, length2 = sorted_list[i+1]
# # print(i, r_id, r_pos, r2_id, r2_pos)
# # print(r2_pos, q2_pos)
# if r_id == r2_id:
# # print("OK", q2_pos <= q_pos + length <= q2_pos+ length, r2_pos <= r_pos + length <= r2_pos + length)
# # print("2", q2_pos, q_pos + length, q2_pos+ length, r2_pos, r_pos + length, r2_pos + length)
# # overlapping on both query and ref
# # print(q2_pos + length2, q_pos + length, curr_merge[3])
# if q2_pos <= q_pos + length <= q2_pos+ length and r2_pos <= r_pos + length <= r2_pos + length:
# # curr_merge = (r_id, curr_merge[1], curr_merge[2], max(q2_pos + length2, q_pos + length ) - q_pos ) # hit length on query sequence
# curr_merge = (r_id, curr_merge[1], curr_merge[2], max(r2_pos + length2, r_pos + length ) - r_pos ) # hit length on reference sequence
# # print("HERER")
# else:
# # time to add old element
# sort_merged_list.append(curr_merge)
# curr_merge = sorted_list[i+1]
# else:
# # time to add old element
# sort_merged_list.append(curr_merge)
# curr_merge = sorted_list[i+1]
# # print(curr_merge)
# # print(sort_merged_list)
# # print(curr_merge)
# sort_merged_list.append(curr_merge)
# return sort_merged_list
def get_matches3(strobes, idx, k, dont_merge_matches, ref_id_to_accession, acc, selfalign):
"""
The merging of matches is a simple linear merging. If there are repetitive matches across e.g. a chromosome
the merging will be broken up at the repetitive kmer. To solve the merging exactly, we would need
to solve the collinear chaining problem after we have out matches. There is no such functionality here.
Another way to solve this is to do a post merging after sorting the merged matches.
If two merged matches also overlaps, they can be merged again.
"""
if dont_merge_matches:
matches = []
for q_p1, q_p2, q_p3, h in strobes:
# print()
# print("Q", q_p1)
if h in idx:
for r_id, r_p1, r_p2, r_p3 in grouper(idx[h], 4):
# print("R", r_id, r_p1)
matches.append( (r_id, r_p1, q_p1, r_p3 - r_p1 + k) )
return sorted(matches, key = lambda x: (x[0], x[2], x[1]) )
else:
cpm = {} # current potential merges
merged_matches = []
for q_p1, q_p2, q_p3, h in strobes: # iterate over query in ascending order
if h in idx:
for r_id, r_p1, r_p2, r_p3 in grouper(idx[h], 4): # iterate over references, all in ascending order
# remove self matches with below if statement, for now commented out to find eventual bugs
if not selfalign and ref_id_to_accession[r_id] == acc:
continue
if r_id in cpm:
is_added_to_an_interval_query = False
# print(q_p1, list(cpm[r_id].keys()))
for end_q in list(cpm[r_id].keys()):
# print()
# print("r_id",r_id, "end_q", end_q)
if q_p1 <= end_q: # overlap on query
is_added_to_an_interval_query = True
is_added_to_an_interval_reference = False
# print(list(cpm[r_id][end_q].keys()))
for end_r in list(cpm[r_id][end_q].keys()):
# print("Case1 end_r", end_r)
# print(q_p1, )
prev_q_p1, prev_q_p2, prev_ref_p1, prev_ref_p2 = cpm[r_id][end_q][end_r]
# print(r_id,q_p1, "CRUCIAL:",prev_q_p1, prev_q_p2, prev_ref_p1, prev_ref_p2)
# print(r_id, q_p1, cpm[r_id][end_q][end_r])
# check all refs
new_q_p2 = max(prev_q_p2, q_p3 + k)
if prev_ref_p1 <= r_p1 <= end_r: # Overlap on reference
is_added_to_an_interval_reference = True
# print("OK", prev_ref_p1, r_p1, end_r)
# print("lol", prev_ref_p1, r_p1, end_r)
new_r_p2 = max(end_r, r_p3 + k)
del cpm[r_id][end_q][end_r]
if not cpm[r_id][end_q]:
del cpm[r_id][end_q]
if new_q_p2 not in cpm[r_id]:
cpm[r_id][ new_q_p2 ] = {}
cpm[r_id][ new_q_p2 ][new_r_p2] = ( prev_q_p1, new_q_p2, prev_ref_p1, new_r_p2)
# print("new:", ( prev_q_p1, new_q_p2, prev_ref_p1, new_r_p2) )
elif new_r_p2 not in cpm[r_id][ new_q_p2 ]:
cpm[r_id][ new_q_p2 ][new_r_p2] = ( prev_q_p1, new_q_p2, prev_ref_p1, new_r_p2)
# print("appended:", ( prev_q_p1, new_q_p2, prev_ref_p1, new_r_p2) )
else:
# print("Was already present:", cpm[r_id][ new_q_p2 ][new_r_p2], "attempted new:", ( prev_q_p1, new_q_p2, prev_ref_p1, new_r_p2) )
( old_q_p1, new_q_p2, old_ref_p1, new_r_p2) = cpm[r_id][ new_q_p2 ][new_r_p2]
cpm[r_id][ new_q_p2 ][new_r_p2] = ( min(old_q_p1, prev_q_p1), new_q_p2, min(old_ref_p1, prev_ref_p1), new_r_p2)
# cpm[r_id][ new_q_p2 ][new_r_p2] = [ prev_q_p1, new_q_p2, prev_ref_p1, new_r_p2]
if not is_added_to_an_interval_reference:
if new_q_p2 not in cpm[r_id]:
cpm[r_id][ new_q_p2 ] = {}
cpm[r_id][ new_q_p2 ][r_p3 + k] = (q_p1, new_q_p2, r_p1, r_p3 + k)
# print("new added1:", (q_p1, new_q_p2, r_p1, r_p3 + k) )
elif r_p3 + k not in cpm[r_id][new_q_p2]:
cpm[r_id][ new_q_p2 ][r_p3 + k] = (q_p1, new_q_p2, r_p1, r_p3 + k )
# print("new added2:", (q_p1, new_q_p2, r_p1, r_p3 + k ) )
else:
# print("Was already present:", cpm[r_id][ new_q_p2 ][r_p3 + k], "attempted new:", (q_p1, new_q_p2, r_p1, r_p3 + k ) )
( old_q_p1, new_q_p2, old_ref_p1, new_r_p2) = cpm[r_id][ new_q_p2 ][r_p3 + k]
cpm[r_id][ new_q_p2 ][new_r_p2] = ( min(old_q_p1, q_p1), new_q_p2, min(old_ref_p1, r_p1), new_r_p2)
else:
# print("Case2 end_r", end_r)
# revove the intervals that we have passed on the query here to not make the cpm dict too large...
# add to merged_matches dict
for r_end in cpm[r_id][end_q]:
(q_pos_start, q_pos_stop, r_pos, r_pos_stop) = cpm[r_id][end_q][r_end]
merged_matches.append( (r_id, r_pos, q_pos_start, r_pos_stop - r_pos) )
del cpm[r_id][end_q]
if not is_added_to_an_interval_query: # no overlap with prev query sequences
cpm[r_id][q_p3 + k] = {}
cpm[r_id][q_p3 + k][r_p3 + k] = (q_p1, q_p3 + k, r_p1, r_p3 + k)
else:
cpm[r_id] = { q_p3 + k : {r_p3 + k : (q_p1, q_p3 + k, r_p1, r_p3 + k) }}
# cpm[r_id] = [q_p1, q_p2 + k, r_p1, r_p3 + k ]
# close all open merge intervals
for r_id in cpm.keys():
for q_stop in cpm[r_id]:
for r_stop in cpm[r_id][q_stop]:
(q_p1, q_pos_stop, r_pos, r_pos_stop) = cpm[r_id][q_stop][r_stop]
merged_matches.append( (r_id, r_pos, q_p1, r_pos_stop - r_pos) )
# print(merged_matches)
if not merged_matches:
return []
return sorted(set(merged_matches), key = lambda x: (x[0], x[2], x[1]) )
def get_matches(strobes, idx, k, dont_merge_matches, ref_id_to_accession, acc, selfalign):
"""
The merging of matches is a simple linear merging. If there are repetitive matches across e.g. a chromosome
the merging will be broken up at the repetitive kmer. To solve the merging exactly, we would need
to solve the collinear chaining problem after we have out matches. There is no such functionality here.
Another way to solve this is to do a post merging after sorting the merged matches.
If two merged matches also overlaps, they can be merged again.
"""
if dont_merge_matches:
matches = []
for q_p1, q_p2, h in strobes:
# print()
# print("Q", q_p1)
if h in idx:
for r_id, r_p1, r_p2 in grouper(idx[h], 3):
# print("R", r_id, r_p1)
matches.append( (r_id, r_p1, q_p1, r_p2 - r_p1 + k) )
return sorted(matches, key = lambda x: (x[0], x[2], x[1]) )
else:
cpm = {} # current potential merges
merged_matches = []
for q_p1, q_p2, h in strobes: # iterate over query in ascending order
if h in idx:
# print()
# print("----------------", q_p1)
# print(cpm)
# print("All pos:", idx[h])
# prev_r_id, prev_hit_r_p1,prev_hit_r_p2 = 0,0,0 # these only keep track of identical consecutive kmers/strobes
for r_id, r_p1, r_p2 in grouper(idx[h], 3): # iterate over references, all in ascending order
# if prev_r_id == r_id and r_p1 == prev_hit_r_p1 + 1 and r_p2 == prev_hit_r_p2+1:
# prev_r_id = r_id
# prev_hit_r_p1 = r_p1
# prev_hit_r_p2 = r_p2
# update_relevant_pos()
# continue
# print(q_p1, q_p2+ k , r_p1, r_p2+k)
# remove self matches with below if statement, for now commented out to find eventual bugs
if not selfalign and ref_id_to_accession[r_id] == acc:
continue
if r_id in cpm:
is_added_to_an_interval_query = False
# print(q_p1, list(cpm[r_id].keys()))
for end_q in list(cpm[r_id].keys()):
# print()
# print("r_id",r_id, "end_q", end_q)
if q_p1 <= end_q: # overlap on query
is_added_to_an_interval_query = True
is_added_to_an_interval_reference = False
# print(list(cpm[r_id][end_q].keys()))
for end_r in list(cpm[r_id][end_q].keys()):
# print("Case1 end_r", end_r)
# print(q_p1, )
prev_q_p1, prev_q_p2, prev_ref_p1, prev_ref_p2 = cpm[r_id][end_q][end_r]
# print(r_id,q_p1, "CRUCIAL:",prev_q_p1, prev_q_p2, prev_ref_p1, prev_ref_p2)
# print(r_id, q_p1, cpm[r_id][end_q][end_r])
# check all refs
new_q_p2 = max(prev_q_p2, q_p2 + k)
if prev_ref_p1 <= r_p1 <= end_r: # Overlap on reference
is_added_to_an_interval_reference = True
# print("OK", prev_ref_p1, r_p1, end_r)
# print("lol", prev_ref_p1, r_p1, end_r)
new_r_p2 = max(end_r, r_p2 + k)
del cpm[r_id][end_q][end_r]
if not cpm[r_id][end_q]:
del cpm[r_id][end_q]
if new_q_p2 not in cpm[r_id]:
cpm[r_id][ new_q_p2 ] = {}
cpm[r_id][ new_q_p2 ][new_r_p2] = ( prev_q_p1, new_q_p2, prev_ref_p1, new_r_p2)
# print("new:", ( prev_q_p1, new_q_p2, prev_ref_p1, new_r_p2) )
elif new_r_p2 not in cpm[r_id][ new_q_p2 ]:
cpm[r_id][ new_q_p2 ][new_r_p2] = ( prev_q_p1, new_q_p2, prev_ref_p1, new_r_p2)
# print("appended:", ( prev_q_p1, new_q_p2, prev_ref_p1, new_r_p2) )
else:
# print("Was already present:", cpm[r_id][ new_q_p2 ][new_r_p2], "attempted new:", ( prev_q_p1, new_q_p2, prev_ref_p1, new_r_p2) )
( old_q_p1, new_q_p2, old_ref_p1, new_r_p2) = cpm[r_id][ new_q_p2 ][new_r_p2]
cpm[r_id][ new_q_p2 ][new_r_p2] = ( min(old_q_p1, prev_q_p1), new_q_p2, min(old_ref_p1, prev_ref_p1), new_r_p2)
# cpm[r_id][ new_q_p2 ][new_r_p2] = [ prev_q_p1, new_q_p2, prev_ref_p1, new_r_p2]
if not is_added_to_an_interval_reference:
if new_q_p2 not in cpm[r_id]:
cpm[r_id][ new_q_p2 ] = {}
cpm[r_id][ new_q_p2 ][r_p2 + k] = (q_p1, new_q_p2, r_p1, r_p2 + k)
# print("new added1:", (q_p1, new_q_p2, r_p1, r_p2 + k) )
elif r_p2 + k not in cpm[r_id][new_q_p2]:
cpm[r_id][ new_q_p2 ][r_p2 + k] = (q_p1, new_q_p2, r_p1, r_p2 + k )
# print("new added2:", (q_p1, new_q_p2, r_p1, r_p2 + k ) )
else:
# print("Was already present:", cpm[r_id][ new_q_p2 ][r_p2 + k], "attempted new:", (q_p1, new_q_p2, r_p1, r_p2 + k ) )
( old_q_p1, new_q_p2, old_ref_p1, new_r_p2) = cpm[r_id][ new_q_p2 ][r_p2 + k]
cpm[r_id][ new_q_p2 ][new_r_p2] = ( min(old_q_p1, q_p1), new_q_p2, min(old_ref_p1, r_p1), new_r_p2)
else:
# print("Case2 end_r", end_r)
# revove the intervals that we have passed on the query here to not make the cpm dict too large...
# add to merged_matches dict
for r_end in cpm[r_id][end_q]:
(q_pos_start, q_pos_stop, r_pos, r_pos_stop) = cpm[r_id][end_q][r_end]
merged_matches.append( (r_id, r_pos, q_pos_start, r_pos_stop - r_pos) )
del cpm[r_id][end_q]
# # print(end_q, cpm[r_id][end_q][1])
# # assert end_q == cpm[r_id][end_q][1]
# # there is overlap in both reference and query to previous hit
# # `q_1 <= q_2 <= q'_1 +k` and `r_1 <= r_2 <= r'_2+k`
# if cpm[r_id][end_q][0] < q_p1 and q_p1 < cpm[r_id][end_q][1] and cpm[r_id][end_q][2] <= r_p1 <= cpm[r_id][end_q][3]:
# prev_q_p1, prev_q_p2, prev_ref_p1, prev_ref_p2 = cpm[r_id][end_q]
# new_q_p2 = max(cpm[r_id][end_q][1], q_p2 + k)
# new_r_p2 = max(cpm[r_id][end_q][3], r_p2 + k)
# del cpm[r_id][end_q]
# cpm[r_id][ new_q_p2 ] = [ prev_q_p1, new_q_p2, prev_ref_p1, new_r_p2]
# is_added_to_an_interval_query = True
# # break
# else:
# prev_q_p1, prev_q_p2, prev_ref_p1, prev_ref_p2 = cpm[r_id][end_q]
# new_q_p2 = max(cpm[r_id][end_q][1], q_p2 + k)
# new_r_p2 = max(cpm[r_id][end_q][3], r_p2 + k)
# del cpm[r_id][end_q]
# cpm[r_id][ new_q_p2 ] = [ prev_q_p1, new_q_p2, prev_ref_p1, new_r_p2]
# is_added_to_an_interval_query = True
# # cpm[r_id][end_q][1] = max(cpm[r_id][end_q][1], q_p2 + k)
# # cpm[r_id][end_q][3] = max(cpm[r_id][end_q][3], r_p2 + k)
# # print(cpm[r_id][0], q_p2 + k)
# # print(cpm[r_id][2], r_p2 + k)
# # if cpm[r_id][1] > q_p2 + k:
# # print("LOOL")
# # if cpm[r_id][3] > r_p2 + k:
# # print("LOOL222")
if not is_added_to_an_interval_query: # no overlap with prev query sequences
# prev_q_p1, prev_q_p2, prev_ref_p1, prev_ref_p2 = cpm[r_id][end_q]
# assert prev_q_p2 - prev_q_p1 == prev_ref_p2 - prev_ref_p1
# print(prev_q_p1,prev_q_p2, prev_q_p2 - prev_q_p1)
# print(prev_ref_p1,prev_ref_p2, prev_ref_p2 - prev_ref_p1)
# merged_matches.append( (r_id, prev_ref_p1, prev_q_p1, prev_ref_p2 - prev_ref_p1) )
# cpm[r_id] = [q_p1, q_p2 + k, r_p1, r_p2 + k ]
# print("HERE")
cpm[r_id][q_p2 + k] = {}
cpm[r_id][q_p2 + k][r_p2 + k] = (q_p1, q_p2 + k, r_p1, r_p2 + k)
else:
cpm[r_id] = { q_p2 + k : {r_p2 + k : (q_p1, q_p2 + k, r_p1, r_p2 + k) }}
# cpm[r_id] = [q_p1, q_p2 + k, r_p1, r_p2 + k ]
# close all open merge intervals
for r_id in cpm.keys():
for q_stop in cpm[r_id]:
for r_stop in cpm[r_id][q_stop]:
(q_p1, q_pos_stop, r_pos, r_pos_stop) = cpm[r_id][q_stop][r_stop]
merged_matches.append( (r_id, r_pos, q_p1, r_pos_stop - r_pos) )
# print(merged_matches)
if not merged_matches:
return []
# print(acc, merged_matches)
# return sorted(merged_matches, key = lambda x: x[2])
# # If there are repetitive matches across e.g. a chromosome
# # the merging will be broken up at the repetitive kmer.
# # here we post merge such spuriously broken up overlapping matches
# # sort first by reference id then by sum of reference and query position to resolve perfect repeats!
# new_sort = sorted(merged_matches, key = lambda x: (x[0], x[1]+x[2], x[1] ) )
# merged_matches = sort_merge(new_sort)
# # print(merged_matches)
# # sort first by reference id then by reference position
# new_sort = sorted(merged_matches, key = lambda x: (x[0], x[1] ) )
# merged_matches = sort_merge(new_sort)
# # print(merged_matches)
# # sort first by reference id then by query position
# new_sort = sorted(merged_matches, key = lambda x: (x[0], x[2] ) )
# merged_matches = sort_merge(new_sort)
# # print(merged_matches)
return sorted(set(merged_matches), key = lambda x: (x[0], x[2], x[1]) )
def seq_to_kmer_iter(seq, k_size, w):
hash_seq_list = [(i, hash(seq[i:i+k_size])) for i in range(len(seq) - k_size +1) if "N" not in seq[i:i+k_size]]
if w > 1:
hash_seq_list = thinner([h for i,h in hash_seq_list], w)
# assert range(len(seq) - k_size + 1) == range(len(hash_seq_list))
for (p, hash_val) in hash_seq_list:
yield p, p, hash_val
def build_kmer_index(refs, k_size, w):
idx = defaultdict(lambda :array("L"))
ref_id_to_accession = {}
cntr = 0
for r_id, (ref_acc, (seq, _)) in enumerate(help_functions.readfq(refs)):
ref_id_to_accession[r_id] = ref_acc
for p1, p2, hash_val in seq_to_kmer_iter(seq, k_size, w):
idx[hash_val].append(r_id)
idx[hash_val].append(p1)
idx[hash_val].append(p2)
cntr += 1
if cntr % 1000000 == 0:
print("{0} kmers created from references".format(cntr))
# print(hash_val, r_id, pos)
return idx, ref_id_to_accession, cntr
# def minstrobes_iter(seq, k_size, strobe_w_min_offset, strobe_w_max_offset, w, order = 2, buffer_size = 10000000):
# for i in range(0, len(seq), buffer_size):
# substring = seq[i:i+buffer_size]
# # print(substring, len(substring))
# for p, m in minstrobes(substring, k_size, strobe_w_min_offset, strobe_w_max_offset, w, order = order).items():
# yield m
# def randstrobes_iter(seq, k_size, strobe_w_min_offset, strobe_w_max_offset, w, order = 2, buffer_size = 10000000):
# for i in range(0, len(seq), buffer_size):
# substring = seq[i:i+buffer_size]
# for p, m in randstrobes(substring, k_size, strobe_w_min_offset, strobe_w_max_offset, w, order = order).items():
# yield m
# def hybridstrobes_iter(seq, k_size, strobe_w_min_offset, strobe_w_max_offset, w, order = 2, buffer_size = 10000000):
# for i in range(0, len(seq), buffer_size):
# substring = seq[i:i+buffer_size]
# # print(substring, len(substring))
# for p, m in hybridstrobes(substring, k_size, strobe_w_min_offset, strobe_w_max_offset, w, order = order).items():
# yield m
# def kmer_iter(seq, k_size, strobe_w_min_offset, strobe_w_max_offset, w, order = 2, buffer_size = 10000000):
# for i in range(0, len(seq), buffer_size):
# substring = seq[i:i+buffer_size]
# # print(substring, len(substring))
# for p, m in kmers(substring, k_size, strobe_w_min_offset, strobe_w_max_offset, w, order = order).items():
# yield m
def print_matches_to_file(query_matches, ref_id_to_accession, outfile, reverse):
for q_acc, read_matches in query_matches:
if reverse:
outfile.write("> {0} Reverse\n".format(q_acc))
else:
outfile.write("> {0}\n".format(q_acc))
for (r_id, ref_p, q_pos, k) in read_matches:
ref_acc = ref_id_to_accession[r_id]
outfile.write(" {0} {1} {2} {3}\n".format(ref_acc, ref_p, q_pos, k))
def main(args):
PRIME = 997
w = args.w
if args.kmer_index:
idx, ref_id_to_accession, cntr = build_kmer_index(open(args.references,'r'), args.k, w)
print("{0} kmers created from references".format(cntr))
# print(idx)
elif args.minstrobe_index:
if args.n == 2:
idx, ref_id_to_accession, cntr = build_minstrobe2_index(open(args.references,'r'),args.k, args.strobe_w_min_offset, args.strobe_w_max_offset, PRIME, w)
print("{0} minstrobes created from references".format(cntr))
elif args.n == 3:
idx, ref_id_to_accession, cntr = build_minstrobe3_index(open(args.references,'r'),args.k, args.strobe_w_min_offset, args.strobe_w_max_offset, PRIME, w)
print("{0} minstrobes created from references".format(cntr))
elif args.randstrobe_index:
if args.n == 2:
idx, ref_id_to_accession, cntr = build_randstrobe2_index(open(args.references,'r'),args.k, args.strobe_w_min_offset, args.strobe_w_max_offset, PRIME, w)
print("{0} randstrobes created from references".format(cntr))
elif args.n == 3:
idx, ref_id_to_accession, cntr = build_randstrobe3_index(open(args.references,'r'),args.k, args.strobe_w_min_offset, args.strobe_w_max_offset, PRIME, w)
print("{0} randstrobes created from references".format(cntr))
else:
if args.n == 2:
idx, ref_id_to_accession, cntr = build_hybridstrobe2_index(open(args.references,'r'),args.k, args.strobe_w_min_offset, args.strobe_w_max_offset, PRIME, w)
print("{0} hybridstrobes created from references".format(cntr))
elif args.n == 3:
idx, ref_id_to_accession, cntr = build_hybridstrobe3_index(open(args.references,'r'),args.k, args.strobe_w_min_offset, args.strobe_w_max_offset, PRIME, w)
print("{0} hybridstrobes created from references".format(cntr))
outfile = open(os.path.join(args.outfolder, args.prefix + ".tsv"), 'w')
query_matches = []
if args.rev_comp:
# outfile_rc = open(os.path.join(args.outfolder, args.prefix + "_revcomp.tsv"), 'w')
matches_rc = []