-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathecovlp.cpp
6362 lines (5296 loc) · 252 KB
/
ecovlp.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 <stdlib.h>
#include <string.h>
#include <assert.h>
#include "Correct.h"
#include "Process_Read.h"
#include "ecovlp.h"
#include "kthread.h"
#include "htab.h"
#define HA_KMER_GOOD_RATIO 0.333
#define E_KHIT 31
#define CNS_DEL_E (0x7fffffffu)
#define del_cns_arc(z, arc_i) ((z).arc.a[(arc_i)].v == CNS_DEL_E)
#define CNS_DEL_V (0x1fffffffu)
#define del_cns_nn(z, nn_i) ((z).a[(nn_i)].sc == CNS_DEL_V)
#define REFRESH_N 128
#define COV_W 3072
KDQ_INIT(uint32_t)
typedef struct {
uint32_t v:31, f:1;
uint32_t sc;
} cns_arc;
typedef struct {size_t n, m, nou; cns_arc *a; } cns_arc_v;
typedef struct {
// uint16_t c:2, t:2, f:1, sc:3;
uint32_t c:2, f:1, sc:29;
cns_arc_v arc;
}cns_t;
typedef struct {
size_t n, m;
cns_t *a;
uint32_t si, ei, off, bn, bb0, bb1, cns_g_wl;
kdq_t(uint32_t) *q;
}cns_gfa;
typedef struct {
// chaining and overlapping related buffers
UC_Read self_read, ovlp_read;
Candidates_list clist;
overlap_region_alloc olist;
ha_abuf_t *ab;
// int64_t num_read_base, num_correct_base, num_recorrect_base;
uint64_t cnt[6], rr;
haplotype_evdience_alloc hap;
bit_extz_t exz;
kv_ul_ov_t pidx;
asg64_v v64;
asg32_v v32;
asg16_v v16;
asg8_v v8q, v8t;
kvec_t_u8_warp k_flag;
st_mt_t sp;
cns_gfa cns;
} ec_ovec_buf_t0;
typedef struct {
ec_ovec_buf_t0 *a;
uint32_t n, rev;
uint8_t *cr;
} ec_ovec_buf_t;
typedef struct {
uint32_t n_thread, n_a, chunk_size, cn;
FILE *fp;
} cal_ec_r_dbg_t;
typedef struct {
ma_hit_t *a;
size_t n, m;
asg16_v ec;
} r_dbg_step_res_t;
typedef struct { // data structure for each step in kt_pipeline()
ec_ovec_buf_t *buf;
r_dbg_step_res_t *res;
uint32_t si, ei;
} cal_ec_r_dbg_step_t;
ec_ovec_buf_t* gen_ec_ovec_buf_t(uint32_t n);
void destroy_ec_ovec_buf_t(ec_ovec_buf_t *p);
#define generic_key(x) (x)
KRADIX_SORT_INIT(ec16, uint16_t, generic_key, 2)
KRADIX_SORT_INIT(ec32, uint32_t, generic_key, 4)
KRADIX_SORT_INIT(ec64, uint64_t, generic_key, 8)
#define kdq_clear(q) ((q)->count = (q)->front = 0)
typedef struct {size_t n, m; asg16_v *a; uint8_t *f; } cc_v;
cc_v scc = {0, 0, NULL, NULL};
cc_v scb = {0, 0, NULL, NULL};
cc_v sca = {0, 0, NULL, NULL};
typedef struct {size_t n, m; char *a; UC_Read z; asg8_v q;} sl_v;
void h_ec_lchain(ha_abuf_t *ab, uint32_t rid, char* rs, uint64_t rl, uint64_t mz_w, uint64_t mz_k, All_reads *rref, overlap_region_alloc *overlap_list, Candidates_list *cl, double bw_thres,
int max_n_chain, int apend_be, kvec_t_u8_warp* k_flag, kvec_t_u64_warp* dbg_ct, st_mt_t *sp, uint32_t *high_occ, uint32_t *low_occ, uint32_t is_accurate, uint32_t gen_off, int64_t mcopy_num, double mcopy_rate, uint32_t chain_cutoff, uint32_t mcopy_khit_cut, uint64_t ocv_w);
void h_ec_lchain_amz(ha_abuf_t *ab, uint32_t rid, char* rs, uint64_t rl, uint64_t mz_w, uint64_t mz_k, All_reads *rref, overlap_region_alloc *overlap_list, Candidates_list *cl, double bw_thres,
int max_n_chain, int apend_be, kvec_t_u8_warp* k_flag, kvec_t_u64_warp* dbg_ct, st_mt_t *sp, uint32_t *high_occ, uint32_t *low_occ, uint32_t is_accurate, uint32_t gen_off, int64_t enable_mcopy, double mcopy_rate, uint32_t chain_cutoff, uint32_t mcopy_khit_cut, uint64_t ocv_w);
void h_ec_lchain_re_gen(ha_abuf_t *ab, uint32_t rid, char* rs, uint64_t rl, uint64_t mz_w, uint64_t mz_k, ha_pt_t *ha_idx, All_reads *rref, overlap_region_alloc *overlap_list, Candidates_list *cl, double bw_thres,
int apend_be, kvec_t_u8_warp* k_flag, kvec_t_u64_warp* dbg_ct, st_mt_t *sp, uint32_t *high_occ, uint32_t *low_occ, uint32_t gen_off, int64_t enable_mcopy, double mcopy_rate, uint32_t mcopy_khit_cut,
int64_t max_skip, int64_t max_iter, int64_t max_dis, int64_t quick_check, double chn_pen_gap, double chn_pen_skip, UC_Read *tu, asg64_v *oidx, asg16_v *scc);
void h_ec_lchain_re_gen3(ha_abuf_t *ab, uint32_t rid, char* rs, uint64_t rl, uint64_t mz_w, uint64_t mz_k, ha_pt_t *ha_idx, All_reads *rref, overlap_region_alloc *overlap_list, Candidates_list *cl, double bw_thres,
int apend_be, kvec_t_u8_warp* k_flag, kvec_t_u64_warp* dbg_ct, st_mt_t *sp, uint32_t *high_occ, uint32_t *low_occ, uint32_t gen_off, int64_t enable_mcopy, double mcopy_rate, uint32_t mcopy_khit_cut,
int64_t max_skip, int64_t max_iter, int64_t max_dis, int64_t quick_check, double chn_pen_gap, double chn_pen_skip, UC_Read *tu, asg64_v *oidx, asg16_v *scc);
uint64_t get_mz1(const char *str, int len, int w, int k, uint32_t rid, int is_hpc, ha_abuf_t *ab, const void *hf, ha_pt_t *ha_idx, int sample_dist, kvec_t_u8_warp* k_flag, kvec_t_u64_warp* dbg_ct, ha_pt_t *pt, int min_freq, int32_t dp_min_len, float dp_e, st_mt_t *mt, int32_t ws, int32_t is_unique, void *km, uint64_t beg_i);
void get_pi_ec_chain(ha_abuf_t *ab, uint64_t rid, uint64_t rl, uint32_t tid, char* ts, uint64_t tl, uint64_t mz_w, uint64_t mz_k, overlap_region_alloc *overlap_list, Candidates_list *cl, double bw_thres,
int apend_be, kvec_t_u8_warp* k_flag, kvec_t_u64_warp* dbg_ct, st_mt_t *sp, uint32_t *high_occ, uint32_t *low_occ, /**uint32_t is_accurate,**/ uint32_t gen_off, int64_t enable_mcopy, double mcopy_rate, uint32_t mcopy_khit_cut,
int64_t max_skip, int64_t max_iter, int64_t max_dis, int64_t quick_check, double chn_pen_gap, double chn_pen_skip);
void set_lchain_dp_op(uint32_t is_accurate, uint32_t mz_k, int64_t *max_skip, int64_t *max_iter, int64_t *max_dis, double *chn_pen_gap, double *chn_pen_skip, int64_t *quick_check);
void h_ec_lchain_re_gen_srt(ha_abuf_t *ab, ha_pt_t *ha_idx, overlap_region_alloc *olst, Candidates_list *cl);
uint64_t h_ec_lchain_re_gen_qry(ha_abuf_t *ab, uint64_t *k, uint64_t *l, uint64_t *i, uint64_t *idx_a, uint64_t idx_n, uint64_t *tid, uint64_t *trev);
uint64_t h_ec_lchain_re_chn(ha_abuf_t *ab, uint64_t si, uint64_t ei, uint32_t rid, char* rs, uint64_t rl, uint64_t tid, char* ts, uint64_t tl, uint64_t trev, uint64_t mz_w, uint64_t mz_k, overlap_region_alloc *olst, Candidates_list *cl, double bw_thres,
int apend_be, uint64_t max_cnt, uint64_t min_cnt, uint32_t gen_off, int64_t enable_mcopy, double mcopy_rate, uint32_t mcopy_khit_cut, int64_t max_skip, int64_t max_iter, int64_t max_dis, int64_t quick_check, double chn_pen_gap, double chn_pen_skip, tiny_queue_t *tq, asg16_v *scc, int64_t *n, int64_t *zn);
overlap_region* h_ec_lchain_fast(ha_abuf_t *ab, uint32_t rid, UC_Read *qu, UC_Read *tu, uint64_t mz_w, uint64_t mz_k, All_reads *rref, overlap_region_alloc *ol, Candidates_list *cl, bit_extz_t *exz, asg16_v *buf, asg64_v *srt_i, double bw_thres,
int apend_be, kvec_t_u8_warp* k_flag, kvec_t_u64_warp* dbg_ct, st_mt_t *sp, uint32_t *high_occ, uint32_t *low_occ, uint32_t is_accurate, uint32_t gen_off, int64_t enable_mcopy, double mcopy_rate, uint32_t mcopy_khit_cut, ma_hit_t_alloc *in0, ma_hit_t_alloc *in1, double sh);
void h_ec_lchain_fast_new(ha_abuf_t *ab, uint32_t rid, UC_Read *qu, UC_Read *tu, All_reads *rref, overlap_region_alloc *ol, Candidates_list *cl, bit_extz_t *exz, asg16_v *buf, asg64_v *srt_i, ma_hit_t_alloc *in0, ma_hit_t_alloc *in1, double sh);
ec_ovec_buf_t* gen_ec_ovec_buf_t(uint32_t n)
{
uint32_t k; ec_ovec_buf_t0 *z = NULL;
ec_ovec_buf_t *p = NULL; CALLOC(p, 1);
p->n = n; CALLOC(p->a, p->n);
for (k = 0; k < p->n; k++) {
z = &(p->a[k]);
init_UC_Read(&z->self_read);
init_UC_Read(&z->ovlp_read);
init_Candidates_list(&z->clist);
init_overlap_region_alloc(&z->olist);
// init_fake_cigar(&(z->tmp.f_cigar));
// memset(&(z->tmp.w_list), 0, sizeof(z->tmp.w_list));
// CALLOC(z->tmp.w_list.a, 1); z->tmp.w_list.n = z->tmp.w_list.m = 1;
// kv_init(z->b_buf.a);
// kv_init(z->r_buf.a);
kv_init(z->k_flag.a);
kv_init(z->sp);
kv_init(z->pidx);
kv_init(z->v64);
kv_init(z->v32);
kv_init(z->v16);
kv_init(z->v8q);
kv_init(z->v8t);
init_bit_extz_t(&(z->exz), 31);
z->ab = ha_abuf_init();
InitHaplotypeEvdience(&z->hap);
z->cns.q = kdq_init(uint32_t);
}
return p;
}
void destroy_cns_gfa(cns_gfa *p)
{
size_t k;
for (k = 0; k < p->m; k++) {
kv_destroy(p->a[k].arc);
}
free(p->a); kdq_destroy(uint32_t, p->q);
}
void destroy_ec_ovec_buf_t(ec_ovec_buf_t *p)
{
uint32_t k; ec_ovec_buf_t0 *z = NULL;
for (k = 0; k < p->n; k++) {
z = &(p->a[k]); z->rr = 0;
destory_UC_Read(&z->self_read);
destory_UC_Read(&z->ovlp_read);
destory_Candidates_list(&z->clist);
destory_overlap_region_alloc(&z->olist);
// destory_fake_cigar(&(z->tmp.f_cigar));
// free(z->tmp.w_list.a); free(z->tmp.w_list.c.a);
// kv_destroy(z->r_buf.a);
kv_destroy(z->k_flag.a);
kv_destroy(z->sp);
kv_destroy(z->pidx);
kv_destroy(z->v64);
kv_destroy(z->v32);
kv_destroy(z->v16);
kv_destroy(z->v8q);
kv_destroy(z->v8t);
destroy_bit_extz_t(&(z->exz));
ha_abuf_destroy(z->ab);
destoryHaplotypeEvdience(&z->hap);
destroy_cns_gfa(&(z->cns));
}
free(p->a); free(p->cr); free(p);
// fprintf(stderr, "[M::%s-chains] #->%lld\n", __func__, asm_opt.num_bases);
// fprintf(stderr, "[M::%s-passed-chains-0] #->%lld\n", __func__, asm_opt.num_corrected_bases);
// fprintf(stderr, "[M::%s-cis-chains-1] #->%lld\n", __func__, asm_opt.num_recorrected_bases);
}
inline void refresh_ec_ovec_buf_t0(ec_ovec_buf_t0 *z, uint64_t n)
{
z->rr++;
if((z->rr%n) == 0) {
free(z->self_read.seq); memset(&(z->self_read), 0, sizeof(z->self_read));
free(z->ovlp_read.seq); memset(&(z->ovlp_read), 0, sizeof(z->ovlp_read));
destory_Candidates_list(&z->clist); memset(&(z->clist), 0, sizeof(z->clist));
destory_overlap_region_alloc(&z->olist); memset(&(z->olist), 0, sizeof(z->olist)); init_overlap_region_alloc(&z->olist);
kv_destroy(z->k_flag.a); kv_init(z->k_flag.a);
kv_destroy(z->sp); kv_init(z->sp);
kv_destroy(z->pidx); kv_init(z->pidx);
kv_destroy(z->v64); kv_init(z->v64);
kv_destroy(z->v32); kv_init(z->v32);
kv_destroy(z->v16); kv_init(z->v16);
kv_destroy(z->v8q); kv_init(z->v8q);
kv_destroy(z->v8t); kv_init(z->v8t);
destroy_bit_extz_t(&(z->exz)); init_bit_extz_t(&(z->exz), 31);
ha_abuf_destroy(z->ab); z->ab = ha_abuf_init();
destoryHaplotypeEvdience(&z->hap); memset(&(z->hap), 0, sizeof(z->hap)); InitHaplotypeEvdience(&z->hap);
destroy_cns_gfa(&(z->cns)); memset(&(z->cns), 0, sizeof(z->cns)); z->cns.q = kdq_init(uint32_t);
// z->rr = 1;
}
}
void prt_chain(overlap_region_alloc *o)
{
uint64_t k;
for (k = 0; k < o->length; k++) {
fprintf(stderr, "[M::%s]\t#%u\tlen::%lu\t%u\t%u\t%c\t#%u\tlen::%lu\t%u\t%u\tsc::%d\taln::%u\terr::%u\n", __func__, o->list[k].x_id, Get_READ_LENGTH(R_INF, o->list[k].x_id), o->list[k].x_pos_s, o->list[k].x_pos_e+1, "+-"[o->list[k].y_pos_strand],
o->list[k].y_id, Get_READ_LENGTH(R_INF, o->list[k].y_id), o->list[k].y_pos_s, o->list[k].y_pos_e+1, o->list[k].shared_seed, o->list[k].align_length, o->list[k].non_homopolymer_errors);
}
}
overlap_region *fetch_aux_ovlp(overlap_region_alloc* ol) /// exactly same to gen_aux_ovlp
{
if (ol->length + 1 >= ol->size) {
uint64_t sl = ol->size;
ol->size = ol->length + 1;
kroundup64(ol->size);
REALLOC(ol->list, ol->size);
/// need to set new space to be 0
memset(ol->list + sl, 0, sizeof(overlap_region)*(ol->size - sl));
}
///debug for memory
// if(ol->length + 1 >= ol->size) {
// fprintf(stderr, "[M::%s] length::%lu, size::%lu\n", __func__, ol->length, ol->size);
// }
return &(ol->list[ol->length+1]);
}
typedef struct {
ul_ov_t *c_idx;
asg64_v *idx;
int64_t i, i0, srt_n, rr, ru;
uint64_t mms, mme;
} cc_idx_t;
///[s, e)
int64_t extract_sub_cigar_mm(overlap_region *z, int64_t s, int64_t e, ul_ov_t *p, uint64_t *ct)
{
int64_t wk = ovlp_cur_wid(*p), xk = ovlp_cur_xoff(*p), yk = ovlp_cur_yoff(*p), ck = ovlp_cur_coff(*p), os, oe, t;
bit_extz_t ez; int64_t bd = ovlp_bd(*p), s0, e0;
s0 = ((int64_t)(z->w_list.a[wk].x_start)) + bd;
e0 = ((int64_t)(z->w_list.a[wk].x_end)) + 1 - bd;
if(s < s0) s = s0; if(e > e0) e = e0;///exclude boundary
if(s >= e) return -1;
os = MAX(s, s0); oe = MIN(e, e0);
if(oe <= os) return -1;
set_bit_extz_t(ez, (*z), wk);
if(!ez.cigar.n) return -1;
int64_t cn = ez.cigar.n, op; int64_t ws, we, ovlp;
if((ck < 0) || (ck > cn)) {//(*ck) == cn is allowed
ck = 0; xk = ez.ts; yk = ez.ps;
}
while (ck > 0 && xk >= s) {///x -> t; y -> p; first insertion and then match/mismatch
--ck;
op = ez.cigar.a[ck]>>14;
if(op!=2) xk -= (ez.cigar.a[ck]&(0x3fff));
if(op!=3) yk -= (ez.cigar.a[ck]&(0x3fff));
}
//some cigar will span s or e
while (ck < cn && xk < e) {//[s, e)
ws = xk;
op = ez.cigar.a[ck]>>14;
///op == 3: -> x; op == 2: -> y;
if(op!=2) xk += (ez.cigar.a[ck]&(0x3fff));
if(op!=3) yk += (ez.cigar.a[ck]&(0x3fff));
ck++; we = xk;
os = MAX(s, ws); oe = MIN(e, we);
ovlp = ((oe>os)? (oe-os):0);
if(op != 2) {
if(!ovlp) continue;
} else {///ws == we
if(ws < s || ws >= e) continue;
}
if(op == 0) {
for (t = os + 1; t < oe; t++) {
ct[(t-s)<<1]++; ct[(t-s)<<1] += ((uint64_t)(0x100000000));
ct[((t-s)<<1)+1]++; ct[((t-s)<<1)+1] += ((uint64_t)(0x100000000));
}
t = os;
if(t < oe) {
ct[(t-s)<<1]++; ct[(t-s)<<1] += ((uint64_t)(0x100000000));
if(os > ws) {
ct[((t-s)<<1)+1]++; ct[((t-s)<<1)+1] += ((uint64_t)(0x100000000));
}
}
} else if(op!=2) {
for (t = os + 1; t < oe; t++) {
ct[(t-s)<<1]++;
ct[((t-s)<<1)+1]++;
}
t = os;
if(t < oe) {
ct[(t-s)<<1]++;
if(os > ws) {
ct[((t-s)<<1)+1]++;
}
}
} else {
ct[((ws-s)<<1)+1]++; ///ct[((ws-s)<<1)+1] += ((uint64_t)(0x100000000));
}
}
ovlp_cur_xoff(*p) = xk; ovlp_cur_yoff(*p) = yk; ovlp_cur_coff(*p) = ck; ovlp_cur_ylen(*p) = 0;
return 1;
}
#define simp_vote_len 6
///[s, e)
uint32_t extract_sub_cigar_ii(overlap_region *z, int64_t ql, All_reads *rref, int64_t s, int64_t e, int64_t iws, int64_t iwe, UC_Read* tu, ul_ov_t *p)
{
int64_t wk = ovlp_cur_wid(*p), xk = ovlp_cur_xoff(*p), yk = ovlp_cur_yoff(*p), ck = ovlp_cur_coff(*p), os, oe, ol;
bit_extz_t ez; int64_t bd = ovlp_bd(*p), s0, e0, ii[2], it[2]; uint32_t res = (uint32_t)-1;
s0 = ((int64_t)(z->w_list.a[wk].x_start)) + bd;
e0 = ((int64_t)(z->w_list.a[wk].x_end)) + 1 - bd;
if(s < s0) s = s0; if(e > e0) e = e0;///exclude boundary
if(s > e) return -1;///it is possible s == e
os = MAX(s, s0); oe = MIN(e, e0);
if(oe < os) return -1;///it is possible os == oe
// fprintf(stderr, "[M::%s] s0::%ld, e0::%ld, iws::%ld, iwe::%ld\n", __func__, s0, e0, iws, iwe);
///make sure that this alignment block could cover the whole [iws, iwe) -> s0 < iws && e0 > iwe
// if((s0 >= iws) || (e0 <= iwe)) return -1;///!(s0 < iws && e0 > iwe) -> only consider the alignment that could cover the whole [s, e)
if(!(((s0 < iws) || (s0 == 0)) && ((e0 > iwe) || (e0 == ql)))) return -1;///!(s0 < iws && e0 > iwe) -> only consider the alignment that could cover the whole [s, e)
set_bit_extz_t(ez, (*z), wk);
if(!ez.cigar.n) return -1;
int64_t cn = ez.cigar.n; uint16_t op; int64_t ws, we, wts, wte, ovlp, cc = 0, cci;
if((ck < 0) || (ck > cn)) {//(*ck) == cn is allowed
ck = 0; xk = ez.ts; yk = ez.ps;
}
while (ck > 0 && xk >= s) {///x -> t; y -> p; first insertion and then match/mismatch
--ck;
op = ez.cigar.a[ck]>>14;
if(op!=2) xk -= (ez.cigar.a[ck]&(0x3fff));
if(op!=3) yk -= (ez.cigar.a[ck]&(0x3fff));
}
// char cm[4]; cm[0] = 'M'; cm[1] = 'S'; cm[2] = 'I'; cm[3] = 'D';
//some cigar will span s or e
ii[0] = ii[1] = it[0] = it[1] = -1; res = cc = 0;
while (ck < cn && xk < e) {//[s, e)
ws = xk; wts = yk;
op = ez.cigar.a[ck]>>14; ol = (ez.cigar.a[ck]&(0x3fff));
///op == 3: -> x; op == 2: -> y;
if(op!=2) xk += ol;
if(op!=3) yk += ol;
ck++; we = xk; wte = yk;
// if(s == 10480) {
// fprintf(stderr, "[%ld, %ld)\t%c\n", ws, we, cm[op]);
// }
os = MAX(s, ws); oe = MIN(e, we);
ovlp = ((oe>os)? (oe-os):0);
if(s == e) {///insertion in comparsion with the reference
if(op != 0 || ws >= s || we <= e || e != iwe || s != iws) continue;///must be a match
} else {
if(op != 2) {
if(!ovlp) continue;
} else {///ws == we
if(ws < s || ws >= e) continue;
}
}
if(ii[0] == -1) {
ii[0] = os;
if(op < 2) {
it[0] = os - ws + wts;
} else {///op == 2: more y; p == 3: more x
it[0] = wts;
}
}
ii[1] = oe;
if(op < 2) {
it[1] = oe - ws + wts;
} else {///op == 2: more y; p == 3: more x
it[1] = wte;
}
if(op != 2) ol = oe-os;
cc += ol;
// if(s == 11851 && e == 11853) {
// if(!ol) fprintf(stderr, "%ld%c", ol, cm[op]);
// }
if(cc <= simp_vote_len) {
for (cci = 0; cci < ol; cci++) {
res <<= 2; res |= op;
}
}
}
while (ck < cn && xk <= e) {//[s, e)
ws = xk; wts = yk;
op = ez.cigar.a[ck]>>14; ol = (ez.cigar.a[ck]&(0x3fff));
if(op != 2) break;
yk += (ez.cigar.a[ck]&(0x3fff));
ck++; we = xk; wte = yk;
if(ws >= s && ws <= e) {
if(ii[0] == -1) {
ii[0] = ws; it[0] = wts;
}
ii[1] = we; it[1] = wte;
cc += ol;
// if(s == 11851 && e == 11853) {
// fprintf(stderr, "%ld%c", ol, cm[op]);
// }
if(cc <= simp_vote_len) {
for (cci = 0; cci < ol; cci++) {
res <<= 2; res |= op;
}
}
}
}
// if(s == 11851 && e == 11853) {
// fprintf(stderr, "\tx::[%ld, %ld)\ty::[%ld, %ld)\tcc::%ld\n", ii[0], ii[1], it[0], it[1], cc);
// }
if((cc <= simp_vote_len)
&& (ii[1] >= ii[0]) && (ii[1] - ii[0] <= simp_vote_len)
&& (it[1] >= it[0]) && (it[1] - it[0] <= simp_vote_len)) {
// ii[0] = ii[0] - s; ii[1] = e - ii[1];
if((ii[0] == iws) && (ii[1] == iwe)) {
op = cc; op <<= 12; res |= op;
char *ystr = NULL; res <<= 16; cc = it[1] - it[0]; op = 0;
if(cc > 0) {
UC_Read_resize(*tu, (it[1] - it[0])); ystr = tu->seq;
recover_UC_Read_sub_region(ystr, it[0], (it[1] - it[0]), z->y_pos_strand, rref, z->y_id);
for (cci = 0; cci < cc; cci++) {
op <<= 2; op |= seq_nt6_table[(uint32_t)(ystr[cci])];
}
}
res |= op;
op = it[1] - it[0]; op <<= 12; res |= op;
} else {
res = (uint32_t)-1;
}
} else {
res = (uint32_t)-1;
}
ovlp_cur_xoff(*p) = xk; ovlp_cur_yoff(*p) = yk; ovlp_cur_coff(*p) = ck; ovlp_cur_ylen(*p) = 0;
return res;
}
typedef struct {
All_reads *rref;
UC_Read *tu;
uint64_t s, e, n0, n1, id, rev;
} rr_seq_t;
inline void insert_cns_arc(cns_gfa *cns, uint32_t src, uint32_t des, uint32_t is_ou, uint32_t plus0, uint32_t rid)
{
if(src >= cns->n) {
fprintf(stderr, "[M::%s] rid::%u, src::%u, des::%u, (*cns).n::%u\n", __func__, rid, src, des, (uint32_t)(*cns).n);
exit(1);
}
cns_arc *p, t; kv_pushp(cns_arc, (*cns).a[src].arc, &p);
p->f = 0; p->sc = plus0; p->v = des;
if(is_ou) {
(*cns).a[src].arc.nou++;
if((*cns).a[src].arc.nou < (*cns).a[src].arc.n) {
t = (*cns).a[src].arc.a[(*cns).a[src].arc.nou-1];
(*cns).a[src].arc.a[(*cns).a[src].arc.nou-1] = *p;
*p = t;
}
}
}
inline uint32_t insert_cns_node(cns_gfa *cns)
{
cns_t *p; uint32_t m0;
if (((*cns)).n == ((*cns)).m) {
m0 = ((*cns)).m;
((*cns)).m = ((*cns)).m? ((*cns)).m<<1 : 2;
((*cns)).a = (cns_t*)realloc(((*cns)).a, sizeof(cns_t) * ((*cns)).m);
if(((*cns)).m > m0) {
memset(((*cns)).a + m0, 0, sizeof(cns_t)*(((*cns)).m-m0));
}
}
*(&p) = &((*cns)).a[((*cns)).n++];
p->arc.n = p->arc.nou = 0;
p->c = p->f = p->sc = 0;
return ((*cns)).n - 1;
}
inline uint32_t add_cns_arc(cns_gfa *cns, uint32_t src, uint32_t des, uint32_t is_ou, uint32_t plus)
{
uint32_t k, s, e;
if(is_ou) {
s = 0; e = (*cns).a[src].arc.nou;
} else {
s = (*cns).a[src].arc.nou; e = (*cns).a[src].arc.n;
}
for (k = s; k < e; k++) {
if((*cns).a[src].arc.a[k].v == des) {
(*cns).a[src].arc.a[k].sc += plus;
break;
}
}
return ((k < e)?(1):(0));
}
inline void prt_cns_arc(cns_gfa *cns, uint32_t src, const char* cmd)
{
uint32_t k;
fprintf(stderr, "\n%s\t[M::%s] src::%u, sc::%u, c::%u\n", cmd, __func__, src, (*cns).a[src].sc, (*cns).a[src].c);
for (k = 0; k < (*cns).a[src].arc.n; k++) {
fprintf(stderr, "%s\t[M::%s] des::%u, sc::%u, is_ou::%u\n", cmd, __func__, (*cns).a[src].arc.a[k].v, (*cns).a[src].arc.a[k].sc, k<(*cns).a[src].arc.nou?1:0);
}
}
inline uint32_t get_cns_arc_bp(cns_gfa *cns, uint32_t src, uint32_t bp, uint32_t is_ou, uint32_t av_bp)
{
uint32_t k, s, e;
if(is_ou) {
s = 0; e = (*cns).a[src].arc.nou;
} else {
s = (*cns).a[src].arc.nou; e = (*cns).a[src].arc.n;
}
for (k = s; k < e; k++) {
if((*cns).a[src].arc.a[k].v == 0 || (*cns).a[src].arc.a[k].v == 1) continue;
if(av_bp && (*cns).a[src].arc.a[k].v >= (*cns).bb0 && (*cns).a[src].arc.a[k].v < (*cns).bb1) continue;///no backbone
if((*cns).a[(*cns).a[src].arc.a[k].v].c == bp) {
return k;
}
}
return ((uint32_t)-1);
}
inline uint32_t add_cns_arc_bp(cns_gfa *cns, uint32_t src, uint32_t bp, uint32_t plus0, uint32_t rid, uint32_t av_bp)
{
uint32_t rr, des;
rr = get_cns_arc_bp(cns, src, bp, 1, av_bp);
if(rr != ((uint32_t)-1)) {///find an existing node
des = (*cns).a[src].arc.a[rr].v;
(*cns).a[des].sc++;
(*cns).a[src].arc.a[rr].sc += plus0;
rr = add_cns_arc(cns, des, src, 0, plus0);
// if(rr == 0) {
// fprintf(stderr, "[M::%s] src::%u -> des::%u\n", __func__, src, des);
// prt_cns_arc(cns, src);
// prt_cns_arc(cns, des);
// }
assert(rr);
return des;
} else {///create a new node
des = insert_cns_node(cns);
(*cns).a[des].sc++; (*cns).a[des].c = bp;
insert_cns_arc(cns, src, des, 1, plus0, rid);
insert_cns_arc(cns, des, src, 0, plus0, rid);
}
return des;
}
void init_cns_g(cns_gfa *cns, char *s, uint64_t sl, uint32_t rid)
{
uint32_t m0 = cns->m, m1 = sl + 2, k; cns_t *p;
if ((*cns).m < (m1)) { ///equal to kv_resize()
(*cns).m = (m1);
(--((*cns).m), ((*cns).m)|=((*cns).m)>>1, ((*cns).m)|=((*cns).m)>>2, ((*cns).m)|=((*cns).m)>>4, ((*cns).m)|=((*cns).m)>>8, ((*cns).m)|=((*cns).m)>>16, ++((*cns).m));
(*cns).a = (cns_t*)realloc((*cns).a, sizeof(cns_t) * (*cns).m);
if((*cns).m > m0) {
memset((*cns).a + m0, 0, sizeof(cns_t)*((*cns).m-m0));
}
}
(*cns).n = 0; (*cns).si = 0; (*cns).ei = 1; (*cns).off = 2;
p = &((*cns).a[(*cns).n++]); p->arc.nou = p->arc.n = p->c = p->f = p->sc = 0; ///beg
p = &((*cns).a[(*cns).n++]); p->arc.nou = p->arc.n = p->c = p->f = p->sc = 0; ///end
(*cns).bb0 = (*cns).n;
for (k = 0; k < sl; k++) {
p = &((*cns).a[(*cns).n++]); p->arc.nou = p->arc.n = p->f = 0;
p->c = seq_nt6_table[(uint32_t)(s[k])]; p->sc = 1;
if(k + 1 < sl) insert_cns_arc(cns, k + (*cns).off, k + 1 + (*cns).off, 1, 1, rid);
if(k > 0) insert_cns_arc(cns, k + (*cns).off, k - 1 + (*cns).off, 0, 1, rid);
}
if(sl) {
insert_cns_arc(cns, (*cns).si, 0 + (*cns).off, 1, 1, rid); insert_cns_arc(cns, 0 + (*cns).off, (*cns).si, 0, 1, rid);
insert_cns_arc(cns, sl - 1 + (*cns).off, (*cns).ei, 1, 1, rid); insert_cns_arc(cns, (*cns).ei, sl - 1 + (*cns).off, 0, 1, rid);
} else {
insert_cns_arc(cns, (*cns).si, (*cns).ei, 1, 1, rid);
insert_cns_arc(cns, (*cns).ei, (*cns).si, 0, 1, rid);
}
// prt_cns_arc(cns, 0, __func__);
// prt_cns_arc(cns, 1, __func__);
(*cns).bn = (*cns).n; (*cns).bb1 = (*cns).n;
}
///[s, e)
uint32_t push_cns_c0(cns_gfa *cns, uint64_t s0, uint64_t s, uint64_t e, uint32_t plus0, uint32_t rid)
{
if(s > e) return s0;///it is possible that s == e
uint32_t rr, k, re;
// rr = add_cns_arc(cns, s0, s, 1, plus0); assert(rr);
// rr = add_cns_arc(cns, s, s0, 0, plus0); assert(rr);
if(!add_cns_arc(cns, s0, s, 1, plus0)) {
insert_cns_arc(cns, s0, s, 1, plus0, rid);
insert_cns_arc(cns, s, s0, 0, plus0, rid);
} else {
rr = add_cns_arc(cns, s, s0, 0, plus0); assert(rr);
}
(*cns).a[s].sc++; re = s;
for (k = s + 1; k < e; k++) {
rr = add_cns_arc(cns, k-1, k, 1, 1);
// if(!rr) {
// fprintf(stderr, "[M::%s] s0::%u, s::%u\n", __func__, k-1, k);
// prt_cns_arc(cns, k-1, __func__); prt_cns_arc(cns, k, __func__);
// }
assert(rr);
rr = add_cns_arc(cns, k, k-1, 0, 1); assert(rr);
(*cns).a[k].sc++; re = k;
}
return re;
}
uint32_t trace_cns_bp(cns_gfa *cns, uint64_t s0, char *tstr, uint64_t tl, asg32_v* b32, uint32_t plus0, uint32_t *rn, uint64_t max_trace, uint32_t av_bp)
{
(*rn) = s0;
if(tl <= 0) return 0;
// fprintf(stderr, "\n[M::%s] tl::%lu\n", __func__, tl);
uint32_t k, i, s, e, m, bp, nm, bi, bn0, src, des, ff = 0; b32->n = 0;
kv_push(uint32_t, (*b32), s0); kv_push(uint32_t, (*b32), ((uint32_t)-1)); nm = 2;
// if(s0 == 2863) {
// fprintf(stderr, "***0***[M::%s] s::%lu\tb32->n::%u\n", __func__, s0, (uint32_t)b32->n);
// }
for (i = 0; (i < tl) && (!ff); i++) {
bp = seq_nt6_table[(uint32_t)(tstr[i])]; bn0 = b32->n;
for (bi = bn0 - nm; bi < bn0; bi += 2) {
m = b32->a[bi]; s = 0; e = (*cns).a[m].arc.nou;
for (k = s; k < e; k++) {
if((*cns).a[m].arc.a[k].v == 0 || (*cns).a[m].arc.a[k].v == 1) continue;
if(av_bp && (*cns).a[m].arc.a[k].v >= (*cns).bb0 && (*cns).a[m].arc.a[k].v < (*cns).bb1) continue;///no backbone
if((*cns).a[(*cns).a[m].arc.a[k].v].c == bp) {
kv_push(uint32_t, (*b32), (*cns).a[m].arc.a[k].v);
kv_push(uint32_t, (*b32), bi);
// if(s0 == 2863) {
// fprintf(stderr, "***1***[M::%s] s::%u\tb32->n::%u\n", __func__, (*cns).a[m].arc.a[k].v, (uint32_t)b32->n);
// }
// if((i + 1) == tl) break;///quick end
// if(b32->n > max_trace) break;///redue the size of b32
if(((i + 1) == tl) || (b32->n > max_trace)) {
ff = 1; break;
}
}
}
if(ff) break;
}
if(b32->n <= bn0) {///no node
break;
} else {
nm = b32->n - bn0;
}
}
// fprintf(stderr, "[M::%s] b32->n::%u, nm::%u\n", __func__, (uint32_t)b32->n, nm);
// if(s0 == 2863) {
// fprintf(stderr, "[M::%s] i::%u\tnm::%u\tb32->n::%u\n", __func__, i, nm, (uint32_t)b32->n);
// }
if(i > 0 && nm > 0) {
(*rn) = b32->a[b32->n - nm];
for (bi = b32->n - nm; b32->a[bi + 1] != ((uint32_t)-1); bi = b32->a[bi + 1]) {
// fprintf(stderr, "[M::%s] bi::%u, p_bi::%u\n", __func__, bi, b32->a[bi + 1]);
des = b32->a[bi]; src = b32->a[b32->a[bi + 1]]; bp = ((src!=s0)?(1):(plus0));
m = add_cns_arc(cns, src, des, 1, bp); assert(m);
m = add_cns_arc(cns, des, src, 0, bp); assert(m);
(*cns).a[des].sc++;
// if(s0 == 2863) {
// fprintf(stderr, "***2***[M::%s] src::%u\tdes::%u\n", __func__, src, des);
// }
}
} else {
i = 0;
}
return i;
}
///[s, e)
uint32_t push_cns_c1(cns_gfa *cns, uint64_t s0, char *tstr, uint64_t tl, uint32_t plus0, asg32_v* b32, uint64_t max_trace, uint32_t rid)
{
if(tl <= 0) return s0;
uint32_t rr = plus0, k, re = s0;
k = trace_cns_bp(cns, s0, tstr, tl, b32, plus0, &re, max_trace, 1);
if(k > 0) rr = 1;
// if(s0 == 2863) {
// fprintf(stderr, "[M::%s] (%.*s)\ts0::%lu\tk::%u\ttl::%lu\tre::%u\n", __func__, tstr?((int)(tl)):0, tstr, s0, k, tl, re);
// }
// fprintf(stderr, "[M::%s] s0::%u, s::%u\n", __func__, k-1, k);
// prt_cns_arc(cns, k-1, __func__); prt_cns_arc(cns, k, __func__);
for (; k < tl; k++) {///the weight of (s0 -> tstr[0]) might be 0
re = add_cns_arc_bp(cns, re, seq_nt6_table[(uint32_t)(tstr[k])], rr, rid, 1); rr = 1;
}
return re;
}
uint64_t append_cns_g(cns_gfa *cns, char *tstr, uint64_t tl, uint64_t qs, uint64_t qe, uint64_t cp, uint64_t cl, uint64_t pe, asg32_v* b32, uint64_t max_trace, uint32_t rid, int64_t insert_pos)
{
// fprintf(stderr, ">q::[%lu, %lu)\n", qs, qe);
uint64_t s0 = pe, plus0 = 1, ns = qs + cns->off, ne = qe + cns->off;
if(pe == ((uint64_t)-1)) {
if(qs > 0) {
s0 = qs - 1 + cns->off;///just before node in backbone
} else {
s0 = 0;//beg
}
// plus0 = 0;
}
// fprintf(stderr, "+n_nodes::%u, tl::%lu, qs::%lu, qe::%lu\n", (uint32_t)cns->n, tl, qs, qe);
if(cp == 0) {
if((cl == 0) && (cp == 0) && (qs == qe) && (((int64_t)qe) == insert_pos)) {
s0 = 0;//beg
ns = ne = 1;//end
plus0 = 1;
}
// if(cp == 0) {
// fprintf(stderr, "cp::%lu, cl::%lu, qs::%lu, qe::%lu, s0::%lu, ns::%lu, ne::%lu, plus0::%lu\n", cp, cl, qs, qe, s0, ns, ne, plus0);
// }
return push_cns_c0(cns, s0, ns, ne, plus0, rid);
} else if(cp == 1 || cp == 2) { ///cp == 2: more y -> insertion
return push_cns_c1(cns, s0, tstr, tl, plus0, b32, max_trace, rid);
} else { ///more x -> do nothing
return s0;
}
}
char *get_sub_seq(rr_seq_t *ssq, uint64_t s, uint64_t e)
{
if(s >= e) return NULL;
if(s >= ssq->s && e <= ssq->e) {
return ssq->tu->seq + s - ssq->s;
}
uint64_t l = e - s;
if(ssq->s >= ssq->e) {
if(l < ssq->n0) l = ssq->n0;
} else {
if(l < ssq->n1) l = ssq->n1;
}
ssq->s = s; ssq->e = s + l;
if(ssq->e > Get_READ_LENGTH((*(ssq->rref)), ssq->id)) {
ssq->e = Get_READ_LENGTH((*(ssq->rref)), ssq->id);
l = ssq->e - ssq->s;
}
UC_Read_resize((*(ssq->tu)), ((int64_t)l));
recover_UC_Read_sub_region(ssq->tu->seq, ssq->s, l, ssq->rev, ssq->rref, ssq->id);
return ssq->tu->seq;
}
///[s, e)
uint32_t extract_sub_cigar_cns(overlap_region *z, int64_t s, int64_t e, int64_t iws, int64_t iwe, int64_t s_end, rr_seq_t *ssq, ul_ov_t *p, cns_gfa *cns, asg32_v* b32, uint64_t max_trace, uint32_t rid)
{
// if(s == 10539 && e == 10760) {
// fprintf(stderr, "\n>>>>>>iw::[%ld, %ld)\tw::[%ld, %ld)\tox::[%u, %u)<<<<<<\n", iws, iwe, s, e, z->x_pos_s, z->x_pos_e + 1);
// }
int64_t wk = ovlp_cur_wid(*p), xk = ovlp_cur_xoff(*p), yk = ovlp_cur_yoff(*p), ck = ovlp_cur_coff(*p), os, oe, ots, ote, ol, insert_pos = ((iws == iwe)? (0): (-1));
bit_extz_t ez; int64_t bd = ovlp_bd(*p), s0, e0, ii[2], it[2]; uint64_t pe = (uint64_t)-1;
s0 = ((int64_t)(z->w_list.a[wk].x_start)) + bd;
e0 = ((int64_t)(z->w_list.a[wk].x_end)) + 1 - bd;
if(s < s0) s = s0; if(e > e0) e = e0;///exclude boundary
if(s > e) return -1;///it is possible s == e
os = MAX(s, s0); oe = MIN(e, e0);
if(oe < os) return -1;///it is possible os == oe
set_bit_extz_t(ez, (*z), wk);
if(!ez.cigar.n) return -1;
int64_t cn = ez.cigar.n; uint16_t op; int64_t ws, we, wts, wte, ovlp; char *tstr;
if((ck < 0) || (ck > cn)) {//(*ck) == cn is allowed
ck = 0; xk = ez.ts; yk = ez.ps;
}
while (ck > 0 && xk >= s) {///x -> t; y -> p; first insertion and then match/mismatch
--ck;
op = ez.cigar.a[ck]>>14;
if(op!=2) xk -= (ez.cigar.a[ck]&(0x3fff));
if(op!=3) yk -= (ez.cigar.a[ck]&(0x3fff));
}
if(s_end == 0 && s == iws) s_end = 0;
else s_end = 1;
// if(s_end == 0 || s != iws) {///do not conside the insertion before s
// while (ck < cn && xk < s) {
// }
// }
// if(s == 10539 && e == 10760) {
// fprintf(stderr, "ck::%ld, cn::%ld, xk::%ld, yk::%ld\n", ck, cn, xk, yk);
// }
// char cm[4]; cm[0] = 'M'; cm[1] = 'S'; cm[2] = 'I'; cm[3] = 'D';
//some cigar will span s or e
ii[0] = ii[1] = it[0] = it[1] = -1;
ssq->s = ssq->e = 0; ssq->n0 = e - s;
ssq->id = z->y_id; ssq->rev = z->y_pos_strand;
if(ssq->n0 == 0) {ssq->n0 = ssq->n1;}
while (ck < cn && xk < e) {//[s, e)
ws = xk; wts = yk;
op = ez.cigar.a[ck]>>14; ol = (ez.cigar.a[ck]&(0x3fff));
for (ck++; (ck < cn) && (op == (ez.cigar.a[ck]>>14)); ck++) {
ol += (ez.cigar.a[ck]&(0x3fff));
}
///op == 3: -> x; op == 2: -> y;
if(op!=2) xk += ol;
if(op!=3) yk += ol;
we = xk; wte = yk;
// fprintf(stderr, "ck::%ld, cn::%ld, op::%u, ol::%ld\n", ck, cn, op, ol);
os = MAX(s, ws); oe = MIN(e, we);
ovlp = ((oe>os)? (oe-os):0);
if(s == e) {///insertion in comparsion with the reference
if(op != 0 || ws >= s || we <= e || e != iwe || s != iws) continue;///must be a match
} else {
if(op != 2) {
if(!ovlp) continue;
} else {///ws == we
if(ws < s || ws >= e) continue;
}
}
if((s_end == 0) && (op == 2) && (ws == s)) continue;///skip the insertion just before s
if(op < 2) {
ots = os - ws + wts; ote = oe - ws + wts;
} else {///op == 2: more y; p == 3: more x
ots = wts; ote = wte;
}
if(ii[0] == -1) {
ii[0] = os; it[0] = ots;
// if(op < 2) {
// it[0] = os - ws + wts;
// } else {///op == 2: more y; p == 3: more x
// it[0] = wts;
// }
}
ii[1] = oe; it[1] = ote;
// if(op < 2) {
// it[1] = oe - ws + wts;
// } else {///op == 2: more y; p == 3: more x
// it[1] = wte;
// }
if(op != 2) ol = oe-os;
tstr = NULL;
if(op != 0) tstr = get_sub_seq(ssq, ots, ote);
// if(s == 10539 && e == 10760) {
// fprintf(stderr, "+0-%ld%c(%.*s)\tpe::%lu", ol, cm[op], tstr?((int)(ote - ots)):0, tstr, pe);
// }
// fprintf(stderr, ">q::[%ld, %ld)\n", ws, we);
// fprintf(stderr, "%ld%c(%.*s)", ol, cm[op], tstr?((int)(ote - ots)):0, tstr);
pe = append_cns_g(cns, tstr, ote - ots, os - iws, oe - iws, op, ol, pe, b32, max_trace, rid, insert_pos);
// if(s == 10539 && e == 10760) {
// fprintf(stderr, "+1-pe::%lu\n", pe);
// }
}
while (ck < cn && xk <= e) {//[s, e)
ws = xk; wts = yk;
op = ez.cigar.a[ck]>>14; ol = (ez.cigar.a[ck]&(0x3fff));
if(op != 2) break;
for (ck++; (ck < cn) && (op == (ez.cigar.a[ck]>>14)); ck++) {
ol += (ez.cigar.a[ck]&(0x3fff));
}
yk += ol;//yk += (ez.cigar.a[ck]&(0x3fff));
we = xk; wte = yk;
if(ws >= s && ws <= e) {
ots = wts; ote = wte;
if(ii[0] == -1) {
ii[0] = ws; it[0] = ots;
}
ii[1] = we; it[1] = ote;
tstr = NULL;
if(op != 0) tstr = get_sub_seq(ssq, ots, ote);
// if(s == 10539 && e == 10760) {
// fprintf(stderr, "-0-%ld%c(%.*s)\tpe::%lu", ol, cm[op], tstr?((int)(ote - ots)):0, tstr, pe);
// }
// fprintf(stderr, "%ld%c(%.*s)", ol, cm[op], tstr?((int)(ote - ots)):0, tstr);
pe = append_cns_g(cns, tstr, ote - ots, ws - iws, we - iws, op, ol, pe, b32, max_trace, rid, insert_pos);
// if(s == 10539 && e == 10760) {
// fprintf(stderr, "-1-pe::%lu\n", pe);
// }
}
}