-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLevenshtein_distance.h
4873 lines (4220 loc) · 155 KB
/
Levenshtein_distance.h
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
#ifndef __LEVENSHTEIN__
#define __LEVENSHTEIN__
#define __STDC_LIMIT_MACROS
#include <stdint.h>
#ifndef SIMDE_ENABLE_NATIVE_ALIASES
#define SIMDE_ENABLE_NATIVE_ALIASES 1
#endif
#ifdef __aarch64__
#include<simde/x86/avx.h>
#include<simde/x86/clmul.h>
#include<simde/x86/gfni.h>
#include<simde/x86/sse2.h>
#include<simde/x86/sse4.2.h>
#include<simde/x86/xop.h>
#include<simde/x86/avx2.h>
#include<simde/x86/f16c.h>
#include<simde/x86/mmx.h>
#include<simde/x86/sse3.h>
#include<simde/x86/ssse3.h>
#include<simde/x86/avx512.h>
#include<simde/x86/fma.h>
#include<simde/x86/sse.h>
#include<simde/x86/sse4.1.h>
#include<simde/x86/svml.h>
#else
#include "emmintrin.h"
#include "nmmintrin.h"
#include "smmintrin.h"
#include <immintrin.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "kvec.h"
extern const unsigned char seq_nt4_table[256];
typedef uint64_t Word;
typedef uint32_t Word_32;
typedef struct {size_t n, m; uint16_t *a; } asg16_v;
inline void get_error(int t_length, int errthold, int init_err, Word VP, Word VN,
unsigned int* return_err, int* back_site)
{
(*return_err) = (unsigned int)-1;
int site = t_length - 1;
int return_site = -1;
///in most cases, p_length should be t_length + 2 * errthold
///int available_i = p_length - t_length;
int available_i = 2 * errthold;
if ((init_err <= errthold) && ((unsigned int)init_err <= (*return_err)))
{
(*return_err) = init_err;
return_site = site;
}
int i = 0;
unsigned int ungap_error = (unsigned int)-1;
while (i < available_i)
{
init_err = init_err + ((VP >> i)&(Word)1);
init_err = init_err - ((VN >> i)&(Word)1);
++i;
if ((init_err <= errthold) && ((unsigned int)init_err <= *return_err))
{
*return_err = init_err;
return_site = site + i;
}
/****************************may have bugs********************************/
if(i == errthold)
{
ungap_error = init_err;
}
/****************************may have bugs********************************/
}
/****************************may have bugs********************************/
if((ungap_error<=(unsigned int)errthold) && (ungap_error == (*return_err)))
{
return_site = site + errthold;
}
/****************************may have bugs********************************/
(*back_site) = return_site;
}
inline int Reserve_Banded_BPM_Extension
(char *pattern, int p_length, char *text, int t_length, unsigned short errthold,
unsigned int* return_err, int* return_p_end, int* return_t_end)
{
(*return_err) = (unsigned int)-1;
(*return_p_end) = -1;
(*return_t_end) = -1;
Word Peq[256];
unsigned int line_error = (unsigned int)-1;
int return_site;
int band_length = (errthold << 1) + 1;
int i = 0;
Word tmp_Peq_1 = (Word)1;
Peq[(uint8_t)'A'] = (Word)0;
Peq[(uint8_t)'T'] = (Word)0;
Peq[(uint8_t)'G'] = (Word)0;
Peq[(uint8_t)'C'] = (Word)0;
Word Peq_A;
Word Peq_T;
Word Peq_C;
Word Peq_G;
///band_length = 2k + 1
for (i = 0; i<band_length; i++)
{
Peq[(uint8_t)pattern[i]] = Peq[(uint8_t)pattern[i]] | tmp_Peq_1;
tmp_Peq_1 = tmp_Peq_1 << 1;
}
Peq_A = Peq[(uint8_t)'A'];
Peq_C = Peq[(uint8_t)'C'];
Peq_T = Peq[(uint8_t)'T'];
Peq_G = Peq[(uint8_t)'G'];
memset(Peq, 0, sizeof(Word)* 256);
Peq[(uint8_t)'A'] = Peq_A;
Peq[(uint8_t)'C'] = Peq_C;
Peq[(uint8_t)'T'] = Peq_T;
Peq[(uint8_t)'G'] = Peq_G;
Word Mask = ((Word)1 << (errthold << 1));
Word VP = 0;
Word VN = 0;
Word X = 0;
Word D0 = 0;
Word HN = 0;
Word HP = 0;
i = 0;
int err = 0;
Word err_mask = (Word)1;
int i_bd = (errthold << 1);
int last_high = (errthold << 1);
int t_length_1 = t_length - 1;
while (i<t_length_1)
{
X = Peq[(uint8_t)text[i]] | VN;
D0 = ((VP + (X&VP)) ^ VP) | X;
HN = VP&D0;
HP = VN | ~(VP | D0);
X = D0 >> 1;
VN = X&HP;
VP = HN | ~(X | HP);
if (!(D0&err_mask))
{
++err;
if ((err - last_high)>errthold)
{
return (*return_t_end);
}
}
get_error(i + 1, errthold, err, VP, VN, &line_error, &return_site);
if(line_error != (unsigned int)-1)
{
(*return_t_end) = i;
(*return_p_end) = return_site;
(*return_err) = line_error;
}
Peq[(uint8_t)'A'] = Peq[(uint8_t)'A'] >> 1;
Peq[(uint8_t)'C'] = Peq[(uint8_t)'C'] >> 1;
Peq[(uint8_t)'G'] = Peq[(uint8_t)'G'] >> 1;
Peq[(uint8_t)'T'] = Peq[(uint8_t)'T'] >> 1;
++i;
++i_bd;
Peq[(uint8_t)pattern[i_bd]] = Peq[(uint8_t)pattern[i_bd]] | Mask;
}
X = Peq[(uint8_t)text[i]] | VN;
D0 = ((VP + (X&VP)) ^ VP) | X;
HN = VP&D0;
HP = VN | ~(VP | D0);
X = D0 >> 1;
VN = X&HP;
VP = HN | ~(X | HP);
if (!(D0&err_mask))
{
++err;
if ((err - last_high)>errthold)
{
return (*return_t_end);
}
}
///i = t_length - 1
get_error(i + 1, errthold, err, VP, VN, &line_error, &return_site);
if(line_error != (unsigned int)-1)
{
(*return_t_end) = i;
(*return_p_end) = return_site;
(*return_err) = line_error;
}
return (*return_t_end);
}
inline int Reserve_Banded_BPM_Extension_REV
(char *pattern, int p_length, char *text, int t_length, unsigned short errthold,
unsigned int* return_err, int* return_p_end, int* return_t_end)
{
(*return_err) = (unsigned int)-1;
(*return_p_end) = -1;
(*return_t_end) = -1;
Word Peq[256];
unsigned int line_error = (unsigned int)-1;
int return_site;
int band_length = (errthold << 1) + 1;
int i = 0;
Word tmp_Peq_1 = (Word)1;
Peq[(uint8_t)'A'] = (Word)0;
Peq[(uint8_t)'T'] = (Word)0;
Peq[(uint8_t)'G'] = (Word)0;
Peq[(uint8_t)'C'] = (Word)0;
Word Peq_A;
Word Peq_T;
Word Peq_C;
Word Peq_G;
///band_length = 2k + 1
for (i = 0; i<band_length; i++)
{
Peq[(uint8_t)pattern[p_length-i-1]] = Peq[(uint8_t)pattern[p_length-i-1]] | tmp_Peq_1;
tmp_Peq_1 = tmp_Peq_1 << 1;
}
Peq_A = Peq[(uint8_t)'A'];
Peq_C = Peq[(uint8_t)'C'];
Peq_T = Peq[(uint8_t)'T'];
Peq_G = Peq[(uint8_t)'G'];
memset(Peq, 0, sizeof(Word)* 256);
Peq[(uint8_t)'A'] = Peq_A;
Peq[(uint8_t)'C'] = Peq_C;
Peq[(uint8_t)'T'] = Peq_T;
Peq[(uint8_t)'G'] = Peq_G;
Word Mask = ((Word)1 << (errthold << 1));
Word VP = 0;
Word VN = 0;
Word X = 0;
Word D0 = 0;
Word HN = 0;
Word HP = 0;
i = 0;
int err = 0;
Word err_mask = (Word)1;
int i_bd = (errthold << 1);
int last_high = (errthold << 1);
int t_length_1 = t_length - 1;
while (i<t_length_1)
{
X = Peq[(uint8_t)text[t_length-i-1]] | VN;
D0 = ((VP + (X&VP)) ^ VP) | X;
HN = VP&D0;
HP = VN | ~(VP | D0);
X = D0 >> 1;
VN = X&HP;
VP = HN | ~(X | HP);
if (!(D0&err_mask))
{
++err;
if ((err - last_high)>errthold)
{
return (*return_t_end);
}
}
get_error(i + 1, errthold, err, VP, VN, &line_error, &return_site);
if(line_error != (unsigned int)-1)
{
(*return_t_end) = t_length-i-1;
(*return_p_end) = p_length-return_site-1;
(*return_err) = line_error;
}
Peq[(uint8_t)'A'] = Peq[(uint8_t)'A'] >> 1;
Peq[(uint8_t)'C'] = Peq[(uint8_t)'C'] >> 1;
Peq[(uint8_t)'G'] = Peq[(uint8_t)'G'] >> 1;
Peq[(uint8_t)'T'] = Peq[(uint8_t)'T'] >> 1;
++i;
++i_bd;
Peq[(uint8_t)pattern[p_length-i_bd-1]] = Peq[(uint8_t)pattern[p_length-i_bd-1]] | Mask;
}
X = Peq[(uint8_t)text[t_length-i-1]] | VN;
D0 = ((VP + (X&VP)) ^ VP) | X;
HN = VP&D0;
HP = VN | ~(VP | D0);
X = D0 >> 1;
VN = X&HP;
VP = HN | ~(X | HP);
if (!(D0&err_mask))
{
++err;
if ((err - last_high)>errthold)
{
return (*return_t_end);
}
}
///i = t_length - 1
get_error(i + 1, errthold, err, VP, VN, &line_error, &return_site);
if(line_error != (unsigned int)-1)
{
(*return_t_end) = t_length-i-1;
(*return_p_end) = p_length-return_site-1;
(*return_err) = line_error;
}
return (*return_t_end);
}
inline void reverse_string(char* str, int strLen)
{
int i, Len;
char k;
Len = strLen / 2;
for (i = 0; i < Len; i++)
{
k = str[i];
str[i] = str[strLen - i - 1];
str[strLen - i - 1] = k;
}
}
inline int alignment_extension(char *pattern, int p_length, char *text, int t_length,
unsigned short errthold, int direction, unsigned int* return_err, int* return_p_end,
int* return_t_end, int* return_aligned_t_len)
{
(*return_aligned_t_len) = 0;
if(direction == 0)
{
Reserve_Banded_BPM_Extension(pattern, p_length, text, t_length, errthold, return_err,
return_p_end, return_t_end);
if((*return_p_end) != -1 && (*return_t_end) != -1)
{
(*return_aligned_t_len) = (*return_t_end) + 1;
return 1;
}
else
{
return -1;
}
}
else
{
reverse_string(pattern, p_length);
reverse_string(text, t_length);
Reserve_Banded_BPM_Extension(pattern, p_length, text, t_length, errthold, return_err,
return_p_end, return_t_end);
reverse_string(pattern, p_length);
reverse_string(text, t_length);
if((*return_p_end) != -1 && (*return_t_end) != -1)
{
(*return_aligned_t_len) = (*return_t_end) + 1;
(*return_p_end) = p_length - (*return_p_end);
(*return_t_end) = t_length - (*return_t_end);
return 1;
}
else
{
return -1;
}
}
}
inline void print_bit(Word z, int64_t w, const char *cmd)
{
int64_t k;//, w = (sizeof(Word)<<3);
fprintf(stderr, "%s\t", cmd);
for (k = w-1; k >= 0; k--) fprintf(stderr, "%llu", (z>>k)&(1ULL));
fprintf(stderr, "\n");
}
inline int32_t ed_band_cal_global(char *pstr, int32_t pn, char *tstr, int32_t tn, int32_t thre)
{
if((pn > tn + thre) || (tn > pn + thre)) return INT32_MAX;
if((pn < thre + 1) || (tn < thre + 1)) return INT32_MAX;
Word Peq[5] = {0}, mm, VP = 0, VN = 0, X = 0, D0 = 0, HN = 0, HP = 0;
int32_t i, err, tn0 = tn - 1, cut = thre+(thre<<1), bd = thre+1, i_bd = thre;
// fprintf(stderr, "\n[M::%s::]\n", __func__);
for (i = 0, mm = (((Word)1)<<thre); i < bd; i++) {
Peq[seq_nt4_table[(uint8_t)pstr[i]]] |= mm; mm <<= 1;
}
Peq[4] = 0;
err = thre;
VN = (((Word)1)<<(thre))-1;
VP = (((Word)1)<<((thre<<1)+1))-1; VP ^= VN;
// print_bit(Peq[0], (thre<<1)+1, "Peq[A]");
// print_bit(Peq[1], (thre<<1)+1, "Peq[C]");
// print_bit(Peq[2], (thre<<1)+1, "Peq[G]");
// print_bit(Peq[3], (thre<<1)+1, "Peq[T]");
// print_bit(Peq[4], (thre<<1)+1);
// print_bit(VN, (thre<<1)+1, "VN");
// print_bit(VP, (thre<<1)+1, "VP");
///should make Peq[4] = 0 if N is always an error
i = 0; mm = ((Word)1 << (thre<<1));///for the incoming char/last char
while (i < tn0) {
X = Peq[seq_nt4_table[(uint8_t)tstr[i]]] | VN;
D0 = ((VP + (X&VP)) ^ VP) | X;
HN = VP&D0;
HP = VN | ~(VP | D0);
X = D0 >> 1;
VN = X&HP;
VP = HN | ~(X | HP);
// fprintf(stderr, "\n[M::%s::i->%d]\n", __func__, i);
// print_bit(VN, (thre<<1)+1, "VN");
// print_bit(VP, (thre<<1)+1, "VP");
// print_bit(HN, (thre<<1)+1, "HN");
// print_bit(HP, (thre<<1)+1, "HP");
// print_bit(D0, (thre<<1)+1, "D0");
if (!(D0&(1ULL))) {
++err;
if (err>cut) return INT32_MAX;
}
// fprintf(stderr, "[M::%s::i->%d] err->%d\n", __func__, i, err);
Peq[0] >>= 1; Peq[1] >>= 1; Peq[2] >>= 1; Peq[3] >>= 1; ///Peq[4] >>= 1;
++i; ++i_bd;
if(i_bd < pn) {
Peq[seq_nt4_table[(uint8_t)pstr[i_bd]]] |= mm; Peq[4] = 0;
}
// if(i < pn) Peq[seq_nt4_table[(uint8_t)pstr[i]]] |= mm;
}
X = Peq[seq_nt4_table[(uint8_t)tstr[i]]] | VN;
D0 = ((VP + (X&VP)) ^ VP) | X;
HN = VP&D0;
HP = VN | ~(VP | D0);
X = D0 >> 1;
VN = X&HP;
VP = HN | ~(X | HP);
// fprintf(stderr, "\n[M::%s::i->%d]\n", __func__, i);
// print_bit(VN, (thre<<1)+1, "VN");
// print_bit(VP, (thre<<1)+1, "VP");
// print_bit(HN, (thre<<1)+1, "HN");
// print_bit(HP, (thre<<1)+1, "HP");
// print_bit(D0, (thre<<1)+1, "D0");
if (!(D0&(1ULL))) {
++err;
if (err>cut) return INT32_MAX;
}
// fprintf(stderr, "[M::%s::i->%d] err->%d\n", __func__, i, err);
int32_t site = tn - 1 - thre;///up bound
for (cut = pn - 1, i = 0; site < cut; site++, i++) {
// fprintf(stderr, "+[M::%s::site->%d] err->%d\n", __func__, site, err);
err += ((VP >> i)&(1ULL)); err -= ((VN >> i)&(1ULL));
// fprintf(stderr, "-[M::%s::site->%d] err->%d\n", __func__, site, err);
}
if (site == cut && err <= thre) return err;
return INT32_MAX;
}
#define EAC_M 0
#define MIS_M 1
#define MOR_YP 2
#define MOR_XT 3
inline void push_trace(asg16_v *res, uint16_t c, uint32_t len)
{
uint16_t p; c <<= 14;
while (len >= (0x3fff)) {
p = (c + (0x3fff)); kv_push(uint16_t, *res, p); len -= (0x3fff);
}
if(len) {
p = (c + len); kv_push(uint16_t, *res, p);
}
}
inline uint32_t pop_trace(asg16_v *res, uint32_t i, uint16_t *c, uint32_t *len)
{
(*c) = (res->a[i]>>14); (*len) = (res->a[i]&(0x3fff));
for (i++; (i < res->n) && ((*c) == (res->a[i]>>14)); i++) {
(*len) += (res->a[i]&(0x3fff));
}
return i;
}
inline int32_t pop_trace_back(asg16_v *res, int32_t i, uint16_t *c, uint32_t *len)
{
(*c) = (res->a[i]>>14); (*len) = (res->a[i]&(0x3fff));
for (i--; (i >= 0) && ((*c) == (res->a[i]>>14)); i--) {
(*len) += (res->a[i]&(0x3fff));
}
return i;
}
///511 -> 16 64-bits
// #define MAX_E 511
// #define MAX_L 2500
///511 -> 32 64-bits
#define MAX_CNS_E 1023
#define MAX_CNS_L 3072
#define FORCE_CNS_L 256
#define MAX_SIN_E 2047
#define MAX_SIN_L 10000
#define FORCE_SIN_L 512
typedef uint64_t w_sig;
#define bitw (6)
#define bitwbit (64)
#define bitz (63)
// typedef uint32_t w_sig;
// #define bitw (5)
// #define bitwbit (32)
// #define bitz (31)
// typedef uint16_t w_sig;
// #define bitw (4)
// #define bitwbit (16)
// #define bitz (15)
// typedef uint8_t w_sig;
// #define bitw (3)
// #define bitwbit (8)
// #define bitz (7)
typedef struct {w_sig *a;} w128_t;
typedef struct {size_t n, m; w_sig *a;} w64_trace_t;
typedef struct {
int32_t done_cigar, cigar_n, done_path, path_n;
int32_t ps, pe, pl, ts, te, tl;
int32_t thre, err, nword, mword;
uint32_t m, mm_thres; w_sig *a;
w128_t Peq[5], mm, VP, VN, X, D0, HN, HP;
asg16_v cigar; w64_trace_t path;
} bit_extz_t;
#define is_align(exz) ((exz).err<=(exz).thre)
#define clear_align(exz) ((exz).err=INT32_MAX)
inline uint32_t cigar_check(char *pstr, char *tstr, bit_extz_t *ez)
{
int32_t pi = ez->ps, ti = ez->ts, err = 0; uint32_t ci = 0, cl, k; uint16_t c;
while (ci < ez->cigar.n) {
ci = pop_trace(&(ez->cigar), ci, &c, &cl);
// fprintf(stderr, "# %u = %u\n", c, cl);
if(c == 0) {
for (k=0;(k<cl)&&(pstr[pi]==tstr[ti]);k++,pi++,ti++);
if(k!=cl) {
fprintf(stderr, "ERROR-d-0\n");
return 0;
}
} else {
err += cl;
if(c == 1) {
for (k=0;(k<cl)&&(pstr[pi]!=tstr[ti]);k++,pi++,ti++);
if(k!=cl) {
fprintf(stderr, "ERROR-d-1\n");
return 0;
}
} else if(c == 2) {///more p
pi+=cl;
} else if(c == 3) {
ti+=cl;
}
}
}
if(err != ez->err) {
fprintf(stderr, "ERROR-err\n");
return 0;
}
return 1;
}
inline int32_t dbg_ext_err(int32_t i, int32_t thre, int32_t err, int32_t pe, w128_t *VP, w128_t *VN)
{
int32_t poff = i-thre, tmp_e = INT32_MAX, k, bd, k_bd;
if((poff) + (thre<<1) >= (pe)) {
tmp_e = err;
for ((k) = 0; (poff) < (pe); (poff)++) {
bd = (k>>bitw); k_bd = (k&bitz);
(tmp_e) += ((VP->a[bd]>>k_bd)&((w_sig)1));
(tmp_e) -= ((VN->a[bd]>>k_bd)&((w_sig)1));
(k)++;
}
}
return tmp_e;
}
inline void print_bits(w_sig *az, int64_t w, const char *cmd)
{
int64_t k, m, s = (sizeof(*az)<<3), sw = (w/s) + (!!(w%s)), ks;
fprintf(stderr, "%s\t", cmd);
for (m = sw - 1, k = w-1; m >= 0 && k >= 0; m--) {
for (ks = k%s; ks >= 0 && k >= 0; ks--, k--) fprintf(stderr, "%llu", (az[m]>>ks)&(1ULL));
}
fprintf(stderr, "\n");
}
#define prt_bit_extz_t(ez, w) do { \
print_bits((ez).Peq[0].a, w, "Peq[0]");\
print_bits((ez).Peq[1].a, w, "Peq[1]");\
print_bits((ez).Peq[2].a, w, "Peq[2]");\
print_bits((ez).Peq[3].a, w, "Peq[3]");\
print_bits((ez).Peq[4].a, w, "Peq[4]");\
print_bits((ez).VP.a, w, "VP");\
print_bits((ez).VN.a, w, "VN");\
print_bits((ez).X.a, w, "X");\
print_bits((ez).D0.a, w, "D0");\
print_bits((ez).HN.a, w, "HN");\
print_bits((ez).HP.a, w, "HP");\
} while (0)
#define resize_bit_extz_t(ex, thres) do { \
if(((int32_t)(thres)) > ((int32_t)(ex).mm_thres)) {\
(ex).mm_thres = (((thres)<<1)+1);\
(ex).mm_thres = (((ex).mm_thres>>bitw)+(!!((ex).mm_thres&bitz)))*12;\
if((ex).mm_thres > (ex).m) {\
(ex).a = (w_sig *)realloc((ex).a, (ex).mm_thres * sizeof(*((ex).a)));\
(ex).mm_thres/=12; (ex).m=0;\
(ex).Peq[0].a = (ex).a; (ex).m+=(ex).mm_thres;\
(ex).Peq[1].a = (ex).a+(ex).m; (ex).m+=(ex).mm_thres;\
(ex).Peq[2].a = (ex).a+(ex).m; (ex).m+=(ex).mm_thres;\
(ex).Peq[3].a = (ex).a+(ex).m; (ex).m+=(ex).mm_thres;\
(ex).Peq[4].a = (ex).a+(ex).m; (ex).m+=(ex).mm_thres;\
(ex).mm.a = (ex).a+(ex).m; (ex).m+=(ex).mm_thres;\
(ex).VP.a = (ex).a+(ex).m; (ex).m+=(ex).mm_thres;\
(ex).VN.a = (ex).a+(ex).m; (ex).m+=(ex).mm_thres;\
(ex).X.a = (ex).a+(ex).m; (ex).m+=(ex).mm_thres;\
(ex).D0.a = (ex).a+(ex).m; (ex).m+=(ex).mm_thres;\
(ex).HN.a = (ex).a+(ex).m; (ex).m+=(ex).mm_thres;\
(ex).HP.a = (ex).a+(ex).m; (ex).m+=(ex).mm_thres;\
(ex).mword = (ex).mm_thres;\
}\
(ex).mm_thres = (thres);\
}\
} while (0)
inline void init_bit_extz_t(bit_extz_t *ex, uint64_t thres) {
memset(ex, 0, sizeof((*ex)));
///(bitwbit<<2) >= (((thres)<<1)+1);->at least 4 cells for each w128_t
if(((uint64_t)thres)<(((((uint64_t)bitwbit)<<2)-1)>>1)) thres=(((bitwbit<<2)-1)>>1);
resize_bit_extz_t((*ex), (thres));
}
inline void destroy_bit_extz_t(bit_extz_t *ex) {
free((*ex).a); free((*ex).path.a); free((*ex).cigar.a);
}
inline void gen_trace(bit_extz_t *ez, int32_t ptrim, int32_t reverse)///ptrim = thre for global and extension; = 0 for semi
{
if(ez->err > ez->thre) return;
ez->cigar.n = 0;
int32_t V, H, D, min, cur, tn = (ez->te+1-ez->ts), pn = tn + (ez->thre<<1), bd = (ez->thre<<1)+1;
int32_t bs = ez->path.n/tn, bbs = bs/5, poff = ez->pe, sft = bd - (pn - ez->pe - ptrim);
int32_t i = tn, low = bd-1, wi, wm, d = 0, pd = -1, pdn = 0; cur = ez->err;
w_sig *D0, *VP, *VN, *HP, *HN;
// fprintf(stderr, "\n[M::%s::] bs::%d, bbs::%d, cur::%d, poff::%d, sft::%d\n", __func__, bs, bbs, cur, poff, sft);
while (i > 0 && cur > 0) {
D0 = ez->path.a + ((i-1)*bs); VP = D0 + bbs; VN = VP + bbs; HP = VN + bbs; HN = HP + bbs;
wi = (sft>>bitw); wm = sft&bitz;
D = cur - ((~(D0[wi]>>wm))&(1ULL)); d = 0; min = D;
H = V = INT32_MAX;
if(sft!=low) {
H = cur + ((HN[wi]>> wm)&(1ULL)) - ((HP[wi]>> wm)&(1ULL));
if ((H+1) == cur && H <= min) {//prefer indels
min = H; d = 3;
}
}
if(sft!=0) {
wi = ((sft-1)>>bitw); wm = (sft-1)&bitz;
V = cur + ((VN[wi]>> wm)&(1ULL)) - ((VP[wi]>> wm)&(1ULL));
if ((V+1) == cur && V <= min) {//prefer indels
min = V; d = 2;
}
}
// fprintf(stderr, "[M::%s::] cur::%d, D::%d, V::%d, H::%d, poff::%d, toff::%d, d::%d\n", __func__, cur, D, V, H, poff, i, d);
if(d == 0) {
if(D != cur) d = 1;
i--; poff--;
} else if(d == 2) {//more pstr
sft--; poff--;
} else if(d == 3) {///more tstr
i--; sft++;
}
if(d == pd) {
pdn++;
} else {
if(pdn > 0) push_trace(&(ez->cigar), pd, pdn);
pd = d; pdn = 1;
}
cur = min;
}
if (i > 0) {
d = 0; poff -= i;
if(d == pd) {
pdn += i;
} else {
if(pdn > 0) push_trace(&(ez->cigar), pd, pdn);
pd = d; pdn = i;
}
}
poff++;
// fprintf(stderr, "[M::%s::] poff::%d, ez->ps::%d\n", __func__, poff, ez->ps);
if(ez->ps < 0 || ez->ps >= ez->pl) {//ps is unavailable
ez->ps = poff;
} else if(poff > ez->ps){
d = 2; i = poff - ez->ps;
if(d == pd) {
pdn += i;
} else {
if(pdn > 0) push_trace(&(ez->cigar), pd, pdn);
pd = d; pdn = i;
}
}
if(pdn > 0) push_trace(&(ez->cigar), pd, pdn);
if(reverse) {
uint16_t t; pdn = ez->cigar.n>>1;
for (i = 0; i < pdn; i++) {
t = ez->cigar.a[i];
ez->cigar.a[i] = ez->cigar.a[ez->cigar.n-i-1];
ez->cigar.a[ez->cigar.n-i-1] = t;
}
}
}
#define init_base_ed(ez, thre, pn, tn) {\
(ez).thre = (thre), (ez).err = INT32_MAX, (ez).pl = pn, (ez).tl = tn;\
(ez).done_cigar = (ez).done_path = (ez).cigar_n = (ez).path_n = 0;\
}
#define w_bit(x, b) ((x).a[((b)>>bitw)]|=(((w_sig)1)<<((b)&bitz)))
#define w_get_bit(x, b) (((x).a[((b)>>bitw)]>>((b)&bitz))&((w_sig)1))
/***********************2 words***********************/
#define w_128_clear(x) ((x).a[0]=(x).a[1]=0)
#define w_128_word (2)
#define w_128_self_not(x) ((x).a[0]=~(x).a[0], \
(x).a[1]=~(x).a[1])
#define w_128_self_or(x, y) ((x).a[0]|=(y).a[0], \
(x).a[1]|=(y).a[1])
#define w_128_or(r, x, y) ((r).a[0] = (x).a[0]|(y).a[0], \
(r).a[1] = (x).a[1]|(y).a[1])
#define w_128_and(r, x, y) ((r).a[0] = (x).a[0]&(y).a[0], \
(r).a[1] = (x).a[1]&(y).a[1])
#define w_128_self_xor(x, y) ((x).a[0]^=(y).a[0], \
(x).a[1]^=(y).a[1])
#define w_128_self_lsft_1(x) ((x).a[1] = ((x).a[1]<<1)|((x).a[0]>>bitz), \
(x).a[0] <<= 1)
#define w_128_self_rsft_1(x) ((x).a[0] = ((x).a[0]>>1)|((x).a[1]<<bitz), \
(x).a[1] >>= 1)
#define w_128_rsft_1(x, y) ((x).a[0] = ((y).a[0]>>1)|((y).a[1]<<bitz), \
(x).a[1] = (y).a[1]>>1)
#define w_128_self_add(x, y, c) ((x).a[0]+=(y).a[0], \
(x).a[1]+=(y).a[1]+((x).a[0]<(y).a[0]))
#define w_128_set_bit_lsub(x, l) do { \
(x).a[0] = (w_sig)-1, (x).a[1] = 0; \
if((l) <= bitwbit) (x).a[0] = (((w_sig)1)<<(l))-1; \
else (x).a[1] = (((w_sig)1)<<((l)-bitwbit))-1;\
} while (0) \
#define w_128_copy(x, y) ((x).a[0]=(y).a[0], (x).a[1]=(y).a[1])
/***********************3 words***********************/
#define w_192_clear(x) ((x).a[0]=(x).a[1]=(x).a[2]=0)
#define w_192_word (3)
#define w_192_self_not(x) ((x).a[0]=~(x).a[0],\
(x).a[1]=~(x).a[1],\
(x).a[2]=~(x).a[2])
#define w_192_self_or(x, y) ((x).a[0]|=(y).a[0],\
(x).a[1]|=(y).a[1],\
(x).a[2]|=(y).a[2])
#define w_192_or(r, x, y) ((r).a[0]=(x).a[0]|(y).a[0],\
(r).a[1] = (x).a[1]|(y).a[1],\
(r).a[2] = (x).a[2]|(y).a[2])
#define w_192_and(r, x, y) ((r).a[0] = (x).a[0]&(y).a[0], \
(r).a[1] = (x).a[1]&(y).a[1], \
(r).a[2] = (x).a[2]&(y).a[2])
#define w_192_self_xor(x, y) ((x).a[0]^=(y).a[0], \
(x).a[1]^=(y).a[1], \
(x).a[2]^=(y).a[2])
#define w_192_self_lsft_1(x) ((x).a[2] = ((x).a[2]<<1)|((x).a[1]>>bitz), \
(x).a[1] = ((x).a[1]<<1)|((x).a[0]>>bitz), \
(x).a[0] <<= 1)
#define w_192_self_rsft_1(x) ((x).a[0] = ((x).a[0]>>1)|((x).a[1]<<bitz), \
(x).a[1] = ((x).a[1]>>1)|((x).a[2]<<bitz), \
(x).a[2] >>= 1)
#define w_192_rsft_1(x, y) ((x).a[0] = ((y).a[0]>>1)|((y).a[1]<<bitz), \
(x).a[1] = ((y).a[1]>>1)|((y).a[2]<<bitz), \
(x).a[2] = (y).a[2]>>1)
#define w_192_self_add(x, y, c) ((x).a[0]+=(y).a[0], c=((x).a[0]<(y).a[0]),\
(x).a[1]+=c, c=((x).a[1]<c), (x).a[1]+=(y).a[1], c|=((x).a[1]<(y).a[1]),\
(x).a[2]+=c, (x).a[2]+=(y).a[2])
#define w_192_set_bit_lsub(x, l) do { \
(x).a[0] = (x).a[1] = (x).a[2] = 0; \
if((l)>>bitw) memset((x).a, -1, sizeof(*((x).a))*((l)>>bitw));\
if((l)&bitz) (x).a[(l)>>bitw] = (((w_sig)1)<<((l)&bitz))-1; \
} while (0) \
#define w_192_copy(x, y) ((x).a[0]=(y).a[0], (x).a[1]=(y).a[1], (x).a[2]=(y).a[2])
/***********************4 words***********************/
#define w_256_clear(x) ((x).a[0]=(x).a[1]=(x).a[2]=(x).a[3]=0)
#define w_256_word (4)
#define w_256_self_not(x) ((x).a[0]=~(x).a[0],\
(x).a[1]=~(x).a[1],\
(x).a[2]=~(x).a[2],\
(x).a[3]=~(x).a[3])
#define w_256_self_or(x, y) ((x).a[0]|=(y).a[0],\
(x).a[1]|=(y).a[1],\
(x).a[2]|=(y).a[2],\
(x).a[3]|=(y).a[3])
#define w_256_or(r, x, y) ((r).a[0]=(x).a[0]|(y).a[0],\
(r).a[1] = (x).a[1]|(y).a[1],\
(r).a[2] = (x).a[2]|(y).a[2],\
(r).a[3] = (x).a[3]|(y).a[3])
#define w_256_and(r, x, y) ((r).a[0] = (x).a[0]&(y).a[0], \
(r).a[1] = (x).a[1]&(y).a[1], \
(r).a[2] = (x).a[2]&(y).a[2], \
(r).a[3] = (x).a[3]&(y).a[3])
#define w_256_self_xor(x, y) ((x).a[0]^=(y).a[0], \
(x).a[1]^=(y).a[1], \
(x).a[2]^=(y).a[2],\
(x).a[3]^=(y).a[3])
#define w_256_self_lsft_1(x) ((x).a[3] = ((x).a[3]<<1)|((x).a[2]>>bitz), \
(x).a[2] = ((x).a[2]<<1)|((x).a[1]>>bitz), \
(x).a[1] = ((x).a[1]<<1)|((x).a[0]>>bitz), \
(x).a[0] <<= 1)
#define w_256_self_rsft_1(x) ((x).a[0] = ((x).a[0]>>1)|((x).a[1]<<bitz), \
(x).a[1] = ((x).a[1]>>1)|((x).a[2]<<bitz), \
(x).a[2] = ((x).a[2]>>1)|((x).a[3]<<bitz), \
(x).a[3] >>= 1)
#define w_256_rsft_1(x, y) ((x).a[0] = ((y).a[0]>>1)|((y).a[1]<<bitz), \
(x).a[1] = ((y).a[1]>>1)|((y).a[2]<<bitz), \
(x).a[2] = ((y).a[2]>>1)|((y).a[3]<<bitz), \
(x).a[3] = (y).a[3]>>1)
#define w_256_self_add(x, y, c) ((x).a[0]+=(y).a[0], c=((x).a[0]<(y).a[0]),\
(x).a[1]+=c, c=((x).a[1]<c), (x).a[1]+=(y).a[1], c|=((x).a[1]<(y).a[1]),\
(x).a[2]+=c, c=((x).a[2]<c), (x).a[2]+=(y).a[2], c|=((x).a[2]<(y).a[2]),\
(x).a[3]+=c, (x).a[3]+=(y).a[3])
#define w_256_set_bit_lsub(x, l) do { \
(x).a[0] = (x).a[1] = (x).a[2] = (x).a[3] = 0; \
if((l)>>bitw) memset((x).a, -1, sizeof(*((x).a))*((l)>>bitw));\
if((l)&bitz) (x).a[(l)>>bitw] = (((w_sig)1)<<((l)&bitz))-1; \
} while (0)
#define w_256_copy(x, y) ((x).a[0]=(y).a[0], (x).a[1]=(y).a[1], (x).a[2]=(y).a[2], (x).a[3]=(y).a[3])
#define cmp_Word(des, src, dd, ws) {\
for ((dd)=0;(dd)<64;dd+=bitwbit){\
if((((des)>>dd)&((w_sig)-1))!=(src).a[dd>>bitw]) break;\
}\
if((dd)<64) {\
print_bit((des), (ws), "des");\
print_bits((src).a, (ws), "src");\
exit(0);\
}\
}
#define dump_Word(des, src, dd, ws) {\
for ((dd)=bitwbit,(des)=((Word)(src).a[0]);(dd)<64;dd+=bitwbit) {\
(des) |= ((Word)(src).a[dd>>bitw])<<dd;\
}\
cmp_Word((des), (src), (dd), (ws));\
}
#define prt_address(Peq, VP, VN, X, D0, HN, HP) {\
fprintf(stderr, "Peq[0].a::%p, Peq[1].a::%p, Peq[2].a::%p, Peq[3].a::%p, Peq[4].a::%p, VP.a::%p, VN.a::%p, X.a::%p, D0.a::%p, HN.a::%p, HP.a::%p\n",\
Peq[0].a, Peq[1].a, Peq[2].a, Peq[3].a, Peq[4].a, VP.a, VN.a, X.a, D0.a, HN.a, HP.a);\
}
#define prt_vector(Peq, VP, VN, X, D0, HN, HP, ws) {\
print_bit((Peq)[0], (ws), "Peq[0]");\
print_bit((Peq)[1], (ws), "Peq[1]");\