-
Notifications
You must be signed in to change notification settings - Fork 1
/
fmd.cpp
1295 lines (1026 loc) · 41.3 KB
/
fmd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "fmd.h"
#include <algorithm>
#include <stdexcept>
namespace CSA
{
const std::string BASES = "TGCNA";
const std::string ALPHABETICAL_BASES = "ACGNT";
FMDPosition::FMDPosition(usint forward_start, usint reverse_start,
usint end_offset): forward_start(forward_start), reverse_start(reverse_start),
end_offset(end_offset)
{
}
FMDPosition::FMDPosition(): forward_start(0), reverse_start(0), end_offset(-1)
{
}
FMDPosition
FMDPosition::flip() const
{
// Swap the two intervals of the bi-interval
return FMDPosition(reverse_start, forward_start, end_offset);
}
bool FMDPosition::operator==(const FMDPosition& other) const
{
// Compare all the fields.
return
forward_start == other.forward_start &&
reverse_start == other.reverse_start &&
end_offset == other.end_offset;
}
sint
FMDPosition::range(const RangeVector& ranges) const
{
// Get an iterator for making rank queries.
// TODO: Is it efficient to do this a lot?
RangeVector::Iterator iter(ranges);
// Look up the range that the forward starting position is in
sint start_range = iter.rank(forward_start);
// And the range the forward end is in
sint end_range = iter.rank(forward_start + end_offset);
if(start_range == end_range)
{
// Both ends of the interval are in the same range.
return start_range;
}
else
{
// There is no range spanning our forward-strand interval.
return -1;
}
}
sint
FMDPosition::ranges(const RangeVector& ranges) const
{
// Get an iterator for making rank queries.
// TODO: Is it efficient to do this a lot?
RangeVector::Iterator iter(ranges);
// Look up the range that the starting position is in
sint start_range = iter.rank(forward_start);
// And the range the end is in
sint end_range = iter.rank(forward_start + end_offset);
// Return the number of ranges we intersect (1s hit plus 1)
return(end_range - start_range + 1);
}
std::ostream& operator<< (std::ostream& o, FMDPosition const& position)
{
// Report both the ranges that we represent.
return o << position.forward_start << "-" <<
(position.forward_start + position.end_offset) << "|" <<
position.reverse_start << "-" << (position.reverse_start +
position.end_offset);
}
Mapping::Mapping(): location(0, 0), is_mapped(false)
{
}
Mapping::Mapping(pair_type location, bool is_mapped): location(location),
is_mapped(is_mapped)
{
}
bool
Mapping::operator==(const Mapping& other) const
{
return location == other.location && is_mapped == other.is_mapped;
}
std::ostream&
operator<< (std::ostream& o, Mapping const& mapping)
{
if(mapping.is_mapped)
{
o << "Text " << mapping.location.first << " offset " <<
mapping.location.second;
}
else
{
o << "-----------------";
}
return o;
}
// Stuff for FMDIterators that traverse the suffix tree.
FMDIterator::FMDIterator(const FMD& parent, usint depth, bool beEnd,
bool reportDeadEnds):
parent(parent), depth(depth), reportDeadEnds(reportDeadEnds), stack(),
pattern()
{
// By default we start out with empty everything, which is what we should have
// at the end.
if(!beEnd)
{
// Start at the beginning.
search();
}
}
FMDIterator::FMDIterator(const FMDIterator& toCopy): parent(toCopy.parent),
depth(toCopy.depth), reportDeadEnds(toCopy.reportDeadEnds),
stack(toCopy.stack), pattern(toCopy.pattern)
{
// Already made a duplicate stack. Nothing to do.
}
FMDIterator& FMDIterator::operator++()
{
// Advance
search();
// Return ourselves.
return *this;
}
FMDIterator FMDIterator::operator++(int)
{
// Copy ourselves
FMDIterator copy(*this);
// Advance
search();
// Return the copy
return copy;
}
std::pair<std::string, FMDPosition> FMDIterator::operator*() const
{
// Just grab the value we stored.
return toYield;
}
void FMDIterator::yield(std::pair<std::string, FMDPosition> value) {
// Save the value for returning when dereferenced.
toYield = value;
}
bool FMDIterator::operator==(const FMDIterator& other) const
{
return
// We have the same parent addresses
&parent == &(other.parent) &&
// And go to the same depth
depth == other.depth &&
// And both report dead ends or not
reportDeadEnds == other.reportDeadEnds &&
// And are at the same depth
stack.size() == other.stack.size() &&
// And followed the same path to get there
std::equal(stack.begin(), stack.end(), other.stack.begin()) &&
// And we have the same string pattern (which the above should imply)
pattern == other.pattern;
}
bool FMDIterator::operator!=(const FMDIterator& other) const
{
// Just use the equality check.
return !(*this == other);
}
void FMDIterator::search()
{
// TODO: Unify with tryRecurseToDepth by making its topDepth a parameter.
if(stack.size() == 0)
{
// Try to recurse down to depth, starting with base 0 at the root branch.
tryRecurseToDepth(0);
// Now we are either at the correct depth at the leftmost suffix tree node,
// or in the state we started in (which is equal to end). So we are done.
}
else if(stack.size() == depth)
{
// We were at the right depth, meaning we don't need to recurse further.
do
{
// Pop the bottom frame (already explored).
std::pair<FMDPosition, usint> lastFrame = pop();
// Try recursing to the next thing at that same level and then down to the
// required depth.
if(tryRecurseToDepth(lastFrame.second + 1))
{
// We got something at the required depth, or ran into a shorter end of
// text suffix to report.
return;
}
// Otherwise, we didn't get something to yield in this subtree. That means
// this subtree has been exhausted and we should pop and try the next one
// over, which we will do on the next loop iteration.
}
while(stack.size() > 0);
// If we get here, we have gone all the way back up and popped and tried
// replacing everything with no more results. That means we have finished
// the search, and should be equal to end.
} else if(reportDeadEnds) {
// We weren't at the right depth; we were at a node that happened to show up
// followed by a text end somewhere. Continue recursing from here.
if(tryRecurseToDepth(0)) {
// We can just go down from here.
return;
}
// Otherwise we need to pop up and continue the main recursion loop from
// above. TODO: Unify.
do
{
// Pop the bottom frame (already explored).
std::pair<FMDPosition, usint> lastFrame = pop();
// Try recursing to the next thing at that same level and then down to the
// required depth.
if(tryRecurseToDepth(lastFrame.second + 1))
{
// We got something at the required depth, or another shorter suffix
// followed by end of text.
return;
}
// Otherwise, we didn't get something at the required depth in this
// subtree. That means this subtree has been exhausted and we should pop
// and try the next one over, which we will do on the next loop iteration.
}
while(stack.size() > 0);
} else {
// We broke something.
throw std::runtime_error("Iterator was at wrong depth");
}
}
bool FMDIterator::recurse(usint baseNumber)
{
if(baseNumber >= NUM_BASES)
{
// Don't even try out-of-range bases.
return false;
}
// What will we extend to?
FMDPosition extension;
if(stack.size() == 0)
{
// Our "extension" is just starting with this base.
extension = parent.getCharPosition(ALPHABETICAL_BASES[baseNumber]);
}
else
{
// Work out what we would select if we extended forwards with this letter
// (i.e. appended it to the suffix).
extension = parent.extend(stack.back().first,
ALPHABETICAL_BASES[baseNumber], false);
}
if(extension.isEmpty())
{
// This would be a suffix that doesn't appear.
return false;
}
// This would not be an empty place. Go there.
// Add a stack frame
stack.push_back(std::make_pair(extension, baseNumber));
// And record the change to the pattern.
pattern.push_back(ALPHABETICAL_BASES[baseNumber]);
return true;
}
bool FMDIterator::tryRecurse(usint baseNumber)
{
// Try every base number after the given starting one until we either recurse
// or run out.
for(; baseNumber < NUM_BASES && !recurse(baseNumber); baseNumber++);
// If we didn't run out, we must have successfully recursed.
return baseNumber < NUM_BASES;
}
bool FMDIterator::tryRecurseToDepth(usint baseNumber)
{
// What depth should we not go above?
size_t topDepth = stack.size();
while(stack.size() < depth)
{
// Until we get to the right depth...
// We never go into an empty thing.
// So we must be at a shallower depth. Try to go deeper.
if(tryRecurse(baseNumber)) {
// We made it down. Reset baseNumber
baseNumber = 0;
if(reportDeadEnds && stack.size() < depth) {
// If we lose some range here (i.e. some positions are followed by end
// of text and don't show up in an extension with any base), we have to
// stop so that we yield them. Those positions will be the first ones in
// our range if they exist.
// See what we would get if we extended. TODO: Can we show that this
// will always work? It seems to work when there are none of the first
// base, and should always work when there is some of the first base,
// but I'm not sure it won't break.
FMDPosition extension = parent.extend(stack.back().first,
ALPHABETICAL_BASES[0], false);
if(extension.forward_start != stack.back().first.forward_start) {
// There are some suffixes that come into this node and leave before
// the first real base. They must end the text.
INFO(
std::cout << "End of text: " << pattern << "$" << std::endl;
std::cout << extension << " vs. " << stack.back().first <<
std::endl;
)
// We got to a place we want to yield.
// Grab the FMDPosition we want to return
FMDPosition toConvert = stack.back().first;
// But before converting it to SA coordinates, fix it up to indicate
// only the part we aren't covering. Forward start is going to be the
// same, but it is going to run until the start of extension. Subtract
// 1 to keep this as an offset where 0 = a 1-base itnerval.
toConvert.end_offset = extension.forward_start -
toConvert.forward_start - 1;
// TODO: The reverse start can't be moved sanely and is thus going to
// be invalid (we can't search anchored to text start).
// We need to convert it from BWT coordinates (which we use internally
// for extension) to SA coordinates (which are natural for locate).
parent.convertToSAIndex(toConvert.forward_start);
parent.convertToSAIndex(toConvert.reverse_start);
// Grab the pattern and the FMDPosition from the top of the stack to
// go with it. Yield the pair.
yield(std::make_pair(pattern, toConvert));
return true;
}
}
} else {
// We can't go to anything nonempty down. So we should try going up.
if(stack.size() == topDepth) {
// We don't want to go any higher than this. Give up on finding a
// nonempty thing at the right depth in this subtree.
return false;
}
// Otherwise, go up and continue on the next branch.
baseNumber = pop().second + 1;
}
}
// We got to the right depth, and we never go to empty things.
// Grab the FMDPosition we want to return
FMDPosition toConvert = stack.back().first;
// We need to convert it from BWT coordinates (which we use internally for
// extension) to SA coordinates (which are natural for locate).
parent.convertToSAIndex(toConvert.forward_start);
parent.convertToSAIndex(toConvert.reverse_start);
// Grab the pattern and the FMDPosition from the top of the stack to go with
// it. Yiueld the pair.
yield(std::make_pair(pattern, toConvert));
return true;
}
std::pair<FMDPosition, usint> FMDIterator::pop()
{
// Drop the character that this added to the pattern.
pattern.erase(pattern.size() - 1, 1);
// Grab the stack frame to return
std::pair<FMDPosition, usint> toReturn(stack.back());
// Drop it from the stack.
stack.pop_back();
// Return it
return toReturn;
}
FMD::FMD(const std::string& base_name, bool print):
RLCSA(base_name, print)
{
}
FMDPosition
FMD::extend(FMDPosition range, usint c, bool backward) const
{
// More or less directly implemented off of algorithms 2 and 3 in "Exploring
// single-sample SNP and INDEL calling with whole-genome de novo assembly"
// (Li, 2012). However, our character indices are one less, since we don't
// allow search patterns to include the end-of-text symbol. We also use
// alphabetical ordering instead of the paper's N-last ordering in the FM-
// index, and consequently need to assign reverse ranges in alphabetical order
// by reverse complement.
if(backward)
{
// Only allow characters in the index
if(c >= CHARS || this->array[c] == 0) {
DEBUG(std::cout << "Character " << c << " not in index." << std::endl;)
return EMPTY_FMD_POSITION;
}
// Only allow DNA bases
if(!isBase(c)) {
DEBUG(std::cout << "Character " << c << " is not a DNA base." <<
std::endl;)
return EMPTY_FMD_POSITION;
}
DEBUG(std::cout << "Extending " << range << " backwards with " << (char)c <<
std::endl;)
// We have an array of FMDPositions, one per base, that we will fill in by a
// tiny dynamic programming.
FMDPosition answers[NUM_BASES];
for(usint base = 0; base < NUM_BASES; base++)
{
// Go through the bases in arbitrary order.
DEBUG(std::cout << "\tThinking about base " << base << "(" <<
BASES[base] << ")" << std::endl;)
// Count up the number of characters < this base, including sequence stop
// characters.
usint start = this->alphabet->cumulative((usint)BASES[base]) +
this->number_of_sequences - 1;
DEBUG(std::cout << "\t\tstart = " << start << std::endl;)
// Get a pointer to the bit vector for this letter, which might be NULL if
// this base never appeared.
PsiVector* vector = this->array[(usint)BASES[base]];
if(vector == NULL)
{
DEBUG(std::cout << "\t\tCharacter never appeared!" << std::endl;)
// Fill in forward_start and length with the knowledge that this
// character doesn't exist. forward_start should never get used, but
// end_offset will get used and probably needs to be -1 for empty.
answers[base].end_offset = -1;
}
else
{
DEBUG(std::cout << "\t\tCharacter appeared." << std::endl;)
// Get an iterator for the bit vector for this character, for
// calculating ranks/occurrences.
PsiVector::Iterator iter(*vector);
DEBUG(std::cout << "\t\tGot iterator" << std::endl;)
// Fill in the forward-strand start positions and range end_offsets for
// each base's answer. TODO: do we want at_least set or not? What does
// it do?
// First cache the forward_start rank we re-use
usint forward_start_rank = iter.rank(range.forward_start, true);
answers[base].forward_start = start + forward_start_rank;
answers[base].end_offset = iter.rank(range.forward_start +
range.end_offset, false) - forward_start_rank;
}
DEBUG(std::cout << "\t\tWould go to: " << answers[base].forward_start <<
"-" << (sint)answers[base].forward_start + answers[base].end_offset <<
" length " << answers[base].getLength() << std::endl;)
}
// Since we don't keep an FMDPosition for the non-base end-of-text
// character, we need to track its length separately in order for the DP
// algorithm given in the paper to be implementable. We calculate
// occurrences of the text end character (i.e. how much of the current range
// is devoted to things where an endOfText comes next) implicitly: it's
// whatever part of the length of the range is unaccounted-for by the other
// characters. We need to use the length accessor because ranges with one
// thing have the .end_offset set to 0.
usint endOfTextLength = range.getLength();
for(usint base = 0; base < NUM_BASES; base++)
{
// Go through the bases in order and account for their lengths.
endOfTextLength -= answers[base].getLength();
}
DEBUG(std::cout << "\tendOfTextLength = " << endOfTextLength << std::endl;)
// The endOfText character is the very first character we need to account
// for when subdividing the reverse range and picking which subdivision to
// take.
DEBUG(std::cout << "\tendOfText reverse_start would be " <<
range.reverse_start << std::endl;)
// Next, allocate the range for the base that comes first in alphabetical
// order by reverse complement.
answers[0].reverse_start = range.reverse_start + endOfTextLength;
DEBUG(std::cout << "\t" << BASES[0] << " reverse_start is " <<
answers[0].reverse_start << std::endl;)
for(usint base = 1; base < NUM_BASES; base++)
{
// For each subsequent base in alphabetical order by reverse complement
// (as stored in BASES), allocate it the next part of the reverse range.
answers[base].reverse_start = answers[base - 1].reverse_start +
answers[base - 1].getLength();
DEBUG(std::cout << "\t" << BASES[base] << " reverse_start is " <<
answers[base].reverse_start << std::endl;)
}
// Now all the per-base answers are filled in.
for(usint base = 0; base < NUM_BASES; base++)
{
// For each base in arbitrary order
if(BASES[base] == (char)c)
{
DEBUG(std::cout << "Moving " << range << " to " << answers[base] <<
" on " << BASES[base] << std::endl;)
// This is the base we're actually supposed to be extending with. Return
// its answer.
return answers[base];
}
}
// If we get here, they gave us something not in BASES somehow.
throw "Unrecognized base";
}
else
{
// Flip the interval, do backwards search with the reverse complement of the
// base, and then flip back.
return this->extend(range.flip(), reverse_complement(c), true).flip();
}
}
FMDPosition
FMD::retract(FMDPosition range, usint c, bool backward) const
{
// TODO: Does not work at all.
if(backward)
{
DEBUG(std::cout << "Going back from " << range << " on " << (char)c <<
std::endl;)
// Keep the original FMDPosition to build up. We call it "original" because
// we logically think about undoing an extension, but in reality it may
// never have existed.
FMDPosition original;
// Get a pointer to the bit vector for this letter, which might be NULL if
// this base never appeared.
PsiVector* vector = this->array[c];
if(vector == NULL) { throw "Character never appeared!"; }
// Get an iterator for the bit vector for this character, for selecting by
// rank.
PsiVector::Iterator iter(*vector);
// Count up the number of characters < this base, including sequence stop
// characters. The same as the "start" variable in extend.
usint start = this->alphabet->cumulative(c) + this->number_of_sequences - 1;
DEBUG(std::cout << "\tOriginal start was " << start << std::endl;)
// Back-derive the original forward_start, which can be done with "start"
// and the inverse of rank(i, true).
original.forward_start = iter.select(range.forward_start - start - 1);
DEBUG(std::cout << "\tOriginal forward range contains " <<
original.forward_start << std::endl;)
// Work out something from reverse range
return original;
}
else
{
// Flip the interval, do backwards retract with the reverse complement of
// the base, and then flip back.
return this->retract(range.flip(), reverse_complement(c), true).flip();
}
}
FMDPosition
FMD::fmdCount(const std::string& pattern, bool backward) const
{
DEBUG(std::cout << "Counting " << pattern << std::endl;)
if(pattern.length() == 0) { return this->getSAPosition(); }
// Keep an FMDPosition to store our intermediate result in.
FMDPosition index_position;
if(backward)
{
// Start at the end of the pattern and work towards the front
std::string::const_reverse_iterator iter = pattern.rbegin();
index_position = this->getCharPosition((uchar)*iter);
if(index_position.isEmpty()) { return index_position; }
DEBUG(std::cout << "Starting with " << index_position << std::endl;)
for(++iter; iter != pattern.rend(); ++iter)
{
// Backwards extend with subsequent characters.
index_position = this->extend(index_position, *iter, true);
DEBUG(std::cout << "Now at " << index_position << " after " << *iter <<
std::endl;)
// Test out retracting
//DEBUG(this->retract(index_position, *iter, true);)
if(index_position.isEmpty()) { return EMPTY_FMD_POSITION; }
}
}
else
{
// Start at the front of the pattern and work towards the end.
std::string::const_iterator iter = pattern.begin();
index_position = this->getCharPosition((uchar)*iter);
if(index_position.isEmpty()) { return index_position; }
DEBUG(std::cout << "Starting with " << index_position << std::endl;)
for(++iter; iter != pattern.end(); ++iter)
{
// Forwards extend with subsequent characters.
index_position = this->extend(index_position, *iter, false);
DEBUG(std::cout << "Now at " << index_position << " after " << *iter <<
std::endl;)
// Test out retracting
//DEBUG(this->retract(index_position, *iter, false);)
if(index_position.isEmpty()) { return EMPTY_FMD_POSITION; }
}
}
this->convertToSAPosition(index_position);
return index_position;
}
std::pair<pair_type, usint>
FMD::countUntilUnique(const std::string& pattern, usint index) const
{
// Mostly copied from the RLCSA count method.
if(pattern.length() == 0 || index == 0) {
return std::make_pair(this->getSARange(), 0);
}
// Start at the index we want to map.
sint i = index;
// And with the range from just that character.
pair_type index_range = this->getCharRange((uchar)pattern[i]);
// Make sure we aren't empty already.
if(isEmpty(index_range)) {
return std::make_pair(index_range, index - i + 1);
}
if(index_range.first == index_range.second) {
// We found a unique place. Return it.
this->convertToSARange(index_range);
return std::make_pair(index_range, index - i + 1);
}
for(--i; i >= 0; --i)
{
// For each base going left...
// Apply the LF mapping to shrink the range.
index_range = this->LF(index_range, (uchar)pattern[i]);
// Stop if we're empty.
if(isEmpty(index_range)) {
return std::make_pair(EMPTY_PAIR, index - i + 1);
}
if(index_range.first == index_range.second) {
// We found a unique place. Return it.
this->convertToSARange(index_range);
return std::make_pair(index_range, index - i + 1);
}
}
// If we get here, we hit the start and still aren't unique. Report our too-
// big range.
this->convertToSARange(index_range);
return std::make_pair(index_range, index - i);
}
MapAttemptResult
FMD::mapPosition(const std::string& pattern, usint index) const
{
DEBUG(std::cout << "Mapping " << index << " in " << pattern << std::endl;)
// Initialize the struct we will use to return our somewhat complex result.
// Contains the FMDPosition (which we work in), an is_mapped flag, and a
// variable counting the number of extensions made to the FMDPosition.
MapAttemptResult result;
// Do a backward search.
// Start at the given index, and get the starting range for that character.
result.is_mapped = false;
result.position = this->getCharPosition(pattern[index]);
result.characters = 1;
if(result.position.isEmpty())
{
// This character isn't even in it. Just return the result with an empty
// FMDPosition; the next character we want to map is going to have to deal
// with having some never-before-seen character right upstream of it.
return result;
}
else if(result.position.getLength() == 1)
{
// We've already mapped.
result.is_mapped = true;
return result;
}
if(index == 0) {
// The rest of the function deals with characters to the left of the one we
// start at. If we start at position 0 there can be none.
return result;
}
DEBUG(std::cout << "Starting with " << result.position << std::endl;)
do
{
// Now consider the next character to the left.
index--;
// Grab the character to extend with.
usint character = pattern[index];
DEBUG(std::cout << "Index " << index << " in " << pattern << " is " <<
(char) character << "(" << character << ")" << std::endl;)
// Backwards extend with subsequent characters.
FMDPosition next_position = this->extend(result.position, character,
true);
extends++;
DEBUG(std::cout << "Now at " << next_position << " after " <<
pattern[index] << std::endl;)
if(next_position.isEmpty())
{
// The next place we would go is empty, so return the result holding the
// last position.
return result;
}
else if(next_position.getLength() == 1)
{
// We have successfully mapped to exactly one place. Update our result to
// reflect the additional extension and our success, and return it.
result.position = next_position;
result.characters++;
result.is_mapped = true;
return result;
}
// Otherwise, we still map to a plurality of places. Record the extension
// and loop again.
result.position = next_position;
result.characters++;
}
while(index > 0);
// Continue considering characters to the left until we hit the start of the
// string.
// If we get here, we ran out of upstream context and still map to multiple
// places. Just give our multi-mapping FMDPosition and unmapped result.
return result;
}
MapAttemptResult
FMD::mapPosition(const RangeVector& ranges, const std::string& pattern,
usint index) const
{
// We're going to right-map so ranges match up with the things we can map to
// (downstream contexts)
// Initialize the struct we will use to return our somewhat complex result.
// Contains the FMDPosition (which we work in), an is_mapped flag, and a
// variable counting the number of extensions made to the FMDPosition.
MapAttemptResult result;
// Do a forward search.
// Start at the given index, and get the starting range for that character.
result.is_mapped = false;
result.position = this->getCharPosition(pattern[index]);
result.characters = 1;
if(result.position.isEmpty())
{
// This character isn't even in it. Just return the result with an empty
// FMDPosition; the next character we want to map is going to have to deal
// with having some never-before-seen character right upstream of it.
return result;
}
else if (result.position.range(ranges) != -1)
{
// We've already mapped.
result.is_mapped = true;
return result;
}
DEBUG(std::cout << "Starting with " << result.position << std::endl;)
for(index++; index < pattern.size(); index++)
{
// Forwards extend with subsequent characters.
FMDPosition next_position = this->extend(result.position, pattern[index],
false);
extends++;
DEBUG(std::cout << "Now at " << next_position << " after " <<
pattern[index] << std::endl;)
if(next_position.isEmpty())
{
// The next place we would go is empty, so return the result holding the
// last position.
return result;
}
if(next_position.range(ranges) != -1)
{
// We have successfully mapped to exactly one range. Update our result to
// reflect the additional extension and our success, and return it.
result.position = next_position;
result.characters++;
result.is_mapped = true;
return result;
}
// Otherwise, we still map to a plurality of ranges. Record the extension
// and loop again.
result.position = next_position;
result.characters++;
}
// If we get here, we ran out of downstream context and still map to multiple
// ranges. Just give our multi-mapping FMDPosition and unmapped result.
return result;
}
std::vector<Mapping>
FMD::map(const std::string& query, usint start, sint length) const
{
if(length == -1) {
// Fix up the length parameter if it is -1: that means the whole rest of the
// string.
length = query.length() - start;
}
// We need a vector to return.
std::vector<Mapping> mappings;
// Keep around the result that we get from the single-character mapping
// function. We use it as our working state to track our FMDPosition and how
// many characters we've extended by. We use the is_mapped flag to indicate
// whether the current iteration is an extension or a restart.
MapAttemptResult location;
// Make sure the scratch position is empty so we re-start on the first base.
// Other fields get overwritten.
location.position = EMPTY_FMD_POSITION;
for(sint i = start; i < (sint)(start + length); i++)
{
if(location.position.isEmpty())
{
INFO(std::cout << "Starting over by mapping position " << i <<
std::endl;)
// We do not currently have a non-empty FMDPosition to extend. Start over
// by mapping this character by itself.
location = this->mapPosition(query, i);
restarts++;
}
else
{
INFO(std::cout << "Extending with position " << i << std::endl;)
// The last base either mapped successfully or failed due to multi-
// mapping. Try to extend the FMDPosition we have to the right (not
// backwards) with the next base.
location.position = this->extend(location.position, query[i], false);
extends++;
location.characters++;
}
if(location.is_mapped && location.position.getLength() == 1)
{
// It mapped. We didn't do a re-start and fail, and there's exactly one
// thing in our interval.
// Take the first (only) thing in the bi-interval's forward strand side,
// and convert to SA coordinates.
usint converted_start = location.position.forward_start;
convertToSAIndex(converted_start);
// Locate it, and then report position as a (text, offset) pair. This will
// give us the position of the first base in the pattern, which lets us
// infer the position of the last base in the pattern.
pair_type text_location = getRelativePosition(locate(converted_start));
INFO(std::cout << "Mapped " << location.characters <<
" context to text " << text_location.first << " position " <<
text_location.second << std::endl;)
// Correct to the position of the last base in the pattern, by offsetting
// by the length of the pattern that was used. A 2-character pattern means
// we need to go 1 further right in the string it maps to to find where
// its rightmost character maps.
text_location.second += (location.characters - 1);
// Add a Mapping for this mapped base.
mappings.push_back(Mapping(text_location));