-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathStrobeMap_Maier
1263 lines (1058 loc) · 60 KB
/
StrobeMap_Maier
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
import sys
import argparse
import copy
import operator
# 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 typing import Iterator
from fractions import Fraction
from modules import help_functions
BITS = sys.hash_info.width
MAX = sys.maxsize
MAX_HASH_VALUE = int((2**BITS)/2) - 1
def argmin(array: list) -> tuple:
"""
Find the value of x which minimizes f(x) over the set of candidates for x
:param array: a list to minimize
:returns: a tuple with the index position and the value of the lowest element
"""
min_index = array.index(min(array))
min_val = array[min_index]
return min_index, min_val
def reverse_complement(seq: str) -> str:
"""
Compute the reverse complement of a given nucleotide sequence
:param seq: a string with a nucleotide sequence
:returns: a string with the reverse complement of the given sequence
"""
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(seq)])
return(rev_comp)
def thinner(hash_list: list, w: int) -> list:
"""
Thins out kmers/strobemers using a sliding window approach
:param hash_list: a list with hash values
:param w: number of hashes used in a sliding window for thinning (w=1 means no thinning)
:returns: 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: list, curr_min: int, min_index: int, new_hash: int, i: int,
start_offset: int, end_offset: int) -> tuple:
"""
Updates windows
:param q: a list with strobe_windows
:param curr_min: an integer with the current minimum value
:param min_index: an integer with the index position of the minimum value
:param new_hash: an integer with the new hash value
:param i: an integer with the position of the first strobe
:param start_offset: minimum window offset
:param end_offset: maximum window offset
:returns: a tuple with the index position of the minimum value and the value
"""
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_hybridstrobes_iter(seq: str, k_size: int, w_min, w_max, prime: int,
w: int, order: int) -> Iterator[tuple]:
"""
Generator for creating hybridstrobes of any orders
:param seq: a string with a nucleotide sequence
:param k_size: length of each strobe
:param w_min: minimum window offset to the previous window (wMin > 0)
:param w_max: maximum window offset to the previous window (wMin <= wMax)
:param w: number of hashes used in a sliding window for thinning (w=1 means no thinning)
:param order: number of substrings/strobes
:returns: an iterator for creating hybridstrobes
"""
if len(seq) < 2*w_max:
return (*(-1 for i in range(order)), None)
hash_list = [
hash(seq[i:i+k_size]) for i in range(len(seq) - k_size + 1)
if "N" not in seq[i:i+k_size]]
n_partition = 3
w_p = (w_max - w_min) // n_partition
tmp_index_dict = dict()
for strobe_num in range(0, order-1):
tmp_index = []
for partition in range(0, n_partition):
start = w_min + w_max*strobe_num + w_p*partition
end = (
w_max + w_max*strobe_num if partition + 1 == n_partition
else w_min + w_max*strobe_num + w_p + w_p*partition
)
strobe_window = deque(hash_list[start: end])
min_index, min_w = argmin(strobe_window)
min_index = min_index + w_min + w_max*strobe_num + w_p*partition
tmp_index.append(
(
strobe_window,
min_w,
min_index,
start, end
)
)
tmp_index_dict[strobe_num] = tmp_index
for i in range(len(hash_list) - w_max*order): # temporary iteration
index_hash = hash_list[i]
positions = [i, ]
for strobe_num in range(0, order-1):
tmp_index = []
for window_numer, window in enumerate(tmp_index_dict[strobe_num]):
# updating windows
strobe_window, min_w, min_index, start, end = window
new_w = hash_list[i + end]
min_index, min_w = update_queue(
strobe_window, min_w, min_index, new_w, i, start, end
)
# update tmp_index_dict
tmp_index_dict[strobe_num][window_numer] = (
strobe_window, min_w, min_index, start, end
)
tmp_index.append((min_index, min_w))
next_i, next_index_hash = tmp_index[index_hash % n_partition]
positions.append(next_i)
index_hash = index_hash + (strobe_num+1) * (-1)**(strobe_num+1) * next_index_hash
yield positions, index_hash
def seq_to_randstrobes_iter(seq: str, k_size: int, strobe_w_min_offset: int,
strobe_w_max_offset: int, prime: int, w: int,
order: int) -> Iterator[tuple]:
"""
Iterator for creation of randstrobes of any order
:param seq: a string with a nucleotide sequence
:param k_size: length of each strobe
:param strobe_w_min_offset: minimum window offset to the previous window (wMin > 0)
:param strobe_w_max_offset: maximum window offset to the previous window (wMin <= wMax)
:param prime: prime number (q) in minimizing h(m)+h(mj) mod q
:param w: number of hashes used in a sliding window for thinning (w=1 means no thinning)
:param order: number of substrings/strobes
:returns: an iterator for creating randstrobes
"""
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]
]
# thinning
if w > 1:
# produce a subset of positions, still with same index as in full sequence
hash_seq_list_thinned = thinner([h for i, h in hash_seq_list], w)
else:
hash_seq_list_thinned = hash_seq_list
for (p1, hash_m1) in hash_seq_list_thinned: # [:-k_size]:
if p1 >= len(hash_seq_list) - (order-1)*k_size:
break
# hash_m1 = hash_seq_list[p]
if p1 + (order-1) * strobe_w_max_offset <= len(hash_seq_list):
windows = list()
for window_order in range(1, order):
start = p1 + strobe_w_min_offset + (window_order-1) * strobe_w_max_offset
end = min(p1 + window_order * strobe_w_max_offset, len(hash_seq_list))
windows.append((start, end))
else:
windows = list()
for window_order in range(1, order):
start = (max(
p1+window_order*k_size,
len(hash_seq_list) + strobe_w_min_offset - (order - window_order) * strobe_w_max_offset
)
)
end = min(p1 + window_order * strobe_w_max_offset, len(hash_seq_list))
windows.append((start, end))
positions = [p1, ]
min_values = []
min_hash_val = hash_m1
for index_order in range(1, order):
min_index, min_value = argmin([
(min_hash_val + hash_seq_list[i][1]) % prime
for i in range(*windows[index_order-1])
])
min_hash_val = min_hash_val + (index_order * (-1)**index_order) * hash_seq_list[windows[index_order-1][0] + min_index][1]
positions.append(min_index+windows[index_order-1][0])
yield positions, min_hash_val
def seq_to_minstrobes_iter(seq: str, k_size: int, strobe_w_min_offset: int,
strobe_w_max_offset: int, prime: int, w: int,
order: int) -> Iterator[tuple]:
"""
Generator for creating minstrobes of any order
:param seq: a string with a nucleotide sequence
:param k_size: length of the strobe
:param strobe_w_min_offset: minimum window offset to the previous window (wMin > 0)
:param strobe_w_max_offset: maximum window offset to the previous window (wMin <= wMax)
:param prime: prime number (q) in minimizing h(m)+h(mj) mod q
:param w: number of hashes used in a sliding window for thinning (w=1 means no thinning)
:param order: number of substrings/strobes
:returns: an iterator for creating minstrobes
"""
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]
]
# produce a subset of positions, still with samme index as in full sequence
strobes = deque(thinner([h for i, h in hash_seq_list], strobe_w_max_offset - strobe_w_min_offset))
strobes_dict = {strobe_num: copy.deepcopy(strobes) for strobe_num in range(1, order)}
if w > 1:
# produce a subset of positions, still with same index as in full sequence
hash_seq_list_thinned = thinner([h for i, h in hash_seq_list], w)
else:
hash_seq_list_thinned = hash_seq_list
for (p1, hash_m1) in hash_seq_list_thinned: # [:-m_size]:
if p1 >= len(hash_seq_list) + k_size - k_size*order:
break
positions = [p1, ]
hash_value = hash_m1
for strobe_num in range(1, order):
if p1 + k_size + strobe_w_min_offset + (strobe_num-1) * strobe_w_max_offset < len(seq):
while strobes_dict[strobe_num][0][0] < min(p1 + k_size + strobe_w_min_offset + (strobe_num-1) * strobe_w_max_offset, len(hash_seq_list)-1):
l = strobes_dict[strobe_num].popleft()
p, hash_val = strobes_dict[strobe_num][0]
positions.append(p)
hash_value += strobe_num * (-1)**strobe_num * hash_val
yield positions, hash_value
def seq_to_mixedhybridstrobes_iter(seq: str, k_size: int, w_min: int,
w_max: int, prime: int, w: int, order: int,
denominator: int,
numerator: int) -> Iterator[tuple]:
"""
Generator for creating mixed hybridstrobes of any orders
:param seq: a string with a nucleotide sequence
:param k_size: length of each strobe
:param w_min: minimum window offset to the previous window (wMin > 0)
:param w_max: maximum window offset to the previous window (wMin <= wMax)
:param w: number of hashes used in a sliding window for thinning (w=1 means no thinning)
:param order: number of substrings/strobes
:param denominator: denominator and numerator determine the fraction of sampled strobemers
:param numerator: denominator and numerator determine the fraction of sampled strobemers
:returns: an iterator for creating mixedhybridstrobes
"""
hash_list = [
hash(seq[i:i+k_size]) for i in range(len(seq) - k_size + 1)
if "N" not in seq[i:i+k_size]
]
n_partition = 3
w_p = (w_max - w_min) // n_partition
tmp_index_dict = dict()
for strobe_num in range(0, order-1):
tmp_index = []
for partition in range(0, n_partition):
start = w_min + w_max*strobe_num + w_p*partition
end = (
w_max + w_max*strobe_num if partition + 1 == n_partition
else w_min + w_max*strobe_num + w_p + w_p*partition
)
strobe_window = deque(hash_list[start: end])
min_index, min_w = argmin(strobe_window)
min_index = min_index + w_min + w_max*strobe_num + w_p*partition
tmp_index.append(
(
strobe_window,
min_w,
min_index,
start, end
)
)
tmp_index_dict[strobe_num] = tmp_index
for i in range(len(hash_list) - w_max*order): # temporary iteration
index_hash = hash_list[i]
positions = [i, ]
for strobe_num in range(0, order-1):
tmp_index = []
for window_numer, window in enumerate(tmp_index_dict[strobe_num]):
# updating windows
strobe_window, min_w, min_index, start, end = window
new_w = hash_list[i + end]
min_index, min_w = update_queue(
strobe_window, min_w, min_index, new_w, i, start, end
)
# update tmp_index_dict
tmp_index_dict[strobe_num][window_numer] = (
strobe_window, min_w, min_index, start, end
)
tmp_index.append((min_index, min_w))
next_i, next_index_hash = tmp_index[index_hash % n_partition]
positions.append(next_i)
index_hash = index_hash + (strobe_num+1) * (-1)**(strobe_num+1) * next_index_hash
# decide whether kmers should be sampled instead of mixedstrobes
if hash_list[i] % denominator >= numerator:
positions = [i + strobe * k_size for strobe in range(order)]
index_hash = hash(seq[i:i+k_size * order])
yield positions, index_hash
def seq_to_mixedminstrobes_iter(seq: str, k_size: int, strobe_w_min_offset: int,
strobe_w_max_offset: int, prime: int, w: int,
order: int, denominator: int,
numerator: int) -> Iterator[tuple]:
"""
Generator for creating mixedminstrobes of any order
:param seq: a string with a nucleotide sequence
:param k_size: length of the strobe
:param strobe_w_min_offset: minimum window offset to the previous window (wMin > 0)
:param strobe_w_max_offset: maximum window offset to the previous window (wMin <= wMax)
:param prime: prime number (q) in minimizing h(m)+h(mj) mod q
:param w: number of hashes used in a sliding window for thinning (w=1 means no thinning)
:param order: number of substrings/strobes
:param denominator: denominator and numerator determine the fraction of sampled strobemers
:param numerator: denominator and numerator determine the fraction of sampled strobemers
:returns: a tuple with positions as first element and hash_value as second element.
"""
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]
]
# produce a subset of positions, still with samme index as in full sequence
strobes = deque(thinner([h for i, h in hash_seq_list], strobe_w_max_offset - strobe_w_min_offset))
strobes_dict = {strobe_num: copy.deepcopy(strobes) for strobe_num in range(1, order)}
if w > 1:
# produce a subset of positions, still with same index as in full sequence
hash_seq_list_thinned = thinner([h for i, h in hash_seq_list], w)
else:
hash_seq_list_thinned = hash_seq_list
# Decision based on hash values
for (p1, hash_m1) in hash_seq_list_thinned: # [:-k_size]:
if p1 >= len(hash_seq_list) + k_size - order*k_size:
break
if hash_m1 % denominator < numerator: # pick minstrobe
positions = [p1, ]
hash_value = hash_m1
for strobe_num in range(1, order):
if p1 + k_size + strobe_w_min_offset + (strobe_num-1) * strobe_w_max_offset < len(seq):
while strobes_dict[strobe_num][0][0] < min(p1 + k_size + strobe_w_min_offset + (strobe_num-1) * strobe_w_max_offset, len(hash_seq_list)-1):
l = strobes_dict[strobe_num].popleft()
p, hash_val = strobes_dict[strobe_num][0]
positions.append(p)
hash_value += strobe_num * (-1)**strobe_num * hash_val
yield positions, hash_value
else: # pick k-mer
positions = tuple(p1 + (strobe_num) * k_size for strobe_num in range(order))
yield positions, hash(seq[p1:p1+k_size*order])
def seq_to_mixedrandstrobes_iter(seq: str, k_size: int, strobe_w_min_offset: int,
strobe_w_max_offset: int, prime: int, w: int,
order: int, denominator: int,
numerator: int) -> Iterator[tuple]:
"""
Iterator for creating of mixedrandstrobes of any order
:param seq: a string with a nucleotide sequence
:param k_size: length of each strobe
:param strobe_w_min_offset: minimum window offset to the previous window (wMin > 0)
:param strobe_w_max_offset: maximum window offset to the previous window (wMin <= wMax)
:param prime: prime number (q) in minimizing h(m)+h(mj) mod q
:param w: number of hashes used in a sliding window for thinning (w=1 means no thinning)
:param order: number of substrings/strobes
:param denominator: denominator and numerator determine the fraction of sampled strobemers
:param numerator: denominator and numerator determine the fraction of sampled strobemers
:returns: an iterator for creating mixedrandstrobes
"""
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]
]
# thinning
if w > 1:
# produce a subset of positions, still with samme index as in full sequence
hash_seq_list_thinned = thinner([h for i, h in hash_seq_list], w)
else:
hash_seq_list_thinned = hash_seq_list
for (p1, hash_m1) in hash_seq_list_thinned: # [:-k_size]:
if p1 >= len(hash_seq_list) - (order-1)*k_size:
break
# hash_m1 = hash_seq_list[p]
if hash_m1 % denominator < numerator: # pick randstrobe
if p1 + (order-1) * strobe_w_max_offset <= len(hash_seq_list):
windows = list()
for window_order in range(1, order):
start = p1 + strobe_w_min_offset + (window_order-1) * strobe_w_max_offset
end = min(p1 + window_order * strobe_w_max_offset, len(hash_seq_list))
windows.append((start, end))
else:
windows = list()
for window_order in range(1, order):
start = (max(
p1+window_order*k_size,
len(hash_seq_list) + strobe_w_min_offset - (order - window_order) * strobe_w_max_offset
)
)
end = min(p1 + window_order * strobe_w_max_offset, len(hash_seq_list))
windows.append((start, end))
positions = [p1, ]
min_hash_val = hash_m1
for index_order in range(1, order):
min_index, min_value = argmin([
(min_hash_val + hash_seq_list[i][1]) % prime
for i in range(*windows[index_order-1])
])
min_hash_val = min_hash_val + (index_order * (-1)**index_order) * hash_seq_list[windows[index_order-1][0] + min_index][1]
positions.append(min_index+windows[index_order-1][0])
yield positions, min_hash_val
else: # pick k-mer
positions = tuple(p1 + (strobe_num) * k_size for strobe_num in range(order))
yield positions, hash(seq[p1: p1+k_size*order])
def seq_to_kmer_iter(seq: str, k_size: int, w: int) -> Iterator[tuple]:
"""
Iterator for creating kmers
:param seq: a string with a nucleotide sequence
:param k_size: length of the kmer
:param w: number of hashes used in a sliding window for thinning (w=1 means no thinning)
:returns: an iterator for creating kmers
"""
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:
# produce a subset of positions, still with samme index as in full sequence
hash_seq_list_thinned = thinner([h for i, h in hash_seq_list], w)
for p, h in hash_seq_list_thinned:
yield ([p,], h)
else:
for p, h in hash_seq_list:
yield ([p,], h)
def grouper(iterable, n: int, fillvalue=None) -> Iterator:
"""
Collect data into fixed-length chunks or blocks without throwing excess items away
:param iterable: sequence data type with values to combine
:param n: length of chunks or blocks
:param fillvalue: plugs the gap if there is no value to match to one of our iterable's elements
:returns: a terminating iterator (zip_longest) with fixed-length chunks or blocks
"""
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
def build_kmer_index(refs, k_size: int, w: int) -> tuple:
"""
Build kmers from references
:param refs: reference fasta file
:param k_size: length of the kmer
:param w: number of hashes used in a sliding window for thinning (w=1 means no thinning)
:returns: a tuple with an index-dictionary, a reference-to-accession-number dictionary and a counter of kmers created from references
"""
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 positions, hash_val in seq_to_kmer_iter(seq, k_size, w):
idx[hash_val].append(r_id)
idx[hash_val].append(positions[0])
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 build_strobemer_index(method: str, refs, k_size: int,
strobe_w_min_offset: int, strobe_w_max_offset: int,
prime: int, w: int, order: int) -> tuple:
"""
Build strobemers from references
:param method: string which specifies whether minstrobes or randstrobes should be sampled
:param refs: reference fasta file
:param k_size: length of all strobes combined
:param strobe_w_min_offset: minimum window offset to the previous window (wMin > 0)
:param strobe_w_max_offset: maximum window offset to the previous window (wMin <= wMax)
:param prime: rime number (q) in minimizing h(m)+h(mj) mod q
:param w: number of hashes used in a sliding window for thinning (w=1 means no thinning)
:param order: number of substrings/strobes
:returns: a tuple with an index-dictionary, a reference-to-accession-number dictionary and a counter of strobemers created from references
"""
idx = defaultdict(lambda: array("L"))
ref_id_to_accession = {}
cntr = 0
method_function = "seq_to_" + method + "_iter"
for r_id, (ref_acc, (seq, _)) in enumerate(help_functions.readfq(refs)):
ref_id_to_accession[r_id] = ref_acc
for positions, hash_val in globals()[method_function](seq, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w, order):
idx[hash_val].append(r_id)
for pos in positions:
idx[hash_val].append(pos)
cntr += 1
if cntr % 1000000 == 0:
print("{0} {1} created from references".format(cntr, method))
# print(hash_val, r_id, pos)
return idx, ref_id_to_accession, cntr
def build_mixedstrobemer_index(method: str, refs, k_size: int,
strobe_w_min_offset: int, strobe_w_max_offset: int,
prime: int, w: int, order: int,
denominator: int, numerator: int) -> tuple:
"""
Build mixedstrobes (strobemer/kmer) from references
:param method: string which specifies whether mixedminstrobes or mixedrandstrobes should be sampled
:param refs: reference fasta file
:param k_size: length of all strobes combined
:param strobe_w_min_offset: minimum window offset to the previous window (wMin > 0)
:param strobe_w_max_offset: maximum window offset to the previous window (wMin <= wMax)
:param prime: prime number (q) in minimizing h(m)+h(mj) mod q
:param w: number of hashes used in a sliding window for thinning (w=1 means no thinning)
:param order: number of substrings/strobes
:param denominator: denominator and numerator determine the fraction of sampled strobemers
:param numerator: denominator and numerator determine the fraction of sampled strobemers
:returns: a tuple with an index-dictionary, a reference-to-accession-number dictionary and a counter of mixedstrobemers created from references
"""
idx = defaultdict(lambda: array("L"))
ref_id_to_accession = {}
cntr = 0
method_function = "seq_to_" + method + "_iter"
for r_id, (ref_acc, (seq, _)) in enumerate(help_functions.readfq(refs)):
ref_id_to_accession[r_id] = ref_acc
for positions, hash_val in globals()[method_function](seq, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w, order, denominator, numerator):
idx[hash_val].append(r_id)
for pos in positions:
idx[hash_val].append(pos)
cntr += 1
if cntr % 1000000 == 0:
print("{0} {1} created from references".format(cntr, method))
# print(hash_val, r_id, pos)
return idx, ref_id_to_accession, cntr
def build_hybridstrobe_index(refs, k_size: int, strobe_w_min_offset: int,
strobe_w_max_offset: int, prime: int, w: int,
order) -> tuple:
"""
Build hybridstrobes from references
:param refs: reference fasta file
:param k_size: length of all strobes combined
:param strobe_w_min_offset: minimum window offset to the previous window (wMin > 0)
:param strobe_w_max_offset: maximum window offset to the previous window (wMin <= wMax)
:param prime: prime number (q) in minimizing h(m)+h(mj) mod q
:param w: number of hashes used in a sliding window for thinning (w=1 means no thinning)
:param order: number of substrings/strobes
:returns: a tuple with an index-dictionary, a reference-to-accession-number dictionary and a counter of hybridstrobes created from references
"""
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 positions, hash_val in seq_to_hybridstrobes_iter(seq, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w, order):
idx[hash_val].append(r_id)
for pos in positions:
idx[hash_val].append(pos)
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 positions, hash_val in seq_to_hybridstrobes_iter(seq, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w, order):
if positions[0] in sampled_positions:
idx[hash_val].append(r_id)
for pos in positions:
idx[hash_val].append(pos)
sampled_positions.remove(positions[0])
assert len(sampled_positions) == 0
cntr += 1
if p1 < w:
info_buffer.append((positions, 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((positions, 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)
(positions_, hash_val_) = info_buffer[min_index]
idx[hash_val].append(r_id)
for pos in positions_:
idx[hash_val].append(pos)
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)
for pos in positions:
idx[hash_val].append(pos)
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_mixedhybridstrobe_index(refs, k_size: int,
strobe_w_min_offset: int, strobe_w_max_offset: int,
prime: int, w: int, order: int,
denominator: int, numerator: int) -> tuple:
"""
Build mixedhybridstrobes from references
:param refs: reference fasta file
:param k_size: length of all strobes combined
:param strobe_w_min_offset: minimum window offset to the previous window (wMin > 0)
:param strobe_w_max_offset: maximum window offset to the previous window (wMin <= wMax)
:param prime: prime number (q) in minimizing h(m)+h(mj) mod q
:param w: number of hashes used in a sliding window for thinning (w=1 means no thinning)
:param order: number of substrings/strobes
:param denominator: denominator and numerator determine the fraction of sampled strobemers
:param numerator: denominator and numerator determine the fraction of sampled strobemers
:returns a tuple with an index-dictionary, a reference-to-accession-number dictionary and a counter of mixedhybridstrobes created from references
"""
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 positions, hash_val in seq_to_mixedhybridstrobes_iter(seq, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w, order, denominator, numerator):
idx[hash_val].append(r_id)
for pos in positions:
idx[hash_val].append(pos)
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 positions, hash_val in seq_to_mixedhybridstrobes_iter(seq, k_size, strobe_w_min_offset, strobe_w_max_offset, prime, w, order, denominator, numerator):
if positions[0] in sampled_positions:
idx[hash_val].append(r_id)
for pos in positions:
idx[hash_val].append(pos)
sampled_positions.remove(positions[0])
assert len(sampled_positions) == 0
cntr += 1
if p1 < w:
info_buffer.append((positions, 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((positions, 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)
(positions_, hash_val_) = info_buffer[min_index]
idx[hash_val].append(r_id)
for pos in positions_:
idx[hash_val].append(pos)
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)
for pos in positions:
idx[hash_val].append(pos)
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 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 matches_query(strobes:list, idx: dict, k: int) -> Iterator:
"""
Iterate over query in ascending order and check if hash value in reference
:param strobes: list of kmers/strobemers with (positions, hash_values) for each sampled kmer/strobemer
:param idx: a dictionary with reference indexes
:param k: strobe size
:returns: iterator with tuple of q_start, q_end and hash value
"""
for q_positions, h in strobes:
if h in idx:
# extract query start and end position
q_start = q_positions[0] + 1
q_end = q_positions[-1] + k + 1
yield q_start, q_end, h
def extend_nams(cpm: dict, merged_matches: list, r_id: int, q_start: int, q_end: int, r_start: int, r_end: int):
"""
Extend Non-Overlapping Approximate Matches and updates the cmp dictionary.
:param cpm: dict with open merge intervals
:param merged_matches: list of merged matches
:param r_id: reference id
:param q_start: query start position of the current hit
:param q_end: query end position of the current hit
:param r_start: reference start position matching the query hit position
:param r_end: reference end position matching the query hit position
:returns: updated dictionary of open merge intervals and list of merged_matches
"""
# query and reference positions are already in cmp
# check all queries for overlap
is_added_to_an_interval_query = False
for prev_q_end in list(cpm[r_id].keys()):
if q_start <= prev_q_end: # overlap on query
is_added_to_an_interval_query = True
is_added_to_an_interval_reference = False
# check all references for overlap
for prev_r_end in list(cpm[r_id][prev_q_end].keys()):
prev_q_start, prev_q_end, prev_r_start, prev_r_end = cpm[r_id][prev_q_end][prev_r_end]
# update query positions
new_q_end = max(prev_q_end, q_end)
if prev_r_start <= r_start <= prev_r_end: # overlap on reference
is_added_to_an_interval_reference = True
# update reference positions
new_r_end = max(prev_r_end, r_end)
# update cpm dictionary
del cpm[r_id][prev_q_end][prev_r_end] # delete old reference positions
if not cpm[r_id][prev_q_end]: # delete old query positions if empty
del cpm[r_id][prev_q_end]
if new_q_end not in cpm[r_id]: # add new query and reference positions
cpm[r_id][new_q_end] = {}
cpm[r_id][new_q_end][new_r_end] = (prev_q_start, new_q_end, prev_r_start, new_r_end)
elif new_r_end not in cpm[r_id][new_q_end]: # append
cpm[r_id][new_q_end][new_r_end] = (prev_q_start, new_q_end, prev_r_start, new_r_end)
else: # was already present
(old_q_start, new_q_end, old_r_start, new_r_end) = cpm[r_id][new_q_end][new_r_end]
cpm[r_id][new_q_end][new_r_end] = (min(old_q_start, prev_q_start), new_q_end, min(old_r_start, prev_r_start), new_r_end)
# no overlap with any reference
if not is_added_to_an_interval_reference:
if new_q_end not in cpm[r_id]: # new added 1
cpm[r_id][new_q_end] = {}
cpm[r_id][new_q_end][r_end] = (q_start, new_q_end, r_start, r_end)
elif r_end not in cpm[r_id][new_q_end]: # new added 2
cpm[r_id][new_q_end][r_end] = (q_start, new_q_end, r_start, r_end)
else: # was already present
(old_q_start, new_q_end, old_r_start, new_r_end) = cpm[r_id][new_q_end][r_end]
cpm[r_id][new_q_end][new_r_end] = (min(old_q_start, q_start), new_q_end, min(old_r_start, r_start), new_r_end)
else:
# 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_pos_stop in cpm[r_id][prev_q_end]:
(q_pos_start, q_pos_stop, r_pos, r_pos_stop) = cpm[r_id][prev_q_end][r_pos_stop]
merged_matches.append((r_id, r_pos, q_pos_start, r_pos_stop - r_pos))
del cpm[r_id][prev_q_end]
# add match to cpm if all all intervals have been passed
if q_end not in cpm[r_id]:
cpm[r_id][q_end] = {r_end: (q_start, q_end, r_start, r_end)}
if not is_added_to_an_interval_query: # no overlap with prev query sequences
cpm[r_id][q_end] = {}
cpm[r_id][q_end][r_end] = (q_start, q_end, r_start, r_end)
return cpm, merged_matches
def get_unmerged_matches(strobes: list, idx: dict, k: int,
ref_id_to_accession: dict, acc, selfalign: bool,
order: int) -> list:
"""
It is seriously advised to merge matches as the files can become huge otherwise and fill up all diskspace.
:param strobes: list of kmers/strobemers with (positions, hash_values) for each sampled kmer/strobemer
:param idx: a dictionary with reference indexes
:param k: strobe size
:param ref_id_to_accession: dictionary with references and accession numbers
:param acc: accession number
:param selfalign: aligns sequences to itself (mainly for bugfixing)
:param order: number of substrings/strobes
:returns: sorted list of (merged) matches
"""
matches = []
# iterate over query in ascending order and check if hash value in reference
for q_start, q_end, h in matches_query(strobes, idx, k):
for r_id, *r_positions in grouper(idx[h], order+1):
matches.append((r_id, r_positions[0] + 1, q_start, r_positions[-1] - r_positions[0] + k))
return sorted(matches, key=lambda x: (x[0], x[2], x[1]))
def get_merged_matches(strobes: list, idx: dict, k: int, ref_id_to_accession: dict,
acc, selfalign: bool, order: int) -> list:
"""
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.
:param strobes: list of kmers/strobemers with (positions, hash_values) for each sampled kmer/strobemer
:param idx: a dictionary with reference indexes
:param k: strobe size
:param ref_id_to_accession: dictionary with references and accession numbers
:param acc: accession number
:param selfalign: aligns sequences to itself (mainly for bugfixing)
:param order: number of substrings/strobes
:returns: sorted list of (merged) matches
"""
cpm = {} # current potential merges
# FORMAT: cpm[r_id] = {end_q: {end_r: (start_q, end_q, start_r, end_r)}}
merged_matches = []
# iterate over query in ascending order and check if hash value in reference
for q_start, q_end, h in matches_query(strobes, idx, k):
# iterate over references, all in ascending order
for r_id, *r_positions in grouper(idx[h], order+1):
# remove self matches with below if statement
if not selfalign and ref_id_to_accession[r_id] == acc:
continue