-
Notifications
You must be signed in to change notification settings - Fork 87
/
gfa_ut.cpp
17932 lines (15952 loc) · 706 KB
/
gfa_ut.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 <math.h>
#include "kdq.h"
#include "kthread.h"
#include "gfa_ut.h"
#include "CommandLines.h"
#include "Correct.h"
#include "inter.h"
#include "Overlaps.h"
#include "hic.h"
#include "Purge_Dups.h"
#define generic_key(x) (x)
KRADIX_SORT_INIT(srt64, uint64_t, generic_key, 8)
#define OU_NOISY 2
#define ASG_ET_MERGEABLE 0
#define ASG_ET_TIP 1
#define ASG_ET_MULTI_OUT 2
#define ASG_ET_MULTI_NEI 3
#define UL_TRAV_HERATE 0.2
#define UL_TRAV_FT_RATE 0.8
KDQ_INIT(uint64_t)
typedef struct { size_t n, m; char *a; } asgc8_v;
typedef struct {
asg64_v cnt;
asg64_v idx_a;
uint64_t idx_n;
asg_t *ext;
uint64_t a_n;
} asg_ext_t;
typedef struct {
uint32_t v, uid, off;
} usg_arc_mm_t;
typedef struct {
size_t n, m;
usg_arc_mm_t *a;
} usg_arc_mm_warp;
typedef struct {
uint64_t ul;
uint32_t v;
uint32_t ol:31, del:1;
uint32_t ou;
uint64_t idx;
} usg_arc_t;
typedef struct {
size_t n, m;
usg_arc_t *a;
} usg_arc_warp;
typedef struct {
uint32_t mm, occ;
uint32_t len;
usg_arc_warp arc[2];
usg_arc_mm_warp arc_mm[2];
uint8_t del, telo;
} usg_seq_t;
#define usg_arc_key(p) ((p).v)
KRADIX_SORT_INIT(usg_arc_srt, usg_arc_t, usg_arc_key, member_size(usg_arc_t, v))
#define usg_arc_mm_key(p) ((p).v)
KRADIX_SORT_INIT(usg_arc_mm_srt, usg_arc_mm_t, usg_arc_mm_key, member_size(usg_arc_mm_t, v))
typedef struct {
uint32_t *a;
size_t n, m;
} mmap_t;
typedef struct {
usg_seq_t *a;
size_t n, m;
kvec_t(mmap_t) mp;
} usg_t;
#define usg_arc_a(g, v) ((g)->a[(v)>>1].arc[(v)&1].a)
#define usg_arc_n(g, v) ((g)->a[(v)>>1].arc[(v)&1].n)
typedef struct{
int64_t tipsLen;
float tip_drop_ratio;
int64_t stops_threshold;
float chimeric_rate;
float drop_ratio;
bub_label_t* b_mask_t;
int64_t clean_round;
double min_ovlp_drop_ratio;
double max_ovlp_drop_ratio;
double hom_check_drop_rate;
double min_path_drop_ratio;
double max_path_drop_ratio;
int64_t max_tip, max_tip_hifi;
uint32_t is_trio;
}ulg_opt_t;
typedef struct {
uint32_t hid;
uint32_t qs, qe, ts, te;
uint32_t qs_k, qe_k, ts_k, te_k;
uint8_t is_rev:6, is_del:1, is_ct:1;
} ul2ul_t;
typedef struct {
ul2ul_t *a;
size_t n, m;
uint32_t id:31, is_del:1;
uint32_t cn;
// uint8_t is_consist;
} ul2ul_item_t;
typedef struct {
uint32_t v, s, e, n;
} uinfo_srt_t;
typedef struct {
size_t n, m;
uinfo_srt_t *a;
} uinfo_srt_warp_t;
typedef struct {
uint32_t *uc, *hc, *raw_uc;
uinfo_srt_warp_t *iug_a;
uint32_t *iug_idx;
uint64_t *iug_b;
} ul_cov_t;
typedef struct {
asg_t *bg;
uint32_t *w_n, *a_n;
} ul_bg_t;
// typedef struct {
// uint32_t v, n, wn;
// } ul_tra_t;
// typedef struct {
// kvec_t(ul_tra_t) arc;
// kvec_t(uint32_t) idx;
// } ul_tra_idx_t;
// #define iug_tra_arc_n(z, v) ((z)->idx.a[(v)+1]-(z)->idx.a[(v)])
// #define iug_tra_arc_a(z, v) ((z)->arc.a + (z)->idx.a[(v)])
typedef struct {
ul2ul_item_t *a;
size_t n, m;
uint64_t uln, gn, tot;
uint32_t *item_idx;
asg_t *i_g; ma_ug_t *i_ug;
ma_ug_t *hybrid_ug;
usg_t *h_usg;
ul_cov_t cc;
ul_bg_t bg;
asg64_v *iug_tra;
uinfo_srt_warp_t *iug_seq; uint64_t iug_cov_thre;
uint8_t *telo;
} ul2ul_idx_t;
#define ul2ul_srt_key(p) ((p).hid)
KRADIX_SORT_INIT(ul2ul_srt, ul2ul_t, ul2ul_srt_key, member_size(ul2ul_t, hid))
typedef struct {
kvec_t(uint64_t) ref;
kvec_t(uint64_t) pat;
kvec_t(uint64_t) pat_cor;
kvec_t(uint8_t) g_flt;
kvec_t(uint8_t) m_dir;
kvec_t(int64_t) m_score;
uint64_t n, m;
} path_dp_t;
typedef struct {
uint32_t p; // the optimal parent vertex
uint32_t d; // the shortest distance from the initial vertex
uint64_t c; // max count of positive reads
uint64_t m; // max count of negative reads
// uint32_t np; // max count of non-positive reads
uint32_t nc; // max count of reads, no matter positive or negative
uint32_t r:31, s:1; // r: the number of remaining incoming arc; s: state
//s: state, s=0, this edge has not been visited, otherwise, s=1
} uinfo_t;
// #define uinfo_srt_t_c_key(p) ((p).se)
// KRADIX_SORT_INIT(uinfo_srt_t_c, uinfo_srt_t, uinfo_srt_t_c_key, member_size(uinfo_srt_t, se))
typedef struct {
///all information for each node
kvec_t(uinfo_t) a;
// kvec_t(uint32_t) u;
kvec_t(uint32_t) S; // set of vertices without parents, nodes with all incoming edges visited
kvec_t(uint32_t) T; // set of tips
kvec_t(uint32_t) b; // visited vertices
kvec_t(uint32_t) e; // visited edges/arcs
// kvec_t(uinfo_srt_t) srt;
kvec_t(uint8_t) us;
path_dp_t dp;
} ubuf_t;
typedef struct{
uint32_t bid, beg, occ;
uint32_t n_path, path_idx, path_occ;
}ul_sub_path_t;
typedef struct{
ma_ug_t *buf_ug;
kvec_t(uint64_t) buf;
}ul_path_t;
typedef struct{
kvec_t(uint64_t) idx;
kvec_t(uint64_t) srt;
uint64_t ul_n;
}ul_path_srt_t;
typedef struct{
size_t n, m;
uint64_t *a;
uint32_t cn:31, is_cir:1;
}ul_str_t;
typedef struct{
kvec_t(uint64_t) idx;
kvec_t(uint64_t) occ;
kvec_t(ul_str_t) str;
}ul_str_idx_t;
typedef struct{
uint32_t v, pi, ai;
uint32_t k:31, is_gc:1;
int32_t dis;
} integer_seq_t;
typedef struct{
size_t n, m;
integer_seq_t *a;
} kv_integer_seq_t;
typedef struct{
uint32_t tk, vq, sc;
uint64_t tn_rev_qk;
} integer_aln_t;
#define integer_aln_t_vqk_key(x) ((x).tn_rev_qk)
KRADIX_SORT_INIT(integer_aln_t_srt, integer_aln_t, integer_aln_t_vqk_key, member_size(integer_aln_t, tn_rev_qk))
typedef struct {
size_t n, m;
integer_aln_t *a;
} integer_aln_vec_t;
typedef struct{
uint32_t s, e, v;
uint64_t sc;
uint32_t q_sidx, q_eidx;
uint32_t t_sidx, t_eidx;
} ul_chain_t;
typedef struct{
uint64_t qidx_occ;
uint32_t tidx_occ;
uint32_t chain_id:31, is_rev:1;
} ul_snp_t;
#define ul_snp_t_srt_key(x) ((x).qidx_occ)
KRADIX_SORT_INIT(ul_snp_t_srt, ul_snp_t, ul_snp_t_srt_key, member_size(ul_snp_t, qidx_occ))
typedef struct {
uint32_t occ, nid;
} poa_nid_t;
typedef struct {
uint64_t ul;
uint32_t v;
} poa_arc_t;
#define poa_arc_key(a) ((a).ul)
KRADIX_SORT_INIT(poa_arc_srt, poa_arc_t, poa_arc_key, member_size(poa_arc_t, ul))
typedef struct {
kvec_t(uint32_t) ind;
kvec_t(uint32_t) stack;
kvec_t(uint32_t) res;
kvec_t(uint32_t) res2nid;
kvec_t(uint64_t) aln;
} topo_srt_t;
typedef struct {
kvec_t(int64_t) sc;
kvec_t(uint8_t) dir;
kvec_t(uint64_t) prefix;
uint64_t n, m;
} poa_dp_t;
#define poa_dp_idx(dp, x, y) ((dp).m*(x)+(y))
#define e_pdp 0
#define ue_pdp 1
#define lstr_dp 2
#define lg_dp 3
typedef struct{
uint64_t pge, ule;
uint32_t ulid;
} emap_t;
#define emap_t_srt_key(x) ((x).pge)
KRADIX_SORT_INIT(emap_t_srt, emap_t, emap_t_srt_key, member_size(emap_t, pge))
typedef struct {
kvec_t(poa_nid_t) seq;
kvec_t(poa_arc_t) arc;
kvec_t(uint64_t) idx;
uint32_t update_seq;
uint32_t update_arc;
topo_srt_t srt_b;
// poa_dp_t dp;
kvec_t(emap_t) e_idx;
ubuf_t bb;
} poa_g_t;
typedef struct {
kv_integer_seq_t q;
kv_integer_seq_t t;
integer_aln_vec_t b;
kvec_t(int64_t) f;
kvec_t(int64_t) p;
kvec_t(uint64_t) o;
kvec_t(uint64_t) u;
kvec_t(uint32_t) vis;
// kvec_t(uint64_t) srt;
// kvec_t(uint64_t) v;
// kvec_t(uint64_t) u;
// kvec_t(uint64_t) d;
kvec_t(ul_chain_t) sc;
kvec_t(ul_snp_t) snp;
poa_g_t pg;
kvec_t(uint64_t) res_dump;
uint64_t n_correct, n_circle;
}integer_t;
typedef struct {
// ul_resolve_t *u;
integer_t *buf;
uint64_t n_thread;
}integer_ml_t;
typedef struct {
asg_t *g;
uint64_t n[2], tot;
} cul_g_t;
typedef struct{
ug_opt_t *uopt;
ma_ug_t *init_ug;
ma_ug_t *l0_ug;
ma_ug_t *l1_ug;
asg_t *sg;
bubble_type *bub;
all_ul_t *idx;
ul_path_t path;
uint8_t *r_het;
ubuf_t buf;
ul_str_idx_t pstr;
integer_ml_t str_b;
cul_g_t *cg;
ul2ul_idx_t uovl;
// ul_path_srt_t psrt;
}ul_resolve_t;
typedef struct{
ug_opt_t *uopt;
asg_t *sg;
ma_ug_t *ug;
buf_t b;
uint32_t *idx, *bid;
uint32_t idx_n, bid_n;
uint8_t is_ou;
uint8_t is_trio;
int64_t max_ext;
uint64_t tlen;
double len_rat;
double ou_rat;
int64_t min_ou;
}ug_clean_t;
void deep_graph_clean(ug_opt_t *uopt, asg_t *sg, uint8_t is_ou, uint8_t is_trio, int64_t max_ext,
double min_ovlp_drop_ratio, double max_ovlp_drop_ratio, double ou_rat, int64_t min_ou, int64_t clean_round, int64_t long_tip);
void init_integer_ml_t(integer_ml_t *x, ul_resolve_t *u, uint64_t n_thread)
{
memset(x, 0, sizeof((*x)));
x->n_thread = n_thread; ///x->u = u;
CALLOC(x->buf, n_thread);
}
int32_t if_sup_chimeric(ma_hit_t_alloc* src, uint64_t rLen, asg64_v *b, int if_exact);
void print_edge(asg_arc_t *t, const char *cmd)
{
uint32_t v = t->ul>>32, w = t->v;
fprintf(stderr, "%s: v->%u(%c)[%u], w->%u(%c)[%u], el->%u, del->%u\n", cmd, v>>1, "+-"[v&1], v, w>>1, "+-"[w&1], w, t->el, t->del);
}
void stats_chimeric(asg_t *g, ma_hit_t_alloc* src, asg64_v *in)
{
asg64_v tx = {0,0,0}, *b = NULL;
uint32_t v, s[2] = {0};
if(in) b = in;
else b = &tx;
b->n = 0;
for (v = 0; v < g->n_seq; ++v) {
if (g->seq[v].del) continue;
s[if_sup_chimeric(&(src[v]), g->seq[v].len, b, 1)]++;
}
fprintf(stderr, "[M::%s::] ==> # non-chimeric:%u, # chimeric:%u\n", __func__, s[0], s[1]);
if(!in) free(tx.a);
}
static void stats_sysm_worker(void *_data, long eid, int tid)
{
asg_t *g = (asg_t*)_data;
asg_arc_t *p = &(g->arc[eid]);
if(p->del) return;
uint32_t k, v = p->v^1, w = (p->ul>>32)^1, nv; asg_arc_t *av;
av = asg_arc_a(g, v); nv = asg_arc_n(g, v);
for (k = 0; k < nv; k++) {
if (av[k].del || av[k].v!=w) continue;
break;
}
assert(k < nv);
v = p->ul>>32; w = p->v;
av = asg_arc_a(g, v); nv = asg_arc_n(g, v);
for (k = 0; k < nv; k++) {
if (av[k].del || av[k].v!=w) continue;
assert((uint32_t)eid == av-g->arc+k);
}
}
void stats_sysm(asg_t *g) {
kt_for(asm_opt.thread_num, stats_sysm_worker, g, g->n_arc);
fprintf(stderr, "[M::%s::]", __func__);
}
uint32_t get_arcs(asg_t *g, uint32_t v, uint32_t* idx, uint32_t idx_n)
{
uint32_t i, kv = 0, an = asg_arc_n(g, v), beg = g->idx[v]>>32;
for (i = 0, kv = 0; i < an; i++) {
if(g->arc[beg+i].del) continue;
if(idx && kv<idx_n) idx[kv] = beg+i;
kv++;
}
return kv;
}
#define flex_arcs0(res, fg, id) ((res) = ((!((id)&(((uint32_t)(0x80000000)))))?(&((fg).g->arc[(id)])):(&((fg).a[(id)-(((uint32_t)(0x80000000)))]))));
uint32_t get_flex_arcs(flex_asg_t *fg, uint32_t v, uint32_t* idx, uint32_t idx_n)
{
asg_t *g = fg->g; uint32_t i, kv = 0;
uint32_t an = asg_arc_n(g, v), beg = g->idx[v]>>32;
for (i = 0, kv = 0; i < an; i++) {
if(g->arc[beg+i].del) continue;
if(idx && kv<idx_n) idx[kv] = beg+i;
kv++;
}
for(i = fg->idx[v]; i != ((uint32_t)-1); i = fg->pi.a[i]) {
if(fg->a[i].del) continue;
if(idx && kv<idx_n) idx[kv] = (i)|((uint32_t)(0x80000000));
kv++;
}
return kv;
}
uint32_t follow_limit_path(asg_t *g, uint32_t s, uint32_t *e, uint32_t *occ, asg64_v *b, uint32_t lim)
{
uint32_t v = s, w = 0;
uint32_t kv, kw;
(*occ) = 0;
while (1) {
(*occ)++;
kv = get_arcs(g, v, &w, 1);
(*e) = v;
///if(b) kv_push(uint32_t, b->b, v>>1);
if(b) kv_push(uint64_t, *b, v);
if(kv == 0) return END_TIPS;
if(kv == 2) return TWO_OUTPUT;
if(kv > 2) return MUL_OUTPUT;
if((*occ) > lim) return LONG_TIPS;
w = g->arc[w].v;
///up to here, kv=1
///kw must >= 1
kw = get_arcs(g, w^1, NULL, 0);
v = w;
if(kw == 2) return TWO_INPUT;
if(kw > 2) return MUL_INPUT;
if(v == s) return LOOP;
}
return LONG_TIPS;
}
static inline int asg_end(const asg_t *g, uint32_t v, uint64_t *lw, uint32_t *ou)
{
///v^1 is the another direction of v
uint32_t w, nv, nw, nw0, nv0 = asg_arc_n(g, v^1);
int i, i0 = -1;
asg_arc_t *aw, *av = asg_arc_a(g, v^1);
///if this arc has not been deleted
for (i = nv = 0; i < (int)nv0; ++i)
if (!av[i].del) i0 = i, ++nv;
///end without any out-degree
if (nv == 0) return ASG_ET_TIP; // tip
if (nv > 1) return ASG_ET_MULTI_OUT; // multiple outgoing arcs
///until here, nv == 1
if (lw) *lw = av[i0].ul<<32 | av[i0].v;
if (ou) *ou = av[i0].ou;
w = av[i0].v ^ 1;
nw0 = asg_arc_n(g, w);
aw = asg_arc_a(g, w);
for (i = nw = 0; i < (int)nw0; ++i)
if (!aw[i].del) ++nw;
if (nw != 1) return ASG_ET_MULTI_NEI;
return ASG_ET_MERGEABLE;
}
uint32_t asg_arc_cut_tips(asg_t *g, uint32_t max_ext, asg64_v *in, uint32_t is_ou, R_to_U *ru, telo_end_t *te)
{
asg64_v tx = {0,0,0}, *b = NULL;
uint32_t n_vtx = g->n_seq<<1, v, w, i, k, cnt = 0, nv, kv, pb, ou, mm_ou, rr, is_u, is_telo;
asg_arc_t *av = NULL; uint64_t lw;
if(in) b = in;
else b = &tx;
b->n = 0;
for (v = 0; v < n_vtx; ++v) {
if (g->seq[v>>1].del) continue;
if(te && te->hh[v>>1]) continue;
av = asg_arc_a(g, v^1); nv = asg_arc_n(g, v^1);
for (i = kv = 0; i < nv; i++) {
if (av[i].del) continue;
kv++; break;
}
if(kv) continue;
kv = 1; mm_ou = (uint32_t)-1; ou = 0; is_telo = 0;
if(te && te->hh[v>>1]) is_telo = 1;
for (i = 0, w = v; i < max_ext; i++) {
if(asg_end(g, w^1, &lw, is_ou?&ou:NULL)!=0) break;
w = (uint32_t)lw; kv++; mm_ou = MIN(mm_ou, ou);
if(te && te->hh[w>>1]) is_telo = 1;
}
if(mm_ou == (uint32_t)-1) mm_ou = 0;
kv += mm_ou; i += mm_ou;
if((i < max_ext/** + (!!is_ou)**/) && (!is_telo)) kv_push(uint64_t, *b, (((uint64_t)kv)<<32)|v);
}
radix_sort_srt64(b->a, b->a + b->n);
for (k = 0; k < b->n; k++) {
v = (uint32_t)(b->a[k]);
if (g->seq[v>>1].del) continue;
if(te && te->hh[v>>1]) continue;
av = asg_arc_a(g, v^1); nv = asg_arc_n(g, v^1);
for (i = kv = 0; i < nv; i++) {
if (av[i].del) continue;
kv++; break;
}
if(kv) continue;
pb = b->n; kv_push(uint64_t, *b, v); mm_ou = (uint32_t)-1; ou = 0; is_telo = 0;
if(te && te->hh[v>>1]) is_telo = 1;
for (i = 0, w = v; i < max_ext; i++) {
if(asg_end(g, w^1, &lw, is_ou?&ou:NULL)!=0) break;
w = (uint32_t)lw; kv_push(uint64_t, *b, lw); mm_ou = MIN(mm_ou, ou);
if(te && te->hh[w>>1]) is_telo = 1;
}
if(mm_ou == (uint32_t)-1) mm_ou = 0;
i += mm_ou;
if((i < max_ext/** + (!!is_ou)**/) && (!is_telo)) {
for (i = pb; i < b->n; i++) asg_seq_del(g, ((uint32_t)b->a[i])>>1);
cnt++;
}
b->n = pb;
}
if(ru && is_ou) {
for (v = b->n = 0; v < n_vtx; ++v) {
if (g->seq[v>>1].del) continue;
av = asg_arc_a(g, v^1); nv = asg_arc_n(g, v^1);
for (i = kv = 0; i < nv; i++) {
if (av[i].del) continue;
kv++; break;
}
if(kv) continue;
get_R_to_U(ru, v>>1, &rr, &is_u);
if(rr == (uint32_t)-1 || is_u == 1) continue;
if(te && te->hh[v>>1]) continue;
kv_push(uint64_t, *b, v);
for (i = 0, w = v; i < max_ext; i++) {
if(asg_end(g, w^1, &lw, NULL)!=0) break;
w = (uint32_t)lw;
get_R_to_U(ru, w>>1, &rr, &is_u);
if(rr == (uint32_t)-1 || is_u == 1) break;
if(te && te->hh[w>>1]) break;
kv_push(uint64_t, *b, lw);
}
for (i = 0; i < b->n; i++) {
asg_seq_del(g, ((uint32_t)b->a[i])>>1);
}
if(b->n) cnt++;
}
}
/**
for (v = 0; v < n_vtx; ++v) {
if (g->seq[v>>1].del) continue;
av = asg_arc_a(g, v^1); nv = asg_arc_n(g, v^1);
for (i = kv = 0; i < nv; i++) {
if (av[i].del) continue;
kv++;
}
if(kv) continue;
pb = b->n; kv_push(uint64_t, *b, v);
for (i = 0, w = v; i < max_ext; i++) {
if(asg_is_utg_end(g, w^1, &lw)!=0) break;
w = (uint32_t)lw; kv_push(uint64_t, *b, lw);
}
if(i < max_ext) {
for (i = pb; i < b->n; i++) asg_seq_del(g, ((uint32_t)b->a[i])>>1);
cnt++;
}
b->n = pb;
}
**/
// stats_sysm(g);
if(!in) free(tx.a);
if (cnt > 0) asg_cleanup(g);
return cnt;
}
static void update_sg_contain(void *data, long i, int tid)
{
sset_aux *sl = (sset_aux *)data; ma_hit_t *h, *z; asg_arc_t t;
ma_hit_t_alloc *src = sl->src; asg_t *g = sl->g; int64_t r, idx;
R_to_U *ridx = sl->ruIndex; uint32_t k, rr, qn, tn, is_u;
g->seq_vis[i] = 0;
if(!(g->seq[i].del)) return;
get_R_to_U(ridx, i, &rr, &is_u);
if(rr == (uint32_t)-1 || is_u == 1) return;
ma_hit_t_alloc *x = &(src[i]);
for (k = 0; k < x->length; k++) {
h = &(x->buffer[k]);
qn = Get_qn((*h)); tn = Get_tn((*h));
if(h->bl < sl->ul_occ) continue;
if(g->seq[tn].del) {
get_R_to_U(ridx, tn, &rr, &is_u);
if(rr == (uint32_t)-1 || is_u == 1) continue;
}
r = ma_hit2arc(h, g->seq[qn].len, g->seq[tn].len, sl->max_hang, asm_opt.max_hang_rate, sl->min_ovlp, &t);
if(r < 0) continue;
idx = get_specific_overlap(&(src[tn]), tn, qn);
z = &(src[tn].buffer[idx]);
assert(z->bl == h->bl);
r = ma_hit2arc(z, g->seq[tn].len, g->seq[qn].len, sl->max_hang, asm_opt.max_hang_rate, sl->min_ovlp, &t);
if(r < 0) continue;
h->del = 0; if(!(g->seq[tn].del)) z->del = 0;
g->seq_vis[i] = 1;
}
}
void recover_contain_g(asg_t *g, ma_hit_t_alloc *src, R_to_U* ruIndex, int64_t max_hang, int64_t min_ovlp, int64_t ul_occ)
{
sset_aux s; s.g = g; s.src = src; s.ruIndex = ruIndex;
s.max_hang = max_hang; s.min_ovlp = min_ovlp; s.ul_occ = ul_occ;
kt_for(asm_opt.thread_num, update_sg_contain, &s, g->n_seq);
uint32_t k;
for (k = 0; k < g->n_seq; k++) {
if(g->seq_vis[k]) g->seq[k].del = 0;
}
memset(g->seq_vis, 0, (sizeof(*(g->seq_vis))*(g->n_seq<<1)));
}
// static void update_norm_arc(void *data, long i, int tid)
// {
// sset_aux *sl = (sset_aux *)data; ma_hit_t *h, *z; asg_arc_t t;
// ma_hit_t_alloc *src = sl->src; int64_t r, idx;
// uint32_t k, rr, qn, tn, is_u;
// ma_hit_t_alloc *x = &(src[i]);
// for (k = 0; k < x->length; k++) {
// h = &(x->buffer[k]);
// qn = Get_qn((*h)); tn = Get_tn((*h));
// if(h->bl < sl->ul_occ) continue;
// if(g->seq[tn].del) {
// get_R_to_U(ridx, tn, &rr, &is_u);
// if(rr == (uint32_t)-1 || is_u == 1) continue;
// }
// r = ma_hit2arc(h, g->seq[qn].len, g->seq[tn].len, sl->max_hang, asm_opt.max_hang_rate, sl->min_ovlp, &t);
// if(r < 0) continue;
// idx = get_specific_overlap(&(src[tn]), tn, qn);
// z = &(src[tn].buffer[idx]);
// assert(z->bl == h->bl);
// r = ma_hit2arc(z, g->seq[tn].len, g->seq[qn].len, sl->max_hang, asm_opt.max_hang_rate, sl->min_ovlp, &t);
// if(r < 0) continue;
// h->del = 0; if(!(g->seq[tn].del)) z->del = 0;
// g->seq_vis[i] = 1;
// }
// }
// void normalize_ma_hit_t_mul(ma_hit_t_alloc *src, uint32_t n_src)
// {
// sset_aux s; s.src = src;
// kt_for(asm_opt.thread_num, update_norm_arc, &s, n_src);
// }
static void normalize_gou0(void *data, long i, int tid)
{
sset_aux *sl = (sset_aux *)data;
asg_t *g = sl->g;
asg_arc_t *e = &(g->arc[i]);
if(e->v > (e->ul>>32)) return;
uint32_t k, v = e->v^1, w = (e->ul>>32)^1, ou;
uint32_t nv = asg_arc_n(g, v);
asg_arc_t *av = asg_arc_a(g, v);
for (k = 0; k < nv; ++k) {
if (av[k].v == w) {
ou = MAX(av[k].ou, e->ou);
av[k].ou = e->ou = ou;
break;
}
}
}
void normalize_gou(asg_t *g)
{
sset_aux s; s.g = g;
kt_for(asm_opt.thread_num, normalize_gou0, &s, g->n_arc);
}
static void update_sg_uo_t(void *data, long i, int tid)
{
sset_aux *sl = (sset_aux *)data;
ma_hit_t_alloc *src = sl->src; asg_t *g = sl->g;
asg_arc_t *e = &(g->arc[i]); uint32_t k, qn, tn;
ma_hit_t_alloc *x = &(src[e->ul>>33]);
e->ou = 0;
if(e->del) return;
for (k = 0; k < x->length; k++) {
qn = Get_qn(x->buffer[k]);
tn = Get_tn(x->buffer[k]);
if(qn == (e->ul>>33) && tn == (e->v>>1)) {
e->ou = (x->buffer[k].bl>OU_MASK?OU_MASK:x->buffer[k].bl);
break;
}
}
assert(k < x->length);
}
void update_sg_uo(asg_t *g, ma_hit_t_alloc *src)
{
sset_aux s; s.g = g; s.src = src;
kt_for(asm_opt.thread_num, update_sg_uo_t, &s, g->n_arc);
uint32_t k, z, nv, occ_a = 0, occ_n = 0; asg_arc_t *av = NULL;
for (k = 0; k < g->n_seq; k++) {
if(g->seq[k].del) continue;
occ_n++;
av = asg_arc_a(g, (k<<1)); nv = asg_arc_n(g, (k<<1));
for (z = 0; z < nv; z++) {
if(av[z].del || av[z].ou == 0) continue;
break;
}
if(z < nv) {
occ_a++;
continue;
}
av = asg_arc_a(g, ((k<<1)+1)); nv = asg_arc_n(g, ((k<<1)+1));
for (z = 0; z < nv; z++) {
if(av[z].del || av[z].ou == 0) continue;
break;
}
if(z < nv) {
occ_a++;
}
}
fprintf(stderr, "[M::%s::] ==> # gfa reads:%u, # covered gfa reads:%u\n", __func__, occ_n, occ_a);
// asg_arc_t *e; uint32_t v, w;
// for (k = 0; k < g->n_arc; k++) {
// e = &(g->arc[k]);
// v = e->v^1; w = (e->ul>>32)^1;
// av = asg_arc_a(g, v); nv = asg_arc_n(g, v);
// for (z = 0; z < nv; z++) {
// if(av[z].v == w) break;
// }
// if(z >= nv || av[z].ou != e->ou) fprintf(stderr, "[M::%s::asymmetry]\n", __func__);
// }
}
int32_t if_sup_chimeric(ma_hit_t_alloc* src, uint64_t rLen, asg64_v *b, int if_exact)
{
uint32_t k, qs, qe, l[2], r[2], st, bn;
int32_t dp, op;
l[0] = r[0] = rLen; l[1] = r[1] = 0;
for (k = 0; k < src->length; k++){
if(src->buffer[k].del) continue;
if(if_exact && !(src->buffer[k].el)) continue;
qs = Get_qs(src->buffer[k]); qe = Get_qe(src->buffer[k]);
///overlaps from left side
if(qs == 0){
if(qs < l[0]) l[0] = qs;
if(qe > l[1]) l[1] = qe;
}
///overlaps from right side
if(qe == rLen){
if(qs < r[0]) r[0] = qs;
if(qe > r[1]) r[1] = qe;
}
///note: if (qs == 0 && qe == rLen)
///this overlap would be added to both b_left and b_right
///that is what we want
}
if (l[1] > r[0]) return 0;
if (l[1] <= l[0] || r[1] <= r[0]) return 1;
bn = b->n;
if(l[1] > l[0]) {
kv_push(uint64_t, *b, (l[0]<<1)); kv_push(uint64_t, *b, (l[1]<<1)|1);
}
if(r[1] > r[0]) {
kv_push(uint64_t, *b, (r[0]<<1)); kv_push(uint64_t, *b, (r[1]<<1)|1);
}
///check contained overlaps
for (k = 0; k < src->length; k++) {
if(src->buffer[k].del) continue;
if(if_exact && !(src->buffer[k].el)) continue;
qs = Get_qs(src->buffer[k]); qe = Get_qe(src->buffer[k]);
if(qs == 0 || qe == rLen) continue;
kv_push(uint64_t, *b, (qs<<1)); kv_push(uint64_t, *b, (qe<<1)|1);
}
radix_sort_srt64(b->a + bn, b->a + b->n);
l[0] = r[0] = rLen; l[1] = r[1] = 0;
for (k = bn, dp = st = 0; k < b->n; k++) {
op = dp;
///if a[j] is qe
if (b->a[k]&1) --dp;
else ++dp;
if(op < 1 && dp >= 1) {
st = b->a[k]>>1;
} else if(op >= 1 && dp < 1) {
if(st == 0) l[0] = st, l[1] = b->a[k]>>1;
if((b->a[k]>>1) == rLen) r[0] = st, r[1] = b->a[k]>>1;
}
}
b->n = bn;
if (l[1] > r[0]) return 0;
return 1;
}
///remove single node
void asg_arc_cut_chimeric(asg_t *g, ma_hit_t_alloc* src, asg64_v *in, uint32_t ou_thres, telo_end_t *te)
{
asg64_v tx = {0,0,0}, *b = NULL;
uint32_t v, w, ei[2] = {0}, k, i, n_vtx = g->n_seq<<1;
uint32_t nw, el_n, cnt = 0; asg_arc_t *aw;
if(in) b = in;
else b = &tx;
b->n = 0;
for (v = 0; v < n_vtx; ++v) {
if (g->seq[v>>1].del) continue;
if (te && te->hh[v>>1]) continue;
if(g->seq_vis[v] == 0) {
if((get_arcs(g, v, &(ei[0]), 1)!=1) || (get_arcs(g, v^1, &(ei[1]), 1)!=1)) continue;
assert((g->arc[ei[0]].ul>>32) == v && (g->arc[ei[1]].ul>>32) == (v^1));
if((get_arcs(g, g->arc[ei[0]].v^1, NULL, 0)<2) || (get_arcs(g, g->arc[ei[1]].v^1, NULL, 0)<2)) continue;
if(g->arc[ei[0]].el) continue;
if(ou_thres!=(uint32_t)-1&&g->arc[ei[0]].ou>=ou_thres&&g->arc[ei[1]].ou>=ou_thres) continue;///UL
if(!if_sup_chimeric(&(src[v>>1]), g->seq[v>>1].len, b, 1)) continue;///HiFi
kv_push(uint64_t, *b, (((uint64_t)(g->arc[ei[0]].ol))<<32)|((uint64_t)(ei[0])));
}
}
radix_sort_srt64(b->a, b->a + b->n);
///here all edges are inexact matches
for (k = 0; k < b->n; k++) {
if(g->arc[(uint32_t)b->a[k]].del) continue;
v = g->arc[(uint32_t)b->a[k]].ul>>32; w = g->arc[(uint32_t)b->a[k]].v^1;
if(g->seq[v>>1].del || g->seq[w>>1].del) continue;
aw = asg_arc_a(g, w); nw = asg_arc_n(g, w);
if((get_arcs(g, v, &(ei[0]), 1)!=1) || (get_arcs(g, v^1, &(ei[1]), 1)!=1)) continue;
if((get_arcs(g, g->arc[ei[0]].v^1, NULL, 0)<2) || (get_arcs(g, g->arc[ei[1]].v^1, NULL, 0)<2)) continue;
for (i = el_n = 0; i < nw; i++) {
if ((aw[i].del) || (aw[i].v==(v^1)) || (!aw[i].el)) continue;
el_n++; break;
}
if(!el_n) continue;
if(te && te->hh[v>>1]) continue;
asg_seq_del(g, v>>1);
cnt++;
}
// stats_sysm(g);
if(!in) free(tx.a);
if (cnt > 0) asg_cleanup(g);
}
void asg_arc_cut_inexact(asg_t *g, ma_hit_t_alloc* src, asg64_v *in, int32_t max_ext, uint32_t is_ou, uint32_t is_trio, uint32_t min_diff, float ou_rat/**, asg64_v *dbg**/)
{
asg64_v tx = {0,0,0}, *b = NULL;
uint32_t v, w, i, k, n_vtx = g->n_seq<<1;
asg_arc_t *av, *aw, *ve, *vmax, *we; uint32_t nv, nw, kv, kw, ol_max, ou_max, to_del, cnt = 0, mm_ol, mm_ou;
uint32_t trioF = (uint32_t)-1, ntrioF = (uint32_t)-1;
if(in) b = in;
else b = &tx;
b->n = 0;
for (v = 0; v < n_vtx; ++v) {
if(g->seq[v>>1].del) continue;
if(g->seq_vis[v] == 0) {
av = asg_arc_a(g, v); nv = asg_arc_n(g, v);
if (nv < 2) continue;
for (i = kv = 0; i < nv; ++i) {
if(av[i].del) continue;
kv++;
}
if(kv < 2) continue;
for (i = 0; i < nv; ++i) {
if(av[i].del || av[i].el) continue;
kv_push(uint64_t, *b, (uint64_t)((((uint64_t)av[i].ol)<<32)|((uint64_t)(av-g->arc+i))));
}
}
}
radix_sort_srt64(b->a, b->a + b->n);
for (k = 0; k < b->n; k++) {
if(g->arc[(uint32_t)b->a[k]].del) continue;
assert((!g->arc[(uint32_t)b->a[k]].el));
v = g->arc[(uint32_t)b->a[k]].ul>>32; w = g->arc[(uint32_t)b->a[k]].v^1;
if(g->seq[v>>1].del || g->seq[w>>1].del) continue;