-
Notifications
You must be signed in to change notification settings - Fork 6
/
dist.cpp
2728 lines (2394 loc) · 123 KB
/
dist.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 <string>
#include <set>
#include <vector>
#include <cstdio>
#include <chrono>
#include <utility>
#include <queue>
#include <thread>
#include "dist.h"
#include "edit.h"
#include "print.h"
#include "cluster.h"
template <typename T>
inline bool contains(const std::unordered_set<T> & wave, const T & idx) {
return wave.find(idx) != wave.end();
}
template <typename T, typename U>
inline bool contains(const std::unordered_map<T,U> & wave, const T & idx) {
return wave.find(idx) != wave.end();
}
/******************************************************************************/
int calc_ng50(std::vector<int> phase_blocks, size_t total_bases) {
size_t total_phased = 0;
std::sort(phase_blocks.begin(), phase_blocks.end(), std::greater<>());
for (size_t i = 0; i < phase_blocks.size(); i++) {
total_phased += phase_blocks[i];
if (total_phased >= total_bases / 2)
return phase_blocks[i];
}
return 0;
}
/******************************************************************************/
/* Reverse two strings and their associated pointers for reverse alignment. */
void reverse_ptrs_strs(
std::string & query, std::string & ref,
std::vector< std::vector<int> > & query_ptrs,
std::vector< std::vector<int> > & ref_ptrs) {
// reverse strings and vectors
std::reverse(query.begin(), query.end());
std::reverse(ref.begin(), ref.end());
std::reverse(query_ptrs[PTRS].begin(), query_ptrs[PTRS].end());
std::reverse(query_ptrs[FLAGS].begin(), query_ptrs[FLAGS].end());
std::reverse(ref_ptrs[PTRS].begin(), ref_ptrs[PTRS].end());
std::reverse(ref_ptrs[FLAGS].begin(), ref_ptrs[FLAGS].end());
// update pointers
for (int & ptr : query_ptrs[PTRS]) { ptr = ref.size()-1 - ptr; }
for (int & ptr : ref_ptrs[PTRS]) { ptr = query.size()-1 - ptr; }
for (int & flags : query_ptrs[FLAGS]) { // flip beg/end if only one is set
if (flags & PTR_VAR_BEG && !(flags & PTR_VAR_END)) {
flags = PTR_VARIANT | PTR_VAR_END;
} else if (flags & PTR_VAR_END && !(flags & PTR_VAR_BEG)) {
flags = PTR_VARIANT | PTR_VAR_BEG;
}
}
for (int & flags : ref_ptrs[FLAGS]) { // flip beg/end if only one is set
if (flags & PTR_VAR_BEG && !(flags & PTR_VAR_END)) {
flags = PTR_VARIANT | PTR_VAR_END;
} else if (flags & PTR_VAR_END && !(flags & PTR_VAR_BEG)) {
flags = PTR_VARIANT | PTR_VAR_BEG;
}
}
return;
}
/******************************************************************************/
/* Generate the new sequence by applying variants to the reference. */
std::string generate_str(
std::shared_ptr<fastaData> ref,
std::shared_ptr<ctgVariants> vars, const std::string & ctg,
int beg_idx, int end_idx, int beg_pos, int end_pos,
int min_qual /* = 0 */) {
int var_idx = beg_idx;
std::string str = "";
while (var_idx < vars->n && vars->poss[var_idx] < beg_pos) var_idx++;
for (int ref_pos = beg_pos; ref_pos < end_pos; ) {
// VARIANT
if (var_idx < end_idx && ref_pos == vars->poss[var_idx]) {
if (vars->var_quals[var_idx] >= min_qual) {
switch (vars->types[var_idx]) {
case TYPE_INS:
str += vars->alts[var_idx];
break;
case TYPE_DEL:
ref_pos += vars->refs[var_idx].size();
break;
case TYPE_SUB:
str += vars->alts[var_idx];
ref_pos++;
break;
case TYPE_CPX:
str += vars->alts[var_idx];
ref_pos += vars->refs[var_idx].size();
break;
}
}
var_idx++; // next variant
} else { // NO VARIANT
try {
// add entire ref seq up to next variant or region end
int ref_end = (var_idx < end_idx) ?
std::min(end_pos, vars->poss[var_idx]) : end_pos;
if (ref_end < ref_pos) {
printf("ref_pos:%d ref_end:%d beg_pos: %d end_pos: %d\n",
ref_pos, ref_end, beg_pos, end_pos);
for(int i = beg_idx; i < end_idx; i++) {
printf("%s:%d %s -> %s\n", ctg.data(), vars->poss[i],
vars->refs[i].data(), vars->alts[i].data());
}
ERROR("No variant, but ref_end < ref_pos (generate_str)");
}
str += ref->fasta.at(ctg).substr(ref_pos, ref_end-ref_pos);
ref_pos = ref_end;
} catch (const std::out_of_range & e) {
ERROR("Contig '%s' not in reference FASTA or position out of range (generate_str)", ctg.data());
}
}
}
return str;
}
/******************************************************************************/
/* For each position on hap1 and hap2, it generates pointers from the query to
* the reference and vice versa, as well as the strings for alignment.
*/
void generate_ptrs_strs(
std::string & query_str, std::string & ref_str,
std::vector< std::vector<int> > & query_ptrs,
std::vector< std::vector<int> > & ref_ptrs,
std::shared_ptr<ctgVariants> query_vars,
size_t query_clust_beg_idx, size_t query_clust_end_idx,
int beg_pos, int end_pos,
std::shared_ptr<fastaData> ref, const std::string & ctg,
int min_qual /* = 0 */
) {
// generate query and ref strings and pointers
query_ptrs.resize(PTR_DIMS);
ref_ptrs.resize(PTR_DIMS);
int query_var_idx = query_vars->clusters.size() ?
query_vars->clusters[query_clust_beg_idx] : 0;
int query_end_idx = query_vars->clusters.size() ?
query_vars->clusters[query_clust_end_idx] : 0;
for (int ref_pos = beg_pos; ref_pos <= end_pos; ) {
if (query_var_idx < query_end_idx &&
ref_pos == query_vars->poss[query_var_idx]) { // start query variant
if (query_vars->var_quals[query_var_idx] >= min_qual) {
switch (query_vars->types[query_var_idx]) {
case TYPE_INS:
query_ptrs[PTRS].insert(query_ptrs[PTRS].end(),
query_vars->alts[query_var_idx].size(), ref_str.size()-1);
query_ptrs[FLAGS].insert(query_ptrs[FLAGS].end(),
query_vars->alts[query_var_idx].size(), PTR_VARIANT);
query_ptrs[FLAGS][query_ptrs[FLAGS].size()-1] |= PTR_VAR_END;
query_ptrs[FLAGS][query_ptrs[FLAGS].size() -
query_vars->alts[query_var_idx].size()] |= PTR_VAR_BEG | PTR_INS_LOC;
query_str += query_vars->alts[query_var_idx];
break;
case TYPE_DEL:
ref_ptrs[PTRS].insert(ref_ptrs[PTRS].end(),
query_vars->refs[query_var_idx].size(), query_str.size()-1);
ref_ptrs[FLAGS].insert(ref_ptrs[FLAGS].end(),
query_vars->refs[query_var_idx].size(), PTR_VARIANT);
ref_ptrs[FLAGS][ref_ptrs[FLAGS].size()-1] |= PTR_VAR_END;
ref_ptrs[FLAGS][ref_ptrs[FLAGS].size() -
query_vars->refs[query_var_idx].size()] |= PTR_VAR_BEG;
ref_str += query_vars->refs[query_var_idx];
ref_pos += query_vars->refs[query_var_idx].size();
break;
case TYPE_SUB:
ref_ptrs[PTRS].push_back(query_str.size());
ref_ptrs[FLAGS].push_back(PTR_VARIANT|PTR_VAR_BEG|PTR_VAR_END);
query_ptrs[PTRS].push_back(ref_str.size());
query_ptrs[FLAGS].push_back(PTR_VARIANT|PTR_VAR_BEG|PTR_VAR_END);
ref_str += query_vars->refs[query_var_idx];
query_str += query_vars->alts[query_var_idx];
ref_pos++;
break;
default:
ERROR("Unexpected variant type '%d' in generate_ptrs_strs()",
query_vars->types[query_var_idx]);
}
}
query_var_idx++; // next variant
} else { // add all matching ref bases
try {
// find next position w/o ref match (next var or end)
int ref_end = (query_var_idx < query_end_idx) ?
query_vars->poss[query_var_idx] : end_pos+1;
// add pointers, query
std::vector<int> new_query_ptrs(ref_end - ref_pos, 0);
query_ptrs[FLAGS].insert(query_ptrs[FLAGS].end(),
new_query_ptrs.begin(), new_query_ptrs.end()); // zeros
for(size_t i = 0; i < new_query_ptrs.size(); i++)
new_query_ptrs[i] = ref_str.size() + i;
query_ptrs[PTRS].insert(query_ptrs[PTRS].end(),
new_query_ptrs.begin(), new_query_ptrs.end());
// add pointers, ref
std::vector<int> new_ref_ptrs(ref_end - ref_pos, 0);
ref_ptrs[FLAGS].insert(ref_ptrs[FLAGS].end(),
new_ref_ptrs.begin(), new_ref_ptrs.end()); // zeros
for(size_t i = 0; i < new_ref_ptrs.size(); i++)
new_ref_ptrs[i] = query_str.size() + i;
ref_ptrs[PTRS].insert(ref_ptrs[PTRS].end(),
new_ref_ptrs.begin(), new_ref_ptrs.end());
// add sequence, update positions
std::string matches = ref->fasta.at(ctg).substr(ref_pos, ref_end-ref_pos);
query_str += matches;
ref_str += matches;
ref_pos = ref_end;
} catch (const std::out_of_range & e) {
ERROR("Contig '%s' not present in reference FASTA", ctg.data());
}
}
}
}
/******************************************************************************/
/* Calculate initial forward-pass alignment for truth and query strings, given
* pointers to/from reference. This function generates the pointer matrix,
* scores for each alignment, and pointer to if alignment ends on QUERY/REF.
*/
void calc_prec_recall_aln(
const std::string & query1, const std::string & query2,
const std::string & truth1, const std::string & truth2,
const std::string & ref,
const std::vector< std::vector<int> > & query1_ref_ptrs,
const std::vector< std::vector<int> > & ref_query1_ptrs,
const std::vector< std::vector<int> > & query2_ref_ptrs,
const std::vector< std::vector<int> > & ref_query2_ptrs,
const std::vector< std::vector<int> > & truth1_ref_ptrs,
const std::vector< std::vector<int> > & truth2_ref_ptrs,
std::vector<int> & s,
std::vector< std::vector< std::vector<uint8_t> > > & ptrs,
std::vector< std::shared_ptr< std::unordered_map<idx1, idx1> > > & swap_pred_maps,
std::vector<int> & pr_query_ref_end,
int aln_start, int aln_stop, bool print
) {
// set loop variables
int ref_len = ref.size();
std::vector<std::string> query {query1, query1, query2, query2};
std::vector<std::string> truth {truth1, truth2, truth1, truth2};
std::vector< std::vector< std::vector<int> > > query_ref_ptrs {
query1_ref_ptrs, query1_ref_ptrs, query2_ref_ptrs, query2_ref_ptrs };
std::vector< std::vector< std::vector<int> > > truth_ref_ptrs {
truth1_ref_ptrs, truth2_ref_ptrs, truth1_ref_ptrs, truth2_ref_ptrs };
std::vector< std::vector< std::vector<int> > > ref_query_ptrs {
ref_query1_ptrs, ref_query1_ptrs, ref_query2_ptrs, ref_query2_ptrs };
std::vector<int> query_lens =
{int(query1.size()), int(query1.size()), int(query2.size()), int(query2.size())};
std::vector<int> truth_lens =
{int(truth1.size()), int(truth2.size()), int(truth1.size()), int(truth2.size())};
std::vector< std::vector< std::vector<bool> > > done;
// for each combination of query and truth
for (int i = aln_start; i < aln_stop; i++) {
int qi = 2*i + QUERY; // query index (ptrs)
int ri = 2*i + REF; // ref index (ptrs)
int dqi = 2*(i-aln_start) + QUERY;
int dri = 2*(i-aln_start) + REF;
// init full pointer/done matrices
done.push_back(std::vector< std::vector<bool> >(query_lens[i],
std::vector<bool>(truth_lens[i], false)));
done.push_back(std::vector< std::vector<bool> >(ref_len,
std::vector<bool>(truth_lens[i], false)));
// set first wavefront
std::queue<idx1> queue; // still to be explored in this wave
queue.push({qi, 0, 0});
ptrs[qi][0][0] |= PTR_MAT;
done[dqi][0][0] = true;
queue.push({ri, 0, 0});
ptrs[ri][0][0] |= PTR_MAT;
done[dqi][0][0] = true;
// continue looping until full alignment found
std::unordered_set<idx1> curr_wave; // everything explored this wave
std::unordered_set<idx1> prev_wave; // everything explored prev wave
/* if (print) printf("\nFWD %s aln: (%d|%d, %d|%d, %d)\n", aln_strs[i].data(), */
/* qi, ri, query_lens[i], ref_len, truth_lens[i]); */
while (true) {
if (print) printf(" s = %d\n", s[i]);
if (queue.empty()) ERROR("Empty queue in 'prec_recall_aln()'.");
// EXTEND WAVEFRONT (stay at same score)
while (!queue.empty()) {
idx1 x = queue.front(); queue.pop();
/* if (print) printf(" x = (%s, %d, %d)\n", */
/* (x.hi % 2) ? "REF " : "QUERY", x.qri, x.ti); */
prev_wave.insert(x);
if (x.hi == qi) { // QUERY
// allow match on query
idx1 y(qi, x.qri+1, x.ti+1);
if (y.qri < query_lens[i] && y.ti < truth_lens[i] &&
query[i][y.qri] == truth[i][y.ti]) {
if (!done[dqi][y.qri][y.ti]) {
if (!contains(curr_wave, y)) {
queue.push(y); curr_wave.insert(y);
}
ptrs[y.hi][y.qri][y.ti] |= PTR_MAT;
}
}
// allow match, swapping to reference
idx1 z(ri, query_ref_ptrs[i][PTRS][x.qri]+1, x.ti+1);
if ( (!(query_ref_ptrs[i][FLAGS][x.qri] & PTR_VARIANT) ||
query_ref_ptrs[i][FLAGS][x.qri] & PTR_VAR_END) &&
(!(truth_ref_ptrs[i][FLAGS][x.ti] & PTR_VARIANT) ||
truth_ref_ptrs[i][FLAGS][x.ti] & PTR_VAR_END)) {
if (z.qri < ref_len && z.ti < truth_lens[i] &&
ref[z.qri] == truth[i][z.ti]) {
if (!done[dri][z.qri][z.ti]) {
if (!contains(curr_wave, z)) {
queue.push(z); curr_wave.insert(z);
}
ptrs[z.hi][z.qri][z.ti] |= PTR_SWP_MAT;
(*swap_pred_maps[i])[z] = x;
}
}
}
} else { // x.hi == ri == REF
// allow match
idx1 y(ri, x.qri+1, x.ti+1);
if (y.qri < ref_len && y.ti < truth_lens[i] &&
ref[y.qri] == truth[i][y.ti]) {
if (!done[dri][y.qri][y.ti]) {
if (!contains(curr_wave, y)) {
queue.push(y); curr_wave.insert(y);
}
ptrs[y.hi][y.qri][y.ti] |= PTR_MAT;
}
}
// allow match, swapping to query
idx1 z(qi, ref_query_ptrs[i][PTRS][x.qri]+1, x.ti+1);
if ( (!(ref_query_ptrs[i][FLAGS][x.qri] & PTR_VARIANT) ||
ref_query_ptrs[i][FLAGS][x.qri] & PTR_VAR_END) &&
(!(truth_ref_ptrs[i][FLAGS][x.ti] & PTR_VARIANT) ||
truth_ref_ptrs[i][FLAGS][x.ti] & PTR_VAR_END)) {
if (z.qri < query_lens[i] && z.ti < truth_lens[i] &&
query[i][z.qri] == truth[i][z.ti]) {
if (!done[dqi][z.qri][z.ti]) {
if (!contains(curr_wave, z)) {
queue.push(z); curr_wave.insert(z);
}
ptrs[z.hi][z.qri][z.ti] |= PTR_SWP_MAT;
(*swap_pred_maps[i])[z] = x;
}
}
}
}
}
// mark all cells visited this wave as done
for (idx1 x : curr_wave) {
done[x.hi == ri ? dri : dqi][x.qri][x.ti] = true;
}
curr_wave.clear();
// exit if we're done aligning
if (done[dqi][query_lens[i]-1][truth_lens[i]-1] ||
done[dri][ref_len-1][truth_lens[i]-1]) break;
// NEXT WAVEFRONT (increase score by one)
for (idx1 x : prev_wave) {
int qr_len = (x.hi == qi) ? query_lens[i] : ref_len;
if (x.qri+1 < qr_len) { // INS
idx1 y(x.hi, x.qri+1, x.ti);
if (!done[y.hi == ri ? dri : dqi][y.qri][y.ti] && !contains(curr_wave, y)) {
queue.push(y);
curr_wave.insert(y);
}
if (!done[y.hi == ri ? dri : dqi][y.qri][y.ti])
ptrs[y.hi][y.qri][y.ti] |= PTR_INS;
}
if (x.ti+1 < truth_lens[i]) { // DEL
idx1 y(x.hi, x.qri, x.ti+1);
if (!done[y.hi == ri ? dri : dqi][y.qri][y.ti] && !contains(curr_wave, y)) {
queue.push(y);
curr_wave.insert(y);
}
if (!done[y.hi == ri ? dri : dqi][y.qri][y.ti])
ptrs[y.hi][y.qri][y.ti] |= PTR_DEL;
}
if (x.qri+1 < qr_len && x.ti+1 < truth_lens[i]) { // SUB
idx1 y(x.hi, x.qri+1, x.ti+1);
if (!done[y.hi == ri ? dri : dqi][y.qri][y.ti] && !contains(curr_wave, y)) {
queue.push(y);
curr_wave.insert(y);
}
if (!done[y.hi == ri ? dri : dqi][y.qri][y.ti])
ptrs[y.hi][y.qri][y.ti] |= PTR_SUB;
}
}
prev_wave.clear();
s[i]++;
} // while loop (this alignment)
if (print) printf("\nAlignment %s, aln_ptrs\n", aln_strs[i].data());
if (print) printf("\nQUERY");
if (print) print_ptrs(ptrs[qi], query[i], truth[i]);
if (print) printf("\nREF");
if (print) print_ptrs(ptrs[ri], ref, truth[i]);
// save where to start backtrack (prefer query to maximize TPs)
if (done[dqi][query_lens[i]-1][truth_lens[i]-1]) {
pr_query_ref_end[i] = qi;
} else if (done[dri][ref_len-1][truth_lens[i]-1]) {
pr_query_ref_end[i] = ri;
} else { ERROR("Alignment not finished in 'prec_recall_aln()'."); }
} // 4 alignments
} // function
/******************************************************************************/
int store_phase(
superclusterData * clusterdata_ptr,
const std::string & ctg, int sc_idx,
const std::vector<int> & s
) {
// calculate best phasing
int orig_phase_dist = s[QUERY1_TRUTH1] + s[QUERY2_TRUTH2];
int swap_phase_dist = s[QUERY2_TRUTH1] + s[QUERY1_TRUTH2];
int phase = PHASE_NONE; // default either way if insufficient evidence
if (orig_phase_dist == swap_phase_dist) { // allow either phasing if equal dist
phase = PHASE_NONE;
} else if (orig_phase_dist == 0) { // protect division by zero
phase = PHASE_ORIG;
} else if (swap_phase_dist == 0) { // protect division by zero
phase = PHASE_SWAP;
} else if (1 - float(swap_phase_dist) / orig_phase_dist > g.phase_threshold) { // significant reduction by swapping
phase = PHASE_SWAP;
} else if (1 - float(orig_phase_dist) / swap_phase_dist > g.phase_threshold) { // significant reduction with orig
phase = PHASE_ORIG;
}
// save alignment information
clusterdata_ptr->superclusters[ctg]->set_phase(sc_idx,
phase, orig_phase_dist, swap_phase_dist);
return phase;
}
/******************************************************************************/
/* Given alignment pointers (aln_ptrs) from minED forward-pass, as well as
* reference pointers to/from truth/query, backtrack and find path which
* maximizes FP variants. Results are stored in path_ptrs. Then call
* get_prec_recall_path_sync() to parse this path.
*/
void calc_prec_recall_path(
const std::string & ref,
const std::string & query1, const std::string & query2,
const std::string & truth1, const std::string & truth2,
std::vector< std::vector<idx1> > & path,
std::vector< std::vector<bool> > & sync,
std::vector< std::vector<bool> > & edits,
std::vector< std::vector< std::vector<uint8_t> > > & aln_ptrs,
std::vector< std::vector< std::vector<uint8_t> > > & path_ptrs,
std::vector< std::vector< std::vector<int16_t> > > & path_scores,
const std::vector< std::vector<int> > & query1_ref_ptrs,
const std::vector< std::vector<int> > & ref_query1_ptrs,
const std::vector< std::vector<int> > & query2_ref_ptrs,
const std::vector< std::vector<int> > & ref_query2_ptrs,
const std::vector< std::vector<int> > & truth1_ref_ptrs,
const std::vector< std::vector<int> > & truth2_ref_ptrs,
const std::vector< std::shared_ptr< std::unordered_map<idx1,idx1> > > & swap_pred_maps,
const std::vector<int> & pr_query_ref_end, bool print
) {
std::vector<std::string> query = {query1, query1, query2, query2};
std::vector<std::string> truth = {truth1, truth2, truth1, truth2};
std::vector< std::vector< std::vector<int> > > query_ref_ptrs = {
query1_ref_ptrs, query1_ref_ptrs, query2_ref_ptrs, query2_ref_ptrs };
std::vector< std::vector< std::vector<int> > > ref_query_ptrs = {
ref_query1_ptrs, ref_query1_ptrs, ref_query2_ptrs, ref_query2_ptrs };
std::vector< std::vector< std::vector<int> > > truth_ref_ptrs = {
truth1_ref_ptrs, truth2_ref_ptrs, truth1_ref_ptrs, truth2_ref_ptrs };
std::vector<int> pr_query_ref_beg(CALLSETS*HAPS);
std::vector< std::vector< std::vector<bool> > > done;
for (int i = 0; i < CALLSETS*HAPS; i++) {
/* printf("\nBKWD %s path\n", aln_strs[i].data()); */
// init
int qi = i*2 + QUERY;
int ri = i*2 + REF;
path_ptrs.push_back(std::vector< std::vector<uint8_t> >(aln_ptrs[qi].size(),
std::vector<uint8_t>(aln_ptrs[qi][0].size(), PTR_NONE)));
path_ptrs.push_back(std::vector< std::vector<uint8_t> >(aln_ptrs[ri].size(),
std::vector<uint8_t>(aln_ptrs[ri][0].size(), PTR_NONE)));
path_scores.push_back(std::vector< std::vector<int16_t> >(aln_ptrs[qi].size(),
std::vector<int16_t>(aln_ptrs[qi][0].size(), -1)));
path_scores.push_back(std::vector< std::vector<int16_t> >(aln_ptrs[ri].size(),
std::vector<int16_t>(aln_ptrs[ri][0].size(), -1)));
done.push_back(std::vector< std::vector<bool> >(aln_ptrs[qi].size(),
std::vector<bool>(aln_ptrs[qi][0].size(), false)));
done.push_back(std::vector< std::vector<bool> >(aln_ptrs[ri].size(),
std::vector<bool>(aln_ptrs[ri][0].size(), false)));
// backtrack start
std::queue<idx1> queue;
int start_hi = pr_query_ref_end[i];
int start_qri = (start_hi % 2 == QUERY) ? query_ref_ptrs[i][0].size()-1 :
ref_query_ptrs[i][0].size()-1;
int start_ti = truth_ref_ptrs[i][0].size()-1;
idx1 start(start_hi, start_qri, start_ti);
path_ptrs[start_hi][start_qri][start_ti] = PTR_MAT;
aln_ptrs[start_hi][start_qri][start_ti] |= MAIN_PATH; // all alignments end here
path_scores[start_hi][start_qri][start_ti] = 0;
queue.push(start);
std::unordered_set<idx1> curr_wave, prev_wave;
while (true) {
while (!queue.empty()) {
idx1 x = queue.front(); queue.pop(); // current cell
prev_wave.insert(x);
/* printf("(%d, %d, %d)\n", x.hi, x.qri, x.ti); */
// MATCH movement
if (aln_ptrs[x.hi][x.qri][x.ti] & PTR_MAT && x.qri > 0 && x.ti > 0) {
// get next cell, add to path
idx1 y = idx1(x.hi, x.qri-1, x.ti-1);
aln_ptrs[y.hi][y.qri][y.ti] |= PATH;
// check if mvmt is in variant
bool in_truth_var = truth_ref_ptrs[i][FLAGS][x.ti] & PTR_VARIANT;
in_truth_var &= !(truth_ref_ptrs[i][FLAGS][x.ti] & PTR_VAR_BEG);
bool in_query_var = false;
if (x.hi == qi) {
in_query_var = query_ref_ptrs[i][FLAGS][x.qri] & PTR_VARIANT;
in_query_var &= !(query_ref_ptrs[i][FLAGS][x.qri] & PTR_VAR_BEG);
}
// check for tp
bool is_tp = x.hi == qi && ((query_ref_ptrs[i][PTRS][x.qri] !=
query_ref_ptrs[i][PTRS][y.qri]+1) || // INS
query_ref_ptrs[i][FLAGS][x.qri] & PTR_VAR_BEG); // SUB/DEL
// update score
if (!done[y.hi][y.qri][y.ti] &&
path_scores[x.hi][x.qri][x.ti] + is_tp > path_scores[y.hi][y.qri][y.ti]) {
path_ptrs[y.hi][y.qri][y.ti] = PTR_MAT;
path_scores[y.hi][y.qri][y.ti] = path_scores[x.hi][x.qri][x.ti] + is_tp;
if (!contains(curr_wave, y)) curr_wave.insert(y);
queue.push(y);
} else if (!done[y.hi][y.qri][y.ti] &&
path_scores[x.hi][x.qri][x.ti] + is_tp == path_scores[y.hi][y.qri][y.ti]) {
path_ptrs[y.hi][y.qri][y.ti] |= PTR_MAT;
}
if (print) printf("(%s, %2d, %2d) --%s-> (%s, %2d, %2d) %s %s %s\n",
x.hi % 2 ? "REF" : "QRY", x.qri, x.ti, "MAT",
y.hi % 2 ? "REF" : "QRY", y.qri, y.ti,
in_query_var ? GREEN("QUERY_VAR").data() : RED("QUERY_VAR").data(),
in_truth_var ? GREEN("TRUTH_VAR").data() : RED("TRUTH_VAR").data(),
is_tp ? GREEN("TP").data() : RED("TP").data());
}
if (x.hi == ri) { // REF -> QUERY SWAP mvmt
if (aln_ptrs[x.hi][x.qri][x.ti] & PTR_SWP_MAT &&
(!(ref_query_ptrs[i][FLAGS][x.qri] & PTR_VARIANT) ||
ref_query_ptrs[i][FLAGS][x.qri] & PTR_VAR_BEG) &&
x.qri > 0 && x.ti > 0) {
// get next cell, add to path
if (!contains((*swap_pred_maps[i]), x))
ERROR("No swap predecessor, but PTR_SWP_MAT set.");
idx1 z = (*swap_pred_maps[i]).at(x);
aln_ptrs[z.hi][z.qri][z.ti] |= PATH;
// check if mvmt is in variant
bool in_truth_var = truth_ref_ptrs[i][FLAGS][x.ti] & PTR_VARIANT;
in_truth_var &= !(truth_ref_ptrs[i][FLAGS][x.ti] & PTR_VAR_BEG);
bool in_query_var = false;
bool is_tp = false;
// update score
if (!done[z.hi][z.qri][z.ti] &&
path_scores[x.hi][x.qri][x.ti] + is_tp > path_scores[z.hi][z.qri][z.ti]) {
path_ptrs[z.hi][z.qri][z.ti] = PTR_SWP_MAT;
path_scores[z.hi][z.qri][z.ti] = path_scores[x.hi][x.qri][x.ti] + is_tp;
if (!contains(curr_wave, z)) curr_wave.insert(z);
queue.push(z);
} else if (!done[z.hi][z.qri][z.ti] &&
path_scores[x.hi][x.qri][x.ti] + is_tp == path_scores[z.hi][z.qri][z.ti]) {
path_ptrs[z.hi][z.qri][z.ti] |= PTR_SWP_MAT;
}
if (print) printf("(%s, %2d, %2d) --%s-> (%s, %2d, %2d) %s %s %s\n",
x.hi % 2 ? "REF" : "QRY", x.qri, x.ti, "SWP",
z.hi % 2 ? "REF" : "QRY", z.qri, z.ti,
in_query_var ? GREEN("QUERY_VAR").data() : RED("QUERY_VAR").data(),
in_truth_var ? GREEN("TRUTH_VAR").data() : RED("TRUTH_VAR").data(),
is_tp ? GREEN("TP").data() : RED("TP").data());
}
} else { // QUERY -> REF SWAP mvmt
if (aln_ptrs[x.hi][x.qri][x.ti] & PTR_SWP_MAT &&
(!(query_ref_ptrs[i][FLAGS][x.qri] & PTR_VARIANT) ||
query_ref_ptrs[i][FLAGS][x.qri] & PTR_VAR_BEG) &&
x.qri > 0 && x.ti > 0) {
// add to path
if (!contains((*swap_pred_maps[i]), x))
ERROR("No swap predecessor, but PTR_SWP_MAT set.");
idx1 z = (*swap_pred_maps[i]).at(x);
aln_ptrs[z.hi][z.qri][z.ti] |= PATH;
// check if mvmt is in variant
bool in_truth_var = truth_ref_ptrs[i][FLAGS][x.ti] & PTR_VARIANT;
in_truth_var &= !(truth_ref_ptrs[i][FLAGS][x.ti] & PTR_VAR_BEG);
bool in_query_var = (x.hi == ri) ? false :
query_ref_ptrs[i][FLAGS][x.qri] & PTR_VARIANT;
in_query_var &= !(query_ref_ptrs[i][FLAGS][x.qri] & PTR_VAR_BEG);
// check for tp
bool is_tp = x.hi == qi && ((query_ref_ptrs[i][PTRS][x.qri] !=
query_ref_ptrs[i][PTRS][x.qri-1]+1) || // INS
query_ref_ptrs[i][FLAGS][x.qri] & PTR_VAR_BEG); // SUB/DEL
// update score
if (!done[z.hi][z.qri][z.ti] &&
path_scores[x.hi][x.qri][x.ti] + is_tp > path_scores[z.hi][z.qri][z.ti]) {
path_ptrs[z.hi][z.qri][z.ti] = PTR_SWP_MAT;
path_scores[z.hi][z.qri][z.ti] = path_scores[x.hi][x.qri][x.ti] + is_tp;
if (!contains(curr_wave, z)) curr_wave.insert(z);
queue.push(z);
} else if (!done[z.hi][z.qri][z.ti] &&
path_scores[x.hi][x.qri][x.ti] + is_tp == path_scores[z.hi][z.qri][z.ti]) {
path_ptrs[z.hi][z.qri][z.ti] |= PTR_SWP_MAT;
}
if (print) printf("(%s, %2d, %2d) --%s-> (%s, %2d, %2d) %s %s %s\n",
x.hi % 2 ? "REF" : "QRY", x.qri, x.ti, "SWP",
z.hi % 2 ? "REF" : "QRY", z.qri, z.ti,
in_query_var ? GREEN("QUERY_VAR").data() : RED("QUERY_VAR").data(),
in_truth_var ? GREEN("TRUTH_VAR").data() : RED("TRUTH_VAR").data(),
is_tp ? GREEN("TP").data() : RED("TP").data());
}
}
} // end of curr_wave, same score
for (idx1 x : curr_wave) {
done[x.hi][x.qri][x.ti] = true;
}
curr_wave.clear();
if (done[qi][0][0] || done[ri][0][0]) break;
for (idx1 x : prev_wave) {
// SUB movement
if (aln_ptrs[x.hi][x.qri][x.ti] & PTR_SUB &&
x.qri > 0 && x.ti > 0) {
// get next cell, add to path
idx1 y = idx1(x.hi, x.qri-1, x.ti-1);
aln_ptrs[y.hi][y.qri][y.ti] |= PATH;
// check if mvmt is in variant
bool in_truth_var = truth_ref_ptrs[i][FLAGS][x.ti] & PTR_VARIANT;
in_truth_var &= !(truth_ref_ptrs[i][FLAGS][x.ti] & PTR_VAR_BEG);
bool in_query_var = false;
if (x.hi == qi) {
in_query_var = query_ref_ptrs[i][FLAGS][x.qri] & PTR_VARIANT;
in_query_var &= !(query_ref_ptrs[i][FLAGS][x.qri] & PTR_VAR_BEG);
}
// check for tp
bool is_tp = x.hi == qi && ((query_ref_ptrs[i][PTRS][x.qri] !=
query_ref_ptrs[i][PTRS][y.qri]+1) || // INS
query_ref_ptrs[i][FLAGS][x.qri] & PTR_VAR_BEG); // SUB/DEL
// update score
if (!done[y.hi][y.qri][y.ti] &&
path_scores[x.hi][x.qri][x.ti] + is_tp > path_scores[y.hi][y.qri][y.ti]) {
path_ptrs[y.hi][y.qri][y.ti] = PTR_SUB;
path_scores[y.hi][y.qri][y.ti] = path_scores[x.hi][x.qri][x.ti] + is_tp;
if (!contains(curr_wave, y)) curr_wave.insert(y);
queue.push(y);
} else if (!done[y.hi][y.qri][y.ti] &&
path_scores[x.hi][x.qri][x.ti] + is_tp == path_scores[y.hi][y.qri][y.ti]) {
path_ptrs[y.hi][y.qri][y.ti] |= PTR_SUB;
}
if (print) printf("(%s, %2d, %2d) --%s-> (%s, %2d, %2d) %s %s %s\n",
x.hi % 2 ? "REF" : "QRY", x.qri, x.ti, "SUB",
y.hi % 2 ? "REF" : "QRY", y.qri, y.ti,
in_query_var ? GREEN("QUERY_VAR").data() : RED("QUERY_VAR").data(),
in_truth_var ? GREEN("TRUTH_VAR").data() : RED("TRUTH_VAR").data(),
is_tp ? GREEN("TP").data() : RED("TP").data());
}
// INS movement
if (aln_ptrs[x.hi][x.qri][x.ti] & PTR_INS && x.qri > 0) {
// get next cell, add to path
idx1 y = idx1(x.hi, x.qri-1, x.ti);
aln_ptrs[y.hi][y.qri][y.ti] |= PATH;
// check if mvmt is in variant
bool in_truth_var = truth_ref_ptrs[i][FLAGS][x.ti] & PTR_VARIANT;
bool in_query_var = false;
if (x.hi == qi) {
in_query_var = query_ref_ptrs[i][FLAGS][x.qri] & PTR_VARIANT;
in_query_var &= !(query_ref_ptrs[i][FLAGS][x.qri] & PTR_VAR_BEG);
}
// check for tp
bool is_tp = x.hi == qi && ((query_ref_ptrs[i][PTRS][x.qri] !=
query_ref_ptrs[i][PTRS][y.qri]+1) || // INS
query_ref_ptrs[i][FLAGS][x.qri] & PTR_VAR_BEG); // SUB/DEL
// update score
if (!done[y.hi][y.qri][y.ti] &&
path_scores[x.hi][x.qri][x.ti] + is_tp > path_scores[y.hi][y.qri][y.ti]) {
path_ptrs[y.hi][y.qri][y.ti] = PTR_INS;
path_scores[y.hi][y.qri][y.ti] = path_scores[x.hi][x.qri][x.ti] + is_tp;
if (!contains(curr_wave, y)) curr_wave.insert(y);
queue.push(y);
} else if (!done[y.hi][y.qri][y.ti] &&
path_scores[x.hi][x.qri][x.ti] + is_tp == path_scores[y.hi][y.qri][y.ti]) {
path_ptrs[y.hi][y.qri][y.ti] |= PTR_INS;
}
if (print) printf("(%s, %2d, %2d) --%s-> (%s, %2d, %2d) %s %s %s\n",
x.hi % 2 ? "REF" : "QRY", x.qri, x.ti, "INS",
y.hi % 2 ? "REF" : "QRY", y.qri, y.ti,
in_query_var ? GREEN("QUERY_VAR").data() : RED("QUERY_VAR").data(),
in_truth_var ? GREEN("TRUTH_VAR").data() : RED("TRUTH_VAR").data(),
is_tp ? GREEN("TP").data() : RED("TP").data());
}
// DEL movement
if (aln_ptrs[x.hi][x.qri][x.ti] & PTR_DEL && x.ti > 0) {
// add to path
idx1 y = idx1(x.hi, x.qri, x.ti-1);
aln_ptrs[y.hi][y.qri][y.ti] |= PATH;
bool in_truth_var = truth_ref_ptrs[i][FLAGS][x.ti] & PTR_VARIANT;
in_truth_var &= !(truth_ref_ptrs[i][FLAGS][x.ti] & PTR_VAR_BEG);
bool in_query_var = (x.hi == ri) ? false :
query_ref_ptrs[i][FLAGS][x.qri] & PTR_VARIANT;
// no need to check for TP since we don't consume a REF/QUERY base
// update score
if (!done[y.hi][y.qri][y.ti] &&
path_scores[x.hi][x.qri][x.ti] > path_scores[y.hi][y.qri][y.ti]) {
path_ptrs[y.hi][y.qri][y.ti] = PTR_DEL;
path_scores[y.hi][y.qri][y.ti] = path_scores[x.hi][x.qri][x.ti];
if (!contains(curr_wave, y)) curr_wave.insert(y);
queue.push(y);
} else if (!done[y.hi][y.qri][y.ti] &&
path_scores[x.hi][x.qri][x.ti] == path_scores[y.hi][y.qri][y.ti]) {
path_ptrs[y.hi][y.qri][y.ti] |= PTR_DEL;
}
if (print) printf("(%s, %2d, %2d) --%s-> (%s, %2d, %2d) %s %s\n",
x.hi % 2 ? "REF" : "QRY", x.qri, x.ti, "DEL",
y.hi % 2 ? "REF" : "QRY", y.qri, y.ti,
in_query_var ? GREEN("QUERY_VAR").data() : RED("QUERY_VAR").data(),
in_truth_var ? GREEN("TRUTH_VAR").data() : RED("TRUTH_VAR").data());
}
} // done adding to next wave
prev_wave.clear();
}
// set pointers
if (aln_ptrs[qi][0][0] & PATH)
pr_query_ref_beg[i] = qi;
else
pr_query_ref_beg[i] = ri;
// debug print
if (print) printf("Alignment %s, path_ptrs\n", aln_strs[i].data());
if (print) printf("\nQUERY");
if (print) print_ptrs(path_ptrs[qi], query[i], truth[i]);
if (print) printf("\nREF");
if (print) print_ptrs(path_ptrs[ri], ref, truth[i]);
} // 2 alignments
// get path and sync points
get_prec_recall_path_sync(path, sync, edits,
aln_ptrs, path_ptrs,
query1_ref_ptrs, ref_query1_ptrs,
query2_ref_ptrs, ref_query2_ptrs,
truth1_ref_ptrs, truth2_ref_ptrs,
pr_query_ref_beg, print
);
}
/******************************************************************************/
/* Follow path which maximizes TP and minimizes ED, saving sync points and edits.
*/
void get_prec_recall_path_sync(
std::vector< std::vector<idx1> > & path,
std::vector< std::vector<bool> > & sync,
std::vector< std::vector<bool> > & edits,
std::vector< std::vector< std::vector<uint8_t> > > & aln_ptrs,
std::vector< std::vector< std::vector<uint8_t> > > & path_ptrs,
const std::vector< std::vector<int> > & query1_ref_ptrs,
const std::vector< std::vector<int> > & ref_query1_ptrs,
const std::vector< std::vector<int> > & query2_ref_ptrs,
const std::vector< std::vector<int> > & ref_query2_ptrs,
const std::vector< std::vector<int> > & truth1_ref_ptrs,
const std::vector< std::vector<int> > & truth2_ref_ptrs,
const std::vector<int> & pr_query_ref_beg, bool print
) {
// query <-> ref pointers
std::vector< std::vector< std::vector<int> > > query_ref_ptrs = {
query1_ref_ptrs, query1_ref_ptrs, query2_ref_ptrs, query2_ref_ptrs };
std::vector< std::vector< std::vector<int> > > truth_ref_ptrs = {
truth1_ref_ptrs, truth2_ref_ptrs, truth1_ref_ptrs, truth2_ref_ptrs };
std::vector< std::vector< std::vector<int> > > ref_query_ptrs = {
ref_query1_ptrs, ref_query1_ptrs, ref_query2_ptrs, ref_query2_ptrs };
for (int i = 0; i < CALLSETS*HAPS; i++) {
/* printf("\nBKWD %s path\n", aln_strs[i].data()); */
// init
int qi = i*2 + QUERY;
int ri = i*2 + REF;
if (print) printf("Alignment %s:\n", aln_strs[i].data());
// path start
int hi = pr_query_ref_beg[i];
int qri = 0;
int ti = 0;
int prev_hi, prev_qri, prev_ti = 0;
int ptr_type = PTR_NONE;
int r_size = ref_query_ptrs[i][PTRS].size();
int q_size = query_ref_ptrs[i][PTRS].size();
int t_size = truth_ref_ptrs[i][PTRS].size();
// find all insertion locations (forbid sync points)
std::vector<bool> ref_has_ins(r_size, false);
for (int j = 0; j < q_size; j++) {
if (query_ref_ptrs[i][FLAGS][j] & PTR_INS_LOC)
ref_has_ins[query_ref_ptrs[i][PTRS][j]] = true;
}
for (int j = 0; j < t_size; j++) {
if (truth_ref_ptrs[i][FLAGS][j] & PTR_INS_LOC)
ref_has_ins[truth_ref_ptrs[i][PTRS][j]] = true;
}
// first position is sync point
sync[i].push_back(true);
edits[i].push_back(false);
path[i].push_back(idx1(hi, qri, ti));
aln_ptrs[hi][qri][ti] |= MAIN_PATH;
aln_ptrs[hi][qri][ti] |= PTR_SYNC;
path_ptrs[hi][qri][ti] |= MAIN_PATH;
// follow best-path pointers, forward iteration
while ( (hi == ri && qri < r_size-1) || (hi == qi && qri < q_size-1) || ti < t_size-1) {
prev_hi = hi; prev_qri = qri; prev_ti = ti;
if (hi == ri && path_ptrs[hi][qri][ti] & PTR_SWP_MAT) { // prefer TPs
// if tie, default to moving to query, where variant may be considered TP
ptr_type = PTR_SWP_MAT;
hi = qi;
qri = ref_query_ptrs[i][PTRS][qri];
qri++; ti++; edits[i].push_back(false);
} else if (path_ptrs[hi][qri][ti] & PTR_MAT) {
ptr_type = PTR_MAT;
qri++; ti++; edits[i].push_back(false);
} else if (path_ptrs[hi][qri][ti] & PTR_SUB) {
ptr_type = PTR_SUB;
qri++; ti++; edits[i].push_back(true);
} else if (path_ptrs[hi][qri][ti] & PTR_INS) {
ptr_type = PTR_INS;
qri++; edits[i].push_back(true);
} else if (path_ptrs[hi][qri][ti] & PTR_DEL) {
ptr_type = PTR_DEL;
ti++; edits[i].push_back(true);
} else if (hi == qi && path_ptrs[hi][qri][ti] & PTR_SWP_MAT) { // last choice is FP
ptr_type = PTR_SWP_MAT;
hi = ri;
qri = query_ref_ptrs[i][PTRS][qri];
qri++; ti++; edits[i].push_back(false);
} else {
ERROR("No valid pointer (value '%d') at (%d, %d, %d)",
path_ptrs[hi][qri][ti], hi, qri, ti);
}
if ((hi == qi && qri >= q_size) || (hi == ri && qri >= r_size) || ti >= t_size) break;
// add point to path
path[i].push_back(idx1(hi, qri, ti));
aln_ptrs[hi][qri][ti] |= MAIN_PATH;
path_ptrs[hi][qri][ti] |= MAIN_PATH;
// set boolean flags for if in query/truth variants
bool in_truth_var = truth_ref_ptrs[i][FLAGS][ti] & PTR_VARIANT; // inside variant
if (ptr_type & (PTR_MAT|PTR_SWP_MAT|PTR_SUB|PTR_DEL)) // if consumes ref, then
in_truth_var &= !(truth_ref_ptrs[i][FLAGS][ti] & PTR_VAR_BEG); // just moved into variant?
bool in_query_var = (hi == ri) ? false :
query_ref_ptrs[i][FLAGS][qri] & PTR_VARIANT;
if (hi == qi && ptr_type & (PTR_MAT|PTR_SWP_MAT|PTR_SUB|PTR_DEL))
in_query_var &= !(query_ref_ptrs[i][FLAGS][qri] & PTR_VAR_BEG);
bool is_ins_loc = ref_has_ins[truth_ref_ptrs[i][PTRS][ti]] ||
(hi == ri ? ref_has_ins[qri] :
ref_has_ins[query_ref_ptrs[i][PTRS][qri]]);
// SYNC POINTS
// enforce on main diag, diag mvmt, not in variant, not at insertion
bool is_sync = !in_truth_var && !in_query_var && !is_ins_loc &&
truth_ref_ptrs[i][PTRS][ti] == (hi == ri ? qri :
query_ref_ptrs[i][PTRS][qri]) &&
ptr_type & (PTR_MAT | PTR_SWP_MAT | PTR_SUB);
if (is_sync) aln_ptrs[hi][qri][ti] |= PTR_SYNC;
sync[i].push_back(is_sync);
// debug print
std::string ptr_str;
if (ptr_type == PTR_MAT) ptr_str = "MAT";
if (ptr_type == PTR_SUB) ptr_str = "SUB";
if (ptr_type == PTR_INS) ptr_str = "INS";
if (ptr_type == PTR_DEL) ptr_str = "DEL";
if (ptr_type == PTR_SWP_MAT) ptr_str = "SWP";
if (print) printf("%s (%s, %2d, %2d) --%s-> (%s, %2d, %2d)\n",
is_sync ? "SYNC" : " ", prev_hi % 2 ? "REF" : "QRY",
prev_qri, prev_ti,
ptr_str.data(), hi % 2 ? "REF" : "QRY", qri, ti);
}
if (print) {
printf("%s ref_has_ins: ", aln_strs[i].data());
for (int j = 0; j < r_size; j++) {
printf("%s ", ref_has_ins[j] ? "T" : "F");
}
printf("\n");
}
// last position is sync
if (print) printf("SYNC (%s, %2d, %2d) DONE \n",
hi % 2 ? "REF" : "QRY", qri, ti);
sync[i].push_back(true);
aln_ptrs[hi][qri][ti] |= PTR_SYNC;
edits[i].push_back(false);
}
}