-
Notifications
You must be signed in to change notification settings - Fork 67
/
IntersectingPairedEndAligner.cpp
4100 lines (3546 loc) · 216 KB
/
IntersectingPairedEndAligner.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
/*++
Module Name:
IntersectingPairedEndAligner.cpp
Abstract:
A paired-end aligner based on set intersections to narrow down possible candidate locations.
Authors:
Bill Bolosky, February, 2013
Environment:
User mode service.
Revision History:
--*/
#include "stdafx.h"
#include "IntersectingPairedEndAligner.h"
#include "SeedSequencer.h"
#include "mapq.h"
#include "exit.h"
#include "Error.h"
#include "BigAlloc.h"
#include "AlignerOptions.h"
#ifdef _DEBUG
extern bool _DumpAlignments; // From BaseAligner.cpp
#endif // _DEBUG
IntersectingPairedEndAligner::IntersectingPairedEndAligner(
GenomeIndex *index_,
unsigned maxReadSize_,
unsigned maxHits_,
unsigned maxK_,
unsigned maxKForIndels_,
unsigned numSeedsFromCommandLine_,
double seedCoverage_,
int minSpacing_, // Minimum distance to allow between the two ends.
unsigned maxSpacing_, // Maximum distance to allow between the two ends.
unsigned maxBigHits_,
unsigned extraSearchDepth_,
unsigned maxCandidatePoolSize,
int maxSecondaryAlignmentsPerContig_,
BigAllocator *allocator,
bool noUkkonen_,
bool noOrderedEvaluation_,
bool noTruncation_,
bool useAffineGap_,
bool ignoreAlignmentAdjustmentsForOm_,
bool altAwareness_,
unsigned maxScoreGapToPreferNonAltAlignment_,
unsigned matchReward_,
unsigned subPenalty_,
unsigned gapOpenPenalty_,
unsigned gapExtendPenalty_,
bool useSoftClip_) :
index(index_), maxReadSize(maxReadSize_), maxHits(maxHits_), maxK(maxK_), maxKForIndels(maxKForIndels_), numSeedsFromCommandLine(__min(MAX_MAX_SEEDS,numSeedsFromCommandLine_)), minSpacing(minSpacing_), maxSpacing(maxSpacing_),
landauVishkin(NULL), reverseLandauVishkin(NULL), maxBigHits(maxBigHits_), seedCoverage(seedCoverage_),
extraSearchDepth(extraSearchDepth_), nLocationsScored(0), noUkkonen(noUkkonen_), noOrderedEvaluation(noOrderedEvaluation_), noTruncation(noTruncation_), useAffineGap(useAffineGap_),
maxSecondaryAlignmentsPerContig(maxSecondaryAlignmentsPerContig_), alignmentAdjuster(index->getGenome()), ignoreAlignmentAdjustmentsForOm(ignoreAlignmentAdjustmentsForOm_), altAwareness(altAwareness_),
maxScoreGapToPreferNonAltAlignment(maxScoreGapToPreferNonAltAlignment_), matchReward(matchReward_), subPenalty(subPenalty_), gapOpenPenalty(gapOpenPenalty_), gapExtendPenalty(gapExtendPenalty_), useSoftClip(useSoftClip_)
{
doesGenomeIndexHave64BitLocations = index->doesGenomeIndexHave64BitLocations();
unsigned maxSeedsToUse;
if (0 != numSeedsFromCommandLine) {
maxSeedsToUse = numSeedsFromCommandLine;
} else {
maxSeedsToUse = (unsigned)(maxReadSize * seedCoverage / index->getSeedLength());
}
allocateDynamicMemory(allocator, maxReadSize, maxBigHits, maxSeedsToUse, MAX_K, extraSearchDepth, maxCandidatePoolSize, maxSecondaryAlignmentsPerContig);
rcTranslationTable['A'] = 'T';
rcTranslationTable['G'] = 'C';
rcTranslationTable['C'] = 'G';
rcTranslationTable['T'] = 'A';
rcTranslationTable['N'] = 'N';
for (unsigned i = 0; i < 256; i++) {
nTable[i] = 0;
}
nTable['N'] = 1;
seedLen = index->getSeedLength();
genome = index->getGenome();
genomeSize = genome->getCountOfBases();
}
IntersectingPairedEndAligner::~IntersectingPairedEndAligner()
{
}
size_t
IntersectingPairedEndAligner::getBigAllocatorReservation(GenomeIndex * index, unsigned maxBigHitsToConsider, unsigned maxReadSize, unsigned seedLen, unsigned numSeedsFromCommandLine,
double seedCoverage, unsigned maxEditDistanceToConsider, unsigned maxExtraSearchDepth, unsigned maxCandidatePoolSize,
int maxSecondaryAlignmentsPerContig)
{
unsigned maxSeedsToUse;
if (0 != numSeedsFromCommandLine) {
maxSeedsToUse = numSeedsFromCommandLine;
} else {
maxSeedsToUse = (unsigned)(maxReadSize * seedCoverage / index->getSeedLength());
}
CountingBigAllocator countingAllocator;
{
IntersectingPairedEndAligner aligner; // This has to be in a nested scope so its destructor is called before that of the countingAllocator
aligner.index = index;
aligner.doesGenomeIndexHave64BitLocations = index->doesGenomeIndexHave64BitLocations();
aligner.allocateDynamicMemory(&countingAllocator, maxReadSize, maxBigHitsToConsider, maxSeedsToUse, maxEditDistanceToConsider, maxExtraSearchDepth, maxCandidatePoolSize,
maxSecondaryAlignmentsPerContig);
return sizeof(aligner) + countingAllocator.getMemoryUsed();
}
}
void
IntersectingPairedEndAligner::allocateDynamicMemory(BigAllocator *allocator, unsigned maxReadSize, unsigned maxBigHitsToConsider, unsigned maxSeedsToUse,
unsigned maxEditDistanceToConsider, unsigned maxExtraSearchDepth, unsigned maxCandidatePoolSize,
int maxSecondaryAlignmentsPerContig)
{
seedUsed = (BYTE *) allocator->allocate(100 + ((size_t)maxReadSize + 7) / 8);
for (unsigned whichRead = 0; whichRead < NUM_READS_PER_PAIR; whichRead++) {
rcReadData[whichRead] = (char *)allocator->allocate(maxReadSize);
rcReadQuality[whichRead] = (char *)allocator->allocate(maxReadSize);
for (Direction dir = 0; dir < NUM_DIRECTIONS; dir++) {
reversedRead[whichRead][dir] = (char *)allocator->allocate(maxReadSize);
hashTableHitSets[whichRead][dir] =(HashTableHitSet *)allocator->allocate(sizeof(HashTableHitSet)); /*new HashTableHitSet();*/
hashTableHitSets[whichRead][dir]->firstInit(maxSeedsToUse, maxMergeDistance, allocator, doesGenomeIndexHave64BitLocations);
}
}
scoringCandidatePoolSize = min(maxCandidatePoolSize, maxBigHitsToConsider * maxSeedsToUse * NUM_READS_PER_PAIR);
scoringCandidates = (ScoringCandidate **) allocator->allocate(sizeof(ScoringCandidate *) * ((size_t)maxEditDistanceToConsider + maxExtraSearchDepth + 1)); //+1 is for 0.
scoringCandidatePool = (ScoringCandidate *)allocator->allocate(sizeof(ScoringCandidate) * scoringCandidatePoolSize);
for (unsigned i = 0; i < NUM_READS_PER_PAIR; i++) {
scoringMateCandidates[i] = (ScoringMateCandidate *) allocator->allocate(sizeof(ScoringMateCandidate) * scoringCandidatePoolSize / NUM_READS_PER_PAIR);
}
mergeAnchorPoolSize = scoringCandidatePoolSize;
mergeAnchorPool = (MergeAnchor *)allocator->allocate(sizeof(MergeAnchor) * mergeAnchorPoolSize);
if (maxSecondaryAlignmentsPerContig > 0) {
size_t size = sizeof(*hitsPerContigCounts) * index->getGenome()->getNumContigs();
hitsPerContigCounts = (HitsPerContigCounts *)allocator->allocate(size);
memset(hitsPerContigCounts, 0, size);
contigCountEpoch = 0;
} else {
hitsPerContigCounts = NULL;
}
}
bool
IntersectingPairedEndAligner::align(
Read *read0,
Read *read1,
PairedAlignmentResult* result,
PairedAlignmentResult* firstALTResult,
int maxEditDistanceForSecondaryResults,
_int64 secondaryResultBufferSize,
_int64 *nSecondaryResults,
PairedAlignmentResult *secondaryResults, // The caller passes in a buffer of secondaryResultBufferSize and it's filled in by align()
_int64 singleSecondaryBufferSize,
_int64 maxSecondaryResultsToReturn,
_int64 *nSingleEndSecondaryResultsForFirstRead,
_int64 *nSingleEndSecondaryResultsForSecondRead,
SingleAlignmentResult *singleEndSecondaryResults, // Single-end secondary alignments for when the paired-end alignment didn't work properly
_int64 maxLVCandidatesForAffineGapBufferSize,
_int64 *nLVCandidatesForAffineGap,
PairedAlignmentResult *lvCandidatesForAffineGap, // Landau-Vishkin candidates that need to be rescored using affine gap
_int64 maxSingleCandidatesForAffineGapBufferSize,
_int64 *nSingleCandidatesForAffineGapFirstRead,
_int64 *nSingleCandidatesForAffineGapSecondRead,
SingleAlignmentResult *singleCandidatesForAffineGap,
int maxK_
)
{
maxK = maxK_;
//
// For the bad aligner, we just skip this level (we're not doing affine gap at all).
//
return alignLandauVishkin(read0, read1, result, firstALTResult, maxEditDistanceForSecondaryResults, secondaryResultBufferSize,
nSecondaryResults, secondaryResults, singleSecondaryBufferSize, maxSecondaryResultsToReturn, nSingleEndSecondaryResultsForFirstRead, nSingleEndSecondaryResultsForSecondRead,
singleEndSecondaryResults, maxLVCandidatesForAffineGapBufferSize, nLVCandidatesForAffineGap, lvCandidatesForAffineGap);
if (!useAffineGap) {
//
// Version with no affine gap scoring
//
return alignLandauVishkin(read0, read1, result, firstALTResult, maxEditDistanceForSecondaryResults, secondaryResultBufferSize,
nSecondaryResults, secondaryResults, singleSecondaryBufferSize, maxSecondaryResultsToReturn, nSingleEndSecondaryResultsForFirstRead, nSingleEndSecondaryResultsForSecondRead,
singleEndSecondaryResults, maxLVCandidatesForAffineGapBufferSize, nLVCandidatesForAffineGap, lvCandidatesForAffineGap);
} else {
//
// Perform seeding, set intersection, LV alignment and identify promising candidates for affine gap scoring
//
bool fitInSecondaryBuffer = alignLandauVishkin(read0, read1, result, firstALTResult, maxEditDistanceForSecondaryResults, secondaryResultBufferSize,
nSecondaryResults, secondaryResults, singleSecondaryBufferSize, maxSecondaryResultsToReturn, nSingleEndSecondaryResultsForFirstRead, nSingleEndSecondaryResultsForSecondRead,
singleEndSecondaryResults, maxLVCandidatesForAffineGapBufferSize, nLVCandidatesForAffineGap, lvCandidatesForAffineGap);
if (*nLVCandidatesForAffineGap > maxLVCandidatesForAffineGapBufferSize) {
*nLVCandidatesForAffineGap = maxLVCandidatesForAffineGapBufferSize + 1;
return false;
}
if (!fitInSecondaryBuffer) {
return false;
}
//
// Try to align unaligned read/mate using a Hamming distance based scoring scheme that clips poorly matching start or ends
// of read/mate.
//
if (useSoftClip && (result->status[0] == NotFound || result->status[1] == NotFound)) {
fitInSecondaryBuffer = alignHamming(read0, read1, result, firstALTResult, maxEditDistanceForSecondaryResults, secondaryResultBufferSize,
nSecondaryResults, secondaryResults, singleSecondaryBufferSize, maxSecondaryResultsToReturn, nSingleEndSecondaryResultsForFirstRead, nSingleEndSecondaryResultsForSecondRead,
singleEndSecondaryResults, maxLVCandidatesForAffineGapBufferSize, nLVCandidatesForAffineGap, lvCandidatesForAffineGap);
if (*nLVCandidatesForAffineGap > maxLVCandidatesForAffineGapBufferSize) {
*nLVCandidatesForAffineGap = maxLVCandidatesForAffineGapBufferSize + 1;
return false;
}
if (!fitInSecondaryBuffer) {
return false;
}
}
//
// Perform affine gap scoring for promising candidates to get best scoring hit
//
fitInSecondaryBuffer = alignAffineGap(read0, read1, result, firstALTResult, maxEditDistanceForSecondaryResults, secondaryResultBufferSize,
nSecondaryResults, secondaryResults, singleSecondaryBufferSize, maxSecondaryResultsToReturn, nSingleEndSecondaryResultsForFirstRead, nSingleEndSecondaryResultsForSecondRead,
singleEndSecondaryResults, maxLVCandidatesForAffineGapBufferSize, nLVCandidatesForAffineGap, lvCandidatesForAffineGap);
if (!fitInSecondaryBuffer) {
return false;
}
return true;
}
}
bool
IntersectingPairedEndAligner::alignLandauVishkin(
Read* read0,
Read* read1,
PairedAlignmentResult* result,
PairedAlignmentResult* firstALTResult,
int maxEditDistanceForSecondaryResults,
_int64 secondaryResultBufferSize,
_int64* nSecondaryResults,
PairedAlignmentResult* secondaryResults, // The caller passes in a buffer of secondaryResultBufferSize and it's filled in by align()
_int64 singleSecondaryBufferSize,
_int64 maxSecondaryResultsToReturn,
_int64* nSingleEndSecondaryResultsForFirstRead,
_int64* nSingleEndSecondaryResultsForSecondRead,
SingleAlignmentResult* singleEndSecondaryResults, // Single-end secondary alignments for when the paired-end alignment didn't work properly
_int64 maxLVCandidatesForAffineGapBufferSize,
_int64* nLVCandidatesForAffineGap,
PairedAlignmentResult* lvCandidatesForAffineGap
)
{
#if INSTRUMENTATION_FOR_PAPER
_int64 startTime = timeInNanos();
_int64 nScored = 0;
_int64 setIntersectionSize = 0;
bool bestCandidateScoredFirst = false;
#endif // INSTRUMENTATION_FOR_PAPER
firstALTResult->status[0] = firstALTResult->status[1] = NotFound;
firstALTResult->refSpan[0] = firstALTResult->refSpan[1] = 0;
result->nLVCalls = 0;
result->nSmallHits = 0;
result->clippingForReadAdjustment[0] = result->clippingForReadAdjustment[1] = 0;
result->usedAffineGapScoring[0] = result->usedAffineGapScoring[1] = false;
result->basesClippedBefore[0] = result->basesClippedBefore[1] = 0;
result->basesClippedAfter[0] = result->basesClippedAfter[1] = 0;
result->agScore[0] = result->agScore[1] = 0;
result->usedGaplessClipping[0] = result->usedGaplessClipping[1] = false;
result->refSpan[0] = result->refSpan[1] = 0;
result->liftover[0] = result->liftover[1] = false;
*nSecondaryResults = 0;
*nSingleEndSecondaryResultsForFirstRead = 0;
*nSingleEndSecondaryResultsForSecondRead = 0;
*nLVCandidatesForAffineGap = 0;
int maxSeeds = 1;
#ifdef _DEBUG
if (_DumpAlignments) {
printf("\nIntersectingAligner aligning reads '%*.s' and '%.*s' with data '%.*s' and '%.*s'\n", read0->getIdLength(), read0->getId(), read1->getIdLength(), read1->getId(), read0->getDataLength(), read0->getData(), read1->getDataLength(), read1->getData());
}
#endif // _DEBUG
lowestFreeScoringCandidatePoolEntry = 0;
for (int k = 0; k <= maxK + extraSearchDepth; k++) {
scoringCandidates[k] = NULL;
}
for (unsigned i = 0; i < NUM_SET_PAIRS; i++) {
lowestFreeScoringMateCandidate[i] = 0;
}
firstFreeMergeAnchor = 0;
Read rcReads[NUM_READS_PER_PAIR];
ScoreSet scoresForAllAlignments;
ScoreSet scoresForNonAltAlignments;
unsigned popularSeedsSkipped[NUM_READS_PER_PAIR];
reads[0][FORWARD] = read0;
reads[1][FORWARD] = read1;
//
// Don't bother if one or both reads are too short. The minimum read length here is the seed length, but usually there's a longer
// minimum enforced by our caller
//
if (read0->getDataLength() < seedLen || read1->getDataLength() < seedLen) {
return true;
}
//
// Build the RC reads.
//
unsigned countOfNs = 0;
for (unsigned whichRead = 0; whichRead < NUM_READS_PER_PAIR; whichRead++) {
Read* read = reads[whichRead][FORWARD];
readLen[whichRead] = read->getDataLength();
popularSeedsSkipped[whichRead] = 0;
countOfHashTableLookups[whichRead] = 0;
#if 0
hitLocations[whichRead]->clear();
mateHitLocations[whichRead]->clear();
#endif // 0
for (Direction dir = FORWARD; dir < NUM_DIRECTIONS; dir++) {
totalHashTableHits[whichRead][dir] = 0;
largestHashTableHit[whichRead][dir] = 0;
hashTableHitSets[whichRead][dir]->init();
}
if (readLen[whichRead] > maxReadSize) {
WriteErrorMessage("IntersectingPairedEndAligner:: got too big read (%d > %d)\n"
"Change MAX_READ_LENTH at the beginning of Read.h and recompile.\n", readLen[whichRead], maxReadSize);
soft_exit(1);
}
for (unsigned i = 0; i < reads[whichRead][FORWARD]->getDataLength(); i++) {
rcReadData[whichRead][i] = rcTranslationTable[read->getData()[readLen[whichRead] - i - 1]];
rcReadQuality[whichRead][i] = read->getQuality()[readLen[whichRead] - i - 1];
countOfNs += nTable[read->getData()[i]];
}
reads[whichRead][RC] = &rcReads[whichRead];
reads[whichRead][RC]->init(read->getId(), read->getIdLength(), rcReadData[whichRead], rcReadQuality[whichRead], read->getDataLength());
}
if ((int)countOfNs > maxK) {
return true;
}
//
// Build the reverse data for both reads in both directions for the backwards LV to use.
//
for (unsigned whichRead = 0; whichRead < NUM_READS_PER_PAIR; whichRead++) {
for (Direction dir = 0; dir < NUM_DIRECTIONS; dir++) {
Read* read = reads[whichRead][dir];
for (unsigned i = 0; i < read->getDataLength(); i++) {
reversedRead[whichRead][dir][i] = read->getData()[read->getDataLength() - i - 1];
}
}
}
unsigned thisPassSeedsNotSkipped[NUM_READS_PER_PAIR][NUM_DIRECTIONS] = { {0,0}, {0,0} };
//
// Initialize the member variables that are effectively stack locals, but are in the object
// to avoid having to pass them to score.
//
localBestPairProbability[0] = 0;
localBestPairProbability[1] = 0;
//
// Phase 1: do the hash table lookups for each of the seeds for each of the reads and add them to the hit sets.
//
GenomeLocation singleHit[NUM_READS_PER_PAIR][NUM_DIRECTIONS];
for (unsigned whichRead = 0; whichRead < NUM_READS_PER_PAIR; whichRead++) {
#if 0 // bad aligner unfinished code
int seedLocation = readLen[whichRead] / 2 - seedLen / 2;
if (!Seed::DoesTextRepresentASeed(reads[whichRead][FORWARD]->getData() + seedLocation, seedLen)) {
//
// Just give up on these reads.
//
return true;
}
Seed seed(reads[whichRead][FORWARD]->getData() + seedLocation, seedLen);
//
// Find all instances of this seed in the genome.
//
_int64 nHits[NUM_DIRECTIONS];
const GenomeLocation* hits[NUM_DIRECTIONS];
const unsigned* hits32[NUM_DIRECTIONS];
if (doesGenomeIndexHave64BitLocations) {
index->lookupSeed(seed, &nHits[FORWARD], &hits[FORWARD], &nHits[RC], &hits[RC], &singleHit[whichRead][FORWARD], &singleHit[whichRead][RC]);
} else {
index->lookupSeed32(seed, &nHits[FORWARD], &hits32[FORWARD], &nHits[RC], &hits32[RC]);
}
} // for each read (seed lookup)
#endif // 0
//
// Phase 2: find locations to score (look at the last n hits of each read and see if they're near enough to hits of the neighbor to score)
//
for (int direction = FORWARD; direction <= RC; direction++) { // direction of read 0
}
int nextSeedToTest = 0;
unsigned wrapCount = 0;
int nPossibleSeeds = (int)readLen[whichRead] - seedLen + 1;
memset(seedUsed, 0, (__max(readLen[0], readLen[1]) + 7) / 8);
bool beginsDisjointHitSet[NUM_DIRECTIONS] = { true, true };
while (countOfHashTableLookups[whichRead] < nPossibleSeeds && countOfHashTableLookups[whichRead] < maxSeeds) {
if (nextSeedToTest >= nPossibleSeeds) {
wrapCount++;
beginsDisjointHitSet[FORWARD] = beginsDisjointHitSet[RC] = true;
if (wrapCount >= seedLen) {
//
// There aren't enough valid seeds in this read to reach our target.
//
break;
}
nextSeedToTest = GetWrappedNextSeedToTest(seedLen, wrapCount);
}
while (nextSeedToTest < nPossibleSeeds && IsSeedUsed(nextSeedToTest)) {
//
// This seed is already used. Try the next one.
//
nextSeedToTest++;
}
if (nextSeedToTest >= nPossibleSeeds) {
//
// Unusable seeds have pushed us past the end of the read. Go back around the outer loop so we wrap properly.
//
continue;
}
SetSeedUsed(nextSeedToTest);
if (!Seed::DoesTextRepresentASeed(reads[whichRead][FORWARD]->getData() + nextSeedToTest, seedLen)) {
//
// It's got Ns in it, so just skip it.
//
nextSeedToTest++;
continue;
}
Seed seed(reads[whichRead][FORWARD]->getData() + nextSeedToTest, seedLen);
//
// Find all instances of this seed in the genome.
//
_int64 nHits[NUM_DIRECTIONS];
const GenomeLocation* hits[NUM_DIRECTIONS];
const unsigned* hits32[NUM_DIRECTIONS];
if (doesGenomeIndexHave64BitLocations) {
index->lookupSeed(seed, &nHits[FORWARD], &hits[FORWARD], &nHits[RC], &hits[RC],
hashTableHitSets[whichRead][FORWARD]->getNextSingletonLocation(), hashTableHitSets[whichRead][RC]->getNextSingletonLocation());
}
else {
index->lookupSeed32(seed, &nHits[FORWARD], &hits32[FORWARD], &nHits[RC], &hits32[RC]);
}
countOfHashTableLookups[whichRead]++;
for (Direction dir = FORWARD; dir < NUM_DIRECTIONS; dir++) {
int offset;
if (dir == FORWARD) {
offset = nextSeedToTest;
}
else {
offset = readLen[whichRead] - seedLen - nextSeedToTest;
}
if (nHits[dir] < maxBigHits) {
totalHashTableHits[whichRead][dir] += nHits[dir];
if (doesGenomeIndexHave64BitLocations) {
hashTableHitSets[whichRead][dir]->recordLookup(offset, nHits[dir], hits[dir], beginsDisjointHitSet[dir]);
}
else {
hashTableHitSets[whichRead][dir]->recordLookup(offset, nHits[dir], hits32[dir], beginsDisjointHitSet[dir]);
}
beginsDisjointHitSet[dir] = false;
}
else {
popularSeedsSkipped[whichRead]++;
}
} // for each direction
//
// If we don't have enough seeds left to reach the end of the read, space out the seeds more-or-less evenly.
//
if ((maxSeeds - countOfHashTableLookups[whichRead] + 1) * (int)seedLen + nextSeedToTest < nPossibleSeeds) {
_ASSERT((nPossibleSeeds - nextSeedToTest - 1) / (maxSeeds - countOfHashTableLookups[whichRead] + 1) >= (int)seedLen);
nextSeedToTest += (nPossibleSeeds - nextSeedToTest - 1) / (maxSeeds - countOfHashTableLookups[whichRead] + 1);
_ASSERT(nextSeedToTest < nPossibleSeeds); // We haven't run off the end of the read.
}
else {
nextSeedToTest += seedLen;
}
} // while we need to lookup seeds for this read
} // for each read
#if INSTRUMENTATION_FOR_PAPER
int hashTableHits[NUM_READS_PER_PAIR] = { hashTableHitSets[0][FORWARD]->getNumDistinctHitLocations(0) + hashTableHitSets[0][RC]->getNumDistinctHitLocations(0),
hashTableHitSets[1][FORWARD]->getNumDistinctHitLocations(0) + hashTableHitSets[1][RC]->getNumDistinctHitLocations(0) };
int log2HashTableHits[NUM_READS_PER_PAIR] = { __min(cheezyLogBase2(hashTableHits[0]), MAX_HIT_SIZE_LOG_2),__min(cheezyLogBase2(hashTableHits[1]), MAX_HIT_SIZE_LOG_2) };
// = { __min(cheezyLogBase2(totalHashTableHits[0][FORWARD] + totalHashTableHits[0][RC]), MAX_HIT_SIZE_LOG_2), __min(cheezyLogBase2(totalHashTableHits[1][FORWARD] + totalHashTableHits[1][RC]), MAX_HIT_SIZE_LOG_2) };
#endif // INSTRUMENTATION_FOR_PAPER
readWithMoreHits = totalHashTableHits[0][FORWARD] + totalHashTableHits[0][RC] > totalHashTableHits[1][FORWARD] + totalHashTableHits[1][RC] ? 0 : 1;
readWithFewerHits = 1 - readWithMoreHits;
#ifdef _DEBUG
if (_DumpAlignments) {
printf("Read 0 has %lld hits, read 1 has %lld hits\n", totalHashTableHits[0][FORWARD] + totalHashTableHits[0][RC], totalHashTableHits[1][FORWARD] + totalHashTableHits[1][RC]);
}
#endif // _DEBUG
Direction setPairDirection[NUM_SET_PAIRS][NUM_READS_PER_PAIR] = {{FORWARD, RC}, {RC, FORWARD}};
//
// Phase 2: find all possible candidates and add them to candidate lists (for the reads with fewer and more hits).
//
int maxUsedBestPossibleScoreList = 0;
for (unsigned whichSetPair = 0; whichSetPair < NUM_SET_PAIRS; whichSetPair++) {
HashTableHitSet *setPair[NUM_READS_PER_PAIR];
if (whichSetPair == 0) {
setPair[0] = hashTableHitSets[0][FORWARD];
setPair[1] = hashTableHitSets[1][RC];
} else {
setPair[0] = hashTableHitSets[0][RC];
setPair[1] = hashTableHitSets[1][FORWARD];
}
unsigned lastSeedOffsetForReadWithFewerHits;
GenomeLocation lastGenomeLocationForReadWithFewerHits;
GenomeLocation lastGenomeLocationForReadWithMoreHits;
unsigned lastSeedOffsetForReadWithMoreHits;
bool outOfMoreHitsLocations = false;
//
// Seed the intersection state by doing a first lookup.
//
if (setPair[readWithFewerHits]->getFirstHit(&lastGenomeLocationForReadWithFewerHits, &lastSeedOffsetForReadWithFewerHits)) {
//
// No hits in this direction.
//
continue; // The outer loop over set pairs.
}
lastGenomeLocationForReadWithMoreHits = InvalidGenomeLocation;
//
// Loop over the candidates in for the read with more hits. At the top of the loop, we have a candidate but don't know if it has
// a mate. Each pass through the loop considers a single hit on the read with fewer hits.
//
for (;;) {
//
// Loop invariant: lastGenomeLocationForReadWithFewerHits is the highest genome offset that has not been considered.
// lastGenomeLocationForReadWithMoreHits is also the highest genome offset on that side that has not been
// considered (or is InvalidGenomeLocation), but higher ones within the appropriate range might already be in scoringMateCandidates.
// We go once through this loop for each. Because the index is ordered high to low, we also go in that direction.
//
if (lastGenomeLocationForReadWithMoreHits > lastGenomeLocationForReadWithFewerHits + maxSpacing) {
//
// The more hits side is too high to be a mate candidate for the fewer hits side. Move it down to the largest
// location that's not too high.
//
if (!setPair[readWithMoreHits]->getNextHitLessThanOrEqualTo(lastGenomeLocationForReadWithFewerHits + maxSpacing,
&lastGenomeLocationForReadWithMoreHits, &lastSeedOffsetForReadWithMoreHits)) {
break; // End of all of the mates. We're done with this set pair.
}
}
if ((lastGenomeLocationForReadWithMoreHits + maxSpacing < lastGenomeLocationForReadWithFewerHits || outOfMoreHitsLocations) &&
(0 == lowestFreeScoringMateCandidate[whichSetPair] ||
!genomeLocationIsWithin(scoringMateCandidates[whichSetPair][lowestFreeScoringMateCandidate[whichSetPair]-1].readWithMoreHitsGenomeLocation, lastGenomeLocationForReadWithFewerHits, maxSpacing))) {
//
// No mates for the hit on the read with fewer hits. Skip to the next candidate.
//
if (outOfMoreHitsLocations) {
//
// Nothing left on the more hits side, we're done with this set pair.
//
break;
}
if (!setPair[readWithFewerHits]->getNextHitLessThanOrEqualTo(lastGenomeLocationForReadWithMoreHits + maxSpacing, &lastGenomeLocationForReadWithFewerHits,
&lastSeedOffsetForReadWithFewerHits)) {
//
// No more candidates on the read with fewer hits side. We're done with this set pair.
//
break;
}
continue;
}
//
// Add all of the mate candidates for the fewer side hit.
//
GenomeLocation previousMoreHitsLocation = lastGenomeLocationForReadWithMoreHits;
while (lastGenomeLocationForReadWithMoreHits + maxSpacing >= lastGenomeLocationForReadWithFewerHits && !outOfMoreHitsLocations) {
unsigned bestPossibleScoreForReadWithMoreHits;
if (noTruncation) {
bestPossibleScoreForReadWithMoreHits = 0;
} else {
bestPossibleScoreForReadWithMoreHits = setPair[readWithMoreHits]->computeBestPossibleScoreForCurrentHit();
}
if (lowestFreeScoringMateCandidate[whichSetPair] >= scoringCandidatePoolSize / NUM_READS_PER_PAIR) {
WriteErrorMessage("Ran out of scoring candidate pool entries. Perhaps trying with a larger value of -mcp will help.\n");
soft_exit(1);
}
scoringMateCandidates[whichSetPair][lowestFreeScoringMateCandidate[whichSetPair]].init(
lastGenomeLocationForReadWithMoreHits, bestPossibleScoreForReadWithMoreHits, lastSeedOffsetForReadWithMoreHits);
#ifdef _DEBUG
if (_DumpAlignments) {
printf("SetPair %d, added more hits candidate %d at genome location %s:%llu, bestPossibleScore %d, seedOffset %d\n",
whichSetPair, lowestFreeScoringMateCandidate[whichSetPair],
genome->getContigAtLocation(lastGenomeLocationForReadWithMoreHits)->name,
lastGenomeLocationForReadWithMoreHits - genome->getContigAtLocation(lastGenomeLocationForReadWithMoreHits)->beginningLocation,
bestPossibleScoreForReadWithMoreHits,
lastSeedOffsetForReadWithMoreHits);
}
#endif // _DEBUG
lowestFreeScoringMateCandidate[whichSetPair]++;
previousMoreHitsLocation = lastGenomeLocationForReadWithMoreHits;
if (!setPair[readWithMoreHits]->getNextLowerHit(&lastGenomeLocationForReadWithMoreHits, &lastSeedOffsetForReadWithMoreHits)) {
lastGenomeLocationForReadWithMoreHits = 0;
outOfMoreHitsLocations = true;
break; // out of the loop looking for candidates on the more hits side.
}
}
//
// And finally add the hit from the fewer hit side. To compute its best possible score, we need to look at all of the mates; we couldn't do it in the
// loop immediately above because some of them might have already been in the mate list from a different, nearby fewer hit location.
//
int bestPossibleScoreForReadWithFewerHits;
if (noTruncation) {
bestPossibleScoreForReadWithFewerHits = 0;
} else {
bestPossibleScoreForReadWithFewerHits = setPair[readWithFewerHits]->computeBestPossibleScoreForCurrentHit();
}
int lowestBestPossibleScoreOfAnyPossibleMate = maxK + extraSearchDepth;
for (int i = lowestFreeScoringMateCandidate[whichSetPair] - 1; i >= 0; i--) {
if (scoringMateCandidates[whichSetPair][i].readWithMoreHitsGenomeLocation > lastGenomeLocationForReadWithFewerHits + maxSpacing) {
break;
}
lowestBestPossibleScoreOfAnyPossibleMate = __min(lowestBestPossibleScoreOfAnyPossibleMate, scoringMateCandidates[whichSetPair][i].bestPossibleScore);
}
if (lowestBestPossibleScoreOfAnyPossibleMate + bestPossibleScoreForReadWithFewerHits <= maxK + extraSearchDepth) {
//
// There's a set of ends that we can't prove doesn't have too large of a score. Allocate a fewer hit candidate and stick it in the
// correct weight list.
//
if (lowestFreeScoringCandidatePoolEntry >= scoringCandidatePoolSize) {
WriteErrorMessage("Ran out of scoring candidate pool entries. Perhaps rerunning with a larger value of -mcp will help.\n");
soft_exit(1);
}
//
// If we have noOrderedEvaluation set, just stick everything on list 0, regardless of what it really is. This will cause us to
// evaluate the candidates in more-or-less inverse genome order.
//
int bestPossibleScore = noOrderedEvaluation ? 0 : lowestBestPossibleScoreOfAnyPossibleMate + bestPossibleScoreForReadWithFewerHits;
scoringCandidatePool[lowestFreeScoringCandidatePoolEntry].init(lastGenomeLocationForReadWithFewerHits, whichSetPair, lowestFreeScoringMateCandidate[whichSetPair] - 1,
lastSeedOffsetForReadWithFewerHits, bestPossibleScoreForReadWithFewerHits,
scoringCandidates[bestPossibleScore]);
scoringCandidates[bestPossibleScore] = &scoringCandidatePool[lowestFreeScoringCandidatePoolEntry];
#if INSTRUMENTATION_FOR_PAPER
setIntersectionSize++;
#endif // INSTRUMENTATION_FOR_PAPER
#ifdef _DEBUG
if (_DumpAlignments) {
printf("SetPair %d, added fewer hits candidate %d at genome location %s:%llu, bestPossibleScore %d, seedOffset %d\n",
whichSetPair, lowestFreeScoringCandidatePoolEntry,
genome->getContigAtLocation(lastGenomeLocationForReadWithFewerHits)->name, lastGenomeLocationForReadWithFewerHits - genome->getContigAtLocation(lastGenomeLocationForReadWithFewerHits)->beginningLocation,
lowestBestPossibleScoreOfAnyPossibleMate + bestPossibleScoreForReadWithFewerHits,
lastSeedOffsetForReadWithFewerHits);
}
#endif // _DEBUG
lowestFreeScoringCandidatePoolEntry++;
maxUsedBestPossibleScoreList = max(maxUsedBestPossibleScoreList, bestPossibleScore);
}
if (!setPair[readWithFewerHits]->getNextLowerHit(&lastGenomeLocationForReadWithFewerHits, &lastSeedOffsetForReadWithFewerHits)) {
break;
}
} // forever (the loop that does the intersection walk)
} // For each set pair
//
// Phase 2a: mark any scoring candidates that are near enough to one another to be indels so that we can increase the score limit
// when considering them.
//
for (int whichSetPair = 0; whichSetPair < NUM_SET_PAIRS; whichSetPair++) {
//
// For every candidate, we're trying to find the candidate that's the farthest away from it in genome
// space that's still within maxDistForIndels and then mark that distance in the candidate. We do that
// by keeping two indices into the array, a bottom and top (relative to the array; the actual genome
// locations go the other way). Each pass through the loop we move one or the other. We move up top if
// bottom is one less or they're within maxDistForIndels of one another. Otherwise we move up bottom.
//
int bottom = 0;
int top = 1;
while (top < lowestFreeScoringMateCandidate[whichSetPair]) {
_ASSERT(bottom < top);
_ASSERT(scoringMateCandidates[whichSetPair][bottom].readWithMoreHitsGenomeLocation > scoringMateCandidates[whichSetPair][top].readWithMoreHitsGenomeLocation); // Remember, they're in backward order.
GenomeDistance spread = DistanceBetweenGenomeLocations(scoringMateCandidates[whichSetPair][bottom].readWithMoreHitsGenomeLocation, scoringMateCandidates[whichSetPair][top].readWithMoreHitsGenomeLocation);
if (spread < maxKForIndels) {
scoringMateCandidates[whichSetPair][bottom].largestBigIndelDetected = __max(spread, scoringMateCandidates[whichSetPair][bottom].largestBigIndelDetected);
scoringMateCandidates[whichSetPair][top].largestBigIndelDetected = __max(spread, scoringMateCandidates[whichSetPair][top].largestBigIndelDetected);
#if _DEBUG
if (_DumpAlignments) {
fprintf(stderr,"Set largest big indel detected to %lld, %lld for set pair %d mate candidates %d and %d\n",
scoringMateCandidates[whichSetPair][bottom].largestBigIndelDetected, scoringMateCandidates[whichSetPair][top].largestBigIndelDetected,
whichSetPair, bottom, top);
}
#endif // DEBUG
top++;
} else if (bottom < top - 1) {
//
// Move up bottom since it will still be less than top.
//
bottom++;
} else {
//
// They're next to each other and still too far apart. Move up both.
//
bottom++;
top++;
}
} // While we're still considering candidates in one set pair.
} // for each set pair
//
// Now do the fewer end candidates. This works the same way as the loop above (except they're not segregated by set pair).
//
{ // a scope for us to declare locals in.
int bottom = 0;
int top = 1;
while (top < lowestFreeScoringCandidatePoolEntry) {
_ASSERT(bottom < top);
if (scoringCandidatePool[bottom].whichSetPair != scoringCandidatePool[top].whichSetPair) {
bottom = top;
top = top + 1;
continue;
}
_ASSERT(scoringCandidatePool[bottom].readWithFewerHitsGenomeLocation > scoringCandidatePool[top].readWithFewerHitsGenomeLocation); // They're in backward order
GenomeDistance spread = DistanceBetweenGenomeLocations(scoringCandidatePool[bottom].readWithFewerHitsGenomeLocation, scoringCandidatePool[top].readWithFewerHitsGenomeLocation);
if (spread < maxKForIndels) {
scoringCandidatePool[bottom].largestBigIndelDetected = __max(spread, scoringCandidatePool[bottom].largestBigIndelDetected);
scoringCandidatePool[top].largestBigIndelDetected = __max(spread, scoringCandidatePool[top].largestBigIndelDetected);
#if _DEBUG
if (_DumpAlignments) {
fprintf(stderr, "Set largest big indel to %d, %d for fewer end candidates %d and %d\n",
scoringCandidatePool[bottom].largestBigIndelDetected, scoringCandidatePool[top].largestBigIndelDetected, bottom, top);
}
#endif // DEBUG
top++;
} else if (bottom < top - 1) {
bottom++;
} else {
bottom++;
top++;
}
} // While we're still looking.
} // Scope for fewer candidate possible indel detection.
//
// Phase 3: score and merge the candidates we've found using Laundau-Vishkin (edit distance, not affine gap).
//
int currentBestPossibleScoreList = 0;
//
// Loop until we've scored all of the candidates, or proven that what's left must have too high of a score to be interesting.
//
//
while (currentBestPossibleScoreList <= maxUsedBestPossibleScoreList &&
currentBestPossibleScoreList <= extraSearchDepth + min(maxK, max( // Never look for worse than our worst interesting score
min(scoresForAllAlignments.bestPairScore, scoresForNonAltAlignments.bestPairScore - maxScoreGapToPreferNonAltAlignment), // Worst we care about for ALT
min(scoresForAllAlignments.bestPairScore + maxScoreGapToPreferNonAltAlignment, scoresForNonAltAlignments.bestPairScore)))) // And for non-ALT
{
if (scoringCandidates[currentBestPossibleScoreList] == NULL) {
//
// No more candidates on this list. Skip to the next one.
//
currentBestPossibleScoreList++;
continue;
}
//
// Grab the first candidate on the highest list and score it.
//
ScoringCandidate *candidate = scoringCandidates[currentBestPossibleScoreList];
int fewerEndScore;
double fewerEndMatchProbability;
int fewerEndGenomeLocationOffset;
bool nonALTAlignment = (!altAwareness) || !genome->isGenomeLocationALT(candidate->readWithFewerHitsGenomeLocation);
int scoreLimit = computeScoreLimit(nonALTAlignment, &scoresForAllAlignments, &scoresForNonAltAlignments, candidate->largestBigIndelDetected);
if (currentBestPossibleScoreList > scoreLimit) {
//
// Remove us from the head of the list and proceed to the next candidate to score. We can get here because now we know ALT/non-ALT, which have different limits.
//
scoringCandidates[currentBestPossibleScoreList] = candidate->scoreListNext;
continue;
}
scoreLocation(readWithFewerHits, setPairDirection[candidate->whichSetPair][readWithFewerHits], candidate->readWithFewerHitsGenomeLocation,
candidate->seedOffset, scoreLimit, &fewerEndScore, &fewerEndMatchProbability, &fewerEndGenomeLocationOffset, &candidate->usedAffineGapScoring,
&candidate->basesClippedBefore, &candidate->basesClippedAfter, &candidate->agScore, &candidate->lvIndels, &candidate->usedGaplessClipping, &candidate->refSpan);
#if INSTRUMENTATION_FOR_PAPER
nScored++;
#endif // INSTRUMENTATION_FOR_PAPER
candidate->matchProbability = fewerEndMatchProbability;
_ASSERT(ScoreAboveLimit == fewerEndScore || fewerEndScore >= candidate->bestPossibleScore);
#ifdef _DEBUG
if (_DumpAlignments) {
printf("Scored fewer end candidate %d, set pair %d, read %d, location %s:%llu, seed offset %d, score limit %d, score %d, offset %d, agScore %d, matchProb %e\n",
(int)(candidate - scoringCandidatePool),
candidate->whichSetPair, readWithFewerHits,
genome->getContigAtLocation(candidate->readWithFewerHitsGenomeLocation)->name,
candidate->readWithFewerHitsGenomeLocation - genome->getContigAtLocation(candidate->readWithFewerHitsGenomeLocation)->beginningLocation,
candidate->seedOffset,
scoreLimit, fewerEndScore, fewerEndGenomeLocationOffset, candidate->agScore, fewerEndMatchProbability);
}
#endif // DEBUG
if (fewerEndScore != ScoreAboveLimit) {
//
// Find and score mates. The index in scoringMateCandidateIndex is the lowest mate (i.e., the highest index number).
//
unsigned mateIndex = candidate->scoringMateCandidateIndex;
for (;;) {
ScoringMateCandidate *mate = &scoringMateCandidates[candidate->whichSetPair][mateIndex];
scoreLimit = computeScoreLimit(nonALTAlignment, &scoresForAllAlignments, &scoresForNonAltAlignments, mate->largestBigIndelDetected);
_ASSERT(genomeLocationIsWithin(mate->readWithMoreHitsGenomeLocation, candidate->readWithFewerHitsGenomeLocation, maxSpacing));
//
// Exclude it if it's strictly smaller than minSpacing; hence, minSpacing -1.
//
if (!genomeLocationIsWithin(mate->readWithMoreHitsGenomeLocation, candidate->readWithFewerHitsGenomeLocation, minSpacing -1) && ((mate->bestPossibleScore <= scoreLimit - fewerEndScore))) {
//
// It's within the range and not necessarily too poor of a match. Consider it.
//
//
// If we haven't yet scored this mate, or we've scored it and not gotten an answer, but had a higher score limit than we'd
// use now, score it.
//
int mateScoreLimit = scoreLimit - fewerEndScore;
if (mate->score == ScoringMateCandidate::LocationNotYetScored || (mate->score == ScoreAboveLimit && mate->scoreLimit < scoreLimit - fewerEndScore)) {
scoreLocation(readWithMoreHits, setPairDirection[candidate->whichSetPair][readWithMoreHits], GenomeLocationAsInt64(mate->readWithMoreHitsGenomeLocation),
mate->seedOffset, mateScoreLimit, &mate->score, &mate->matchProbability,
&mate->genomeOffset, &mate->usedAffineGapScoring, &mate->basesClippedBefore, &mate->basesClippedAfter, &mate->agScore, &mate->lvIndels, &mate->usedGaplessClipping, &mate->refSpan);
#ifdef _DEBUG
if (_DumpAlignments) {
printf("Scored mate candidate %d, set pair %d, read %d, location %s:%llu, seed offset %d, score limit %d, score %d, offset %d, agScore %d, matchProb %e\n",
(int)(mate - scoringMateCandidates[candidate->whichSetPair]), candidate->whichSetPair, readWithMoreHits,
genome->getContigAtLocation(mate->readWithMoreHitsGenomeLocation)->name,
mate->readWithMoreHitsGenomeLocation - genome->getContigAtLocation(mate->readWithMoreHitsGenomeLocation)->beginningLocation,
mate->seedOffset, mateScoreLimit, mate->score, mate->genomeOffset, mate->agScore, mate->matchProbability);
}
#endif // _DEBUG
#if INSTRUMENTATION_FOR_PAPER
nScored++;
#endif // INSTRUMENTATION_FOR_PAPER
_ASSERT(ScoreAboveLimit == mate->score || mate->score >= mate->bestPossibleScore);
mate->scoreLimit = scoreLimit - fewerEndScore;
}
if (mate->score != ScoreAboveLimit && (fewerEndScore + mate->score <= scoreLimit)) { // We need to check to see that we're below scoreLimit because we may have scored this earlier when scoreLimit was higher.
double pairProbability = mate->matchProbability * fewerEndMatchProbability;
int pairScore = mate->score + fewerEndScore;
int pairAGScore = mate->agScore + candidate->agScore;
//
// See if this should be ignored as a merge, or if we need to back out a previously scored location
// because it's a worse version of this location.
//
MergeAnchor *mergeAnchor = candidate->mergeAnchor;
if (NULL == mergeAnchor) {
//
// Look up and down the array of candidates to see if we have possible merge candidates.
//
for (ScoringCandidate *mergeCandidate = candidate - 1;
mergeCandidate >= scoringCandidatePool &&
genomeLocationIsWithin(mergeCandidate->readWithFewerHitsGenomeLocation, candidate->readWithFewerHitsGenomeLocation + fewerEndGenomeLocationOffset, 50) &&
mergeCandidate->whichSetPair == candidate->whichSetPair;
mergeCandidate--) {
if (mergeCandidate->mergeAnchor != NULL) {
candidate->mergeAnchor = mergeAnchor = mergeCandidate->mergeAnchor;
break;
}
}
if (NULL == mergeAnchor) {
for (ScoringCandidate *mergeCandidate = candidate + 1;
mergeCandidate < scoringCandidatePool + lowestFreeScoringCandidatePoolEntry &&
genomeLocationIsWithin(mergeCandidate->readWithFewerHitsGenomeLocation, candidate->readWithFewerHitsGenomeLocation + fewerEndGenomeLocationOffset, 50) &&
mergeCandidate->whichSetPair == candidate->whichSetPair;
mergeCandidate++) {
if (mergeCandidate->mergeAnchor != NULL) {
candidate->mergeAnchor = mergeAnchor = mergeCandidate->mergeAnchor;
break;
}
}
}
}
bool eliminatedByMerge; // Did we merge away this result. If this is false, we may still have merged away a previous result.
double oldPairProbability;
bool mergeReplacement = false; // Did we replace the anchor with the new candidate ?
if (NULL == mergeAnchor) {