-
Notifications
You must be signed in to change notification settings - Fork 88
/
horder.cpp
4575 lines (4037 loc) · 148 KB
/
horder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define __STDC_LIMIT_MACROS
#include "float.h"
#include "horder.h"
#include <math.h>
#include "hic.h"
#include "htab.h"
#include "assert.h"
#include "Overlaps.h"
#include "Hash_Table.h"
#include "Correct.h"
#include "Purge_Dups.h"
#include "rcut.h"
#include "khashl.h"
#include "kthread.h"
#include "ksort.h"
#include "kseq.h" // FASTA/Q parser
#include "kdq.h"
#include "tovlp.h"
KSEQ_INIT(gzFile, gzread)
KDQ_INIT(uint64_t)
#define pe_hit_an1_idx_key(x) ((x).s<<1)
KRADIX_SORT_INIT(pe_hit_idx_hn1, pe_hit, pe_hit_an1_idx_key, member_size(pe_hit, s))
#define pe_hit_an2_idx_key(x) ((x).e<<1)
KRADIX_SORT_INIT(pe_hit_idx_hn2, pe_hit, pe_hit_an2_idx_key, member_size(pe_hit, e))
#define generic_key(x) (x)
KRADIX_SORT_INIT(ho64, uint64_t, generic_key, 8)
#define osg_arc_key(a) ((a).u)
KRADIX_SORT_INIT(osg, osg_arc_t, osg_arc_key, member_size(osg_arc_t, u))
#define OVL(s_0, e_0, s_1, e_1) ((MIN((e_0), (e_1)) > MAX((s_0), (s_1)))? MIN((e_0), (e_1)) - MAX((s_0), (s_1)):0)
#define BREAK_THRES 5000000
#define BREAK_CUTOFF 0.1
#define BREAK_BOUNDARY 0.015
void reduce_hamming_error_adv(ma_ug_t *iug, asg_t *sg, ma_hit_t_alloc* sources, ma_sub_t *coverage_cut,
int max_hang, int min_ovlp, long long gap_fuzz, R_to_U *ru, bubble_type* bub);
typedef struct {
uint64_t ruid;
uint64_t off;
} hit_aux_t;
typedef struct {
hit_aux_t *a;
size_t n, m;
kvec_t(uint64_t) idx;
} u_hits_t;
typedef struct {
uint64_t e, d;
double w;
} hw_aux_t;
typedef struct {
hw_aux_t *a;
size_t n, m;
} h_w_t;
#define hw_e_key(x) ((x).e)
KRADIX_SORT_INIT(hw_e, hw_aux_t, hw_e_key, member_size(hw_aux_t, e))
#define hw_d_key(x) ((x).d)
KRADIX_SORT_INIT(hw_d, hw_aux_t, hw_d_key, member_size(hw_aux_t, d))
#define hw_ew_key(x) ((uint32_t)((x).e))
KRADIX_SORT_INIT(hw_ew, hw_aux_t, hw_ew_key, member_size(hw_aux_t, e))
#define hw_dw_key(x) ((uint32_t)((x).d))
KRADIX_SORT_INIT(hw_dw, hw_aux_t, hw_dw_key, member_size(hw_aux_t, d))
typedef struct {
kvec_t(uint64_t) pos;
uint64_t *a;
size_t n, m;
} dens_idx_t;
typedef struct {
uint64_t s, e, dp;
} h_cov_t;
typedef struct {
h_cov_t *a;
size_t n, m;
} h_covs;
typedef struct {
uint32_t *a;
size_t n, m;
}lay_t;
typedef struct {
lay_t *a;
size_t n, m;
}sc_lay_t;
typedef struct {
uint64_t uid, sid;
uint64_t iid:63, ori:1;
} sc_id_t;
typedef struct {
sc_id_t *a;
size_t n, m;
sc_lay_t *sl;
osg_t *sg;
uint32_t n_thread;
} sc_mul;
#define h_cov_s_key(x) ((x).s)
KRADIX_SORT_INIT(h_cov_s, h_cov_t, h_cov_s_key, member_size(h_cov_t, s))
#define h_cov_e_key(x) ((x).e)
KRADIX_SORT_INIT(h_cov_e, h_cov_t, h_cov_e_key, member_size(h_cov_t, e))
#define h_cov_dp_key(x) ((x).dp)
KRADIX_SORT_INIT(h_cov_dp, h_cov_t, h_cov_dp_key, member_size(h_cov_t, dp))
#define hit_aux_ruid_key(x) ((x).ruid)
KRADIX_SORT_INIT(hit_aux_ruid, hit_aux_t, hit_aux_ruid_key, member_size(hit_aux_t, ruid))
typedef struct {
kv_u_trans_t *ref;
trans_chain* idx;
} trans_col_t;
typedef struct {
uint32_t Spre, Epre, Scur, Ecur, uCur, uPre;///[qSp, qEp) && [qSn, qEn]
} u_hit_t;
#define u_hit_t_key(x) ((x).uPre)
KRADIX_SORT_INIT(u_hit, u_hit_t, u_hit_t_key, member_size(u_hit_t, uPre))
typedef struct {
ma_ug_t *ug;
kvec_pe_hit hits;
kv_u_trans_t k_trans;
h_covs *b_points;
} debug_phasing_t;
debug_phasing_t *init_debug_phasing(ma_ug_t *ug, kvec_pe_hit *hits, kv_u_trans_t *k_trans, h_covs *b_points)
{
debug_phasing_t *p = NULL; CALLOC(p, 1);
p->ug = copy_untig_graph(ug);
p->b_points = b_points;
p->hits.pos_mode = hits->pos_mode; p->hits.uID_bits = hits->pos_mode;
p->hits.a.n = p->hits.a.m = hits->a.n = hits->a.m;
MALLOC(p->hits.a.a, p->hits.a.n);
memcpy(p->hits.a.a, hits->a.a, p->hits.a.n*sizeof(pe_hit));
p->k_trans.n = p->k_trans.m = k_trans->n;
MALLOC(p->k_trans.a, p->k_trans.n);
memcpy(p->k_trans.a, k_trans->a, p->k_trans.n*sizeof(u_trans_t));
p->k_trans.idx.n = p->k_trans.idx.m = k_trans->idx.n;
MALLOC(p->k_trans.idx.a, p->k_trans.idx.n);
memcpy(p->k_trans.idx.a, k_trans->idx.a, p->k_trans.idx.n*sizeof(uint64_t));
return p;
}
void destory_debug_phasing_t(debug_phasing_t **x)
{
ma_ug_destroy((*x)->ug);
free((*x)->hits.a.a); free((*x)->hits.idx.a); free((*x)->hits.occ.a);
free((*x)->k_trans.a); free((*x)->k_trans.idx.a);
free(*x);
}
uint64_t new_node(uint64_t v, h_covs *join, uint64_t *idx, uint64_t is_ul)
{
if(v&1) return (((uint32_t)join->a[(idx[v>>1]>>32)+(is_ul?0:(((uint32_t)idx[v>>1])-1))].dp)<<1)+1;
else return (((uint32_t)join->a[(idx[v>>1]>>32)+(is_ul?(((uint32_t)idx[v>>1])-1):0)].dp)<<1);
}
uint32_t iter_rid(buf_t *b, uint64_t *ui, uint64_t *ri, ma_ug_t *ug)
{
ma_utg_t *u = NULL;
while ((*ui) < b->b.n)
{
u = &(ug->u.a[b->b.a[(*ui)]>>1]);
while ((*ri) < u->n) return u->a[(*ri)++]>>32;
(*ui)++; (*ri) = 0;
}
return (uint32_t)-1;
}
void debug_debug_phasing_t(debug_phasing_t *x, ma_ug_t *cug, kvec_pe_hit *chits, kv_u_trans_t *ck_trans,
h_covs *join, uint64_t *idx)
{
uint64_t i, k, m, pv, cv, pui, pri, cui, cri, e;
uint32_t p_cvx, c_cvx, pr, cr, occ;
long long p_nodeLen, p_baseLen, t1, t2;
long long c_nodeLen, c_baseLen;
asg_arc_t *ap, *ac;
u_trans_t *pk, *ck, *p;
buf_t pb, cb;
memset(&pb, 0, sizeof(buf_t));
memset(&cb, 0, sizeof(buf_t));
uint32_t *cnt = NULL; CALLOC(cnt, cug->g->n_seq<<1);
for (cv = 0; cv < (uint64_t)(cug->g->n_seq<<1); ++cv) {
///out-nodes of v
ac = asg_arc_a(cug->g, cv);
///if v just have one out-node, there is no muti-edge
if (asg_arc_n(cug->g, cv) < 2) continue;
for (i = 0; i < asg_arc_n(cug->g, cv); ++i) ++cnt[ac[i].v];
for (i = 0; i < asg_arc_n(cug->g, cv); ++i)
if (--cnt[ac[i].v] != 0) fprintf(stderr, "ERROR-9\n");
}
free(cnt);
for (e = 0; e < cug->g->n_arc; ++e) {
uint32_t v = cug->g->arc[e].v^1, u = cug->g->arc[e].ul>>32^1;
asg_arc_t *av = asg_arc_a(cug->g, v);
for (i = 0; i < asg_arc_n(cug->g, v); ++i)
if (av[i].v == u) break;
if (i == asg_arc_n(cug->g, v)) fprintf(stderr, "ERROR-10\n");
}
for (i = 0; i < x->ug->g->n_seq; i++)
{
pv = (i<<1);
cv = new_node(pv, join, idx, 1);
if(asg_arc_n(x->ug->g, pv) != asg_arc_n(cug->g, cv)) fprintf(stderr, "ERROR-1\n");
ap = asg_arc_a(x->ug->g, pv); ac = asg_arc_a(cug->g, cv);
for (k = 0; k < asg_arc_n(x->ug->g, pv); k++)
{
for (m = 0; m < asg_arc_n(cug->g, cv); m++)
{
if(new_node(ap[k].v, join, idx, 0) == ac[m].v) break;
}
if(m >= asg_arc_n(cug->g, cv))
{
fprintf(stderr, "\n+ERROR-2\n");
fprintf(stderr, "+putg%.6lul (%lu), cutg%.6lul (%lu)\n", (pv>>1) + 1, pv&1, (cv>>1) + 1, cv&1);
fprintf(stderr, "+p-occ: %u, c-occ: %u\n", asg_arc_n(x->ug->g, pv), asg_arc_n(cug->g, cv));
fprintf(stderr, "+ap[k]-utg%.6ul (%u), new-utg%.6lul (%lu)\n", (ap[k].v>>1)+1, ap[k].v&1,
(new_node(ap[k].v, join, idx, 0)>>1)+1, new_node(ap[k].v, join, idx, 0)&1);
for (m = 0; m < asg_arc_n(cug->g, cv); m++)
{
fprintf(stderr, "+ac[%lu]-utg%.6ul (%u)\n", m, (ac[m].v>>1) + 1, ac[m].v&1);
}
}
}
pv = (i<<1) + 1;
cv = new_node(pv, join, idx, 1);
if(asg_arc_n(x->ug->g, pv) != asg_arc_n(cug->g, cv)) fprintf(stderr, "ERROR-1\n");
ap = asg_arc_a(x->ug->g, pv); ac = asg_arc_a(cug->g, cv);
for (k = 0; k < asg_arc_n(x->ug->g, pv); k++)
{
for (m = 0; m < asg_arc_n(cug->g, cv); m++)
{
if(new_node(ap[k].v, join, idx, 0) == ac[m].v) break;
}
if(m >= asg_arc_n(cug->g, cv))
{
fprintf(stderr, "\n-ERROR-2\n");
fprintf(stderr, "-putg%.6lul (%lu), cutg%.6lul (%lu)\n", (pv>>1) + 1, pv&1, (cv>>1) + 1, cv&1);
fprintf(stderr, "-p-occ: %u, c-occ: %u\n", asg_arc_n(x->ug->g, pv), asg_arc_n(cug->g, cv));
fprintf(stderr, "-ap[k]-utg%.6ul (%u), new-utg%.6lul (%lu)\n", (ap[k].v>>1)+1, ap[k].v&1,
(new_node(ap[k].v, join, idx, 0)>>1)+1, new_node(ap[k].v, join, idx, 0)&1);
for (m = 0; m < asg_arc_n(cug->g, cv); m++)
{
fprintf(stderr, "-ac[%lu]-utg%.6ul (%u)\n", m, (ac[m].v>>1) + 1, ac[m].v&1);
}
}
}
pb.b.n = cb.b.n = 0;
if(get_unitig(x->ug->g, NULL, i<<1, &p_cvx, &p_nodeLen, &p_baseLen, &t1, &t2, 1, &pb) !=
get_unitig(cug->g, NULL, new_node((i<<1)+1, join, idx, 1)^1, &c_cvx, &c_nodeLen, &c_baseLen, &t1, &t2, 1, &cb))
{
fprintf(stderr, "ERROR-3\n");
}
if(new_node(p_cvx, join, idx, 1) != c_cvx) fprintf(stderr, "ERROR-4\n");
if(p_baseLen != c_baseLen)
{
fprintf(stderr, "ERROR-6\n");
fprintf(stderr, "-putg%.6lul, pb.b.n: %u, p_nodeLen: %lld, p_baseLen: %lld, cb.b.n: %u, c_nodeLen: %lld, c_baseLen: %lld\n",
i+1, (uint32_t)pb.b.n, p_nodeLen, p_baseLen, (uint32_t)cb.b.n, c_nodeLen, c_baseLen);
}
pui = pri = cui = cri = 0;
while(1)
{
pr = iter_rid(&pb, &pui, &pri, x->ug);
cr = iter_rid(&cb, &cui, &cri, cug);
if(pr != cr) fprintf(stderr, "ERROR-7\n");
if(pr == (uint32_t)-1 || cr == (uint32_t)-1) break;
}
}
for (i = 0; i < x->k_trans.n; i++)
{
pk = &(x->k_trans.a[i]);
if(((uint32_t)idx[pk->qn]) <= 1 && ((uint32_t)idx[pk->tn]) <= 1)
{
get_u_trans_spec(ck_trans, ((uint32_t)join->a[idx[pk->qn]>>32].dp),
((uint32_t)join->a[idx[pk->tn]>>32].dp), &ck, &occ);
if(occ != 1 || !ck) fprintf(stderr, "ERROR-8\n");
if(pk->qs != ck->qs || pk->qe != ck->qe || pk->ts != ck->ts || pk->te != ck->te ||
pk->f != ck->f || pk->rev != ck->rev || pk->del != ck->del)
{
fprintf(stderr, "ERROR-9\n");
}
}
else
{
p = pk;
fprintf(stderr, "\n+q-utg%.6ul\tqs(%u)\tqe(%u)\tt-utg%.6ul\tts(%u)\tte(%u)\trev(%u)\tw(%f)\tf(%u)\n",
p->qn+1, p->qs, p->qe, p->tn+1, p->ts, p->te, p->rev, p->nw, p->f);
uint64_t qi, ti, qid = p->qn, tid = p->tn;
for (qi = 0; qi < (uint32_t)idx[qid]; qi++)
{
for (ti = 0; ti < (uint32_t)idx[tid]; ti++)
{
get_u_trans_spec(ck_trans, (uint32_t)(join->a[(idx[qid]>>32)+qi].dp),
(uint32_t)(join->a[(idx[tid]>>32)+ti].dp), &ck, &occ);
// fprintf(stderr, "s-utg%.6ul\td-utg%.6ul\tocc:%u\n",
// (uint32_t)(join->a[(idx[qid]>>32)+qi].dp)+1,
// (uint32_t)(join->a[(idx[tid]>>32)+ti].dp)+1,
// occ);
for (k = 0; k < occ; k++)
{
p = &(ck[k]);
fprintf(stderr, "-q-utg%.6ul\tqs(%u)\tqe(%u)\tt-utg%.6ul\tts(%u)\tte(%u)\trev(%u)\tw(%f)\tf(%u)\n",
p->qn+1, p->qs, p->qe, p->tn+1, p->ts, p->te, p->rev, p->nw, p->f);
}
}
}
// get_u_trans_spec(ck_trans, ((uint32_t)join->a[idx[pk->qn]>>32].dp),
// ((uint32_t)join->a[idx[pk->tn]>>32].dp), &ck, &occ);
// for (k = 0; k < occ; k++)
// {
// p = &(ck[k]);
// fprintf(stderr, "-q-utg%.6ul\tqs(%u)\tqe(%u)\tt-utg%.6ul\tts(%u)\tte(%u)\trev(%u)\tw(%f)\tf(%u)\n",
// p->qn+1, p->qs, p->qe, p->tn+1, p->ts, p->te, p->rev, p->nw, p->f);
// }
}
}
free(pb.b.a); free(cb.b.a);
}
void print_N50(ma_ug_t* ug)
{
kvec_t(uint64_t) b; kv_init(b);
uint64_t i, s, len;
for (i = s = 0; i < ug->u.n; ++i)
{
kv_push(uint64_t, b, ug->u.a[i].len);
s += ug->u.a[i].len;
}
len = s;
radix_sort_ho64(b.a, b.a+b.n);
fprintf(stderr, "[M::%s::] Genome Size: %lu, # Contigs: %u, Largest Contig: %lu\n",
__func__, len, (uint32_t)ug->u.n, b.a[b.n-1]);
i = b.n; s = 0;
while (i > 0)
{
i--;
s += b.a[i];
if(s >= (len>>1))
{
fprintf(stderr, "[M::%s::] N50: %lu\n", __func__, b.a[i]);
break;
}
}
kv_destroy(b);
}
void print_N50_layout(ma_ug_t* ug, sc_lay_t* sl)
{
kvec_t(uint64_t) b; kv_init(b);
lay_t *p = NULL;
uint64_t i, k, s, ulen, len, occ = 0;
for (i = s = 0; i < sl->n; ++i)
{
p = &(sl->a[i]);
for (k = ulen = 0; k < p->n; k+=2)
{
ulen += ug->u.a[p->a[k]>>1].len;
}
occ += p->n;
kv_push(uint64_t, b, ulen);
s += ulen;
}
len = s;
radix_sort_ho64(b.a, b.a+b.n);
fprintf(stderr, "[M::%s::] Scaffold Size: %lu, # Scaffolds: %u (occ-%lu), Largest Scaffold: %lu\n",
__func__, len, (uint32_t)sl->n, occ, b.a[b.n-1]);
i = b.n; s = 0;
while (i > 0)
{
i--;
s += b.a[i];
if(s >= (len>>1))
{
fprintf(stderr, "[M::%s::] N50: %lu\n", __func__, b.a[i]);
break;
}
}
kv_destroy(b);
}
trans_col_t *init_trans_col(ma_ug_t *ug, uint64_t r_num, kv_u_trans_t *ref)
{
trans_col_t *p = NULL;
CALLOC(p, 1);
p->ref = ref;
p->idx = init_trans_chain(ug, r_num);
return p;
}
void destory_trans_col(trans_col_t **p)
{
destory_trans_chain(&((*p)->idx));
free(*p);
}
void resolve_hit(uint64_t x, uint32_t rLen, uint64_t uID_bits, uint64_t pos_mode, uint64_t *uid, uint64_t *beg, uint64_t *end)
{
if(uid) (*uid) = ((x<<1)>>(64 - uID_bits));
uint32_t rev = (x>>63);
long long ref_p = x & pos_mode;
long long p_beg, p_end;
if(rev)
{
p_end = ref_p;
p_beg = p_end + 1 - rLen;
}
else
{
p_beg = ref_p;
p_end = p_beg + rLen - 1;
}
if(p_beg < 0) p_beg = 0;
if(p_end < 0) p_end = 0;
if(beg) (*beg) = p_beg;
if(end) (*end) = p_end + 1;
}
void idx_hits(kvec_pe_hit* hits, uint64_t n)
{
uint64_t k, l;
kv_resize(uint64_t, hits->idx, n);
hits->idx.n = n;
memset(hits->idx.a, 0, hits->idx.n*sizeof(uint64_t));
radix_sort_pe_hit_idx_hn1(hits->a.a, hits->a.a + hits->a.n);
for (k = 1, l = 0; k <= hits->a.n; ++k) {
if (k == hits->a.n || (get_hit_suid(*hits, k) != get_hit_suid(*hits, l))) {
if (k - l > 1) radix_sort_pe_hit_idx_hn2(hits->a.a + l, hits->a.a + k);
hits->idx.a[get_hit_suid(*hits, l)] = (uint64_t)l << 32 | (k - l);
l = k;
}
}
}
kvec_pe_hit *get_r_hits_for_trio(kvec_pe_hit *u_hits, asg_t* r_g, ma_ug_t* ug, bubble_type* bub, uint64_t uID_bits, uint64_t pos_mode)
{
kvec_pe_hit *r_hits = NULL;
CALLOC(r_hits, 1);
uint64_t k, l, i, r_i, offset, rid, rev, rBeg, rEnd, ubits, p_mode, upos, rpos, update, ubeg, uend, suid, euid;
ma_utg_t *u = NULL;
memset(r_hits, 0, sizeof(*r_hits));
r_hits->uID_bits = uID_bits;
r_hits->pos_mode = pos_mode;
//reset for reads
for (ubits=1; (uint64_t)(1<<ubits)<(uint64_t)r_g->n_seq; ubits++);
p_mode = ((uint64_t)-1) >> (ubits + 1);
u_hits->uID_bits = uID_bits; u_hits->pos_mode = pos_mode;
for (i = r_i = 0; i < u_hits->a.n; i++)
{
suid = get_hit_suid(*u_hits, i);
euid = get_hit_euid(*u_hits, i);
if(IF_HOM(suid, *bub)) continue;
if(IF_HOM(euid, *bub)) continue;
if(suid == euid) continue;
kv_push(pe_hit, r_hits->a, u_hits->a.a[i]);
resolve_hit(r_hits->a.a[r_i].s, r_hits->a.a[r_i].len>>32, r_hits->uID_bits,
r_hits->pos_mode, NULL, &ubeg, &uend);
upos = (ubeg+uend-1)>>1;
r_hits->a.a[r_i].s -= get_hit_spos(*r_hits, r_i);
r_hits->a.a[r_i].s += upos;
resolve_hit(r_hits->a.a[r_i].e, (uint32_t)r_hits->a.a[r_i].len, r_hits->uID_bits,
r_hits->pos_mode, NULL, &ubeg, &uend);
upos = (ubeg+uend-1)>>1;
r_hits->a.a[r_i].e -= get_hit_epos(*r_hits, r_i);///pos at unitig
r_hits->a.a[r_i].e += upos;
r_hits->a.a[r_i].id = (suid<<32)|euid;
r_i++;
}
radix_sort_pe_hit_idx_hn1(r_hits->a.a, r_hits->a.a + r_hits->a.n);
for (k = 1, l = 0; k <= r_hits->a.n; ++k)
{
if (k == r_hits->a.n || get_hit_suid(*r_hits, k) != get_hit_suid(*r_hits, l))//same suid
{
///already sort by spos
u = &(ug->u.a[get_hit_suid(*r_hits, l)]);
update = 0;
for (i = offset = 0, r_i = l; i < u->n; i++)
{
rid = u->a[i]>>33;
rBeg = offset;
rEnd = rBeg + r_g->seq[rid].len - 1;
for (; r_i < k; r_i++)
{
upos = get_hit_spos(*r_hits, r_i);///pos at unitig
if(upos > rEnd) break;
if(upos >= rBeg && upos <= rEnd)
{
rpos = (((u->a[i]>>32)&1)? rEnd - upos : upos - rBeg);///pos at read
rev = ((u->a[i]>>32)&1) ^ (r_hits->a.a[r_i].s>>63);
r_hits->a.a[r_i].s = (rev<<63) | ((rid << (64-ubits))>>1) | (rpos & p_mode);
update++;
}
}
offset += (uint32_t)u->a[i];
}
if(r_i != k || update != k - l) fprintf(stderr, "ERROR-r_i\n");
l = k;
}
}
radix_sort_pe_hit_idx_hn2(r_hits->a.a, r_hits->a.a + r_hits->a.n);
for (k = 1, l = 0; k <= r_hits->a.n; ++k)
{
if (k == r_hits->a.n || get_hit_euid(*r_hits, k) != get_hit_euid(*r_hits, l))//same euid
{
///already sort by epos
u = &(ug->u.a[get_hit_euid(*r_hits, l)]);
update = 0;
for (i = offset = 0, r_i = l; i < u->n; i++)
{
rid = u->a[i]>>33;
rBeg = offset;
rEnd = rBeg + r_g->seq[rid].len - 1;
for (; r_i < k; r_i++)
{
upos = get_hit_epos(*r_hits, r_i);///pos at unitig
if(upos > rEnd) break;
if(upos >= rBeg && upos <= rEnd)
{
rpos = (((u->a[i]>>32)&1)? rEnd - upos : upos - rBeg);///pos at read
rev = ((u->a[i]>>32)&1) ^ (r_hits->a.a[r_i].e>>63);
r_hits->a.a[r_i].e = (rev<<63) | ((rid << (64-ubits))>>1) | (rpos & p_mode);
update++;
}
}
offset += (uint32_t)u->a[i];
}
if(r_i != k || update != k - l) fprintf(stderr, "ERROR-r_i\n");
l = k;
}
}
r_hits->uID_bits = ubits;
r_hits->pos_mode = p_mode;
return r_hits;
}
void get_r_hits(kvec_pe_hit *u_hits, kvec_pe_hit *r_hits, asg_t* r_g, ma_ug_t* ug, bubble_type* bub, uint64_t uID_bits, uint64_t pos_mode)
{
uint64_t k, l, i, r_i, offset, rid, rev, rBeg, rEnd, ubits, p_mode, upos, rpos, update;
ma_utg_t *u = NULL;
memset(r_hits, 0, sizeof(*r_hits));
r_hits->uID_bits = uID_bits; r_hits->pos_mode = pos_mode;
//reset for reads
for (ubits=1; (uint64_t)(1<<ubits)<(uint64_t)r_g->n_seq; ubits++);
p_mode = ((uint64_t)-1) >> (ubits + 1);
kv_malloc(r_hits->a, u_hits->a.n); r_hits->a.n = r_hits->a.m = u_hits->a.n;
memcpy(r_hits->a.a, u_hits->a.a, r_hits->a.n*sizeof(pe_hit));
radix_sort_pe_hit_idx_hn1(r_hits->a.a, r_hits->a.a + r_hits->a.n);
for (k = 1, l = 0; k <= r_hits->a.n; ++k) {
if (k == r_hits->a.n || get_hit_suid(*r_hits, k) != get_hit_suid(*r_hits, l)) {//same suid
///already sort by spos
u = &(ug->u.a[get_hit_suid(*r_hits, l)]); update = 0;
for (i = offset = 0, r_i = l; i < u->n; i++) {
rid = u->a[i]>>33;
rBeg = offset;
rEnd = rBeg + r_g->seq[rid].len - 1;
for (; r_i < k; r_i++) {
upos = get_hit_spos(*r_hits, r_i);///pos at unitig
if(upos > rEnd) break;
if(upos >= rBeg && upos <= rEnd) {
if(bub) {
r_hits->a.a[r_i].id = (uint32_t)r_hits->a.a[r_i].id;
if(!IF_HOM(get_hit_suid(*r_hits, r_i), *bub)) {
r_hits->a.a[r_i].id += ((uint64_t)(1)<<32);
}
}
rpos = (((u->a[i]>>32)&1)? rEnd - upos : upos - rBeg);///pos at read
rev = ((u->a[i]>>32)&1) ^ (r_hits->a.a[r_i].s>>63);
r_hits->a.a[r_i].s = (rev<<63) | ((rid << (64-ubits))>>1) | (rpos & p_mode);
update++;
}
}
offset += (uint32_t)u->a[i];
}
assert((r_i == k) && (update == (k-l)));
// if(r_i != k || update != k - l) fprintf(stderr, "ERROR-r_i\n");
l = k;
}
}
radix_sort_pe_hit_idx_hn2(r_hits->a.a, r_hits->a.a + r_hits->a.n);
for (k = 1, l = 0; k <= r_hits->a.n; ++k) {
if (k == r_hits->a.n || get_hit_euid(*r_hits, k) != get_hit_euid(*r_hits, l)) {//same euid
///already sort by epos
u = &(ug->u.a[get_hit_euid(*r_hits, l)]); update = 0;
for (i = offset = 0, r_i = l; i < u->n; i++) {
rid = u->a[i]>>33;
rBeg = offset;
rEnd = rBeg + r_g->seq[rid].len - 1;
for (; r_i < k; r_i++) {
upos = get_hit_epos(*r_hits, r_i);///pos at unitig
if(upos > rEnd) break;
if(upos >= rBeg && upos <= rEnd) {
if(bub) {
r_hits->a.a[r_i].id >>= 32;
r_hits->a.a[r_i].id <<= 32;
if(!IF_HOM(get_hit_euid(*r_hits, r_i), *bub)) {
r_hits->a.a[r_i].id += 1;
}
}
rpos = (((u->a[i]>>32)&1)? rEnd - upos : upos - rBeg);///pos at read
rev = ((u->a[i]>>32)&1) ^ (r_hits->a.a[r_i].e>>63);
r_hits->a.a[r_i].e = (rev<<63) | ((rid << (64-ubits))>>1) | (rpos & p_mode);
update++;
}
}
offset += (uint32_t)u->a[i];
}
assert((r_i == k) && (update == (k - l)));
// if(r_i != k || update != k - l) fprintf(stderr, "ERROR-r_i\n");
l = k;
}
}
r_hits->uID_bits = ubits;
r_hits->pos_mode = p_mode;
idx_hits(r_hits, r_g->n_seq);
}
uint64_t get_corresp_usite(uint64_t rid, uint64_t rpos, uint64_t rev, uint64_t rlen, u_hits_t *x, uint64_t ubits, uint64_t p_mode, kvec_t_u64_warp *buf)
{
hit_aux_t *a = NULL;
uint64_t a_n, i, new_uid, new_pos, new_rev;
a = x->a + (x->idx.a[rid]>>32);
a_n = (uint32_t)x->idx.a[rid];
for (i = 0; i < a_n; i++)
{
new_uid = (uint32_t)a[i].ruid;
new_pos = (((a[i].ruid>>32)&1)? a[i].off + rlen - 1 - rpos : a[i].off + rpos);
new_rev = ((a[i].ruid>>32)&1)^rev;
kv_push(uint64_t, buf->a, (new_rev<<63) | ((new_uid << (64-ubits))>>1) | (new_pos & p_mode));
}
return a_n;
}
void update_u_hits(kvec_pe_hit *u_hits, kvec_pe_hit *r_hits, ma_ug_t* ug, asg_t* r_g)
{
u_hits_t x; memset(&x, 0, sizeof(x));
hit_aux_t *p = NULL;
ma_utg_t *u = NULL;
pe_hit *t = NULL;
uint64_t v, i, l, k, offset, occ_1, occ_2, *a_1, *a_2, i_1, i_2;
for (v = 0; v < ug->u.n; v++)
{
u = &(ug->u.a[v]);
for (i = offset = 0; i < u->n; i++)
{
if(u->a[i] != (uint64_t)-1)
{
kv_pushp(hit_aux_t, x, &p);
p->ruid = u->a[i]>>32;
p->ruid <<= 32;
p->ruid |= v;///rid|rev|uid
p->off = offset;
offset += (uint32_t)u->a[i];
}
else
{
offset += GAP_LEN;
}
}
}
radix_sort_hit_aux_ruid(x.a, x.a + x.n);///sort by (rid|rev|uid)
x.idx.n = x.idx.m = (x.n?(x.a[x.n-1].ruid>>33)+1:0);///how many reads?
CALLOC(x.idx.a, x.idx.n);
for (k = 1, l = 0; k <= x.n; ++k)
{
if (k == x.n || (x.a[k].ruid>>33) != (x.a[l].ruid>>33))//same rid
{
x.idx.a[x.a[l].ruid>>33] = (uint64_t)l << 32 | (k - l);
l = k;
}
}
u_hits->a.n = u_hits->idx.n = u_hits->occ.n = 0;
for (u_hits->uID_bits=1; (uint64_t)(1<<u_hits->uID_bits)<(uint64_t)ug->u.n; u_hits->uID_bits++);
u_hits->pos_mode = ((uint64_t)-1) >> (u_hits->uID_bits + 1);
kvec_t_u64_warp buf; kv_init(buf.a);
for (i = 0; i < r_hits->a.n; i++)
{
buf.a.n = 0;
occ_1 = get_corresp_usite(get_hit_suid(*r_hits, i), get_hit_spos(*r_hits, i),
r_hits->a.a[i].s>>63, r_g->seq[get_hit_suid(*r_hits, i)].len, &x, u_hits->uID_bits,
u_hits->pos_mode, &buf);
occ_2 = get_corresp_usite(get_hit_euid(*r_hits, i), get_hit_epos(*r_hits, i),
r_hits->a.a[i].e>>63, r_g->seq[get_hit_euid(*r_hits, i)].len, &x, u_hits->uID_bits,
u_hits->pos_mode, &buf);
if(occ_1 == 0 || occ_2 == 0) continue;
a_1 = buf.a.a; a_2 = buf.a.a + occ_1;
for (i_1 = 0; i_1 < occ_1; i_1++)
{
for (i_2 = 0; i_2 < occ_2; i_2++)
{
kv_pushp(pe_hit, u_hits->a, &t);
t->id = ((occ_1 == 1) && (occ_2 == 1));
t->len = r_hits->a.a[i].len;
t->s = a_1[i_1];
t->e = a_2[i_2];
}
}
}
free(x.a); free(x.idx.a); kv_destroy(buf.a);
idx_hits(u_hits, ug->u.n);
}
ma_ug_t* get_trio_unitig_graph(asg_t *sg, uint8_t flag, ug_opt_t *opt)
{
kvec_asg_arc_t_warp new_rtg_edges;
kv_init(new_rtg_edges.a);
ma_ug_t *ug = NULL;
ug = ma_ug_gen(sg);
adjust_utg_by_trio(&ug, sg, flag, TRIO_THRES, opt->sources, opt->reverse_sources,
opt->coverage_cut, opt->tipsLen, opt->tip_drop_ratio, opt->stops_threshold,
opt->ruIndex, opt->chimeric_rate, opt->drop_ratio, opt->max_hang, opt->min_ovlp,
opt->gap_fuzz, &new_rtg_edges, opt->b_mask_t);
kv_destroy(new_rtg_edges.a);
return ug;
}
static inline void asg_arc_unique_del(asg_t *g, uint32_t v, uint32_t w, int del)
{
uint32_t i, nv = asg_arc_n(g, v);
asg_arc_t *av = asg_arc_a(g, v);
for (i = 0; i < nv; ++i)
{
if (av[i].v == w)
{
av[i].del = !!del;
break;
}
}
}
void horder_clean_sg_by_utg(asg_t *sg, ma_ug_t *ug)
{
uint32_t i, v, w, k, nv, vx, wx;
asg_arc_t *av = NULL;
ma_utg_t *u = NULL;
for (i = 0; i < sg->n_arc; i++) sg->arc[i].del = (!!1);
for (i = 0; i < sg->n_seq; i++) sg->seq[i].del = (!!1);
for (i = 0; i < ug->g->n_seq; ++i) {
if(ug->g->seq[i].del) continue;
u = &(ug->u.a[i]);
for (k = 0; (k + 1) < u->n; k++) {
v = u->a[k]>>32; w = u->a[k+1]>>32;
asg_arc_unique_del(sg, v, w, 0);
asg_arc_unique_del(sg, w^1, v^1, 0);
}
for (k = 0; k < u->n; k++) sg->seq[u->a[k]>>33].del = (!!0);
v = i<<1;
nv = asg_arc_n(ug->g, v); av = asg_arc_a(ug->g, v);
for (k = 0; k < nv; k++)
{
if(av[k].del) continue;
w = av[k].v;
vx = (v&1?((ug->u.a[v>>1].a[0]>>32)^1):(ug->u.a[v>>1].a[ug->u.a[v>>1].n-1]>>32));
wx = (w&1?((ug->u.a[w>>1].a[ug->u.a[w>>1].n-1]>>32)^1):(ug->u.a[w>>1].a[0]>>32));
asg_arc_unique_del(sg, vx, wx, 0); asg_arc_unique_del(sg, wx^1, vx^1, 0);
}
v = (i<<1)+1;
nv = asg_arc_n(ug->g, v); av = asg_arc_a(ug->g, v);
for (k = 0; k < nv; k++)
{
if(av[k].del) continue;
w = av[k].v;
vx = (v&1?((ug->u.a[v>>1].a[0]>>32)^1):(ug->u.a[v>>1].a[ug->u.a[v>>1].n-1]>>32));
wx = (w&1?((ug->u.a[w>>1].a[ug->u.a[w>>1].n-1]>>32)^1):(ug->u.a[w>>1].a[0]>>32));
asg_arc_unique_del(sg, vx, wx, 0); asg_arc_unique_del(sg, wx^1, vx^1, 0);
}
if(u->circ) {
v = w = i<<1;
vx = (v&1?((ug->u.a[v>>1].a[0]>>32)^1):(ug->u.a[v>>1].a[ug->u.a[v>>1].n-1]>>32));
wx = (w&1?((ug->u.a[w>>1].a[ug->u.a[w>>1].n-1]>>32)^1):(ug->u.a[w>>1].a[0]>>32));
asg_arc_unique_del(sg, vx, wx, 0); asg_arc_unique_del(sg, wx^1, vx^1, 0);
}
}
asg_cleanup(sg);
/*******************************for debug************************************/
// ma_ug_t *dbg = ma_ug_gen(sg);
// print_N50(dbg);
// print_N50(ug);
// uint8_t *end = NULL; CALLOC(end, sg->n_seq<<1);
// for (i = 0; i < dbg->g->n_seq; ++i)
// {
// u = &(dbg->u.a[i]);
// if(u->n == 0) continue;
// end[(u->a[0]>>32)^1] = 1;
// end[u->a[u->n-1]>>32] = 2;
// }
// for (i = 0; i < ug->g->n_seq; ++i)
// {
// u = &(ug->u.a[i]);
// if(u->n == 0) continue;
// for (k = 1; (k + 1) < u->n; k++)
// {
// if(end[(u->a[k]>>32)])
// {
// fprintf(stderr, "(1) node-%lu, v-%lu, w-%lu, sg(v).n: %u\n",
// u->a[k]>>33, (u->a[k]>>32), (u->a[k+1]>>32), asg_arc_n(sg, (u->a[k]>>32)));
// }
// if(end[(u->a[k]>>32)^1])
// {
// fprintf(stderr, "(2) node-%lu, v-%lu, w-%lu, sg(v).n: %u\n",
// u->a[k]>>33, (u->a[k]>>32)^1, (u->a[k-1]>>32)^1, asg_arc_n(sg, (u->a[k]>>32)^1));
// }
// }
// }
// free(end);
// ma_ug_destroy(dbg);
/*******************************for debug************************************/
}
uint64_t get_hic_cov_interval(uint64_t *b, uint64_t b_n, int64_t min_dp, int64_t *boundS, int64_t *boundE,
h_covs *res)
{
if(res) res->n = 0;
if(min_dp == 0 || b_n == 0) return (uint64_t)-1;
uint64_t i, len = 0;
int64_t dp, old_dp, start = 0, bs = b[0]>>1, be = b[b_n-1]>>1, olen;
h_cov_t *p = NULL;
if(boundS) bs = (*boundS);
if(boundE) be = (*boundE);
for (i = 0, dp = 0, start = 0; i < b_n; ++i)
{
old_dp = dp;
///if a[j] is qe
if (b[i]&1) --dp;
else ++dp;
if (old_dp < min_dp && dp >= min_dp) ///old_dp < dp, b.a[j] is qs
{
///case 2, a[j] is qs
start = b[i]>>1;
}
else if (old_dp >= min_dp && dp < min_dp) ///old_dp > min_dp, b.a[j] is qe
{
olen = OVL(start, (int64_t)(b[i]>>1), bs, be);
if(olen == 0) continue;
if(res)
{
kv_pushp(h_cov_t, *res, &p);
p->s = MAX(start, bs);
p->e = MIN((int64_t)(b[i]>>1), be);
p->dp = old_dp;
}
len += olen;
}
}
return len;
}
void get_hic_breakpoint(uint64_t *b, uint64_t b_n, int64_t cutoff, h_covs *res,
int64_t cov_s_pos, int64_t cov_e_pos, uint64_t *s, uint64_t *e)
{
uint64_t i;
(*s) = (*e) = (uint64_t)-1;
res->n = 0;
get_hic_cov_interval(b, b_n, cutoff, &cov_s_pos, &cov_e_pos, res);
if(res->n == 0) return;///res keeps all intervals with >= cutoff coverage
int64_t max = -1, max_cur = 0;
int64_t max_s_idx, max_e_idx, cur_s_idx;
for (i = 0; i < res->n; i++)//all intervals have cov >= cutoff
{
if(i > 0 && (res->a[i].s - res->a[i-1].e) > 0)//cov < cutoff
{
max_cur += (res->a[i].s - res->a[i-1].e);//at least positive
if(max < max_cur)
{
max_s_idx = (max < 0? res->a[i-1].e:cur_s_idx);
max_e_idx = res->a[i].s;
max = max_cur;
cur_s_idx = max_s_idx;
}
}
//cov >= cutoff
max_cur -= (res->a[i].e - res->a[i].s);
if(max_cur < 0)
{
max_cur = 0;
cur_s_idx = res->a[i].e;
}
}
if(max > 0)
{
(*s) = max_s_idx;
(*e) = max_e_idx;
}
}
void get_consensus_break(h_covs *res, h_covs *tmp)
{
uint64_t i, k, n, m, max_cut = 0;
h_cov_t *p = NULL;
tmp->n = 0;
if(res->n == 0) return;
n = res->n;
for (i = 0; i < n; i++)
{
res->a[i].s <<= 1;
if(max_cut < res->a[i].dp) max_cut = res->a[i].dp;
kv_pushp(h_cov_t, *res, &p);
*p = res->a[i];
p->s = (res->a[i].e<<1)|1;
}
radix_sort_h_cov_s(res->a, res->a+res->n);
int64_t dp, old_dp, start = 0, max_dp;
p = NULL; max_dp = -1; tmp->n = 0;
for (i = 0, dp = 0, start = 0; i < res->n; ++i)
{
old_dp = dp;
///if a[j] is qe
if (res->a[i].s&1) --dp;
else ++dp;
if (old_dp < dp) ///old_dp < dp, b.a[j] is qs
{
///case 2, a[j] is qs