forked from chhylp123/hifiasm
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Assembly.cpp
2256 lines (1930 loc) · 85.8 KB
/
Assembly.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 <assert.h>
#include <zlib.h>
#include "Assembly.h"
#include "Process_Read.h"
#include "CommandLines.h"
#include "Hash_Table.h"
#include "POA.h"
#include "Correct.h"
#include "htab.h"
#include "kthread.h"
#include "meta_util.h"
#include "ksort.h"
#define __STDC_FORMAT_MACROS 1 // cpp special (ref: https://stackoverflow.com/questions/14535556/why-doesnt-priu64-work-in-this-code)
#include <inttypes.h> // debug, for printing uint64
#include "meta_util_debug.h"
#include "khashl.h"
void hamt_count_new_candidates(int64_t rid, UC_Read *ucr, All_reads *rs, int sort_mode);
void ha_get_new_candidates(ha_abuf_t *ab, int64_t rid, UC_Read *ucr, overlap_region_alloc *overlap_list, Candidates_list *cl, double bw_thres, int max_n_chain, int keep_whole_chain);
void ha_get_candidates_interface(ha_abuf_t *ab, int64_t rid, UC_Read *ucr, overlap_region_alloc *overlap_list, overlap_region_alloc *overlap_list_hp, Candidates_list *cl, double bw_thres,
int max_n_chain, int keep_whole_chain, kvec_t_u8_warp* k_flag, kvec_t_u64_warp* chain_idx, ma_hit_t_alloc* paf, ma_hit_t_alloc* rev_paf, overlap_region* f_cigar, kvec_t_u64_warp* dbg_ct,
int tid);
void ha_sort_list_by_anchor(overlap_region_alloc *overlap_list);
KRADIX_SORT_INIT(radix64, uint64_t, uint64_t, sizeof(uint64_t))
// #define hamt_ov_eq(a, b) ((a) == (b))
// #define hamt_ov_hash(a) ((a))
// KHASHL_SET_INIT(static klib_unused, hamt_ov_t, hamt_ov, uint64_t, hamt_ov_hash, hamt_ov_eq)
All_reads R_INF;
Debug_reads R_INF_FLAG;
void get_corrected_read_from_cigar(Cigar_record* cigar, char* pre_read, int pre_length, char* new_read, int* new_length)
{
int i, j;
int pre_i, new_i;
int operation, operation_length;
pre_i = new_i = 0;
int diff_char_i = 0;
for (i = 0; i < (long long)cigar->length; i++)
{
operation = Get_Cigar_Type(cigar->record[i]);
operation_length = Get_Cigar_Length(cigar->record[i]);
if (operation == 0)
{
memcpy(new_read + new_i, pre_read + pre_i, operation_length);
pre_i = pre_i + operation_length;
new_i = new_i + operation_length;
}
else if (operation == 1)
{
for (j = 0; j < operation_length; j++)
{
new_read[new_i] = Get_MisMatch_Base(cigar->lost_base[diff_char_i]);
new_i++;
diff_char_i++;
}
pre_i = pre_i + operation_length;
}
else if (operation == 3)
{
pre_i = pre_i + operation_length;
diff_char_i = diff_char_i + operation_length;
}
else if (operation == 2)
{
memcpy(new_read + new_i, cigar->lost_base + diff_char_i, operation_length);
new_i = new_i + operation_length;
diff_char_i = diff_char_i + operation_length;
}
}
*new_length = new_i;
}
void get_uncorrected_read_from_cigar(Cigar_record* cigar, char* new_read, int new_length, char* pre_read, int* pre_length)
{
int i, j;
int pre_i, new_i;
int operation, operation_length;
pre_i = new_i = 0;
int diff_char_i = 0;
for (i = 0; i < (long long)cigar->length; i++)
{
operation = Get_Cigar_Type(cigar->record[i]);
operation_length = Get_Cigar_Length(cigar->record[i]);
if (operation == 0)
{
memcpy(pre_read + pre_i, new_read + new_i, operation_length);
pre_i = pre_i + operation_length;
new_i = new_i + operation_length;
}
else if (operation == 1)
{
for (j = 0; j < operation_length; j++)
{
pre_read[pre_i] = Get_Match_Base(cigar->lost_base[diff_char_i]);
pre_i++;
diff_char_i++;
}
new_i = new_i + operation_length;
}
else if (operation == 3)
{
memcpy(pre_read + pre_i, cigar->lost_base + diff_char_i, operation_length);
pre_i = pre_i + operation_length;
diff_char_i = diff_char_i + operation_length;
}
else if (operation == 2)
{
new_i = new_i + operation_length;
diff_char_i = diff_char_i + operation_length;
}
}
*pre_length = pre_i;
}
inline int get_cigar_errors(Cigar_record* cigar)
{
int i;
int total_errors = 0;
for (i = 0; i < (long long)cigar->length; i++)
{
if (Get_Cigar_Type(cigar->record[i]) > 0)
{
total_errors = total_errors + Get_Cigar_Length(cigar->record[i]);
}
}
return total_errors;
}
int debug_cigar(Cigar_record* cigar, char* pre_read, int pre_length, char* new_read, int new_length, int correct_base)
{
int i;
int total_errors = 0;
for (i = 0; i < (long long)cigar->length; i++)
{
if (Get_Cigar_Type(cigar->record[i]) > 0)
{
total_errors = total_errors + Get_Cigar_Length(cigar->record[i]);
}
}
if(total_errors!=correct_base)
{
fprintf(stderr, "total_errors: %d, correct_base: %d\n", total_errors, correct_base);
}
int pre_i, new_i;
int operation, operation_length;
pre_i = new_i = 0;
for (i = 0; i < (long long)cigar->length; i++)
{
operation = Get_Cigar_Type(cigar->record[i]);
operation_length = Get_Cigar_Length(cigar->record[i]);
if (operation == 0)
{
pre_i = pre_i + operation_length;
new_i = new_i + operation_length;
}
if (operation == 1)
{
pre_i = pre_i + operation_length;
new_i = new_i + operation_length;
}
if (operation == 3)
{
pre_i = pre_i + operation_length;
}
if (operation == 2)
{
new_i = new_i + operation_length;
}
}
if (pre_i != pre_length)
{
fprintf(stderr, "pre_i: %d, pre_length: %d\n", pre_i, pre_length);
}
if(new_i != new_length)
{
fprintf(stderr, "new_i: %d, new_length: %d\n", new_i, new_length);
}
return 1;
char* tmp_seq = (char*)malloc(new_length + pre_length);
int tmp_length;
get_corrected_read_from_cigar(cigar, pre_read, pre_length, tmp_seq, &tmp_length);
if(tmp_length != new_length)
{
fprintf(stderr, "tmp_length: %d, new_length: %d\n", tmp_length, new_length);
}
if(memcmp(new_read, tmp_seq, new_length)!=0)
{
fprintf(stderr, "error new string\n");
}
get_uncorrected_read_from_cigar(cigar, new_read, new_length, tmp_seq, &tmp_length);
if(tmp_length != pre_length)
{
fprintf(stderr, "tmp_length: %d, pre_length: %d\n", tmp_length, pre_length);
}
if(memcmp(pre_read, tmp_seq, pre_length)!=0)
{
fprintf(stderr, "error pre string\n");
}
free(tmp_seq);
if((int)cigar->new_read_length != new_length)
{
fprintf(stderr, "cigar->new_read_length: %d, new_length: %d\n", cigar->new_read_length, new_length);
}
}
inline void push_cigar(Compressed_Cigar_record* records, long long ID, Cigar_record* input)
{
if (input->length > records[ID].size)
{
records[ID].size = input->length;
records[ID].record = (uint32_t*)realloc(records[ID].record, records[ID].size*sizeof(uint32_t));
}
records[ID].length = input->length;
memcpy(records[ID].record, input->record, input->length*sizeof(uint32_t));
if (input->lost_base_length > records[ID].lost_base_size)
{
records[ID].lost_base_size = input->lost_base_length;
records[ID].lost_base = (char*)realloc(records[ID].lost_base, records[ID].lost_base_size);
}
records[ID].lost_base_length = input->lost_base_length;
memcpy(records[ID].lost_base, input->lost_base, input->lost_base_length);
records[ID].new_length = input->new_read_length;
}
void push_overlaps(ma_hit_t_alloc* paf, overlap_region_alloc* overlap_list, int flag, All_reads* R_INF, int if_reverse)
{
// flag==1 for R_INF.paf
// flag==2 for R_INF.reverse_paf
long long i = 0, xLen, yLen;
int32_t size = 0;
ma_hit_t tmp;
for (i = 0; i < (long long)overlap_list->length; ++i)
if (overlap_list->list[i].is_match == flag)
++size;
resize_ma_hit_t_alloc(paf, size);
clear_ma_hit_t_alloc(paf);
for (i = 0; i < (long long)overlap_list->length; i++)
{
if (overlap_list->list[i].is_match == flag)
{
xLen = Get_READ_LENGTH((*R_INF), overlap_list->list[i].x_id);
yLen = Get_READ_LENGTH((*R_INF), overlap_list->list[i].y_id);
tmp.qns = overlap_list->list[i].x_id;
tmp.qns = tmp.qns << 32;
tmp.tn = overlap_list->list[i].y_id;
if(if_reverse != 0)
{
tmp.qns = tmp.qns | (uint64_t)(xLen - overlap_list->list[i].x_pos_s - 1);
tmp.qe = xLen - overlap_list->list[i].x_pos_e - 1;
tmp.ts = yLen - overlap_list->list[i].y_pos_s - 1;
tmp.te = yLen - overlap_list->list[i].y_pos_e - 1;
}
else
{
tmp.qns = tmp.qns | (uint64_t)(overlap_list->list[i].x_pos_s);
tmp.qe = overlap_list->list[i].x_pos_e;
tmp.ts = overlap_list->list[i].y_pos_s;
tmp.te = overlap_list->list[i].y_pos_e;
}
///for overlap_list, the x_strand of all overlaps are 0, so the tmp.rev is the same as the y_strand
tmp.rev = overlap_list->list[i].y_pos_strand;
///tmp.bl = R_INF.read_length[overlap_list->list[i].y_id];
tmp.bl = Get_READ_LENGTH((*R_INF), overlap_list->list[i].y_id);
tmp.ml = overlap_list->list[i].strong;
tmp.no_l_indel = overlap_list->list[i].without_large_indel;
add_ma_hit_t_alloc(paf, &tmp);
}
}
}
long long push_final_overlaps(ma_hit_t_alloc* paf, ma_hit_t_alloc* reverse_paf_list, overlap_region_alloc* overlap_list, int flag)
{
// for paf: flag = 1
// for reverse_paf: flag = 2
long long i = 0;
long long available_overlaps = 0;
ma_hit_t tmp;
clear_ma_hit_t_alloc(paf); // paf has been preallocated, so we don't need preallocation
for (i = 0; i < (long long)overlap_list->length; i++)
{
// if ( (overlap_list->list[i].x_id==0 && overlap_list->list[i].y_id==35) ||
// (overlap_list->list[i].x_id==0 && overlap_list->list[i].y_id==35) ){
// fprintf(stderr, "[push overlap] the read pair\n");
// fprintf(stderr, " alignment length: %d\n", (int)overlap_list->list[i].align_length);
// fprintf(stderr, " is_match : %d\n", (int)overlap_list->list[i].is_match);
// fprintf(stderr, " strong: %d\n", (int)overlap_list->list[i].strong);
// fprintf(stderr, " large indel: %d\n", (int)overlap_list->list[i].without_large_indel);
// }
if (overlap_list->list[i].is_match == flag)
{
available_overlaps++;
/**********************query***************************/
//the interval of overlap is half-open [start, end)
tmp.qns = overlap_list->list[i].x_id;
tmp.qns = tmp.qns << 32;
tmp.qns = tmp.qns | (uint64_t)(overlap_list->list[i].x_pos_s);
///the end pos is open
tmp.qe = overlap_list->list[i].x_pos_e + 1;
/**********************query***************************/
///for overlap_list, the x_strand of all overlaps are 0, so the tmp.rev is the same as the y_strand
tmp.rev = overlap_list->list[i].y_pos_strand;
/**********************target***************************/
tmp.tn = overlap_list->list[i].y_id;
if(tmp.rev == 1)
{
long long y_readLen = R_INF.read_length[overlap_list->list[i].y_id];
tmp.ts = y_readLen - overlap_list->list[i].y_pos_e - 1;
tmp.te = y_readLen - overlap_list->list[i].y_pos_s - 1;
}
else
{
tmp.ts = overlap_list->list[i].y_pos_s;
tmp.te = overlap_list->list[i].y_pos_e;
}
///the end pos is open
tmp.te++;
/**********************target***************************/
tmp.bl = R_INF.read_length[overlap_list->list[i].y_id];
tmp.ml = overlap_list->list[i].strong;
tmp.no_l_indel = overlap_list->list[i].without_large_indel;
tmp.el = overlap_list->list[i].shared_seed;
add_ma_hit_t_alloc(paf, &tmp);
}
}
return available_overlaps;
}
long long push_final_overlaps_increment(ma_hit_t_alloc* paf, ma_hit_t_alloc* reverse_paf_list, overlap_region_alloc* overlap_list, int flag)
{
long long i = 0;
long long available_overlaps = paf->length;
ma_hit_t tmp;
///clear_ma_hit_t_alloc(paf); // paf has been preallocated, so we don't need preallocation
for (i = 0; i < (long long)overlap_list->length; i++)
{
if (overlap_list->list[i].is_match == flag)
{
available_overlaps++;
/**********************query***************************/
//the interval of overlap is half-open [start, end)
tmp.qns = overlap_list->list[i].x_id;
tmp.qns = tmp.qns << 32;
tmp.qns = tmp.qns | (uint64_t)(overlap_list->list[i].x_pos_s);
///the end pos is open
tmp.qe = overlap_list->list[i].x_pos_e + 1;
/**********************query***************************/
///for overlap_list, the x_strand of all overlaps are 0, so the tmp.rev is the same as the y_strand
tmp.rev = overlap_list->list[i].y_pos_strand;
/**********************target***************************/
tmp.tn = overlap_list->list[i].y_id;
if(tmp.rev == 1)
{
long long y_readLen = R_INF.read_length[overlap_list->list[i].y_id];
tmp.ts = y_readLen - overlap_list->list[i].y_pos_e - 1;
tmp.te = y_readLen - overlap_list->list[i].y_pos_s - 1;
}
else
{
tmp.ts = overlap_list->list[i].y_pos_s;
tmp.te = overlap_list->list[i].y_pos_e;
}
///the end pos is open
tmp.te++;
/**********************target***************************/
tmp.bl = R_INF.read_length[overlap_list->list[i].y_id];
tmp.ml = overlap_list->list[i].strong;
tmp.no_l_indel = overlap_list->list[i].without_large_indel;
tmp.el = overlap_list->list[i].shared_seed;
add_ma_hit_t_alloc(paf, &tmp);
}
}
return available_overlaps;
}
typedef struct {
int is_final, save_ov;
// chaining and overlapping related buffers
UC_Read self_read, ovlp_read;
Candidates_list clist;
overlap_region_alloc olist;
overlap_region_alloc olist_hp;
ha_abuf_t *ab;
// error correction related buffers
int64_t num_read_base, num_correct_base, num_recorrect_base;
Cigar_record cigar1;
Graph POA_Graph;
Graph DAGCon;
Correct_dumy correct;
haplotype_evdience_alloc hap;
Round2_alignment round2;
kvec_t_u32_warp b_buf;
kvec_t_u64_warp r_buf;
kvec_t_u8_warp k_flag;
overlap_region tmp_region;
} ha_ovec_buf_t;
ha_ovec_buf_t *ha_ovec_init(int is_final, int save_ov)
{
ha_ovec_buf_t *b;
CALLOC(b, 1);
b->is_final = !!is_final, b->save_ov = !!save_ov;
init_UC_Read(&b->self_read);
init_UC_Read(&b->ovlp_read);
init_Candidates_list(&b->clist);
init_overlap_region_alloc(&b->olist);
init_overlap_region_alloc(&b->olist_hp);
init_fake_cigar(&(b->tmp_region.f_cigar));
kv_init(b->b_buf.a);
kv_init(b->r_buf.a);
kv_init(b->k_flag.a);
b->ab = ha_abuf_init();
if (!b->is_final) {
init_Cigar_record(&b->cigar1);
init_Graph(&b->POA_Graph);
init_Graph(&b->DAGCon);
init_Correct_dumy(&b->correct);
InitHaplotypeEvdience(&b->hap);
init_Round2_alignment(&b->round2);
}
return b;
}
void hamt_ovec_shrink_clist(Candidates_list *cl,
long long cl_size,
long long chain_size){ // Candidates_list
if (chain_size>0 && chain_size<cl->chainDP.size){
resize_Chain_Data(&cl->chainDP, chain_size);
}
if (cl_size<cl->size){
cl->size = cl_size;
REALLOC(cl->list, cl_size);
cl->length = cl->length>cl_size? cl_size : cl->length;
}
}
void hamt_ovec_shrink_olist(overlap_region_alloc *ol,
long long ol_size){ // overlap_region_alloc
if (ol_size>=ol->size)
return;
for (long long i=ol_size+1; i<ol->size; i++){
destory_fake_cigar(&ol->list[i].f_cigar);
destory_window_list_alloc(&ol->list[i].boundary_cigars);
if (ol->list[i].w_list_size!=0)
free(ol->list[i].w_list);
}
REALLOC(ol->list, ol_size);
ol->size = ol_size;
ol->length = ol->length>ol_size? ol_size : ol->length;
}
void ha_ovec_simple_shrink_alloc(ha_ovec_buf_t *b,
long long target_size){
hamt_ovec_shrink_clist(&b->clist, target_size, -1);
hamt_ovec_shrink_olist(&b->olist, target_size);
hamt_ovec_shrink_olist(&b->olist_hp, target_size);
}
void ha_ovec_destroy(ha_ovec_buf_t *b)
{
destory_UC_Read(&b->self_read);
destory_UC_Read(&b->ovlp_read);
destory_Candidates_list(&b->clist);
destory_overlap_region_alloc(&b->olist);
destory_overlap_region_alloc(&b->olist_hp);
ha_abuf_destroy(b->ab);
destory_fake_cigar(&(b->tmp_region.f_cigar));
kv_destroy(b->b_buf.a);
kv_destroy(b->r_buf.a);
kv_destroy(b->k_flag.a);
if (!b->is_final) {
destory_Cigar_record(&b->cigar1);
destory_Graph(&b->POA_Graph);
destory_Graph(&b->DAGCon);
destory_Correct_dumy(&b->correct);
destoryHaplotypeEvdience(&b->hap);
destory_Round2_alignment(&b->round2);
}
free(b);
}
char* hamt_debug_get_phasing_variant(ha_ovec_buf_t *b, long i_read);
static int64_t ha_Graph_mem(const Graph *g)
{
int64_t i, mem = 0;
mem = sizeof(Graph) + g->node_q.size * 8 + g->g_nodes.size * sizeof(Node);
for (i = 0; i < (int64_t)g->g_nodes.size; ++i) {
Node *n = &g->g_nodes.list[i];
mem += n->mismatch_edges.size * sizeof(Edge);
mem += n->deletion_edges.size * sizeof(Edge);
mem += n->insertion_edges.size * sizeof(Edge);
}
mem += g->g_nodes.sort.size * 9;
return mem;
}
int64_t ha_ovec_mem(const ha_ovec_buf_t *b)
{
int64_t i, mem = 0, mem_clist, mem_olist;
mem_clist = b->clist.size * sizeof(k_mer_hit) + b->clist.chainDP.size * 7 * 4;
//fprintf(stderr, "[dbg::%s] window_list size: %d\n", __func__, (int)sizeof(window_list));
//fprintf(stderr, "[dbg::%s] olist size: %d \n", __func__, (int)b->olist.size);
int tot1 = 0, tot2 = 0, tot3 = 0;
mem_olist = b->olist.size * sizeof(overlap_region);
for (i = 0; i < (int64_t)b->olist.size; ++i) {
const overlap_region *r = &b->olist.list[i];
mem_olist += r->w_list_size * sizeof(window_list);
mem_olist += r->f_cigar.size * 8;
mem_olist += r->boundary_cigars.size * sizeof(window_list);
tot1 += r->w_list_size;
tot2 += r->f_cigar.size;
tot3 += r->boundary_cigars.size;
}
//fprintf(stderr, "[dbg::%s] sizes: %d %d %d\n", __func__, tot1, tot2, tot3);
mem_olist += b->olist_hp.size * sizeof(overlap_region);
for (i = 0; i < (int64_t)b->olist_hp.size; ++i) {
const overlap_region *r = &b->olist_hp.list[i];
mem_olist += r->w_list_size * sizeof(window_list);
mem_olist += r->f_cigar.size * 8;
mem_olist += r->boundary_cigars.size * sizeof(window_list);
}
mem = ha_abuf_mem(b->ab) + mem_clist + mem_olist;
if (!b->is_final) {
mem += sizeof(Cigar_record) + b->cigar1.lost_base_size + b->cigar1.size * 4;
mem += sizeof(Correct_dumy) + b->correct.size * 8;
mem += sizeof(Round2_alignment) + b->round2.cigar.size * 4 + b->round2.tmp_cigar.size * 4;
mem += sizeof(haplotype_evdience_alloc) + b->hap.size * sizeof(haplotype_evdience) + b->hap.snp_matrix_size + b->hap.snp_stat_size * sizeof(SnpStats);
mem += ha_Graph_mem(&b->POA_Graph);
mem += ha_Graph_mem(&b->DAGCon);
}
return mem;
}
static void worker_read_selection_by_est_ov(void *data, long i, int tid){
// collect the number of overlap targets for each read but don't do any alignment
// we want to limit ovec to an affordable volume, AND want to drop as less reads as possible
if (R_INF.mask_readnorm[i] & 1) return; // hamt
UC_Read ucr;
init_UC_Read(&ucr);
recover_UC_Read(&ucr, &R_INF, i);
hamt_count_new_candidates(i, &ucr, &R_INF, 0);
destory_UC_Read(&ucr);
}
static void worker_read_selection_by_est_ov_v2(void *data, long i, int tid){
// FUNC
// Only guess the total number of desired reads, won't do the actual read selection.
// Done by collecting the number of overlap targets for each read (no alignment).
// Rationale: we want to limit ovec to an affordable volume, AND want to drop as less reads as possible
// In real data it seems read selection isn't required. In mock data, it depends.
UC_Read ucr;
init_UC_Read(&ucr);
recover_UC_Read(&ucr, &R_INF, i);
hamt_count_new_candidates(i, &ucr, &R_INF, 0); // stored in rs->nb_target_reads[rid], packed as: upper 32 is nb_candidates, lower 32 is rid
destory_UC_Read(&ucr);
}
// tap unideal overlap info from ovec; other routines in Process_Read.cpp
void hamt_ovecinfo_workerpush(ovecinfo_v *v, long readID, overlap_region_alloc* olist){
ovecinfo_t *h;
uint32_t y_id;
uint8_t is_match;
for (long long i=0; i<(long long)olist->length; i++){
if (olist->list[i].is_match!=1 && olist->list[i].is_match!=2){
h = &v->a[olist->list[i].x_id];
if ((h->n+1)>=h->m){
if (h->m==0){
h->m = 8;
h->tn = (uint32_t*)malloc(h->m*sizeof(uint32_t));
h->is_match = (uint8_t*)malloc(h->m*sizeof(uint8_t));
}else{
h->m = h->m + (h->m>>1);
h->tn = (uint32_t*)realloc(h->tn, h->m*sizeof(uint32_t));
h->is_match = (uint8_t*)realloc(h->is_match, h->m*sizeof(uint8_t));
}
assert(h->tn);
assert(h->is_match);
}
// h->qn = olist->list[i].x_id;ss
h->tn[h->n] = olist->list[i].y_id;
h->is_match[h->n] = olist->list[i].is_match;
h->n++;
// fprintf(stderr, "[ovecpush] q %.*s t %.*s\n", (int)Get_NAME_LENGTH(R_INF, readID), Get_NAME(R_INF, readID),
// (int)Get_NAME_LENGTH(R_INF, y_id), Get_NAME(R_INF, y_id));
}
}
}
static void worker_ovec(void *data, long i, int tid)
{
/////////// meta ///////////////
if (R_INF.mask_readnorm[i] & 1) return;
// (this relies on malloc_all_reads to ensure every location is accessible. I think there's no malloc happening in worker_ovec.)
////////////////////////////////
ha_ovec_buf_t *b = ((ha_ovec_buf_t**)data)[tid];
int fully_cov, abnormal;
int e1, e2;
// shrink buffer when it is too large - most reads shouldn't have too many overlaps
// TODO: arbitrary is bad, find a way to determine when to realloc
// TODO: using asm_opt directly is not ideal
int realloc_thre = asm_opt.max_n_chain*16 > 64? asm_opt.max_n_chain*16 : 64 ;
if (b->olist.size>realloc_thre){
//fprintf(stderr, "[dbg::%s] thread %d, shrink: %d -> %d\n", __func__,
// tid, (int)b->olist.size, realloc_thre);
ha_ovec_simple_shrink_alloc(b, realloc_thre);
}
ha_get_candidates_interface(b->ab, i, &b->self_read, &b->olist, &b->olist_hp, &b->clist,
0.02, asm_opt.max_n_chain, 1, &(b->k_flag), &b->r_buf, &(R_INF.paf[i]), &(R_INF.reverse_paf[i]), &(b->tmp_region), NULL,
tid);
// ha_get_new_candidates_11(b->ab, i, &b->self_read, &b->olist, &b->clist, 0.02, asm_opt.max_n_chain, 1);
clear_Cigar_record(&b->cigar1);
clear_Round2_alignment(&b->round2);
correct_overlap(&b->olist, &R_INF, &b->self_read, &b->correct, &b->ovlp_read, &b->POA_Graph, &b->DAGCon,
&b->cigar1, &b->hap, &b->round2, 0, 1, &fully_cov, &abnormal);
b->num_read_base += b->self_read.length;
b->num_correct_base += b->correct.corrected_base;
b->num_recorrect_base += b->round2.dumy.corrected_base;
push_cigar(R_INF.cigars, i, &b->cigar1);
push_cigar(R_INF.second_round_cigar, i, &b->round2.cigar);
e1 = get_cigar_errors(&b->cigar1);
e2 = get_cigar_errors(&b->round2.cigar);
if (asm_opt.is_use_exp_graph_cleaning){
R_INF.nb_error_corrected[i] += (uint16_t)e1 + (uint16_t)e2;
}
R_INF.paf[i].is_fully_corrected = 0;
if (fully_cov) {
// if (get_cigar_errors(&b->cigar1) == 0 && get_cigar_errors(&b->round2.cigar) == 0)
if (e1==0 && e2==0)
R_INF.paf[i].is_fully_corrected = 1;
}
R_INF.paf[i].is_abnormal = abnormal;
R_INF.trio_flag[i] = AMBIGU;
///need to be fixed in r305
// if(ha_idx_hp == NULL)
// {
// R_INF.trio_flag[i] += collect_hp_regions(&b->olist, &R_INF, &(b->k_flag), RESEED_HP_RATE, Get_READ_LENGTH(R_INF, i), NULL);
// }
if (R_INF.trio_flag[i] != AMBIGU || b->save_ov) { // save_ov: is set only in last ovlp round and the final round
int is_rev = (asm_opt.number_of_round % 2 == 0);
push_overlaps(&(R_INF.paf[i]), &b->olist, 1, &R_INF, is_rev);
push_overlaps(&(R_INF.reverse_paf[i]), &b->olist, 2, &R_INF, is_rev);
}
// char *debug = hamt_debug_get_phasing_variant(b, i);
// fprintf(stderr, "%s", debug);
// free(debug);
//fprintf(stderr, "[dbg::%s] read %d (%d): %f | ~\n", __func__,
// (int)i, (int)b->olist.length, Get_U());
}
static void worker_ovec_related_reads(void *data, long i, int tid)
{
if (R_INF.mask_readnorm[i] & 1){
return;
}
ha_ovec_buf_t *b = ((ha_ovec_buf_t**)data)[tid];
uint64_t k, queryNameLen;
for (k = 0; k < R_INF_FLAG.query_num; k++)
{
queryNameLen = strlen(R_INF_FLAG.read_name[k]);
if (queryNameLen != Get_NAME_LENGTH((R_INF),i)) continue;
if (memcmp(R_INF_FLAG.read_name[k], Get_NAME((R_INF), i), Get_NAME_LENGTH((R_INF),i)) == 0)
{
break;
}
}
if(k < R_INF_FLAG.query_num)
{
int fully_cov, abnormal, q_idx = k;
ha_get_candidates_interface(b->ab, i, &b->self_read, &b->olist, &b->olist_hp, &b->clist,
0.02, asm_opt.max_n_chain, 1, &(b->k_flag), &b->r_buf, &(R_INF.paf[i]), &(R_INF.reverse_paf[i]), &(b->tmp_region), &(R_INF_FLAG.candidate_count[q_idx]),
tid);
// ha_get_new_candidates_11(b->ab, i, &b->self_read, &b->olist, &b->clist, 0.02, asm_opt.max_n_chain, 1);
clear_Cigar_record(&b->cigar1);
clear_Round2_alignment(&b->round2);
correct_overlap(&b->olist, &R_INF, &b->self_read, &b->correct, &b->ovlp_read, &b->POA_Graph, &b->DAGCon,
&b->cigar1, &b->hap, &b->round2, 0, 1, &fully_cov, &abnormal);
b->num_read_base += b->self_read.length;
b->num_correct_base += b->correct.corrected_base;
b->num_recorrect_base += b->round2.dumy.corrected_base;
push_cigar(R_INF.cigars, i, &b->cigar1);
push_cigar(R_INF.second_round_cigar, i, &b->round2.cigar);
R_INF.paf[i].is_fully_corrected = 0;
if (fully_cov) {
if (get_cigar_errors(&b->cigar1) == 0 && get_cigar_errors(&b->round2.cigar) == 0)
R_INF.paf[i].is_fully_corrected = 1;
}
R_INF.paf[i].is_abnormal = abnormal;
pthread_mutex_lock(&R_INF_FLAG.OutputMutex);
fprintf(R_INF_FLAG.fp, "\n>%.*s\n", (int)Get_NAME_LENGTH((R_INF), i), Get_NAME((R_INF), i));
fprintf(R_INF_FLAG.fp, "%d-th round, len: %lu, hom_cov: %d, max_n_chain: %d\n",
asm_opt.number_of_round, Get_READ_LENGTH(R_INF, i), asm_opt.hom_cov, asm_opt.max_n_chain);
fprintf(R_INF_FLAG.fp, "***************************k-mer counts (%d)***************************\n", (int)(R_INF_FLAG.candidate_count[q_idx].a.n));
sort_kvec_t_u64_warp(&(R_INF_FLAG.candidate_count[q_idx]), 0);
for (k = 0; k < R_INF_FLAG.candidate_count[q_idx].a.n; k++)
{
fprintf(R_INF_FLAG.fp, "[%lu] Count(%u): %lu, filtered: %lu\n", k,
(uint32_t)R_INF_FLAG.candidate_count[q_idx].a.a[k], R_INF_FLAG.candidate_count[q_idx].a.a[k]>>33,
(R_INF_FLAG.candidate_count[q_idx].a.a[k]>>32)&(uint64_t)1);
}
fprintf(R_INF_FLAG.fp, "***************************forward ovlp***************************\n");
for (k = 0; k < b->olist.length; k++)
{
if(b->olist.list[k].is_match != 1) continue;
fprintf(R_INF_FLAG.fp, "%.*s\n", (int)Get_NAME_LENGTH((R_INF), b->olist.list[k].y_id), Get_NAME((R_INF), b->olist.list[k].y_id));
fprintf(R_INF_FLAG.fp, "qs: %u, qe: %u, ts: %u, te: %u, rev: %u, strong: %u, no_l_indel: %u, len: %lu\n",
b->olist.list[k].x_pos_s, b->olist.list[k].x_pos_e, b->olist.list[k].y_pos_s, b->olist.list[k].y_pos_e,
b->olist.list[k].y_pos_strand, b->olist.list[k].strong, b->olist.list[k].without_large_indel,
Get_READ_LENGTH(R_INF, b->olist.list[k].y_id));
}
fprintf(R_INF_FLAG.fp, "***************************reverse ovlp***************************\n");
for (k = 0; k < b->olist.length; k++)
{
if(b->olist.list[k].is_match != 2) continue;
fprintf(R_INF_FLAG.fp, "%.*s\n", (int)Get_NAME_LENGTH((R_INF), b->olist.list[k].y_id), Get_NAME((R_INF), b->olist.list[k].y_id));
fprintf(R_INF_FLAG.fp, "qs: %u, qe: %u, ts: %u, te: %u, rev: %u, strong: %u, no_l_indel: %u, len: %lu\n",
b->olist.list[k].x_pos_s, b->olist.list[k].x_pos_e, b->olist.list[k].y_pos_s, b->olist.list[k].y_pos_e,
b->olist.list[k].y_pos_strand, b->olist.list[k].strong, b->olist.list[k].without_large_indel,
Get_READ_LENGTH(R_INF, b->olist.list[k].y_id));
}
fprintf(R_INF_FLAG.fp, "***************************unmatched ovlp***************************\n");
for (k = 0; k < b->olist.length; k++)
{
if(b->olist.list[k].is_match == 1) continue;
if(b->olist.list[k].is_match == 2) continue;
fprintf(R_INF_FLAG.fp, "%.*s\n", (int)Get_NAME_LENGTH((R_INF), b->olist.list[k].y_id), Get_NAME((R_INF), b->olist.list[k].y_id));
fprintf(R_INF_FLAG.fp, "qs: %u, qe: %u, ts: %u, te: %u, rev: %u, strong: %u, no_l_indel: %u, len: %lu\n",
b->olist.list[k].x_pos_s, b->olist.list[k].x_pos_e, b->olist.list[k].y_pos_s, b->olist.list[k].y_pos_e,
b->olist.list[k].y_pos_strand, b->olist.list[k].strong, b->olist.list[k].without_large_indel,
Get_READ_LENGTH(R_INF, b->olist.list[k].y_id));
}
R_INF.trio_flag[i] = AMBIGU;
///need to be fixed in r305
// if(ha_idx_hp == NULL)
// {
// R_INF.trio_flag[i] += collect_hp_regions(&b->olist, &R_INF, &(b->k_flag), RESEED_HP_RATE, Get_READ_LENGTH(R_INF, i), R_INF_FLAG.fp);
// }
fprintf(R_INF_FLAG.fp, "R_INF.trio_flag[%ld]: %u\n", i, R_INF.trio_flag[i]);
pthread_mutex_unlock(&R_INF_FLAG.OutputMutex);
}
}
static inline long long get_N_occ(char* seq, long long length)
{
long long j, N_occ = 0;
for (j = 0; j < length; j++)
if(seq_nt6_table[(uint8_t)seq[j]] >= 4)
N_occ++;
return N_occ;
}
typedef struct {
UC_Read g_read;
int first_round_read_size;
int second_round_read_size;
char *first_round_read;
char *second_round_read;
} ha_ecsave_buf_t;
static void worker_ec_save(void *data, long i, int tid)
{
/////////// meta ///////////////
if (R_INF.mask_readnorm[i] & 1) return;
// (this relies on malloc_all_reads to ensure every location is accessible. I think there's no malloc happening in worker_ovec.)
////////////////////////////////
ha_ecsave_buf_t *e = (ha_ecsave_buf_t*)data + tid;
Cigar_record cigar;
int first_round_read_length;
int second_round_read_length;
uint64_t N_occ;
char *new_read;
int new_read_length;
recover_UC_Read(&e->g_read, &R_INF, i);
// round 1
if ((long long)R_INF.cigars[i].new_length > e->first_round_read_size) {
e->first_round_read_size = R_INF.cigars[i].new_length;
REALLOC(e->first_round_read, e->first_round_read_size);
}
cigar.length = R_INF.cigars[i].length;
cigar.lost_base_length = R_INF.cigars[i].lost_base_length;
cigar.record = R_INF.cigars[i].record;
cigar.lost_base = R_INF.cigars[i].lost_base;
get_corrected_read_from_cigar(&cigar, e->g_read.seq, e->g_read.length, e->first_round_read, &first_round_read_length);
// round 2
if ((long long)R_INF.second_round_cigar[i].new_length > e->second_round_read_size) {
e->second_round_read_size = R_INF.second_round_cigar[i].new_length;
REALLOC(e->second_round_read, e->second_round_read_size);
}
cigar.length = R_INF.second_round_cigar[i].length;
cigar.lost_base_length = R_INF.second_round_cigar[i].lost_base_length;
cigar.record = R_INF.second_round_cigar[i].record;
cigar.lost_base = R_INF.second_round_cigar[i].lost_base;
if (!(R_INF.mask_readnorm[i] & 1)){ ///// todo: commenting this out will break other hifiasm debug functions....
get_corrected_read_from_cigar(&cigar, e->first_round_read, first_round_read_length, e->second_round_read, &second_round_read_length);
new_read = e->second_round_read;
new_read_length = second_round_read_length;
if (asm_opt.roundID != asm_opt.number_of_round - 1)
{
///need modification
reverse_complement(new_read, new_read_length);
}
else if(asm_opt.number_of_round % 2 == 0)
{
///need modification
reverse_complement(new_read, new_read_length);
}
N_occ = get_N_occ(new_read, new_read_length);
if ((long long)R_INF.read_size[i] < new_read_length) {
R_INF.read_size[i] = new_read_length;
REALLOC(R_INF.read_sperate[i], R_INF.read_size[i]/4+1);
}
R_INF.read_length[i] = new_read_length;
ha_compress_base(Get_READ(R_INF, i), new_read, new_read_length, &R_INF.N_site[i], N_occ);
}
}
void Output_corrected_reads()
{
long long i;
UC_Read g_read;
init_UC_Read(&g_read);
char* gfa_name = (char*)malloc(strlen(asm_opt.output_file_name)+35);
sprintf(gfa_name, "%s.ec.fa", asm_opt.output_file_name);
FILE* output_file = fopen(gfa_name, "w");
free(gfa_name);
for (i = 0; i < (long long)R_INF.total_reads; i++)
{
recover_UC_Read(&g_read, &R_INF, i);
fwrite(">", 1, 1, output_file);
fwrite(Get_NAME(R_INF, i), 1, Get_NAME_LENGTH(R_INF, i), output_file);
fwrite("\n", 1, 1, output_file);
fwrite(g_read.seq, 1, g_read.length, output_file);
fwrite("\n", 1, 1, output_file);
}
destory_UC_Read(&g_read);
fclose(output_file);
}
// TODO debug bit flag
// #define HAMT_DISCARD 0x1
// #define HAMT_VIA_MEDIAN 0x2
// #define HAMT_VIA_LONGLOW 0x4
// #define HAMT_VIA_KMER 0x8
// #define HAMT_VIA_PREOVEC 0x80
int hamt_pre_ovec_v2(int threshold){
// read selection considering:
// - estimate how many reads we might want to drop (could well be zero) (dont use guessed number of target counts!)
// - if dropping any, then
// - sort reads based on median + lowq (aka lower quantile value)
// - set all reads to 'drop'
// - 1st pass: keep all reads with rare kmers (based on lower quantile value), update a hashtable accordingly
// - 2nd pass: recruite reads until we don't want more of them
int ret;
double t_profiling = Get_T();
// TODO: clean up
// (temp treatment: overide any exisiting markings)
double startTime = Get_T();
fprintf(stderr, "[M::%s] Entered pre-ovec read selection.\n", __func__);
int hom_cov, het_cov;
if(ha_idx == NULL) ha_idx = ha_pt_gen(&asm_opt, ha_flt_tab, R_INF.is_all_in_mem, 0, &R_INF, &hom_cov, &het_cov);
fprintf(stderr, "[prof::%s] start ~ done ha_idx: %.2f s\n", __func__, Get_T()-t_profiling); t_profiling = Get_T();
R_INF.nb_target_reads = (uint64_t*)calloc(R_INF.total_reads, sizeof(uint64_t));
int cutoff = (int) ((float)R_INF.total_reads*2/3 +0.499); // heuristic: need less than 2/3 reads to have at most 300 ovlp targets
// determine if we might want to drop some reads
// heuristic: let half of the reads (excluding reads with no overlap) to have less than $threshold candidates
// self note:
// There's two thresholds:
// - preovec threshold (targets median kmer freq), aka the `threshold` in this function
// - lowq-10 (targets lower 10% quantile kmer freq), used in `hamt_flt_withsorting_supervised`
// The first one was introduced merely in hope to reduce rounds of read selection (using lowq-10),
// it's not a different criteria, but a subset of lowq-10.