-
Notifications
You must be signed in to change notification settings - Fork 19
/
hi_aligner.h
4368 lines (4083 loc) · 169 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 2014, Daehwan Kim <[email protected]>
*
* This file is part of HISAT.
*
* HISAT 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 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. 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"
// Maximum insertion length
static const uint32_t maxInsLen = 3;
// Maximum deletion length
static const uint32_t maxDelLen = 3;
// Minimum anchor length required for canonical splice sites
static const uint32_t minAnchorLen = 7;
// Minimum anchor length required for non-canonical splice sites
static const uint32_t minAnchorLen_noncan = 14;
// Allow longer introns for long anchored reads involving canonical splice sites
inline uint32_t MaxIntronLen(uint32_t anchor) {
uint32_t intronLen = 0;
if(anchor >= minAnchorLen) {
assert_geq(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 intronLen = 0;
if(anchor >= minAnchorLen_noncan) {
assert_geq(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;
_fw = true;
_bwoff = (index_t)OFF_MASK;
_len = 0;
_coords.clear();
_anchor_examined = false;
_hit_type = CANDIDATE_HIT;
}
void init(
index_t top,
index_t bot,
bool fw,
uint32_t bwoff,
uint32_t len,
index_t hit_type = CANDIDATE_HIT)
{
_top = top;
_bot = bot;
_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)OFF_MASK);
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
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; }
size_t offsetSize() { return _partialHits.size(); }
size_t numPartialSearch() { return _numPartialSearch; }
size_t numActualPartialSearch()
{
assert_leq(_numUniqueSearch, _numPartialSearch);
return _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)OFF_MASK;
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;
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;
};
/**
* 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)OFF_MASK),
_len((index_t)OFF_MASK),
_trim5(0),
_trim3(0),
_tidx((index_t)OFF_MASK),
_toff((index_t)OFF_MASK),
_edits(NULL),
_score(MIN_I64),
_hitcount(1),
_edits_node(NULL),
_sharedVars(NULL)
{
}
GenomeHit(const GenomeHit& otherHit) :
_fw(false),
_rdoff((index_t)OFF_MASK),
_len((index_t)OFF_MASK),
_trim5(0),
_trim3(0),
_tidx((index_t)OFF_MASK),
_toff((index_t)OFF_MASK),
_edits(NULL),
_score(MIN_I64),
_hitcount(1),
_edits_node(NULL),
_sharedVars(NULL)
{
init(otherHit._fw,
otherHit._rdoff,
otherHit._len,
otherHit._trim5,
otherHit._trim3,
otherHit._tidx,
otherHit._toff,
*(otherHit._sharedVars),
otherHit._edits,
otherHit._score,
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._sharedVars),
otherHit._edits,
otherHit._score,
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;
_sharedVars = NULL;
}
}
void init(
bool fw,
index_t rdoff,
index_t len,
index_t trim5,
index_t trim3,
index_t tidx,
index_t toff,
SharedTempVars<index_t>& sharedVars,
EList<Edit>* edits = NULL,
int64_t score = 0,
double splicescore = 0.0)
{
_fw = fw;
_rdoff = rdoff;
_len = len;
_trim5 = trim5;
_trim3 = trim3;
_tidx = tidx;
_toff = toff;
_score = score;
_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;
_hitcount = 1;
}
bool inited() const {
return _len >= 0 && _len < (index_t)OFF_MASK;
}
/**
* 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 BitPairReference& ref,
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 can_mal = minAnchorLen, // minimum anchor length for canonical splice site
index_t noncan_mal = minAnchorLen_noncan, // minimum anchor length for non-canonical splice site
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 BitPairReference& ref,
SpliceSiteDB& ssdb,
SwAligner& swa,
SwMetrics& swm,
PerReadMetrics& prm,
const Scoring& sc,
TAlScore minsc,
RandomSource& rnd, // pseudo-random source
index_t minK_local,
index_t minIntronLen,
index_t maxIntronLen,
index_t& leftext,
index_t& rightext,
index_t mm = 0);
/**
* 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) { _trim5 = trim5; }
void trim3(index_t trim3) { _trim3 = trim3; }
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; }
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) {
len = edit.pos;
break;
}
if(score != NULL) {
if(edit.type == EDIT_TYPE_MM) {
assert(qual != NULL);
*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 = _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) {
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--;
}
toff = getRightOff() - len;
break;
}
if(score != NULL) {
if(edit.type == EDIT_TYPE_MM) {
assert(qual != NULL);
*score += sc->score(
dna2col[edit.qchr] - '0',
asc2dnamask[edit.chr],
(*qual)[this->_rdoff + edit.pos] - 33);
}
}
}
assert_gt(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);
toff--;
}
}
return toff;
}
/**
* Retrieve left anchor length and number of edits in the anchor
*/
void getLeftAnchor(index_t& leftanchor,
index_t& nedits) const
{
assert(inited());
leftanchor = _len;
nedits = 0;
for(index_t i = 0; i < _edits->size(); i++) {
const Edit& edit = (*_edits)[i];
if(edit.type == EDIT_TYPE_SPL) {
leftanchor = edit.pos;
break;
} else if(edit.type == EDIT_TYPE_MM ||
edit.type == EDIT_TYPE_READ_GAP ||
edit.type == EDIT_TYPE_REF_GAP) {
nedits++;
}
}
}
/**
* Retrieve right anchor length and number of edits in the anchor
*/
void getRightAnchor(index_t& rightanchor,
index_t& nedits) const
{
rightanchor = _len;
nedits = 0;
if(_edits->size() == 0) return;
for(int i = _edits->size() - 1; i >= 0; i--) {
const Edit& edit = (*_edits)[i];
if(edit.type == EDIT_TYPE_SPL) {
rightanchor = _len - edit.pos - 1;
break;
} else if(edit.type == EDIT_TYPE_MM ||
edit.type == EDIT_TYPE_READ_GAP ||
edit.type == EDIT_TYPE_REF_GAP) {
nedits++;
}
}
}
/**
* Is it spliced alignment?
*/
bool spliced() const {
for(index_t i = 0; i < _edits->size(); i++) {
if((*_edits)[i].type == EDIT_TYPE_SPL) {
return true;
}
}
return false;
}
/**
*
*/
bool spliced_consistently() const {
uint32_t splDir = EDIT_SPL_UNKNOWN;
for(index_t i = 0; i < _edits->size(); i++) {
const Edit& edit = (*_edits)[i];
if(edit.type == EDIT_TYPE_SPL) {
if(splDir != EDIT_SPL_UNKNOWN) {
if(edit.splDir != EDIT_SPL_UNKNOWN) {
if(splDir != edit.splDir)
return false;
}
} else {
splDir = _edits[i].splDir;
}
}
}
return true;
}
bool operator== (const GenomeHit<index_t>& other) const {
if(_fw != other._fw ||
_rdoff != other._rdoff ||
_len != other._len ||
_tidx != other._tidx ||
_toff != other._toff ||
_trim5 != other._trim5 ||
_trim3 != other._trim3) {
return false;
}
if(_edits->size() != other._edits->size()) return false;
for(index_t i = 0; i < _edits->size(); i++) {
if(!((*_edits)[i] == (*other._edits)[i])) return false;
}
// daehwan - this may not be true when some splice sites are provided from outside
// assert_eq(_score, other._score);
return true;
}
bool contains(const GenomeHit<index_t>& other) const {
return (*this) == other;
}
/**
* Return number of mismatches in the alignment.
*/
int mms() const {
#if 0
if (_e2.inited()) return 2;
else if(_e1.inited()) return 1;
else return 0;
#endif
return 0;
}
/**
* Return the number of Ns involved in the alignment.
*/
int ns() const {
#if 0
int ns = 0;
if(_e1.inited() && _e1.hasN()) {
ns++;
if(_e2.inited() && _e2.hasN()) {
ns++;
}
}
return ns;
#endif
return 0;
}
int ngaps() const {
return 0;
}
#if 0
/**
* Return the number of Ns involved in the alignment.
*/
int refns() const {
int ns = 0;
if(_e1.inited() && _e1.chr == 'N') {
ns++;
if(_e2.inited() && _e2.chr == 'N') {
ns++;
}
}
return ns;
}
/**
* Higher score = higher priority.
*/
bool operator<(const GenomeHit& o) const {
return _len > o._len;
}
#endif
#ifndef NDEBUG
/**
* Check that hit is sane w/r/t read.
*/
bool repOk(const Read& rd, const BitPairReference& ref);
#endif
private:
/**
* Calculate alignment score
*/
int64_t calculateScore(
const Read& rd,
SpliceSiteDB& ssdb,
const Scoring& sc,
index_t minK_local,
index_t minIntronLen,
index_t maxIntronLen,
const BitPairReference& ref);
public:
bool _fw;
index_t _rdoff;
index_t _len;
index_t _trim5;
index_t _trim3;
index_t _tidx;
index_t _toff;
EList<Edit>* _edits;
int64_t _score;
double _splicescore;
index_t _hitcount; // for selection purposes
LinkedEListNode<EList<Edit> >* _edits_node;
SharedTempVars<index_t>* _sharedVars;
};
/**
* Check if it is compatible with another GenomeHit with respect to indels or introns
*/
template <typename index_t>
bool GenomeHit<index_t>::compatibleWith(
const GenomeHit<index_t>& otherHit,
index_t minIntronLen,
index_t maxIntronLen,
bool no_spliced_alignment) const
{
if(this == &otherHit) return false;
// check if they are on the same strand and on the same contig
if(_fw != otherHit._fw || _tidx != otherHit._tidx) return false;
// make sure itself is closer to the left end of read than otherHit
if(_rdoff > otherHit._rdoff) return false;
// do not consider a case itself (read portion) includes otherHit
if(_rdoff + _len > otherHit._rdoff + otherHit._len) return false;
// make sure itself comes before otherHit wrt. genomic positions
if(_toff > otherHit._toff) return false;
index_t this_rdoff, this_len, this_toff;
this->getRight(this_rdoff, this_len, this_toff);
assert_geq(this_len, 0);
index_t other_rdoff, other_len, other_toff;
otherHit.getLeft(other_rdoff, other_len, other_toff);
assert_geq(other_len, 0);
if(this_rdoff > other_rdoff) return false;
if(this_rdoff + this_len > other_rdoff + other_len) return false;
if(this_toff > other_toff) return false;
index_t refdif = other_toff - this_toff;
index_t rddif = other_rdoff - this_rdoff;
// check if there is a deletion, an insertion, or a potential intron
// between the two partial alignments
if(rddif != refdif) {
if(rddif > refdif) {
if(rddif > refdif + maxInsLen) return false;
} else {
assert_geq(refdif, rddif);
if(refdif - rddif < minIntronLen) {
if(refdif - rddif > maxDelLen) return false;
} else {
if(no_spliced_alignment) return false;
if(refdif - rddif > maxIntronLen) {
return false;
}
}
}
}
return true;
}
/**
* Combine itself with another GenomeHit
* while allowing mismatches, an insertion, a deletion, or an intron
*/
template <typename index_t>
bool GenomeHit<index_t>::combineWith(
const GenomeHit& otherHit,
const Read& rd,
const BitPairReference& ref,
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 can_mal, // minimum anchor length for canonical splice site
index_t noncan_mal, // minimum anchor length for non-canonical splice site
const SpliceSite* spliceSite, // penalty for splice site
bool no_spliced_alignment)
{
if(this == &otherHit) return false;
assert(compatibleWith(otherHit, minIntronLen, maxIntronLen, no_spliced_alignment));
assert_eq(this->_tidx, otherHit._tidx);
assert_lt(this->_tidx, ref.numRefs());
// get the partial part of the alignment from the right
// until an indel or splice sites
index_t this_rdoff, this_len, this_toff;
int64_t this_score;
this->getRight(this_rdoff, this_len, this_toff, &this_score, &rd, &sc);
assert_geq(this_len, 0);
assert_leq(this_score, 0);
assert_geq(this_score, this->_score);