forked from chhylp123/hifiasm
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Overlaps_hamt.cpp
13912 lines (12709 loc) · 532 KB
/
Overlaps_hamt.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 <sys/stat.h>
#include <stdint.h>
#include <math.h>
#define __STDC_FORMAT_MACROS 1 // cpp special (ref: https://stackoverflow.com/questions/14535556/why-doesnt-priu64-work-in-this-code)
#include <inttypes.h>
#include <assert.h>
#include <pthread.h>
#include "Overlaps.h"
#include "Process_Read.h"
#include "CommandLines.h"
#include "Overlaps_hamt.h"
#include "ksort.h"
#include "kvec.h"
#include "htab.h"
#include "kthread.h"
#include "t-sne.h"
#include "khashl.h"
#define HAMT_PI 3.141592653589793
#define HAMT_TWOPI 6.283185307179586
#define HAMT_MAX(x, y) ((x >= y)?(x):(y))
#define HAMT_MIN(x, y) ((x <= y)?(x):(y))
#define HAMT_DIFF(x, y) ((HAMT_MAX((x), (y))) - (HAMT_MIN((x), (y))))
#define UTG_LEN(ug, vu) ((ug)->u.a[(vu)>>1].len) // vu has the direction bit
#define paf_ht_eq(a, b) ((a)==(b))
#define paf_ht_hash(a) ((a))
KHASHL_MAP_INIT(static klib_unused, paf_ht_t, paf_ht, uint64_t, uint16_t, paf_ht_hash, paf_ht_eq)
#define PAF_CT_PRE 12
#define PAF_CT_PRE_M ((1<<(PAF_CT_PRE))-1)
#define paf_ct_eq(a, b) ((a>>PAF_CT_PRE)==(b>>PAF_CT_PRE))
#define paf_ct_hash(a) ((a>>PAF_CT_PRE))
KHASHL_MAP_INIT(static klib_unused, paf_ct_t, paf_ct, uint64_t, uint32_t, paf_ct_hash, paf_ct_eq)
KDQ_INIT(uint64_t)
KDQ_INIT(uint32_t)
KRADIX_SORT_INIT(ovhamt64, uint64_t, uint64_t, 8)
KRADIX_SORT_INIT(ovhamt32, uint32_t, uint32_t, 4)
typedef kvec_t(char*) kvec_strings_t;
#define HAMT_PRIMARY_LABEL 0
#define HAMT_ALTER_LABEL 1
void hamt_ug_util_BFS_markSubgraph_trailing(ma_ug_t *ug_old, ma_ug_t *ug_new, int base_label);
uint16_t index5NF[1024]={
0, 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, 31,
47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 15,
62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 58,
77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 43,
92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 88, 103, 104, 105, 27,
106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 73, 117, 118, 119, 11,
120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 113, 131, 132, 133, 54,
134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 99, 145, 146, 147, 39,
148, 149, 150, 151, 152, 153, 154, 141, 155, 156, 157, 84, 158, 159, 160, 23,
161, 162, 163, 164, 165, 166, 167, 127, 168, 169, 170, 69, 171, 172, 173, 7,
174, 175, 176, 177, 178, 179, 180, 164, 181, 182, 183, 109, 184, 185, 186, 50,
187, 188, 189, 190, 191, 192, 193, 151, 194, 195, 196, 95, 197, 198, 199, 35,
200, 201, 202, 190, 203, 204, 205, 137, 206, 207, 208, 80, 209, 210, 211, 19,
212, 213, 214, 177, 215, 216, 217, 123, 218, 219, 220, 65, 221, 222, 223, 3,
224, 225, 226, 223, 227, 228, 229, 173, 230, 231, 232, 119, 233, 234, 235, 61,
236, 237, 238, 211, 239, 240, 241, 160, 242, 243, 244, 105, 245, 246, 247, 46,
248, 249, 250, 199, 251, 252, 253, 147, 254, 255, 256, 91, 257, 258, 247, 30,
259, 260, 261, 186, 262, 263, 264, 133, 265, 266, 267, 76, 268, 269, 235, 14,
270, 271, 272, 220, 273, 274, 275, 170, 276, 277, 278, 116, 279, 280, 267, 57,
281, 282, 283, 208, 284, 285, 286, 157, 287, 288, 289, 102, 290, 291, 256, 42,
292, 293, 294, 196, 295, 296, 297, 144, 298, 299, 289, 87, 300, 301, 244, 26,
302, 303, 304, 183, 305, 306, 307, 130, 308, 309, 278, 72, 310, 311, 232, 10,
312, 313, 314, 217, 315, 316, 317, 167, 318, 319, 307, 112, 320, 321, 264, 53,
322, 323, 324, 205, 325, 326, 327, 154, 328, 329, 297, 98, 330, 331, 253, 38,
332, 333, 334, 193, 335, 336, 327, 140, 337, 338, 286, 83, 339, 340, 241, 22,
341, 342, 343, 180, 344, 345, 317, 126, 346, 347, 275, 68, 348, 349, 229, 6,
350, 351, 352, 214, 353, 354, 343, 163, 355, 356, 304, 108, 357, 358, 261, 49,
359, 360, 361, 202, 362, 363, 334, 150, 364, 365, 294, 94, 366, 367, 250, 34,
368, 369, 361, 189, 370, 371, 324, 136, 372, 373, 283, 79, 374, 375, 238, 18,
376, 377, 352, 176, 378, 379, 314, 122, 380, 381, 272, 64, 382, 383, 226, 2,
384, 385, 383, 222, 386, 387, 349, 172, 388, 389, 311, 118, 390, 391, 269, 60,
392, 393, 375, 210, 394, 395, 340, 159, 396, 397, 301, 104, 398, 399, 258, 45,
400, 401, 367, 198, 402, 403, 331, 146, 404, 405, 291, 90, 406, 399, 246, 29,
407, 408, 358, 185, 409, 410, 321, 132, 411, 412, 280, 75, 413, 391, 234, 13,
414, 415, 381, 219, 416, 417, 347, 169, 418, 419, 309, 115, 420, 412, 266, 56,
421, 422, 373, 207, 423, 424, 338, 156, 425, 426, 299, 101, 427, 405, 255, 41,
428, 429, 365, 195, 430, 431, 329, 143, 432, 426, 288, 86, 433, 397, 243, 25,
434, 435, 356, 182, 436, 437, 319, 129, 438, 419, 277, 71, 439, 389, 231, 9,
440, 441, 379, 216, 442, 443, 345, 166, 444, 437, 306, 111, 445, 410, 263, 52,
446, 447, 371, 204, 448, 449, 336, 153, 450, 431, 296, 97, 451, 403, 252, 37,
452, 453, 363, 192, 454, 449, 326, 139, 455, 424, 285, 82, 456, 395, 240, 21,
457, 458, 354, 179, 459, 443, 316, 125, 460, 417, 274, 67, 461, 387, 228, 5,
462, 463, 377, 213, 464, 458, 342, 162, 465, 435, 303, 107, 466, 408, 260, 48,
467, 468, 369, 201, 469, 453, 333, 149, 470, 429, 293, 93, 471, 401, 249, 33,
472, 468, 360, 188, 473, 447, 323, 135, 474, 422, 282, 78, 475, 393, 237, 17,
476, 463, 351, 175, 477, 441, 313, 121, 478, 415, 271, 63, 479, 385, 225, 1,
480, 479, 382, 221, 481, 461, 348, 171, 482, 439, 310, 117, 483, 413, 268, 59,
484, 475, 374, 209, 485, 456, 339, 158, 486, 433, 300, 103, 487, 406, 257, 44,
488, 471, 366, 197, 489, 451, 330, 145, 490, 427, 290, 89, 487, 398, 245, 28,
491, 466, 357, 184, 492, 445, 320, 131, 493, 420, 279, 74, 483, 390, 233, 12,
494, 478, 380, 218, 495, 460, 346, 168, 496, 438, 308, 114, 493, 411, 265, 55,
497, 474, 372, 206, 498, 455, 337, 155, 499, 432, 298, 100, 490, 404, 254, 40,
500, 470, 364, 194, 501, 450, 328, 142, 499, 425, 287, 85, 486, 396, 242, 24,
502, 465, 355, 181, 503, 444, 318, 128, 496, 418, 276, 70, 482, 388, 230, 8,
504, 477, 378, 215, 505, 459, 344, 165, 503, 436, 305, 110, 492, 409, 262, 51,
506, 473, 370, 203, 507, 454, 335, 152, 501, 430, 295, 96, 489, 402, 251, 36,
508, 469, 362, 191, 507, 448, 325, 138, 498, 423, 284, 81, 485, 394, 239, 20,
509, 464, 353, 178, 505, 442, 315, 124, 495, 416, 273, 66, 481, 386, 227, 4,
510, 476, 376, 212, 509, 457, 341, 161, 502, 434, 302, 106, 491, 407, 259, 47,
511, 472, 368, 200, 508, 452, 332, 148, 500, 428, 292, 92, 488, 400, 248, 32,
511, 467, 359, 187, 506, 446, 322, 134, 497, 421, 281, 77, 484, 392, 236, 16,
510, 462, 350, 174, 504, 440, 312, 120, 494, 414, 270, 62, 480, 384, 224, 0,
};
float cosine_similarity(float *a, float *b, int l){
float d=0, A=0, B=0;
for (int i=0; i<l; i++){
d+= a[i]*b[i];
A+= a[i]*a[i];
B+= b[i]*b[i];
}
return d/(sqrt(A)*sqrt(B));
}
char seqcmp(char n){
if (n=='A') return 'T';
if (n=='T') return 'A';
if (n=='C') return 'G';
if (n=='G') return 'C';
return n;
}
// for debug fprintf from kthreads
// (note: printf operates in multithreaded; might crash if race)
void hamt_dbgmsg_init(dbgmsg_t *h){
h->n = 0;
h->m = 128;
h->a = (char*)malloc(h->m * 1);
assert(h->a);
}
void hamt_dbgmsg_destroy(dbgmsg_t *h){
free(h->a);
}
void hamt_dbgmsg_reset(dbgmsg_t *h){
h->n = 0;
}
void hamt_dbgmsg_append(dbgmsg_t *h, char *s, int l){
while (h->n+l>=h->m){
h->m = h->m + (h->m>>1);
h->a = (char*)realloc(h->a, h->m);
assert(h->a);
}
sprintf(h->a+h->n, "%s", s);
h->n += l;
}
int hamt_dbgmsg_is_empty(dbgmsg_t *h){
if (h->n==0){return 1;}
return 0;
}
void write_debug_auxsg(asg_t *sg, char *file_base_name){
char* index_name = (char*)malloc(strlen(file_base_name)+15);
sprintf(index_name, "%s.auxsg.gfa", file_base_name);
FILE* fp = fopen(index_name, "w");
uint32_t v, w, dir1, dir2;
for (uint32_t i=0; i<sg->n_arc; i++){
v = sg->arc[i].ul>>32;
w = sg->arc[i].v;
dir1 = (uint32_t)v&1;
dir2 = (uint32_t)w&2;
fprintf(fp, "#%d\t%d\t#%d\t%d\n", (int)v+1, (int)dir1, (int)w+1, (int)dir2);
}
fclose(fp);
free(index_name);
}
void ma_ug_print2_lite(const ma_ug_t *ug, All_reads *RNF, asg_t* read_g,
int print_seq, const char* prefix, FILE *fp)
{
// lite: no seq, no coverage
uint32_t i, j, l;
char name[32];
for (i = 0; i < ug->u.n; ++i) { // the Segment lines in GFA
ma_utg_t *p = &ug->u.a[i];
if(p->m == 0) continue;
sprintf(name, "%s%.6d%c", prefix, i + 1, "lc"[p->circ]);
if (print_seq) fprintf(fp, "S\t%s\t%s\tLN:i:%d\tdp:f:1\n", name, p->s? p->s : "*", p->len);
else fprintf(fp, "S\t%s\t*\tLN:i:%d\tdp:f:1\n", name, p->len);
for (j = l = 0; j < p->n; l += (uint32_t)p->a[j++]) {
uint32_t x = p->a[j]>>33;
if(x<RNF->total_reads)
{
fprintf(fp, "A\t%s\t%d\t%c\t%.*s\t%d\t%d\tid:i:%d\tHG:A:%c\n", name, l, "+-"[p->a[j]>>32&1],
(int)Get_NAME_LENGTH((*RNF), x), Get_NAME((*RNF), x),
0, 10000, x, "apmaaa"[RNF->trio_flag[x]]); // was coverage_cut[x].s and coverage_cut[x].e
}
else
{
fprintf(fp, "A\t%s\t%d\t%c\t%s\t%d\t%d\tid:i:%d\tHG:A:%c\n", name, l, "+-"[p->a[j]>>32&1],
"FAKE", 0, 10000, x, '*'); // was coverage_cut[x].s and coverage_cut[x].e
}
}
}
for (i = 0; i < ug->g->n_arc; ++i) { // the Link lines in GFA
uint32_t u = ug->g->arc[i].ul>>32, v = ug->g->arc[i].v;
fprintf(fp, "L\t%s%.6d%c\t%c\t%s%.6d%c\t%c\t%dM\tL1:i:%d\n",
prefix, (u>>1)+1, "lc"[ug->u.a[u>>1].circ], "+-"[u&1],
prefix, (v>>1)+1, "lc"[ug->u.a[v>>1].circ], "+-"[v&1], ug->g->arc[i].ol, asg_arc_len(ug->g->arc[i]));
}
}
void hamtdebug_add_fake_utg_coverage(ma_ug_t *ug){
if (ug->utg_coverage){free(ug->utg_coverage);}
ug->utg_coverage = (int*)calloc(ug->g->n_seq, sizeof(int));
}
void hamt_ug_print_simple(const ma_ug_t *ug, All_reads *RNF, FILE *fp)
{
// simplified `ma_ug_print2`
// NOTE
// Assume coverage has been collected
// Does not include subgraph info.
uint32_t i, j, l;
char name[32];
for (i = 0; i < ug->u.n; ++i) { // the Segment lines in GFA
ma_utg_t *p = &ug->u.a[i];
if(p->m == 0) continue;
sprintf(name, "utg%.6d%c", i + 1, "lc"[p->circ]);
fprintf(fp, "S\t%s\t*\tLN:i:%d\tdp:f:%u\n", name, p->len, ug->utg_coverage[i]);
for (j = l = 0; j < p->n; l += (uint32_t)p->a[j++]) {
uint32_t x = p->a[j]>>33;
if(x<RNF->total_reads)
{
fprintf(fp, "A\t%s\t%d\t%c\t%.*s\t%d\t%d\tid:i:%d\tHG:A:%c\n", name, l, "+-"[p->a[j]>>32&1],
(int)Get_NAME_LENGTH((*RNF), x), Get_NAME((*RNF), x),
0, 8888, x, "apmaaa"[RNF->trio_flag[x]]);
}else{
fprintf(stderr, "[%s] ERROR\n", __func__);
exit(1);
}
}
}
for (i = 0; i < ug->g->n_arc; ++i) { // the Link lines in GFA
uint32_t u = ug->g->arc[i].ul>>32, v = ug->g->arc[i].v;
fprintf(fp, "L\t%s%.6d%c\t%c\t%s%.6d%c\t%c\t%dM\tL1:i:%d\n",
"utg", (u>>1)+1, "lc"[ug->u.a[u>>1].circ], "+-"[u&1],
"utg", (v>>1)+1, "lc"[ug->u.a[v>>1].circ], "+-"[v&1], ug->g->arc[i].ol, asg_arc_len(ug->g->arc[i]));
}
}
void hamtdebug_output_unitig_graph_ug(ma_ug_t *ug, char *base_fn, const char *suffix, int cleanID){
// write gfa without ma_ug_gen and sequence
// solely meant for graph cleaning debug
char* gfa_name = (char*)malloc(strlen(base_fn)+100);
sprintf(gfa_name, "%s.G%s_%d.utg.gfa", base_fn, suffix, cleanID);
FILE *output_file = fopen(gfa_name, "w");
hamt_ug_print_simple(ug, &R_INF, output_file);
fclose(output_file);
free(gfa_name);
}
typedef struct {
uint32_t *a;
int n, m; // n is the count, m is the capacity
int i_head, i_tail;
int earlywrap, prev_m; // because variable length
}queue32_t;
void queue32_init(queue32_t *q){
q->a = (uint32_t*)malloc(sizeof(uint32_t)*16);
assert(q->a);
q->n = 0;
q->m = 16;
q->i_head = 0;
q->i_tail = 0;
q->prev_m = q->m;
}
void queue32_destroy(queue32_t *q){
free(q->a);
}
void queue32_enqueue(queue32_t *q, uint32_t d){
if ((q->n+1)>=q->m){
uint32_t shift = (q->m>>1);
q->a = (uint32_t*)realloc(q->a, sizeof(uint32_t)*(q->m + shift));
assert(q->a);
q->prev_m = q->m;
q->m = q->m + shift;
if (q->i_head>q->i_tail){
// need to shift the content between head and prev_m
for (int idx=q->prev_m-1; idx>=q->i_head; idx--){
q->a[idx+shift] = q->a[idx];
}
q->i_head += shift;
}
}
q->a[q->i_tail] = d;
q->n++;
if (q->i_tail==(q->m-1)){
q->i_tail = 0;
}else{
q->i_tail++;
}
}
int queue32_dequeue(queue32_t *q, uint32_t *d){
if (q->n==0){ // underflow
return 0;
}
*d = q->a[q->i_head];
q->n--;
if (q->i_head==(q->m-1)){
q->i_head = 0;
}else{
q->i_head++;
}
return 1;
}
void queue32_reset(queue32_t *q){
q->n = 0;
q->i_head = 0;
q->i_tail = 0;
q->prev_m = q->m;
}
int queue32_get_size(queue32_t *q){
return q->n;
}
int queue32_isempty(queue32_t *q){
return !(q->n);
}
int queue32_is_in_queue(queue32_t *q, uint32_t d){
// check if a given value is in the current queue
// RETURN
// 0 if no
// 1 if yes
if (q->n==0){return 0;} // queue is empty
int i = q->i_head;
while (i!=q->i_tail){
if (q->a[i]==d){return 1;}
if (i==(q->m-1)){
i = 0;
}else{
i++;
}
}
if (q->a[i]==d){return 1;}
return 0;
}
typedef struct {
uint64_t *a;
int n, m;
}vecu64_t;
void vecu64_init(vecu64_t *v){
v->n = 0;
v->m = 16;
v->a = (uint64_t*)malloc(16*sizeof(uint64_t));
}
void vecu64_destroy(vecu64_t *v){
free(v->a);
}
void vecu64_push(vecu64_t *v, uint64_t d){
if (v->n==v->m){
v->m = v->m + (v->m>>1);
v->a = (uint64_t*)realloc(v->a, sizeof(uint64_t)*v->m);
}
v->a[v->n++] = d;
}
int vecu64_is_in_vec(vecu64_t *v, uint64_t d){
for (int i=0; i<v->n; i++){
if (v->a[i]==d){
return 1;
}
}
return 0;
}
int vecu64_get_size(vecu64_t *v){
return v->n;
}
typedef struct {
uint32_t *a;
int n, m;
}vecu32_t;
void vecu32_init(vecu32_t *v){
v->n = 0;
v->m = 16;
v->a = (uint32_t*)malloc(16*sizeof(uint32_t));
}
void vecu32_destroy(vecu32_t *v){
free(v->a);
}
void vecu32_push(vecu32_t *v, uint32_t d){
if (v->n==v->m){
v->m = v->m + (v->m>>1);
v->a = (uint32_t*)realloc(v->a, sizeof(uint32_t)*v->m);
}
v->a[v->n++] = d;
}
void vecu32_reset(vecu32_t *v){
v->n = 0;
}
int vecu32_is_in_vec(vecu32_t *v, uint32_t d){
for (int i=0; i<v->n; i++){
if (v->a[i]==d){
return 1;
}
}
return 0;
}
int vecu32_get_size(vecu32_t *v){
return v->n;
}
typedef struct {
uint32_t *a;
int n, m; // n is the count, m is the capacity
}stacku32_t;
void stacku32_init(stacku32_t *stack){
stack->n = 0;
stack->m = 16;
stack->a = (uint32_t*)malloc(16*sizeof(uint32_t));
assert(stack->a);
}
void stacku32_destroy(stacku32_t *stack){
free(stack->a);
}
int stacku32_is_empty(stacku32_t *stack){
if (stack->n==0) return 1;
return 0;
}
int stacku32_get_length(stacku32_t *stack){
return stack->n;
}
int stacku32_peek_last_item(stacku32_t *stack, uint32_t *buf){
// RETURN
// 1 if success
// 0 if fail (stack is empty)
if (stack->n>0){
*buf = stack->a[stack->n-1];
return 1;
}
return 0;
}
int stacku32_peek_prev_item(stacku32_t *stack, int i, uint32_t *buf){
// RETURN
// 1 if success
// 0 if invalid i (larger than stack size) or fail (stack is empty)
if (stack->n>0 && i<stack->n){
*buf = stack->a[i-1];
return 1;
}
return 0;
}
void stacku32_invert_buffer(stacku32_t *stack){
if (stack->n<=1) return;
uint32_t tmp;
for (int i=0; i<stack->n/2; i++){ // TODO/BUG: is flooring always guaranteed?
tmp = stack->a[i];
stack->a[i] = stack->a[stack->n-1-i];
stack->a[stack->n-1-i] = tmp;
}
}
void stacku32_push(stacku32_t *stack, uint32_t value){
if (stack->n+2>stack->m){
stack->m = stack->m<16? 16 : (stack->m + (stack->m>>1));
stack->a = (uint32_t*)realloc(stack->a, sizeof(uint32_t)*stack->m);
assert(stack->a);
}
stack->a[stack->n++] = value;
}
int stacku32_pop(stacku32_t *stack, uint32_t *value){
if (stack->n==0){
return 0;
}
*value = stack->a[stack->n-1];
stack->n--;
return 1;
}
void stacku32_reset(stacku32_t *stack){
stack->n = 0;
}
void stacku32_copyover(stacku32_t *source, stacku32_t *dest){
for (int i=0; i<source->n; i++){
stacku32_push(dest, source->a[i]);
}
}
int stack32_is_in_stack(stacku32_t *stack, uint32_t d){
// FUNC
// check if a given value is in the buffer
// RETURN
// 1 if yes
// 0 if no
if (stack->n==0){return 0;}
for (int i=0; i<stack->n; i++){
if (stack->a[i]==d){
return 1;
}
}
return 0;
}
int stack32_is_in_stack_givenrange(stacku32_t *stack, uint32_t d, int start, int end){
// FUNC
// check if a given value is in the buffer's segment [start, end)
// RETURN
// 1 if yes
// 0 if no
if (stack->n==0){return 0;}
end = end>stack->n? stack->n : end;
start = start<0? 0 : start;
for (int i=start; i<end; i++){
if (stack->a[i]==d){
return 1;
}
}
return 0;
}
int uint32_buffer_unordered_equal(uint32_t *buf1, int buf1_l, uint32_t *buf2, int buf2_l){
if (buf1_l!=buf2_l) return 0;
int ret = 1;
uint32_t *b1 = (uint32_t*)malloc(buf1_l * sizeof(uint32_t));
uint32_t *b2 = (uint32_t*)malloc(buf1_l * sizeof(uint32_t));
memcpy(b1, buf1, buf1_l);
memcpy(b2, buf2, buf2_l);
radix_sort_ovhamt32(b1, b1+buf1_l);
radix_sort_ovhamt32(b2, b2+buf2_l);
for (int i=0; i<buf1_l; i++){
if (b1[i]!=b2[i]){
ret = 0;
break;
}
}
return ret;
}
int stacku32_unordered_equal(stacku32_t *stack1, stacku32_t *stack2){
// FUNC
// Compare if stack1 and stack2 has the same content
return uint32_buffer_unordered_equal(stack1->a, stack1->n, stack2->a, stack2->n);
}
int stacku32_index_value(stacku32_t *stack, uint32_t d){
// FUNC
// if a given value is in the buffer, return the index
// return -1 otherwise
if (stack->n==0){return -1;}
for (int i=0; i<stack->n; i++){
if (stack->a[i]==d){
return i;
}
}
return -1;
}
// quick and dirty struct for topo sort
typedef struct {
uint64_t *a;
int n, m; // n is the count, m is the capacity
}stacku64_t;
void stacku64_init(stacku64_t *stack){
stack->n = 0;
stack->m = 16;
stack->a = (uint64_t*)malloc(16*sizeof(uint64_t));
assert(stack->a);
}
void stacku64_destroy(stacku64_t *stack){
free(stack->a);
}
void stacku64_push(stacku64_t *stack, uint64_t value){
if (stack->n+2>stack->m){
stack->m = stack->m<16? 16 : (stack->m + (stack->m>>1));
stack->a = (uint64_t*)realloc(stack->a, sizeof(uint64_t)*stack->m);
assert(stack->a);
}
stack->a[stack->n++] = value;
}
int stacku64_pop(stacku64_t *stack, uint64_t *value){
if (stack->n==0){
return 0;
}
*value = stack->a[stack->n-1];
stack->n--;
return 1;
}
void stacku64_reset(stacku64_t *stack){
stack->n = 0;
}
void stacku64_invert_buffer(stacku64_t *stack){
if (stack->n<=1) return;
uint32_t tmp;
for (int i=0; i<stack->n/2; i++){ // TODO/BUG: is flooring always guaranteed?
tmp = stack->a[i];
stack->a[i] = stack->a[stack->n-1-i];
stack->a[stack->n-1-i] = tmp;
}
}
/**
* @brief fixed length single bit array
*/
typedef struct {
uint8_t *a; // left to right
uint32_t m, mm; // array size and expected bit-wise array size
}hamt_ba_t;
/**
* @par bitsize the precise array length. Allocation is 8-bit blocks,
* length sancheck is precise.
* @func Allocate memory; will zero out.
*/
hamt_ba_t *hamt_ba_t_init(uint32_t bitsize){
hamt_ba_t *d = (hamt_ba_t*)calloc(1, sizeof *d);
uint32_t size = bitsize/8+1;
d->a = (uint8_t*)calloc(size, 1);
d->m = size;
d->mm = bitsize;
return d;
}
void hamt_ba_t_destroy(hamt_ba_t *d){
free(d->a);
free(d);
}
/**
* @func Reallocate, will zero out new allocation.
*/
void hamt_ba_t_resize(hamt_ba_t *d, uint32_t bitsize){
uint32_t prev_size = d->m;
uint8_t prev_last = d->a[d->m-1];
d->m = bitsize/8 +1 ;
d->mm = bitsize;
d->a = (uint8_t*)realloc(d->a, d->m);
if (!d->a){
fprintf(stderr, "[E::%s] realloc failed\n", __func__);
exit(1);
}
d->a[prev_size-1] = prev_last;
memset(d->a+prev_size, 0, d->m - prev_size);
}
/**
* @par i index in the bitarray
*/
uint8_t hamt_ba_t_get(hamt_ba_t *d, uint32_t i){
if (i>d->mm){
fprintf(stderr, "[E::%s] index exceeds array size\n", __func__);
exit(1);
}
int idx = i/8;
int shift = i%8;
uint8_t ret = d->a[idx] & ((((uint8_t)1)<<7) >> shift);
return ret>>(7-shift);
}
/**
@par op 0 for unmark, 1 for mark, 2 for toggle
@par i index in the bitarray
*/
void hamt_ba_t_write(hamt_ba_t *d, int op, uint32_t i){
if (i>d->mm){
fprintf(stderr, "[E::%s] index exceeds array size\n", __func__);
exit(1);
}
int shift = i%8;
int index = i/8;
uint8_t mask = (((uint8_t)1)<<7) >> shift;
if (op==0){
d->a[index] &= (~mask);
}else if (op==1){
d->a[index] |= mask;
}else if (op==2){
d->a[index] ^= mask;
}
}
/**
* @brief print out bits
*/
void hamt_bat_t_debugprint(hamt_ba_t *d){
for (int i=0; i<=d->mm; i++){
fprintf(stderr, "[debug::%s] %d => %d\n", __func__,
i, (int)hamt_ba_t_get(d, i));
}
}
//////////////////////////////////////////////////////////////////////////
// routines for overlaps //
//////////////////////////////////////////////////////////////////////////
int hamt_ovlp_read_coverage_nbreads(ma_hit_t_alloc *sources, long long i_read, uint32_t qs, uint32_t qe){
// FUNC
// Roughly estimate read ocverage by counting overlap lengths of the given read.
if (qe<=qs){return 0;}
float ret=0;
ma_hit_t *h;
uint32_t s, e;
for (int i=0; i<sources[i_read].length; i++){
h = &sources[i_read].buffer[i];
if (h->del) {continue;}
s = (uint32_t)h->qns > qs? (uint32_t)h->qns : qs;
e = h->qe < qe? h->qe : qe;
if (e>s) {ret+=e-s;}
}
ret = ret/(qe-qs);
return ret<=1? 1 : (int)ret; // waterproof zero for easier use
}
//////////////////////////////////////////////////////////////////////
// helper routines //
//////////////////////////////////////////////////////////////////////
int does_ovlp_ever_exist(ma_hit_t_alloc *sources, uint32_t v0, uint32_t w0, ma_hit_t **h0, ma_hit_t **h0_rev){
// RETURN
// 1 if yes
// 0 if no
int verbose = 0;
int nb_targets;
int found = 0;
// v0 to w0
nb_targets = sources[v0>>1].length;
for (int i=0; i<nb_targets; i++){
if (sources[v0>>1].buffer[i].tn==(w0>>1)){
if (verbose){fprintf(stderr, "[debug::%s] found (direction 1)\n", __func__);}
found++;
*h0 = &sources[v0>>1].buffer[i];
break;
}
}
// w0^1 to v0^1
nb_targets = sources[w0>>1].length;
for (int i=0; i<nb_targets; i++){
if (sources[w0>>1].buffer[i].tn==(v0>>1)){
if (verbose){fprintf(stderr, "[debug::%s] found (direction 2)\n", __func__);}
found++;
*h0_rev = &sources[w0>>1].buffer[i];
break;
}
}
if (found==2){return 1;}
if (verbose){fprintf(stderr, "[debug::%s] failed\n", __func__);}
return 0;
}
// set .del for the arc between unitig vu and wu (vu and wu both have direction,
// which is as defined in ma_ug_gen a.k.a. as in ug->g)
static inline void ugasg_arc_del(asg_t *g, ma_ug_t *ug, uint32_t vu, uint32_t wu, int del)
{
uint32_t v, w;
if ((vu&1)==0){
v = ug->u.a[vu>>1].end^1;
}else{
v = ug->u.a[vu>>1].start^1;
}
if ((wu&1)==0){
w = ug->u.a[wu>>1].start;
}else{
w = ug->u.a[wu>>1].end;
}
asg_arc_del(g, v, w, del);
}
static inline void hamt_ug_arc_del(asg_t *sg, ma_ug_t *ug, uint32_t vu, uint32_t wu, int del){
// drop arcs between vu and wu in both directions, on sg and ug
asg_arc_del(ug->g, vu, wu, del);
asg_arc_del(ug->g, wu^1, vu^1, del);
ugasg_arc_del(sg, ug, vu, wu, del);
ugasg_arc_del(sg, ug, wu^1, vu^1, del);
// fprintf(stderr, "- droparc: utg%.6d - utg%.6d\n", (int)(vu>>1)+1, (int)(wu>>1)+1);
}
static inline void hamt_ug_utg_softdel(asg_t *sg, ma_ug_t *ug, uint32_t vu, int label){
// label unitig vu and its corresponding reads
uint32_t v;
if (ug->u.a[vu>>1].len>1000000 && label>0){
fprintf(stderr, "[W::%s] (tried to mark a very long utg (%.6d l %d) as alt; blocked)\n", __func__, (int)(vu>>1)+1, (int)ug->u.a[vu>>1].len);
return;
}
// fprintf(stderr, "- softdel: utg%.6d (length %d), to label %d\n", (int)(vu>>1)+1, (int)ug->u.a[vu>>1].len, label);
for (int i=0; i<ug->u.a[vu>>1].n; i++){
v = (uint32_t) (ug->u.a[vu>>1].a[i]>>33);
sg->seq[v].c = label;
}
ug->u.a[vu>>1].c = label;
ug->g->seq_vis[vu>>1] = label; // note seq_vis is actually n_seq*2 long
}
static inline void hamt_ug_remove_a_unitig(asg_t *sg, ma_ug_t *ug, uint32_t vu, int put_to_alt){
// given vu, drop all of its arcs and move it to the alt graph (optional if put_to_alt)
asg_t *auxsg = ug->g;
uint32_t nv = asg_arc_n(auxsg, vu);
asg_arc_t *av = asg_arc_a(auxsg, vu);
for (int i=0; i<nv; i++){
if (!av[i].del) hamt_ug_arc_del(sg, ug, vu, av[i].v, 1);
}
nv = asg_arc_n(auxsg, vu^1);
av = asg_arc_a(auxsg, vu^1);
for (int i=0; i<nv; i++){
if (!av[i].del) hamt_ug_arc_del(sg, ug, vu^1, av[i].v, 1);
}
if (put_to_alt){
hamt_ug_utg_softdel(sg, ug, vu, 1);
}
}
int hamt_ug_arc_del_between(asg_t *sg, ma_ug_t *ug, uint32_t start, uint32_t end, int del, int label){
// treat every arc between vu and wu
// NOTE
// set label to -1 to ignore sequence label
uint8_t *color = (uint8_t*)calloc(ug->g->n_seq*2, 1);
uint32_t vu, nv;
asg_arc_t *av;
asg_t *auxsg = ug->g;
int nb_cut = 0;
int yes_terminate = 0;
queue32_t q;
queue32_init(&q);
queue32_enqueue(&q, start);
while (queue32_dequeue(&q, &vu)){
if (color[vu]==2) {continue;}
color[vu] = 2;
nv = asg_arc_n(auxsg, vu);
av = asg_arc_a(auxsg, vu);
for (uint32_t i=0; i<nv; i++){
if (av[i].v==end){
yes_terminate = 1;
// fprintf(stderr, "debug: terminating... finish remaining stuff\n");
// break;
}
if (color[av[i].v]!=0) {continue;}
color[av[i].v] = 1;
if (!yes_terminate){
queue32_enqueue(&q, av[i].v);
}
hamt_ug_arc_del(sg, ug, vu, av[i].v, del);
nb_cut++;
// fprintf(stderr, "debug: cut_betwee - utg%.6d , utg%.6d\n", (int)(vu>>1)+1, (int)(av[i].v>>1)+1);
}
// if (yes_terminate){
// break;
// }
}
free(color);
queue32_destroy(&q);
return nb_cut;
}
int hamt_ug_arc_flip_between(asg_t *sg, ma_ug_t *ug, uint32_t start, uint32_t end, int base_label, int new_label){
// mark every vertex between start and end with new_label
// NOTE
// set label to -1 to ignore sequence label
int verbose = 0;
uint8_t *color = (uint8_t*)calloc(ug->g->n_seq*2, 1);
uint32_t vu, nv;
asg_arc_t *av;
asg_t *auxsg = ug->g;
int nb_cut = 0;
int yes_terminate = 0;
queue32_t q;
queue32_init(&q);
queue32_enqueue(&q, start);
while (queue32_dequeue(&q, &vu)){
if (color[vu]==2) {continue;}
color[vu] = 2;
nv = asg_arc_n(auxsg, vu);
av = asg_arc_a(auxsg, vu);
for (uint32_t i=0; i<nv; i++){
// if (ug->u.a[vu>>1].c!=base_label) {continue;}
if (av[i].v==end){
yes_terminate = 1;
// fprintf(stderr, "debug: terminating... finish remaining stuff\n");
// break;
}
if (color[av[i].v]!=0) {continue;}
color[av[i].v] = 1;
if (!yes_terminate){
queue32_enqueue(&q, av[i].v);
}
// mark
if ((av[i].v>>1)!=(end>>1) && (av[i].v>>1)!=(start>>1)){
hamt_ug_utg_softdel(sg, ug, av[i].v, new_label);
if (verbose){
fprintf(stderr, "[debug::%s] removed %.6d dir %d\n", __func__, (int)(av[i].v>>1)+1, (int)(av[i].v&1));
}
}
nb_cut++;
}
// if (yes_terminate){
// break;
// }
}
free(color);
queue32_destroy(&q);
return nb_cut;
}
void hamt_collect_utg_coverage(asg_t *sg, ma_ug_t *ug,
const ma_sub_t* coverage_cut,
ma_hit_t_alloc* sources, R_to_U* ruIndex){
// FUNC
// collect unitig coverages and store in ug->utg_coverage
// double startTime = Get_T();
if (ug->utg_coverage){
free(ug->utg_coverage);
}
ug->utg_coverage = (int*)calloc(ug->u.n, sizeof(int));
uint8_t* primary_flag = (uint8_t*)calloc(sg->n_seq, sizeof(uint8_t));
for (uint32_t i=0; i<ug->u.n; i++){
ug->utg_coverage[i] = get_ug_coverage(&ug->u.a[i], sg, coverage_cut, sources, ruIndex, primary_flag);
}
free(primary_flag);
// if (VERBOSE>1){
// fprintf(stderr, "[M::%s] collected ug coverages.\n", __func__);
// fprintf(stderr, "[T::%s] took %0.2f s\n\n", __func__, Get_T()-startTime);
// }
}
void hamt_destroy_utg_coverage(ma_ug_t *ug){
if (ug->utg_coverage){
free(ug->utg_coverage);
ug->utg_coverage = 0;
}
}
void hamt_ug_cleanup_arc_by_labels(asg_t *sg, ma_ug_t *ug){
// (legacy)
// NOTE
// ma_ug_gen_primary only looks at the label when seeding unitig,
// so for soft cut we still need to drop some arcs.
// Normaly routines should handle arc removal themselves,
// this function meant for waterproof/debug.
asg_t *auxsg = ug->g;
uint32_t vu, wu;
int cnt = 0;
for (uint32_t i=0; i<auxsg->n_arc; i++){
if (auxsg->arc[i].del) {continue;}
vu = (uint32_t) (auxsg->arc[i].ul>>32);
wu = auxsg->arc[i].v;
if (auxsg->seq_vis[vu>>1]!=auxsg->seq_vis[wu>>1]){
hamt_ug_arc_del(sg, ug, vu, wu, 1);
cnt++;
}
}
if (cnt){
asg_cleanup(sg);
asg_cleanup(auxsg);
}
// if (VERBOSE>1){
// fprintf(stderr, "[debug::%s] cleaned up %d arcs\n", __func__, cnt);
// }
}
void hamt_ug_init_seq_vis(ma_ug_t *ug, uint8_t flag){
if (!ug->g->seq_vis){
ug->g->seq_vis = (uint8_t*)calloc(ug->g->n_seq*2, 1);
}else{
memset(ug->g->seq_vis, 0, ug->g->n_seq*2);
}
}
ma_ug_t *hamt_ug_gen(asg_t *sg,
const ma_sub_t* coverage_cut,
ma_hit_t_alloc* sources, R_to_U* ruIndex, int flag){
ma_ug_t *ug;
if (flag<0){