forked from DaehwanKimLab/hisat2
-
Notifications
You must be signed in to change notification settings - Fork 6
/
hi_aligner.h
5995 lines (5673 loc) · 245 KB
/
hi_aligner.h
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
/*
* Copyright 2015, Daehwan Kim <infphilo@gmail.com>
*
* This file is part of HISAT 2.
*
* HISAT 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HISAT 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HISAT 2. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef HI_ALIGNER_H_
#define HI_ALIGNER_H_
#include <iostream>
#include <utility>
#include <limits>
#include "qual.h"
#include "ds.h"
#include "sstring.h"
#include "alphabet.h"
#include "edit.h"
#include "read.h"
// Threading is necessary to synchronize the classes that dump
// intermediate alignment results to files. Otherwise, all data herein
// is constant and shared, or per-thread.
#include "threading.h"
#include "aligner_result.h"
#include "aligner_cache.h"
#include "scoring.h"
#include "mem_ids.h"
#include "simple_func.h"
#include "aligner_driver.h"
#include "aligner_sw_driver.h"
#include "group_walk.h"
#include "tp.h"
#include "gp.h"
// Allow longer introns for long anchored reads involving canonical splice sites
inline uint32_t MaxIntronLen(uint32_t anchor, uint32_t minAnchorLen) {
uint32_t intronLen = 0;
if(anchor >= minAnchorLen) {
if(anchor < 2) anchor = 2;
uint32_t shift = (anchor << 1) - 4;
shift = min<uint32_t>(max<uint32_t>(shift, 13), 30);
intronLen = 1 << shift;
}
return intronLen;
}
inline float intronLen_prob(uint32_t anchor, uint32_t intronLen, uint32_t maxIntronLen) {
uint32_t expected_intron_len = maxIntronLen;
if(anchor < 14) expected_intron_len = 1 << ((anchor << 1) + 4);
if(expected_intron_len > maxIntronLen) expected_intron_len = maxIntronLen;
assert_gt(expected_intron_len, 0);
float result = ((float)intronLen) / ((float)expected_intron_len);
if(result > 1.0f) result = 1.0f;
return result;
}
// Allow longer introns for long anchored reads involving non-canonical splice sites
inline uint32_t MaxIntronLen_noncan(uint32_t anchor, uint32_t minAnchorLen_noncan) {
uint32_t intronLen = 0;
if(anchor >= minAnchorLen_noncan) {
if(anchor < 5) anchor = 5;
uint32_t shift = (anchor << 1) - 10;
shift = min<uint32_t>(shift, 30);
intronLen = 1 << shift;
}
return intronLen;
}
inline float intronLen_prob_noncan(uint32_t anchor, uint32_t intronLen, uint32_t maxIntronLen) {
uint32_t expected_intron_len = maxIntronLen;
if(anchor < 16) expected_intron_len = 1 << (anchor << 1);
if(expected_intron_len > maxIntronLen) expected_intron_len = maxIntronLen;
assert_gt(expected_intron_len, 0);
float result = ((float)intronLen) / ((float)expected_intron_len);
if(result > 1.0f) result = 1.0f;
return result;
}
/**
* Hit types for BWTHit class below
* Three hit types to anchor a read on the genome
*
*/
enum {
CANDIDATE_HIT = 1,
PSEUDOGENE_HIT,
ANCHOR_HIT,
};
/**
* Simple struct for holding a partial alignment for the read
* The alignment locations are represented by FM offsets [top, bot),
* and later genomic offsets are calculated when necessary
*/
template <typename index_t>
struct BWTHit {
BWTHit() { reset(); }
void reset() {
_top = _bot = 0;
_node_top = _node_bot = 0;
_node_iedge_count.clear();
_fw = true;
_bwoff = (index_t)INDEX_MAX;
_len = 0;
_coords.clear();
_anchor_examined = false;
_hit_type = CANDIDATE_HIT;
}
void init(
index_t top,
index_t bot,
index_t node_top,
index_t node_bot,
const EList<pair<index_t, index_t> >& node_iedge_count,
bool fw,
uint32_t bwoff,
uint32_t len,
index_t hit_type = CANDIDATE_HIT)
{
assert_leq(node_bot - node_top, bot - top);
#ifndef NDEBUG
if(node_bot - node_top < bot - top) {
assert_gt(node_iedge_count.size(), 0);
}
#endif
_top = top;
_bot = bot;
_node_top = node_top;
_node_bot = node_bot;
_node_iedge_count = node_iedge_count;
_fw = fw;
_bwoff = bwoff;
_len = len;
_coords.clear();
_anchor_examined = false;
_hit_type = hit_type;
}
bool hasGenomeCoords() const { return !_coords.empty(); }
/**
* Return true iff there is no hit.
*/
bool empty() const {
return _bot <= _top;
}
/**
* Higher score = higher priority.
*/
bool operator<(const BWTHit& o) const {
return _len > o._len;
}
/**
* Return the size of the alignments SA ranges.
*/
index_t size() const {
assert_leq(_top, _bot);
return _bot - _top;
}
index_t len() const {
assert_gt(_len, 0);
return _len;
}
#ifndef NDEBUG
/**
* Check that hit is sane w/r/t read.
*/
bool repOk(const Read& rd) const {
assert_gt(_bot, _top);
assert_neq(_bwoff, (index_t)INDEX_MAX);
assert_gt(_len, 0);
return true;
}
#endif
index_t _top; // start of the range in the FM index
index_t _bot; // end of the range in the FM index
index_t _node_top;
index_t _node_bot;
EList<pair<index_t, index_t> > _node_iedge_count;
bool _fw; // whether read is forward or reverse complemented
index_t _bwoff; // current base of a read to search from the right end
index_t _len; // read length
EList<Coord> _coords; // genomic offsets corresponding to [_top, _bot)
bool _anchor_examined; // whether or not this hit is examined
index_t _hit_type; // hit type (anchor hit, pseudogene hit, or candidate hit)
};
/**
* Simple struct for holding alignments for the read
* The alignments are represented by chains of BWTHits
*/
template <typename index_t>
struct ReadBWTHit {
ReadBWTHit() { reset(); }
void reset() {
_fw = true;
_len = 0;
_cur = 0;
_done = false;
_numPartialSearch = 0;
_numUniqueSearch = 0;
_partialHits.clear();
}
void init(
bool fw,
index_t len)
{
_fw = fw;
assert_gt(len, 0);
_len = len;
_cur = 0;
_done = false;
_numPartialSearch = 0;
_numUniqueSearch = 0;
_partialHits.clear();
}
bool done() {
#ifndef NDEBUG
assert_gt(_len, 0);
if(_cur >= _len) {
assert(_done);
}
#endif
return _done;
}
void done(bool done) {
assert(!_done);
assert(done);
_done = done;
}
index_t len() const { return _len; }
index_t cur() const { return _cur; }
index_t offsetSize() { return (index_t)_partialHits.size(); }
size_t numPartialSearch() { return _numPartialSearch; }
index_t numActualPartialSearch()
{
assert_leq(_numUniqueSearch, _numPartialSearch);
return (index_t)(_numPartialSearch - _numUniqueSearch);
}
bool width(index_t offset_) {
assert_lt(offset_, _partialHits.size());
return _partialHits[offset_].size();
}
bool hasGenomeCoords(index_t offset_) {
assert_lt(offset_, _partialHits.size());
index_t width_ = width(offset_);
if(width_ == 0) {
return true;
} else {
return _partialHits[offset_].hasGenomeCoords();
}
}
bool hasAllGenomeCoords() {
if(_cur < _len) return false;
if(_partialHits.size() <= 0) return false;
for(size_t oi = 0; oi < _partialHits.size(); oi++) {
if(!_partialHits[oi].hasGenomeCoords())
return false;
}
return true;
}
/**
*
*/
index_t minWidth(index_t& offset) const {
index_t minWidth_ = (index_t)INDEX_MAX;
index_t minWidthLen_ = 0;
for(size_t oi = 0; oi < _partialHits.size(); oi++) {
const BWTHit<index_t>& hit = _partialHits[oi];
if(hit.empty()) continue;
// if(!hit.hasGenomeCoords()) continue;
assert_gt(hit.size(), 0);
if((minWidth_ > hit.size()) ||
(minWidth_ == hit.size() && minWidthLen_ < hit.len())) {
minWidth_ = hit.size();
minWidthLen_ = hit.len();
offset = (index_t)oi;
}
}
return minWidth_;
}
// add policy for calculating a search score
int64_t searchScore(index_t minK) {
int64_t score = 0;
const int64_t penaltyPerOffset = minK * minK;
for(size_t i = 0; i < _partialHits.size(); i++) {
index_t len = _partialHits[i]._len;
score += (len * len);
}
assert_geq(_numPartialSearch, _partialHits.size());
index_t actualPartialSearch = numActualPartialSearch();
score -= (actualPartialSearch * penaltyPerOffset);
score -= (1 << (actualPartialSearch << 1));
return score;
}
BWTHit<index_t>& getPartialHit(index_t offset_) {
assert_lt(offset_, _partialHits.size());
return _partialHits[offset_];
}
bool adjustOffset(index_t minK) {
assert_gt(_partialHits.size(), 0);
const BWTHit<index_t>& hit = _partialHits.back();
if(hit.len() >= minK + 3) {
return false;
}
assert_geq(_cur, hit.len());
index_t origCur = _cur - hit.len();
_cur = origCur + max(hit.len(), minK + 1) - minK;
_partialHits.pop_back();
return true;
}
void setOffset(index_t offset) {
assert_lt(offset, _len);
_cur = offset;
}
#ifndef NDEBUG
/**
*/
bool repOk() const {
for(size_t i = 0; i < _partialHits.size(); i++) {
if(i == 0) {
assert_geq(_partialHits[i]._bwoff, 0);
}
if(i + 1 < _partialHits.size()) {
assert_leq(_partialHits[i]._bwoff + _partialHits[i]._len, _partialHits[i+1]._bwoff);
} else {
assert_eq(i+1, _partialHits.size());
assert_eq(_partialHits[i]._bwoff + _partialHits[i]._len, _cur);
}
}
return true;
}
#endif
bool _fw;
index_t _len;
index_t _cur;
bool _done;
index_t _numPartialSearch;
index_t _numUniqueSearch;
index_t _cur_local;
EList<BWTHit<index_t> > _partialHits;
};
/**
* this is per-thread data, which are shared by GenomeHit classes
* the main purpose of this struct is to avoid extensive use of memory related functions
* such as new and delete - those are really slow and lock based
*/
template <typename index_t>
struct SharedTempVars {
SStringExpandable<char> raw_refbuf;
SStringExpandable<char> raw_refbuf2;
EList<int64_t> temp_scores;
EList<int64_t> temp_scores2;
// Align with alternatives
EList<pair<index_t, int> > ssOffs;
EList<pair<index_t, int> > offDiffs;
EList<SStringExpandable<char> > raw_refbufs;
EList<Edit> alt_edits;
ELList<Edit, 128, 4> candidate_edits;
ELList<pair<index_t, index_t> > ht_llist;
Haplotype<index_t> cmp_ht;
ASSERT_ONLY(SStringExpandable<uint32_t> destU32);
ASSERT_ONLY(BTDnaString editstr);
ASSERT_ONLY(BTDnaString partialseq);
ASSERT_ONLY(BTDnaString refstr);
ASSERT_ONLY(EList<index_t> reflens);
ASSERT_ONLY(EList<index_t> refoffs);
LinkedEList<EList<Edit> > raw_edits;
LinkedEList<EList<pair<index_t, index_t> > > raw_ht_lists;
};
/**
* GenomeHit represents read alignment or alignment of a part of a read
* Two GenomeHits that represents alignments of different parts of a read
* can be combined together. Also, GenomeHit can be extended in both directions.
*/
template <typename index_t>
struct GenomeHit {
GenomeHit() :
_fw(false),
_rdoff((index_t)INDEX_MAX),
_len((index_t)INDEX_MAX),
_trim5(0),
_trim3(0),
_tidx((index_t)INDEX_MAX),
_toff((index_t)INDEX_MAX),
_joinedOff((index_t)INDEX_MAX),
_edits(NULL),
_ht_list(NULL),
_score(MIN_I64),
_localscore(MIN_I64),
_hitcount(1),
_edits_node(NULL),
_ht_list_node(NULL),
_sharedVars(NULL)
{
}
GenomeHit(const GenomeHit& otherHit) :
_edits(NULL),
_ht_list(NULL),
_hitcount(1),
_edits_node(NULL),
_ht_list_node(NULL),
_sharedVars(NULL)
{
init(otherHit._fw,
otherHit._rdoff,
otherHit._len,
otherHit._trim5,
otherHit._trim3,
otherHit._tidx,
otherHit._toff,
otherHit._joinedOff,
*(otherHit._sharedVars),
otherHit._edits,
otherHit._ht_list,
otherHit._score,
otherHit._localscore,
otherHit._splicescore);
}
GenomeHit<index_t>& operator=(const GenomeHit<index_t>& otherHit) {
if(this == &otherHit) return *this;
init(otherHit._fw,
otherHit._rdoff,
otherHit._len,
otherHit._trim5,
otherHit._trim3,
otherHit._tidx,
otherHit._toff,
otherHit._joinedOff,
*(otherHit._sharedVars),
otherHit._edits,
otherHit._ht_list,
otherHit._score,
otherHit._localscore,
otherHit._splicescore);
return *this;
}
~GenomeHit() {
if(_edits_node != NULL) {
assert(_edits != NULL);
assert(_sharedVars != NULL);
_sharedVars->raw_edits.delete_node(_edits_node);
_edits = NULL;
_edits_node = NULL;
}
if(_ht_list_node != NULL) {
assert(_ht_list != NULL);
assert(_sharedVars != NULL);
_sharedVars->raw_ht_lists.delete_node(_ht_list_node);
_ht_list = NULL;
_ht_list_node = NULL;
}
_sharedVars = NULL;
}
void init(
bool fw,
index_t rdoff,
index_t len,
index_t trim5,
index_t trim3,
index_t tidx,
index_t toff,
index_t joinedOff,
SharedTempVars<index_t>& sharedVars,
EList<Edit>* edits = NULL,
EList<pair<index_t, index_t> >* ht_list = NULL,
int64_t score = 0,
int64_t localscore = 0,
double splicescore = 0.0)
{
_fw = fw;
_rdoff = rdoff;
_len = len;
_trim5 = trim5;
_trim3 = trim3;
_tidx = tidx;
_toff = toff;
_joinedOff = joinedOff;
_score = score;
_localscore = localscore;
_splicescore = splicescore;
assert(_sharedVars == NULL || _sharedVars == &sharedVars);
_sharedVars = &sharedVars;
if(_edits == NULL) {
assert(_edits_node == NULL);
_edits_node = _sharedVars->raw_edits.new_node();
assert(_edits_node != NULL);
_edits = &(_edits_node->payload);
}
assert(_edits != NULL);
_edits->clear();
if(edits != NULL) *_edits = *edits;
if(_ht_list == NULL) {
assert(_ht_list_node == NULL);
_ht_list_node = _sharedVars->raw_ht_lists.new_node();
assert(_ht_list_node != NULL);
_ht_list = &(_ht_list_node->payload);
}
assert(_ht_list != NULL);
_ht_list->clear();
if(ht_list != NULL) *_ht_list = *ht_list;
_hitcount = 1;
}
bool inited() const {
return _len >= 0 && _len < (index_t)INDEX_MAX;
}
/**
* Check if it is compatible with another GenomeHit with respect to indels or introns
*/
bool compatibleWith(
const GenomeHit<index_t>& otherHit,
index_t minIntronLen,
index_t maxIntronLen,
bool no_spliced_alignment = false) const;
/**
* Combine itself with another GenomeHit
*/
bool combineWith(
const GenomeHit& otherHit,
const Read& rd,
const GFM<index_t>& gfm,
const BitPairReference& ref,
const ALTDB<index_t>& altdb,
SpliceSiteDB& ssdb,
SwAligner& swa,
SwMetrics& swm,
const Scoring& sc,
TAlScore minsc,
RandomSource& rnd, // pseudo-random source
index_t minK_local,
index_t minIntronLen,
index_t maxIntronLen,
index_t minAnchorLen, // minimum anchor length for canonical splice site
index_t minAnchorLen_noncan, // minimum anchor length for non-canonical splice site
const index_t maxAltsTried,
const SpliceSite* spliceSite = NULL, // penalty for splice site
bool no_spliced_alignment = false);
/**
* Extend the partial alignment (GenomeHit) bidirectionally
*/
bool extend(
const Read& rd,
const GFM<index_t>& gfm,
const BitPairReference& ref,
const ALTDB<index_t>& altdb,
SpliceSiteDB& ssdb,
SwAligner& swa,
SwMetrics& swm,
PerReadMetrics& prm,
const Scoring& sc,
TAlScore minsc,
RandomSource& rnd, // pseudo-random source
index_t minK_local,
const TranscriptomePolicy& tpol,
const GraphPolicy& gpol,
index_t& leftext,
index_t& rightext,
index_t mm = 0);
/**
* Adjust alignment with respect to SNPs, usually updating Edits
*
*/
static bool adjustWithALT(
index_t rdoff,
index_t len,
const Coord& coord,
SharedTempVars<index_t>& sharedVars,
EList<GenomeHit<index_t> >& genomeHits,
const Read& rd,
const GFM<index_t>& gfm,
const ALTDB<index_t>& altdb,
const BitPairReference& ref,
const GraphPolicy& gpol);
/**
* Adjust alignment with respect to SNPs, usually updating Edits
*
*/
bool adjustWithALT(
const Read& rd,
const GFM<index_t>& gfm,
const ALTDB<index_t>& altdb,
const BitPairReference& ref,
const GraphPolicy& gpol);
/*
*
*/
static void findSSOffs(
const GFM<index_t>& gfm,
const ALTDB<index_t>& altdb,
index_t start,
index_t end,
EList<pair<index_t, int> >& ssOffs);
/*
* Find offset differences due to deletions
*/
static void findOffDiffs(
const GFM<index_t>& gfm,
const ALTDB<index_t>& altdb,
index_t start,
index_t end,
EList<pair<index_t, int> >& offDiffs);
/*
*
*/
static index_t alignWithALTs(
const EList<ALT<index_t> >& alts,
const EList<Haplotype<index_t> >& haplotypes,
const EList<index_t>& haplotype_maxrights,
index_t joinedOff,
const BTDnaString& rdseq,
index_t base_rdoff,
index_t rdoff,
index_t rdlen,
const BitPairReference& ref,
SharedTempVars<index_t>& sharedVar,
index_t tidx,
int rfoff,
index_t rflen,
bool left,
const GraphPolicy& gpol,
EList<Edit>& edits,
ELList<pair<index_t, index_t> >& ht_llist,
EList<pair<index_t, index_t> >& ht_list,
Haplotype<index_t>& cmp_ht,
ELList<Edit, 128, 4>* candidate_edits = NULL,
index_t mm = 0,
index_t* numNs = NULL)
{
int best_rdoff = (int)rdoff;
if(numNs != NULL) *numNs = 0;
index_t numALTsTried = 0;
EList<Edit>& alt_edits = sharedVar.alt_edits;
alt_edits = edits;
index_t nedits = (index_t)edits.size();
if(candidate_edits != NULL) candidate_edits->clear();
ht_llist.clear();
// ht_llist.expand();
// ht_llist[0] = ht_list;
alignWithALTs_recur(
alts,
haplotypes,
haplotype_maxrights,
joinedOff,
rdseq,
rdoff - base_rdoff,
rdoff,
rdlen,
ref,
sharedVar.raw_refbufs,
ASSERT_ONLY(sharedVar.destU32,)
alt_edits,
best_rdoff,
NULL, /* rfseq */
tidx,
rfoff,
rflen,
left,
edits,
mm,
ht_llist,
cmp_ht,
candidate_edits,
0, /* tmp_numNs */
numNs,
0, /* dep */
gpol,
numALTsTried);
index_t extlen = 0;
if(left) {
assert_geq(best_rdoff, -1);
assert_leq(best_rdoff, (int)rdoff);
extlen = rdoff - best_rdoff;
} else {
assert_leq(best_rdoff, (int)(rdoff + rdlen));
assert_geq(best_rdoff, (int)rdoff);
extlen = best_rdoff - rdoff;
}
if(extlen > 0 && edits.size() > 0) {
const Edit& f = edits.front();
if(f.pos + extlen == base_rdoff + 1) {
if(f.type == EDIT_TYPE_READ_GAP ||
f.type == EDIT_TYPE_REF_GAP ||
f.type == EDIT_TYPE_SPL) {
extlen = 0;
}
if(f.type == EDIT_TYPE_MM && f.chr == 'N') {
extlen = 0;
}
}
const Edit& b = edits.back();
if(extlen > 0 && b.pos == rdoff - base_rdoff + extlen - 1) {
if(b.type == EDIT_TYPE_READ_GAP ||
b.type == EDIT_TYPE_REF_GAP) {
extlen = 0;
}
}
if(extlen == 0 && edits.size() > nedits) {
if(left) {
edits.erase(0, edits.size() - nedits);
} else {
edits.resize(nedits);
}
}
}
return extlen;
}
/*
*
*/
static index_t alignWithALTs_recur(
const EList<ALT<index_t> >& alts,
const EList<Haplotype<index_t> >& haplotypes,
const EList<index_t>& haplotype_maxrights,
index_t joinedOff,
const BTDnaString& rdseq,
index_t rdoff_add,
index_t rdoff,
index_t rdlen,
const BitPairReference& ref,
EList<SStringExpandable<char> >& raw_refbufs,
ASSERT_ONLY(SStringExpandable<uint32_t> destU32,)
EList<Edit>& tmp_edits,
int& best_rdoff,
const char* rfseq,
index_t tidx,
int rfoff,
index_t rflen,
bool left,
EList<Edit>& edits,
index_t mm,
ELList<pair<index_t, index_t> >& ht_llist,
Haplotype<index_t>& cmp_ht,
ELList<Edit, 128, 4>* candidate_edits,
index_t tmp_numNs,
index_t* numNs,
index_t dep,
const GraphPolicy& gpol,
index_t& numALTsTried,
ALT_TYPE prev_alt_type = ALT_NONE);
/**
* For alignment involving indel, move the indels
* to the left most possible position
*/
void leftAlign(const Read& rd);
index_t rdoff() const { return _rdoff; }
index_t len() const { return _len; }
index_t trim5() const { return _trim5; }
index_t trim3() const { return _trim3; }
void trim5(index_t trim5,
const Read& rd,
SpliceSiteDB& ssdb,
const Scoring& sc,
index_t minK_local,
index_t minIntronLen,
index_t maxIntronLen,
index_t minAnchorLen,
index_t minAnchorLen_noncan,
const BitPairReference& ref)
{
assert_eq(_rdoff, trim5);
assert_eq(_trim5, 0);
_trim5 = trim5;
calculateScore(rd,
ssdb,
sc,
minK_local,
minIntronLen,
maxIntronLen,
minAnchorLen,
minAnchorLen_noncan,
ref);
}
void trim3(index_t trim3,
const Read& rd,
SpliceSiteDB& ssdb,
const Scoring& sc,
index_t minK_local,
index_t minIntronLen,
index_t maxIntronLen,
index_t minAnchorLen,
index_t minAnchorLen_noncan,
const BitPairReference& ref)
{
_trim3 = trim3;
calculateScore(rd,
ssdb,
sc,
minK_local,
minIntronLen,
maxIntronLen,
minAnchorLen,
minAnchorLen_noncan,
ref);
}
index_t ref() const { return _tidx; }
index_t refoff() const { return _toff; }
index_t fw() const { return _fw; }
index_t hitcount() const { return _hitcount; }
/**
* Leftmost coordinate
*/
Coord coord() const {
return Coord(_tidx, _toff, _fw);
}
int64_t score() const { return _score; }
int64_t localscore() const { return _localscore; }
double splicescore() const { return _splicescore; }
const EList<Edit>& edits() const { return *_edits; }
/**
* Retrieve the partial alignment from the left until indel or intron
*/
void getLeft(index_t& rdoff,
index_t& len,
index_t& toff,
int64_t* score = NULL,
const Read* rd = NULL,
const Scoring* sc = NULL) const
{
assert(inited());
toff = _toff, rdoff = _rdoff, len = _len;
const BTString* qual = NULL;
if(score != NULL) {
assert(rd != NULL);
assert(sc != NULL);
*score = 0;
qual = &(_fw ? rd->qual : rd->qualRev);
}
for(index_t i = 0; i < _edits->size(); i++) {
const Edit& edit = (*_edits)[i];
if(edit.type == EDIT_TYPE_SPL ||
edit.type == EDIT_TYPE_READ_GAP ||
edit.type == EDIT_TYPE_REF_GAP ||
(edit.type == EDIT_TYPE_MM && edit.snpID != (index_t)INDEX_MAX)) {
len = edit.pos;
break;
}
if(score != NULL) {
if(edit.type == EDIT_TYPE_MM) {
assert(qual != NULL);
if(edit.snpID == (index_t)INDEX_MAX) {
*score += sc->score(
dna2col[edit.qchr] - '0',
asc2dnamask[edit.chr],
(*qual)[this->_rdoff + edit.pos] - 33);
}
}
}
}
assert_geq(len, 0);
}
/**
* Retrieve the partial alignment from the right until indel or intron
*/
void getRight(index_t& rdoff,
index_t& len,
index_t& toff,
int64_t* score = NULL,
const Read* rd = NULL,
const Scoring* sc = NULL) const
{
assert(inited());
toff = _toff, rdoff = _rdoff, len = _len;
const BTString* qual = NULL;
if(score != NULL) {
assert(rd != NULL);
assert(sc != NULL);
*score = 0;
qual = &(_fw ? rd->qual : rd->qualRev);
}
if(_edits->size() == 0) return;
for(int i = (int)_edits->size() - 1; i >= 0; i--) {
const Edit& edit = (*_edits)[i];
if(edit.type == EDIT_TYPE_SPL ||
edit.type == EDIT_TYPE_READ_GAP ||
edit.type == EDIT_TYPE_REF_GAP ||
(edit.type == EDIT_TYPE_MM && edit.snpID != (index_t)INDEX_MAX)) {
rdoff = _rdoff + edit.pos;
assert_lt(edit.pos, _len);
len = _len - edit.pos;
if(edit.type == EDIT_TYPE_REF_GAP) {
assert_lt(edit.pos + 1, _len);
assert_gt(len, 1);
rdoff++;
len--;
} else if(edit.type == EDIT_TYPE_MM) {
assert_leq(edit.pos + 1, _len);
assert_geq(len, 1);
rdoff++;
len--;
}
toff = getRightOff() - len;
break;
}
if(score != NULL) {
if(edit.type == EDIT_TYPE_MM) {
assert(qual != NULL);
if(edit.snpID == (index_t)INDEX_MAX) {
*score += sc->score(
dna2col[edit.qchr] - '0',
asc2dnamask[edit.chr],
(*qual)[this->_rdoff + edit.pos] - 33);
}
}
}
}
assert_geq(len, 0);
}
/**
* Retrieve the genomic offset of the right end
*/
index_t getRightOff() const {
assert(inited());
index_t toff = _toff + _len;
for(index_t i = 0; i < _edits->size(); i++) {
const Edit& ed = (*_edits)[i];
if(ed.type == EDIT_TYPE_SPL) {
toff += ed.splLen;
} else if(ed.type == EDIT_TYPE_READ_GAP) {
toff++;
} else if(ed.type == EDIT_TYPE_REF_GAP) {
assert_gt(toff, 0);