forked from chhylp123/hifiasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Correct.cpp
21459 lines (18963 loc) · 855 KB
/
Correct.cpp
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
#define __STDC_LIMIT_MACROS
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <pthread.h>
#include <assert.h>
#include <float.h>
#include "Correct.h"
#include "Levenshtein_distance.h"
#include "Assembly.h"
#include "CommandLines.h"
// #include "ksw2.h"
#include "ksort.h"
#include "kalloc.h"
#include "htab.h"
#include "Overlaps.h"
#include "inter.h"
#define A_L 16
#define ext_w 6
#define r_simi_w 0.05
#define rphase_thres 4
#define generic_key(x) (x)
KRADIX_SORT_INIT(b32, uint32_t, generic_key, 4)
KRADIX_SORT_INIT(bc64, uint64_t, generic_key, 8)
#define haplotype_evdience_key(x) ((x).site)
KRADIX_SORT_INIT(haplotype_evdience_srt, haplotype_evdience, haplotype_evdience_key, member_size(haplotype_evdience, site))
#define haplotype_evdience_id_key(x) ((x).overlapID)
KRADIX_SORT_INIT(haplotype_evdience_id_srt, haplotype_evdience, haplotype_evdience_id_key, member_size(haplotype_evdience, overlapID))
#define overlap_region_dp_key(x) ((x).x_pos_e)
KRADIX_SORT_INIT(overlap_region_dp_srt, overlap_region, overlap_region_dp_key, member_size(overlap_region, x_pos_e))
#define window_list_xs_key(x) ((x).x_start)
KRADIX_SORT_INIT(window_list_xs_srt, window_list, window_list_xs_key, member_size(window_list, x_start))
#define uov_qs_key(p) ((p).qs)
KRADIX_SORT_INIT(uov_srt_qs, ul_ov_t, uov_qs_key, member_size(ul_ov_t, qs))
#define k_mer_hit_self_key(p) ((p).self_offset)
KRADIX_SORT_INIT(k_mer_hit_self, k_mer_hit, k_mer_hit_self_key, member_size(k_mer_hit, self_offset))
#define k_mer_hit_off_key(p) ((p).offset)
KRADIX_SORT_INIT(k_mer_hit_off, k_mer_hit, k_mer_hit_off_key, member_size(k_mer_hit, offset))
#define ul_ov_srt_qs1_key(p) ((p).qs)
KRADIX_SORT_INIT(ul_ov_srt_qs1, ul_ov_t, ul_ov_srt_qs1_key, member_size(ul_ov_t, qs))
#define ul_ov_srt_tn1_key(p) ((p).tn)
KRADIX_SORT_INIT(ul_ov_srt_tn1, ul_ov_t, ul_ov_srt_tn1_key, member_size(ul_ov_t, tn))
#define ul_ov_srt_qn1_key(p) ((p).qn)
KRADIX_SORT_INIT(ul_ov_srt_qn1, ul_ov_t, ul_ov_srt_qn1_key, member_size(ul_ov_t, qn))
#define ul_ov_srt_qe1_key(p) ((p).qe)
KRADIX_SORT_INIT(ul_ov_srt_qe1, ul_ov_t, ul_ov_srt_qe1_key, member_size(ul_ov_t, qe))
#define MAX_SEC_ERR (0x3fffffffU)
int ha_ov_type(const overlap_region *r, uint32_t len);
void set_lchain_dp_op(uint32_t is_accurate, uint32_t mz_k, int64_t *max_skip, int64_t *max_iter, int64_t *max_dis, double *chn_pen_gap, double *chn_pen_skip, int64_t *quick_check);
void clear_Round2_alignment(Round2_alignment* h)
{
clear_Correct_dumy_pure(&(h->dumy));
clear_Cigar_record(&(h->cigar));
clear_Cigar_record(&(h->tmp_cigar));
h->obtained_cigar_length = 0;
}
void init_Round2_alignment(Round2_alignment* h)
{
init_Correct_dumy(&(h->dumy));
init_Cigar_record(&(h->cigar));
init_Cigar_record(&(h->tmp_cigar));
h->obtained_cigar_length = 0;
}
void init_Round2_alignment_buf(Round2_alignment* h, void *km)
{
init_Correct_dumy_buf(&(h->dumy), km);
init_Cigar_record_buf(&(h->cigar), km);
init_Cigar_record_buf(&(h->tmp_cigar), km);
h->obtained_cigar_length = 0;
}
void destory_Round2_alignment(Round2_alignment* h)
{
destory_Correct_dumy(&(h->dumy));
destory_Cigar_record(&(h->cigar));
destory_Cigar_record(&(h->tmp_cigar));
}
inline int get_interval(long long window_start, long long window_end, overlap_region_alloc* overlap_list, Correct_dumy* dumy, long long blockLen)
{
uint64_t i, fud = 0;
long long Len;
if(window_start == 0) dumy->start_i = 0;
for (i = dumy->start_i; i < overlap_list->length; i++)
{
///this interval is smaller than all overlaps
///in this case, the next interval should start from 0
if (window_end < (long long)overlap_list->list[i].x_pos_s)
{
dumy->start_i = 0;
dumy->length = 0;
dumy->lengthNT = 0;
return 0;
}
else ///if window_end >= overlap_list->list[i].x_pos_s,this overlap might be overlapped with current interval
{
dumy->start_i = i;
break;
}
}
///this interval is larger than all overlaps, so we don't need to scan next overlap
if (i >= overlap_list->length)
{
dumy->start_i = overlap_list->length;
dumy->length = 0;
dumy->lengthNT = 0;
return -2;
}
dumy->length = 0;
dumy->lengthNT = 0;
fud = 0;
for (; i < overlap_list->length; i++)
{
if((Len = OVERLAP(window_start, window_end, (long long)overlap_list->list[i].x_pos_s, (long long)overlap_list->list[i].x_pos_e)) > 0)
{
///sometimes the length of window > WINDOW, but overlap length == WINDOW
// if (Len == WINDOW && window_end - window_start + 1 == WINDOW)
if (Len == blockLen && window_end - window_start + 1 == blockLen)
{
dumy->overlapID[dumy->length] = i;
dumy->length++;
}
else
{
dumy->lengthNT++;
dumy->overlapID[dumy->size - dumy->lengthNT] = i;
}
if(fud == 0) fud = 1, dumy->start_i = i;
}
if((long long)overlap_list->list[i].x_pos_s > window_end)
{
break;
}
}
if ( dumy->length + dumy->lengthNT == 0)
{
return 0;
}
else
{
return 1;
}
}
inline int get_available_interval(long long window_start, long long window_end, overlap_region_alloc* overlap_list, Correct_dumy* dumy)
{
uint64_t i, fud = 0;
long long Len;
if(window_start == 0) dumy->start_i = 0;
for (i = dumy->start_i; i < overlap_list->length; i++)
{
///this interval is smaller than all overlaps
///in this case, the next interval should start from 0
if (window_end < (long long)overlap_list->list[i].x_pos_s)
{
dumy->start_i = 0;
dumy->length = 0;
dumy->lengthNT = 0;
return 0;
}
else ///if window_end >= overlap_list->list[i].x_pos_s,this overlap might be overlapped with current interval
{
dumy->start_i = i;
break;
}
}
///this interval is larger than all overlaps, so we don't need to scan next overlap
if (i >= overlap_list->length)
{
dumy->start_i = overlap_list->length;
dumy->length = 0;
dumy->lengthNT = 0;
return -2;
}
dumy->length = 0;
dumy->lengthNT = 0;
fud = 0;
long long fake_length = 0;
for (; i < overlap_list->length; i++)
{
///check if the interval is overlapped with current overlap
if((Len = OVERLAP(window_start, window_end, (long long)overlap_list->list[i].x_pos_s, (long long)overlap_list->list[i].x_pos_e)) > 0)
{
///number of overlaps
fake_length++;
///check if this overlap is available
if (overlap_list->list[i].is_match == 1)
{
dumy->overlapID[dumy->length] = i;
dumy->length++;
}
if(fud == 0) fud = 1, dumy->start_i = i;
}
if((long long)overlap_list->list[i].x_pos_s > window_end)
{
break;
}
}
///fake_length is the number of overlaps, instead of the number of available overlaps
if (fake_length == 0)
{
return 0;
}
else
{
return 1;
}
}
///Len = OVERLAP(window_start, window_end, overlap_list->list[i].x_pos_s, overlap_list->list[i].x_pos_e))
void print_string(char* s, int l)
{
int i;
for (i = 0; i < l; i++)
{
fprintf(stderr, "%c", s[i]);
}
fprintf(stderr, "\n");
}
void fill_subregion(char* r, long long start_pos, long long length, uint8_t strand, All_reads* R_INF, long long ID,
int extra_begin, int extra_end)
{
recover_UC_Read_sub_region(r+extra_begin, start_pos, length, strand, R_INF, ID);
memset(r, 'N', extra_begin);
memset(r+extra_begin+length, 'N', extra_end);
}
void fill_subregion_ul(char* r, long long start_pos, long long length, uint8_t strand, const ul_idx_t *uref, long long ID,
int extra_begin, int extra_end)
{
retrieve_u_seq(NULL, r+extra_begin, &(uref->ug->u.a[ID]), strand, start_pos, length, NULL);
memset(r, 'N', extra_begin);
memset(r+extra_begin+length, 'N', extra_end);
}
int determine_overlap_region(int threshold, long long y_start, long long y_ID, long long Window_Len, /**All_reads* R_INF**/long long y_len,
int* r_extra_begin, int* r_extra_end, long long* r_y_start, long long* r_y_length)
{
int extra_begin;
int extra_end;
long long currentIDLen;
long long o_len;
///the length of y
// currentIDLen = Get_READ_LENGTH((*R_INF), y_ID);
currentIDLen = y_len;
///since Window_Len == x_len + (threshold << 1)
if(y_start < 0 || currentIDLen <= y_start ||
currentIDLen - y_start + 2 * threshold + THRESHOLD_MAX_SIZE < Window_Len)
{
return 0;
}
extra_begin = extra_end = 0;
///y maybe less than 0
y_start = y_start - threshold;
o_len = MIN(Window_Len, currentIDLen - y_start);
extra_end = Window_Len - o_len;
if (y_start < 0)
{
extra_begin = -y_start;
y_start = 0;
o_len = o_len - extra_begin;
}
(*r_extra_begin) = extra_begin;
(*r_extra_end) = extra_end;
(*r_y_start) = y_start;
(*r_y_length) = o_len;
return 1;
}
int verify_single_window(long long x_start, long long x_end,
long long overlap_x_s, long long overlap_y_s, int x_id,
int y_id, int x_strand, char* x_buffer, char* y_buffer,
All_reads* R_INF)
{
char* x_string = NULL;
char* y_string = NULL;
int extra_begin, extra_end, x_len, threshold;
long long y_start;
long long Window_Len, o_len;
unsigned int error;
x_len = x_end - x_start + 1;
threshold = x_len * asm_opt.max_ov_diff_ec;
/****************************may have bugs********************************/
threshold = Adjust_Threshold(threshold, x_len);
/****************************may have bugs********************************/
y_start = (x_start - overlap_x_s) + overlap_y_s;
Window_Len = x_len + (threshold << 1);
if(!determine_overlap_region(threshold, y_start, y_id, Window_Len, Get_READ_LENGTH((*R_INF), y_id),
&extra_begin, &extra_end, &y_start, &o_len))
{
return 0;
}
///use unusual direction here
/**
fill_subregion(y_buffer, y_start, o_len, y_strand, R_INF, y_id, extra_begin, extra_end);
///x is always forward strand
recover_UC_Read_sub_region(x_buffer, x_start, x_len, 0, R_INF, x_id);
**/
///use unusual direction here, here y is always forward strand
fill_subregion(y_buffer, y_start, o_len, 0, R_INF, y_id, extra_begin, extra_end);
recover_UC_Read_sub_region(x_buffer, x_start, x_len, x_strand, R_INF, x_id);
x_string = x_buffer;
y_string = y_buffer;
Reserve_Banded_BPM(y_string, Window_Len, x_string, x_len, threshold, &error);
if (error!=(unsigned int)-1)
{
return 1;
}
return 0;
}
void verify_window(long long window_start, long long window_end, overlap_region_alloc* overlap_list,Correct_dumy* dumy, All_reads* R_INF,
char* r_string)
{
long long i;
long long currentID;
long long x_start, y_start, o_len;
long long Window_Len = WINDOW + (THRESHOLD << 1);
char* x_string = NULL;
char* y_string = NULL;
long long x_end, x_len;
int end_site;
unsigned int error;
int groupLen = 0;
int return_sites[GROUP_SIZE];
unsigned int return_sites_error[GROUP_SIZE];
uint64_t overlapID[GROUP_SIZE];
uint64_t y_startGroup[GROUP_SIZE];
int y_extra_begin[GROUP_SIZE];
int y_extra_end[GROUP_SIZE];
int error_threshold[GROUP_SIZE];
int extra_begin;
int extra_end;
///here are overlaps fully covered by WINDOW
for (i = 0; i < (long long)dumy->length; i++)
{
extra_begin = extra_end = 0;
///if the window has been fully covered, the interval at x is [window_start, window_end]
x_len = WINDOW;
currentID = dumy->overlapID[i];
x_start = window_start;
///offset of y
y_start = (x_start - overlap_list->list[currentID].x_pos_s) + overlap_list->list[currentID].y_pos_s;
/****************************may have bugs********************************/
y_start += y_start_offset(x_start, &(overlap_list->list[currentID].f_cigar));
/****************************may have bugs********************************/
if(!determine_overlap_region(THRESHOLD, y_start, overlap_list->list[currentID].y_id, Window_Len, Get_READ_LENGTH((*R_INF), overlap_list->list[currentID].y_id),
&extra_begin, &extra_end, &y_start, &o_len)) {
continue;
}
fill_subregion(dumy->overlap_region_group[groupLen], y_start, o_len, overlap_list->list[currentID].y_pos_strand,
R_INF, overlap_list->list[currentID].y_id, extra_begin, extra_end);
y_extra_begin[groupLen] = extra_begin;
y_extra_end[groupLen] = extra_end;
overlapID[groupLen] = currentID;
y_startGroup[groupLen] = y_start;
error_threshold[groupLen] = THRESHOLD;
x_string = r_string + x_start;
groupLen++;
if (groupLen == GROUP_SIZE)
{
#if defined(__x86_64__) || defined(__x86__)
Reserve_Banded_BPM_4_SSE_only(dumy->overlap_region_group[0], dumy->overlap_region_group[1],
dumy->overlap_region_group[2], dumy->overlap_region_group[3], Window_Len, x_string, WINDOW,
return_sites, return_sites_error, THRESHOLD, dumy->Peq_SSE);
#else
Reserve_Banded_BPM_4(dumy->overlap_region_group[0], dumy->overlap_region_group[1],
dumy->overlap_region_group[2], dumy->overlap_region_group[3], Window_Len, x_string, WINDOW,
return_sites, return_sites_error, THRESHOLD);
#endif
groupLen = 0;
if (return_sites_error[0]!=(unsigned int)-1) {
overlap_list->list[overlapID[0]].align_length += x_len;
append_window_list(&overlap_list->list[overlapID[0]], window_start, window_end,
y_startGroup[0], y_startGroup[0] + return_sites[0], (int)return_sites_error[0],
y_extra_begin[0], y_extra_end[0], error_threshold[0], WINDOW, NULL);
}
if (return_sites_error[1]!=(unsigned int)-1) {
overlap_list->list[overlapID[1]].align_length += x_len;
append_window_list(&overlap_list->list[overlapID[1]], window_start, window_end,
y_startGroup[1], y_startGroup[1] + return_sites[1], (int)return_sites_error[1],
y_extra_begin[1], y_extra_end[1], error_threshold[1], WINDOW, NULL);
}
if (return_sites_error[2]!=(unsigned int)-1) {
overlap_list->list[overlapID[2]].align_length += x_len;
append_window_list(&overlap_list->list[overlapID[2]], window_start, window_end,
y_startGroup[2], y_startGroup[2] + return_sites[2], (int)return_sites_error[2],
y_extra_begin[2], y_extra_end[2], error_threshold[2], WINDOW, NULL);
}
if (return_sites_error[3]!=(unsigned int)-1) {
overlap_list->list[overlapID[3]].align_length += x_len;
append_window_list(&overlap_list->list[overlapID[3]], window_start, window_end,
y_startGroup[3], y_startGroup[3] + return_sites[3], (int)return_sites_error[3],
y_extra_begin[3], y_extra_end[3], error_threshold[3], WINDOW, NULL);
}
}
}
if (groupLen == 1)
{
end_site = Reserve_Banded_BPM(dumy->overlap_region_group[0], Window_Len, x_string, WINDOW, THRESHOLD, &error);
if (error!=(unsigned int)-1) {
overlap_list->list[overlapID[0]].align_length += x_len;
append_window_list(&overlap_list->list[overlapID[0]], window_start, window_end,
y_startGroup[0], y_startGroup[0] + end_site, (int)error,
y_extra_begin[0], y_extra_end[0], error_threshold[0], WINDOW, NULL);
}
}
else if (groupLen > 1)
{
#if defined(__x86_64__) || defined(__x86__)
Reserve_Banded_BPM_4_SSE_only(dumy->overlap_region_group[0], dumy->overlap_region_group[1],
dumy->overlap_region_group[2], dumy->overlap_region_group[3], Window_Len, x_string, WINDOW,
return_sites, return_sites_error, THRESHOLD, dumy->Peq_SSE);
#else
Reserve_Banded_BPM_4(dumy->overlap_region_group[0], dumy->overlap_region_group[1],
dumy->overlap_region_group[2], dumy->overlap_region_group[3], Window_Len, x_string, WINDOW,
return_sites, return_sites_error, THRESHOLD);
#endif
for (i = 0; i < groupLen; i++)
{
if (return_sites_error[i]!=(unsigned int)-1) {
overlap_list->list[overlapID[i]].align_length += x_len;
append_window_list(&overlap_list->list[overlapID[i]], window_start, window_end,
y_startGroup[i], y_startGroup[i] + return_sites[i], (int)return_sites_error[i],
y_extra_begin[i], y_extra_end[i], error_threshold[i], WINDOW, NULL);
}
}
groupLen = 0;
}
long long reverse_i = dumy->size - 1;
int threshold;
///here are overlaps partially covered by WINDOW
for (i = 0; i < (long long)dumy->lengthNT; i++)
{
extra_begin = extra_end = 0;
currentID = dumy->overlapID[reverse_i--];
x_start = MAX(window_start, (long long)overlap_list->list[currentID].x_pos_s);
x_end = MIN(window_end, (long long)overlap_list->list[currentID].x_pos_e);
///overlap length between [window_start, window_end]
x_len = x_end - x_start + 1;
threshold = x_len * asm_opt.max_ov_diff_ec;
/****************************may have bugs********************************/
threshold = Adjust_Threshold(threshold, x_len);
if(threshold > THRESHOLD_MAX_SIZE) threshold = THRESHOLD_MAX_SIZE;
/****************************may have bugs********************************/
///offset of y
y_start = (x_start - overlap_list->list[currentID].x_pos_s) + overlap_list->list[currentID].y_pos_s;
/****************************may have bugs********************************/
y_start += y_start_offset(x_start, &(overlap_list->list[currentID].f_cigar));
/****************************may have bugs********************************/
Window_Len = x_len + (threshold << 1);
if(!determine_overlap_region(threshold, y_start, overlap_list->list[currentID].y_id, Window_Len, Get_READ_LENGTH((*R_INF), overlap_list->list[currentID].y_id),
&extra_begin, &extra_end, &y_start, &o_len)) {
continue;
}
fill_subregion(dumy->overlap_region, y_start, o_len, overlap_list->list[currentID].y_pos_strand,
R_INF, overlap_list->list[currentID].y_id, extra_begin, extra_end);
x_string = r_string + x_start;
y_string = dumy->overlap_region;
end_site = Reserve_Banded_BPM(y_string, Window_Len, x_string, x_len, threshold, &error);
if (error!=(unsigned int)-1) {
overlap_list->list[currentID].align_length += x_len;
append_window_list(&overlap_list->list[currentID], x_start, x_end, y_start, y_start + end_site, (int)error,
extra_begin, extra_end, threshold, WINDOW, NULL);
}
}
}
void verify_ul_window(long long window_start, long long window_end, overlap_region_alloc* overlap_list,Correct_dumy* dumy, const ul_idx_t *uref,
char* r_string, double max_ov_diff_ec, long long blockLen, long long max_error, void *km)
{
long long i;
long long currentID;
long long x_start, y_start, o_len;
long long Window_Len = blockLen + (max_error << 1);
char* x_string = NULL;
char* y_string = NULL;
long long x_end, x_len;
int end_site;
unsigned int error;
int groupLen = 0;
int return_sites[GROUP_SIZE];
unsigned int return_sites_error[GROUP_SIZE];
uint64_t overlapID[GROUP_SIZE];
uint64_t y_startGroup[GROUP_SIZE];
int y_extra_begin[GROUP_SIZE];
int y_extra_end[GROUP_SIZE];
int error_threshold[GROUP_SIZE];
int extra_begin;
int extra_end;
///here are overlaps fully covered by blockLen
for (i = 0; i < (long long)dumy->length; i++)
{
extra_begin = extra_end = 0;
///if the window has been fully covered, the interval at x is [window_start, window_end]
x_len = blockLen;
currentID = dumy->overlapID[i];
x_start = window_start;
///offset of y
y_start = (x_start - overlap_list->list[currentID].x_pos_s) + overlap_list->list[currentID].y_pos_s;
/****************************may have bugs********************************/
y_start += y_start_offset(x_start, &(overlap_list->list[currentID].f_cigar));
/****************************may have bugs********************************/
if(!determine_overlap_region(max_error, y_start, overlap_list->list[currentID].y_id, Window_Len, uref->ug->u.a[overlap_list->list[currentID].y_id].len,
&extra_begin, &extra_end, &y_start, &o_len)) {
continue;
}
// if(overlap_list->list[currentID].y_id == 4) {
// fprintf(stderr, "[M::%s] q_s::%lld, t_s::%lld, t_pri_l::%lld, aux_beg::%d, aux_end::%d, aln_l::%lld\n", __func__,
// x_start, y_start, o_len, extra_begin, extra_end, Window_Len);
// }
fill_subregion_ul(dumy->overlap_region_group[groupLen], y_start, o_len, overlap_list->list[currentID].y_pos_strand,
uref, overlap_list->list[currentID].y_id, extra_begin, extra_end);
y_extra_begin[groupLen] = extra_begin;
y_extra_end[groupLen] = extra_end;
overlapID[groupLen] = currentID;
y_startGroup[groupLen] = y_start;
error_threshold[groupLen] = max_error;
x_string = r_string + x_start;
groupLen++;
if (groupLen == GROUP_SIZE)
{
// Reserve_Banded_BPM_4_SSE_only(dumy->overlap_region_group[0], dumy->overlap_region_group[1],
// dumy->overlap_region_group[2], dumy->overlap_region_group[3], Window_Len, x_string, blockLen,
// return_sites, return_sites_error, max_error, dumy->Peq_SSE);
return_sites[0] =
Reserve_Banded_BPM(dumy->overlap_region_group[0], Window_Len, x_string, blockLen, max_error, &return_sites_error[0]);
return_sites[1] =
Reserve_Banded_BPM(dumy->overlap_region_group[1], Window_Len, x_string, blockLen, max_error, &return_sites_error[1]);
return_sites[2] =
Reserve_Banded_BPM(dumy->overlap_region_group[2], Window_Len, x_string, blockLen, max_error, &return_sites_error[2]);
return_sites[3] =
Reserve_Banded_BPM(dumy->overlap_region_group[3], Window_Len, x_string, blockLen, max_error, &return_sites_error[3]);
groupLen = 0;
if (return_sites_error[0]!=(unsigned int)-1) {
overlap_list->list[overlapID[0]].align_length += x_len;
append_window_list(&overlap_list->list[overlapID[0]], window_start, window_end,
y_startGroup[0], y_startGroup[0] + return_sites[0], (int)return_sites_error[0],
y_extra_begin[0], y_extra_end[0], error_threshold[0], blockLen, km);
}
if (return_sites_error[1]!=(unsigned int)-1) {
overlap_list->list[overlapID[1]].align_length += x_len;
append_window_list(&overlap_list->list[overlapID[1]], window_start, window_end,
y_startGroup[1], y_startGroup[1] + return_sites[1], (int)return_sites_error[1],
y_extra_begin[1], y_extra_end[1], error_threshold[1], blockLen, km);
}
if (return_sites_error[2]!=(unsigned int)-1) {
overlap_list->list[overlapID[2]].align_length += x_len;
append_window_list(&overlap_list->list[overlapID[2]], window_start, window_end,
y_startGroup[2], y_startGroup[2] + return_sites[2], (int)return_sites_error[2],
y_extra_begin[2], y_extra_end[2], error_threshold[2], blockLen, km);
}
if (return_sites_error[3]!=(unsigned int)-1) {
overlap_list->list[overlapID[3]].align_length += x_len;
append_window_list(&overlap_list->list[overlapID[3]], window_start, window_end,
y_startGroup[3], y_startGroup[3] + return_sites[3], (int)return_sites_error[3],
y_extra_begin[3], y_extra_end[3], error_threshold[3], blockLen, km);
}
}
}
if (groupLen == 1)
{
end_site = Reserve_Banded_BPM(dumy->overlap_region_group[0], Window_Len, x_string, blockLen, max_error, &error);
if (error!=(unsigned int)-1) {
overlap_list->list[overlapID[0]].align_length += x_len;
append_window_list(&overlap_list->list[overlapID[0]], window_start, window_end,
y_startGroup[0], y_startGroup[0] + end_site, (int)error,
y_extra_begin[0], y_extra_end[0], error_threshold[0], blockLen, km);
}
}
else if (groupLen > 1)
{
// Reserve_Banded_BPM_4_SSE_only(dumy->overlap_region_group[0], dumy->overlap_region_group[1],
// dumy->overlap_region_group[2], dumy->overlap_region_group[3], Window_Len, x_string, blockLen,
// return_sites, return_sites_error, max_error, dumy->Peq_SSE);
for (i = 0; i < groupLen; i++)
{
return_sites[i] = Reserve_Banded_BPM(dumy->overlap_region_group[i], Window_Len, x_string, blockLen, max_error, &return_sites_error[i]);
if (return_sites_error[i]!=(unsigned int)-1) {
overlap_list->list[overlapID[i]].align_length += x_len;
append_window_list(&overlap_list->list[overlapID[i]], window_start, window_end,
y_startGroup[i], y_startGroup[i] + return_sites[i], (int)return_sites_error[i],
y_extra_begin[i], y_extra_end[i], error_threshold[i], blockLen, km);
}
}
groupLen = 0;
}
long long reverse_i = dumy->size - 1;
int threshold;
///here are overlaps partially covered by blockLen
for (i = 0; i < (long long)dumy->lengthNT; i++)
{
extra_begin = extra_end = 0;
currentID = dumy->overlapID[reverse_i--];
x_start = MAX(window_start, (long long)overlap_list->list[currentID].x_pos_s);
x_end = MIN(window_end, (long long)overlap_list->list[currentID].x_pos_e);
///overlap length between [window_start, window_end]
x_len = x_end - x_start + 1;
threshold = x_len * max_ov_diff_ec;
/****************************may have bugs********************************/
threshold = Adjust_Threshold(threshold, x_len);
if(threshold > THRESHOLD_MAX_SIZE) threshold = THRESHOLD_MAX_SIZE;
/****************************may have bugs********************************/
///offset of y
y_start = (x_start - overlap_list->list[currentID].x_pos_s) + overlap_list->list[currentID].y_pos_s;
/****************************may have bugs********************************/
y_start += y_start_offset(x_start, &(overlap_list->list[currentID].f_cigar));
/****************************may have bugs********************************/
Window_Len = x_len + (threshold << 1);
if(!determine_overlap_region(threshold, y_start, overlap_list->list[currentID].y_id, Window_Len, uref->ug->u.a[overlap_list->list[currentID].y_id].len,
&extra_begin, &extra_end, &y_start, &o_len)) {
continue;
}
// if(overlap_list->list[currentID].y_id == 4) {
// fprintf(stderr, "[M::%s] q_s::%lld, t_s::%lld, t_pri_l::%lld, aux_beg::%d, aux_end::%d, aln_l::%lld\n", __func__,
// x_start, y_start, o_len, extra_begin, extra_end, Window_Len);
// }
fill_subregion_ul(dumy->overlap_region, y_start, o_len, overlap_list->list[currentID].y_pos_strand,
uref, overlap_list->list[currentID].y_id, extra_begin, extra_end);
x_string = r_string + x_start;
y_string = dumy->overlap_region;
end_site = Reserve_Banded_BPM(y_string, Window_Len, x_string, x_len, threshold, &error);
if (error!=(unsigned int)-1) {
overlap_list->list[currentID].align_length += x_len;
append_window_list(&overlap_list->list[currentID], x_start, x_end, y_start, y_start + end_site, (int)error,
extra_begin, extra_end, threshold, blockLen, km);
}
}
}
int32_t init_waln(int64_t err, int64_t s, int64_t l, int64_t w_l, int64_t* aux_beg, int64_t* aux_end, int64_t* r_s, int64_t* r_l)
{
(*aux_beg) = (*aux_end) = (*r_s) = (*r_l) = -1;
///since w_l == x_len + (err << 1)
if((s < 0) || (s >= l) || ((l-s+(2*err)+THRESHOLD_MAX_SIZE) < w_l)) return 0;
(*aux_beg) = (*aux_end) = 0;
///s might be less than 0
(*r_s) = s - err;
(*r_l) = l-(*r_s); if((*r_l) > w_l) (*r_l) = w_l;
(*aux_end) = w_l - (*r_l);
if ((*r_s) < 0) {
(*aux_beg) = -(*r_s); (*r_s) = 0; (*r_l) -= (*aux_beg);
}
return 1;
}
///[s, e)
int64_t get_num_wins(int64_t s, int64_t e, int64_t block_s)
{
int64_t nl = e - ((s/block_s)*block_s), nw;
nw = (nl/block_s); if((nl%block_s)>0) nw++;
return nw;
}
void gen_str_seq(char *dst, int64_t s, int64_t pri_l, uint8_t rev, const ul_idx_t *uref, long long id, int64_t aux_beg, int64_t aux_end)
{
// int64_t l = pri_l + aux_beg + aux_end;
memset(dst, 'N', aux_beg);
retrieve_u_seq(NULL, dst+aux_beg, &(uref->ug->u.a[id]), rev, s, pri_l, NULL);
memset(dst+aux_beg+pri_l, 'N', aux_end);
}
void verify_ul_window_s(overlap_region *z, const ul_idx_t *uref, char* qstr, char *tstr,
double e_rate, int64_t w_l, int64_t e_max, void *km)
{
int64_t q_s, q_e, nw, k, q_l;
int64_t aux_beg, aux_end, t_s, thre, aln_l, t_pri_l, t_end;
char *q_string, *t_string; unsigned int error;
z->w_list.n = 0; nw = get_num_wins(z->x_pos_s, z->x_pos_e+1, w_l);
get_win_se_by_normalize_xs(z, (z->x_pos_s/w_l)*w_l, w_l, &q_s, &q_e);
// q_s = z->x_pos_s; get_win_id_by_s(z, q_s, w_l, &q_e);
for (k = 0; k < nw; k++) {
aux_beg = aux_end = 0; q_l = 1 + q_e - q_s;
thre = q_l*e_rate; thre = Adjust_Threshold(thre, q_l);
if(thre > THRESHOLD_MAX_SIZE) thre = THRESHOLD_MAX_SIZE;
///offset of y
t_s = (q_s - z->x_pos_s) + z->y_pos_s;
t_s += y_start_offset(q_s, &(z->f_cigar));
aln_l = q_l + (thre<<1);
// if(z->y_id == 115) {
// fprintf(stderr, "+[M::] q_s::%ld, t_s::%ld, t_pri_l::%ld, aux_beg::%ld, aux_end::%ld, aln_l::%ld, t_end::%ld, error::%u\n",
// q_s, t_s, t_pri_l, aux_beg, aux_end, aln_l, t_end, error);
// }
if(init_waln(thre, t_s, uref->ug->u.a[z->y_id].len, aln_l, &aux_beg, &aux_end, &t_s, &t_pri_l)) {
gen_str_seq(tstr, t_s, t_pri_l, z->y_pos_strand, uref, z->y_id, aux_beg, aux_end);
q_string = qstr+q_s; t_string = tstr;
t_end = Reserve_Banded_BPM(t_string, aln_l, q_string, q_l, thre, &error);
// if(z->y_id == 115) {
// fprintf(stderr, "-[M::] q_s::%ld, t_s::%ld, t_pri_l::%ld, aux_beg::%ld, aux_end::%ld, aln_l::%ld, t_end::%ld, error::%u, thre::%ld\n",
// q_s, t_s, t_pri_l, aux_beg, aux_end, aln_l, t_end, error, thre);
// }
if (error!=((unsigned int)-1)) {
z->align_length += q_l;
///t_s do not have aux_beg, while t_s + t_end (aka, te) has
append_window_list(z, q_s, q_e, t_s, t_s + t_end, error, aux_beg, aux_end, thre, w_l, km);
}
}
q_s = q_e + 1; q_e = q_s + w_l - 1;
if(q_e >= (int64_t)z->x_pos_e) q_e = z->x_pos_e;
}
// if(q_e != (int64_t)z->x_pos_e) {
// fprintf(stderr, "[M::%s] q_e::%ld, z->x_pos_s::%u, z->x_pos_e::%u, w_l::%ld, nw::%ld\n", __func__,
// q_e, z->x_pos_s, z->x_pos_e, w_l, nw);
// }
assert(q_e == (int64_t)z->x_pos_e);
}
///error_rate should be 30%
long long get_high_error(long long x_start, long long x_end,
long long y_start, long long y_end, long long y_id, long long y_strand, long long pre_threshold,
long long n_steps, float error_rate, All_reads* R_INF, Correct_dumy* dumy,
UC_Read* g_read)
{
long long stepLen = (x_end - x_start + 1) / n_steps;
if((x_end - x_start + 1) % n_steps != 0)
{
stepLen++;
}
long long SubLen, SubWindowLen;
long long SubThreshold = THRESHOLD_MAX_SIZE;
int extra_begin, extra_end;
long long o_len;
long long T_error = 0;
y_start = y_start + pre_threshold;
while (x_start <= x_end)
{
SubLen = x_end - x_start + 1;
if(SubLen > stepLen)
{
SubLen = stepLen;
}
SubThreshold = SubLen * error_rate;
if(SubThreshold > THRESHOLD_MAX_SIZE)
{
SubThreshold = THRESHOLD_MAX_SIZE;
}
SubThreshold = Adjust_Threshold(SubThreshold, SubLen);
SubWindowLen = SubLen + (SubThreshold << 1);
if(determine_overlap_region(SubThreshold, y_start, y_id, SubWindowLen, Get_READ_LENGTH((*R_INF), y_id),
&extra_begin, &extra_end, &y_start, &o_len) == 0)
{
T_error = T_error + (x_end - x_start + 1) * error_rate * 1.5;
break;
}
fill_subregion(dumy->overlap_region, y_start, o_len, y_strand, R_INF, y_id,
extra_begin, extra_end);
char* x_string = g_read->seq + x_start;
char* y_string = dumy->overlap_region;
int end_site;
unsigned int error;
end_site = Reserve_Banded_BPM(y_string, SubWindowLen, x_string, SubLen, SubThreshold, &error);
///if error = -1, unmatched
if (error!=(unsigned int)-1)
{
T_error = T_error + error;
y_start = y_start + end_site - extra_begin + 1;
}
else
{
T_error = T_error + SubLen * error_rate * 1.5;
y_start = y_start + SubThreshold - extra_begin + SubLen;
}
x_start = x_start + SubLen;
}
return T_error;
}
inline int double_error_threshold(int pre_threshold, int x_len)
{
pre_threshold = Adjust_Threshold(pre_threshold, x_len);
int threshold = pre_threshold * 2;
///may have some bugs
if(x_len >= 300 && threshold < THRESHOLD_MAX_SIZE)
{
threshold = THRESHOLD_MAX_SIZE;
}
if(threshold > THRESHOLD_MAX_SIZE)
{
threshold = THRESHOLD_MAX_SIZE;
}
return threshold;
}
inline int double_ul_error_threshold(int pre_threshold, int x_len)
{
pre_threshold = Adjust_Threshold(pre_threshold, x_len);
int threshold = THRESHOLD_UL_MAX * x_len;
if(threshold < pre_threshold) threshold = pre_threshold;
if(threshold > THRESHOLD_MAX_SIZE) threshold = THRESHOLD_MAX_SIZE;
return threshold;
}
inline int verify_sub_window(All_reads* R_INF, Correct_dumy* dumy, UC_Read* g_read,
long long x_beg, long long xLen, long long y_beg, long long yLen, uint64_t y_id,
uint64_t y_pos_strand, int threshold, int alignment_strand,
unsigned int* get_error, int* get_y_end, int* get_x_end, int* get_aligned_xLen)
{
(*get_aligned_xLen) = 0;
(*get_y_end) = -1;
(*get_x_end) = -1;
(*get_error) = (unsigned int)-1;
int extra_begin, extra_end, r_x_end, r_y_end, aligned_xLen;
long long o_len;
unsigned int r_error;
if(!determine_overlap_region(threshold, y_beg, y_id, yLen,
Get_READ_LENGTH((*R_INF), y_id), &extra_begin, &extra_end, &y_beg, &o_len))
{
return 0;
}
fill_subregion(dumy->overlap_region, y_beg, o_len, y_pos_strand, R_INF,
y_id, extra_begin, extra_end);
char* x_string = g_read->seq + x_beg;
char* y_string = dumy->overlap_region;
aligned_xLen = 0;
alignment_extension(y_string, yLen, x_string, xLen, threshold,
alignment_strand, &r_error, &r_y_end, &r_x_end, &aligned_xLen);
(*get_error) = r_error;
(*get_y_end) = r_y_end;
(*get_x_end) = r_x_end;
(*get_aligned_xLen) = aligned_xLen;
if(aligned_xLen == 0)
{
return 0;
}
else
{
return 1;
}
}
inline int verify_ul_sub_window(const ul_idx_t *uref, Correct_dumy* dumy, UC_Read* g_read,
long long x_beg, long long xLen, long long y_beg, long long yLen, uint64_t y_id,
uint64_t y_pos_strand, int threshold, int alignment_strand,
unsigned int* get_error, int* get_y_end, int* get_x_end, int* get_aligned_xLen)
{
(*get_aligned_xLen) = 0;