forked from chhylp123/hifiasm
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Correct.cpp
8493 lines (7090 loc) · 284 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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <pthread.h>
#include "Correct.h"
#include "Levenshtein_distance.h"
#include "Assembly.h"
#include "CommandLines.h"
#include "ksw2.h"
#include "ksort.h"
#define generic_key(x) (x)
KRADIX_SORT_INIT(b32, uint32_t, generic_key, 4)
int ha_ov_type(const overlap_region *r, uint32_t len);
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 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)
{
uint64_t i;
long long Len;
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;
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)
{
dumy->overlapID[dumy->length] = i;
dumy->length++;
}
else
{
dumy->lengthNT++;
dumy->overlapID[dumy->size - dumy->lengthNT] = 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;
long long Len;
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;
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((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);
}
int determine_overlap_region(int threshold, long long y_start, long long y_ID, long long Window_Len, All_reads* R_INF,
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);
///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, R_INF,
&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, R_INF,
&extra_begin, &extra_end, &y_start, &o_len))
{
append_window_list(&overlap_list->list[currentID], window_start, window_end,
-1, -1, -1, -1, -1, -1);
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)
{
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);
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]);
}
else
{
append_window_list(&overlap_list->list[overlapID[0]], window_start, window_end, y_startGroup[0], -1, -1,
y_extra_begin[0], y_extra_end[0], error_threshold[0]);
}
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]);
}
else
{
append_window_list(&overlap_list->list[overlapID[1]], window_start, window_end, y_startGroup[1], -1, -1,
y_extra_begin[1], y_extra_end[1], error_threshold[1]);
}
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]);
}
else
{
append_window_list(&overlap_list->list[overlapID[2]], window_start, window_end, y_startGroup[2], -1, -1,
y_extra_begin[2], y_extra_end[2], error_threshold[2]);
}
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]);
}
else
{
append_window_list(&overlap_list->list[overlapID[3]], window_start, window_end, y_startGroup[3], -1, -1,
y_extra_begin[3], y_extra_end[3], error_threshold[3]);
}
}
}
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]);
}
else
{
append_window_list(&overlap_list->list[overlapID[0]], window_start, window_end, y_startGroup[0], -1, -1,
y_extra_begin[0], y_extra_end[0], error_threshold[0]);
}
}
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, WINDOW,
return_sites, return_sites_error, THRESHOLD, dumy->Peq_SSE);
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]);
}
else
{
append_window_list(&overlap_list->list[overlapID[i]], window_start, window_end, y_startGroup[i], -1, -1,
y_extra_begin[i], y_extra_end[i], error_threshold[i]);
}
}
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);
/****************************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, R_INF,
&extra_begin, &extra_end, &y_start, &o_len))
{
append_window_list(&overlap_list->list[currentID], x_start, x_end,
-1, -1, -1, -1, -1, -1);
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);
}
else
{
append_window_list(&overlap_list->list[currentID], x_start, x_end, y_start, -1, -1,
extra_begin, extra_end, threshold);
}
}
}
inline double trim_error_rate(overlap_region_alloc* overlap_list, long long ID)
{
long long tLen, tError,i, subWinLen, subWinNum;
tLen = 0;
tError = 0;
subWinNum = overlap_list->list[ID].w_list_length;
if(subWinNum < 5)
{
for (i = 0; i < subWinNum; i++)
{
subWinLen = overlap_list->list[ID].w_list[i].x_end - overlap_list->list[ID].w_list[i].x_start + 1;
tLen += subWinLen;
if(overlap_list->list[ID].w_list[i].y_end != -1)
{
tError += overlap_list->list[ID].w_list[i].error;
}
else
{
///tError += (Adjust_Threshold(subWinLen*asm_opt.max_ov_diff_ec, subWinLen) * 2);
tError += Adjust_Threshold(subWinLen * asm_opt.max_ov_diff_ec, subWinLen) * 3;
}
}
}
else
{
for (i = 1; i < subWinNum - 1; i++)
{
subWinLen = overlap_list->list[ID].w_list[i].x_end - overlap_list->list[ID].w_list[i].x_start + 1;
tLen += subWinLen;
if(overlap_list->list[ID].w_list[i].y_end != -1)
{
tError += overlap_list->list[ID].w_list[i].error;
}
else
{
///tError += (Adjust_Threshold(subWinLen*asm_opt.max_ov_diff_ec, subWinLen) * 2);
tError += Adjust_Threshold(subWinLen * asm_opt.max_ov_diff_ec, subWinLen) * 3;
}
}
}
double error_rate = (double)(tError)/(double)(tLen);
return error_rate;
}
///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, R_INF,
&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 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,
R_INF, &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 double non_trim_error_rate(overlap_region_alloc* overlap_list, long long ID,
All_reads* R_INF, Correct_dumy* dumy, UC_Read* g_read)
{
long long tLen, tError,i, subWinLen, subWinNum;
tLen = 0;
tError = 0;
subWinNum = overlap_list->list[ID].w_list_length;
for (i = 0; i < subWinNum; i++)
{
subWinLen = overlap_list->list[ID].w_list[i].x_end - overlap_list->list[ID].w_list[i].x_start + 1;
tLen += subWinLen;
if(overlap_list->list[ID].w_list[i].y_end != -1)
{
tError += overlap_list->list[ID].w_list[i].error;
}
else
{
int x_len = subWinLen;
int threshold = double_error_threshold(overlap_list->list[ID].w_list[i].error_threshold, x_len);
int Window_Len = x_len + (threshold << 1);
unsigned int r_error_left = 0;
int r_x_end_left, r_y_end_left, aligned_xLen_left;
unsigned int r_error_right = 0;
int r_x_end_right, r_y_end_right, aligned_xLen_right;
long long y_beg_left, y_beg_right;
aligned_xLen_left = aligned_xLen_right = 0;
y_beg_left = y_beg_right = -1;
if(overlap_list->list[ID].w_list[i].y_start == -1)
{
tError += x_len;
continue;
}
///if the previous window is mapped
if(i > 0 && overlap_list->list[ID].w_list[i - 1].y_end != -1)
{
y_beg_left = overlap_list->list[ID].w_list[i - 1].y_end + 1;
}
///if the next window is mapped
if(i < (long long)(overlap_list->list[ID].w_list_length - 1) && overlap_list->list[ID].w_list[i + 1].y_end != -1)
{
y_beg_right = 1 + overlap_list->list[ID].w_list[i + 1].y_start - 1 - x_len;
}
if(y_beg_left == -1 && y_beg_right == -1)
{
y_beg_left = overlap_list->list[ID].w_list[i].y_start;
if(overlap_list->list[ID].w_list[i].extra_begin >= 0)
{
y_beg_left = y_beg_left + overlap_list->list[ID].w_list[i].error_threshold -
overlap_list->list[ID].w_list[i].extra_begin;
}
y_beg_right = y_beg_left;
}
if(y_beg_left == -1 && y_beg_right != -1)
{
y_beg_left = y_beg_right;
}
if(y_beg_right == -1 && y_beg_left != -1)
{
y_beg_right = y_beg_left;
}
if(y_beg_left != -1)
{
verify_sub_window(R_INF, dumy, g_read, overlap_list->list[ID].w_list[i].x_start,
x_len, y_beg_left, Window_Len, overlap_list->list[ID].y_id, overlap_list->list[ID].y_pos_strand,
threshold, 0, &r_error_left, &r_y_end_left, &r_x_end_left, &aligned_xLen_left);
}
if(y_beg_right != -1)
{
verify_sub_window(R_INF, dumy, g_read, overlap_list->list[ID].w_list[i].x_start,
x_len, y_beg_right, Window_Len, overlap_list->list[ID].y_id, overlap_list->list[ID].y_pos_strand,
threshold, 1, &r_error_right, &r_y_end_right, &r_x_end_right, &aligned_xLen_right);
}
///aligned in both direction
if(aligned_xLen_left != 0 && aligned_xLen_right != 0)
{
if(aligned_xLen_left + aligned_xLen_right <= x_len)
{
tError = tError + r_error_left + r_error_right +
(x_len - aligned_xLen_left - aligned_xLen_right);
}
else
{
float E_rate = (float)(x_len)/(float)(aligned_xLen_left + aligned_xLen_right);
tError = tError + (r_error_left + r_error_right)*E_rate;
}
}///not aligned in both direction
else if(aligned_xLen_left == 0 && aligned_xLen_right == 0)
{
tError += x_len;
}///only aligned in left
else if(aligned_xLen_left != 0)
{
tError = tError + r_error_left + (x_len - aligned_xLen_left);
}///only aligned in right
else if(aligned_xLen_right != 0)
{
tError = tError + r_error_right + (x_len - aligned_xLen_right);
}
}
}
double error_rate = (double)(tError)/(double)(tLen);
return error_rate;
}
int calculate_hpm_errors(char* x, int x_len, char* y, int y_len, CIGAR* cigar, int error)
{
int x_i, y_i, cigar_i;
x_i = 0;
y_i = 0;
cigar_i = 0;
int operation;
int operationLen;
int i;
int cigar_error = 0;
int hpm_error = 0;
while (cigar_i < cigar->length)
{
operation = cigar->C_C[cigar_i];
operationLen = cigar->C_L[cigar_i];
if (operation == 0)
{
x_i = x_i + operationLen;
y_i = y_i + operationLen;
}
else if (operation == 1)
{
cigar_error += operationLen;
for (i = 0; i < operationLen; i++)
{
if(if_is_homopolymer_repeat(x_i, x, x_len) || if_is_homopolymer_repeat(y_i, y, y_len))
{
hpm_error++;
}
x_i++;
y_i++;
}
}
else if (operation == 2)
{
if(if_is_homopolymer_repeat(x_i, x, x_len) || if_is_homopolymer_repeat(y_i, y, y_len))
{
hpm_error++;
}
cigar_error += operationLen;
y_i += operationLen;
}
else if (operation == 3)
{
if(if_is_homopolymer_repeat(x_i, x, x_len) || if_is_homopolymer_repeat(y_i, y, y_len))
{
hpm_error++;
}
cigar_error += operationLen;
x_i += operationLen;
}
cigar_i++;
}
return hpm_error;
}
void count_no_HPM_errors(overlap_region_alloc* overlap_list, All_reads* R_INF,
UC_Read* g_read, Correct_dumy* dumy, UC_Read* overlap_read,
long long* total_errors, long long* total_hpm_errors)
{
long long j, i;
long long y_id, y_strand;
long long x_start, x_end, x_len, y_start, y_end, y_len, error;
char* x_string;
char* y_string;
CIGAR* cigar;
int hpm_error;
(*total_errors) = 0;
(*total_hpm_errors) = 0;
for (j = 0; j < (long long)overlap_list->length; j++)
{
y_id = overlap_list->list[j].y_id;
y_strand = overlap_list->list[j].y_pos_strand;
if (overlap_list->list[j].is_match == 1)
{
///for (i = 0; i < overlap_list->list[j].w_list_length; i++)
for (i = 1; i < (long long)overlap_list->list[j].w_list_length - 1; i++)
{
if(overlap_list->list[j].w_list[i].y_end != -1)
{
x_start = overlap_list->list[j].w_list[i].x_start;
x_end = overlap_list->list[j].w_list[i].x_end;
x_len = x_end - x_start + 1;
x_string = g_read->seq + x_start;
y_start = overlap_list->list[j].w_list[i].y_start;
y_end = overlap_list->list[j].w_list[i].y_end;
y_len = y_end - y_start + 1;
recover_UC_Read_sub_region(dumy->overlap_region, y_start, y_len, y_strand, R_INF, y_id);
y_string = dumy->overlap_region;
cigar = &overlap_list->list[j].w_list[i].cigar;
error = overlap_list->list[j].w_list[i].error;
hpm_error = calculate_hpm_errors(x_string, x_len, y_string, y_len, cigar, error);
///fprintf(stderr, "hpm_error: %d, error: %d\n", hpm_error, error);
(*total_errors) += error;
(*total_hpm_errors) += hpm_error;
}
}
}
}
}
int verify_cigar(char* x, int x_len, char* y, int y_len, CIGAR* cigar, int error)
{
int x_i, y_i, cigar_i;
x_i = 0;
y_i = 0;
cigar_i = 0;
int operation;
int operationLen;
int i;
int cigar_error = 0;
int flag_error = 0;
///0 is match, 1 is mismatch, 2 is up, 3 is left
///2 means there are more y, 3 means there are more x
while (cigar_i < cigar->length)
{
operation = cigar->C_C[cigar_i];
operationLen = cigar->C_L[cigar_i];
if (operation == 0)
{
for (i = 0; i < operationLen; i++)
{
if (x[x_i]!=y[y_i])
{
///fprintf(stderr, "error match\n");