| 1 | #include "PRD_SequenceIterator.hxx" |
|---|
| 2 | #include <cstdio> |
|---|
| 3 | |
|---|
| 4 | using namespace std; |
|---|
| 5 | |
|---|
| 6 | const int SequenceIterator::FORWARD; |
|---|
| 7 | const int SequenceIterator::BACKWARD; |
|---|
| 8 | |
|---|
| 9 | // |
|---|
| 10 | // Constructor |
|---|
| 11 | // |
|---|
| 12 | SequenceIterator::SequenceIterator (const char *sequence_, PRD_Sequence_Pos start_pos_, PRD_Sequence_Pos stop_pos_, int max_length_, int direction_) { |
|---|
| 13 | sequence = sequence_; |
|---|
| 14 | max_length = max_length_; |
|---|
| 15 | stop_position = stop_pos_; |
|---|
| 16 | direction = (direction_ < 0) ? BACKWARD : FORWARD; |
|---|
| 17 | pos = start_pos_ - direction; // -direction because nextBase() starts with pos+direction |
|---|
| 18 | delivered = 0; |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | SequenceIterator::SequenceIterator (const char *sequence_) { |
|---|
| 22 | sequence = sequence_; |
|---|
| 23 | max_length = IGNORE; |
|---|
| 24 | stop_position = IGNORE; |
|---|
| 25 | direction = FORWARD; |
|---|
| 26 | pos = 0; |
|---|
| 27 | delivered = 0; |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | // |
|---|
| 32 | // restart at new position |
|---|
| 33 | // |
|---|
| 34 | void SequenceIterator::restart (PRD_Sequence_Pos start_pos_, PRD_Sequence_Pos stop_pos_, int max_length_, int direction_) { |
|---|
| 35 | max_length = max_length_; |
|---|
| 36 | stop_position = stop_pos_; |
|---|
| 37 | direction = (direction_ < 0) ? BACKWARD : FORWARD; |
|---|
| 38 | pos = start_pos_ - direction; // -direction because nextBase() starts with pos+direction |
|---|
| 39 | delivered = 0; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | // |
|---|
| 44 | // get next base if not at the end of sequence or already delivered max_length bases |
|---|
| 45 | // |
|---|
| 46 | unsigned char SequenceIterator::nextBase () { |
|---|
| 47 | if (((max_length != IGNORE) && (delivered >= max_length)) || |
|---|
| 48 | ((stop_position != IGNORE) && (pos == stop_position))) return EOS; |
|---|
| 49 | |
|---|
| 50 | char cur_char; |
|---|
| 51 | |
|---|
| 52 | pos += direction; // <=> pos++ if direction=FORWARD else pos-- |
|---|
| 53 | if (pos < 0) return EOS; // reached front end ? |
|---|
| 54 | cur_char = sequence[pos]; |
|---|
| 55 | if (cur_char > 'Z') cur_char = cur_char - ('a' - 'A'); // convert to upper case |
|---|
| 56 | |
|---|
| 57 | while ((cur_char != 'A') && |
|---|
| 58 | (cur_char != 'T') && |
|---|
| 59 | (cur_char != 'U') && |
|---|
| 60 | (cur_char != 'C') && |
|---|
| 61 | (cur_char != 'G') && |
|---|
| 62 | (cur_char != 'N') && |
|---|
| 63 | (cur_char != 'R') && |
|---|
| 64 | (cur_char != 'Y') && |
|---|
| 65 | (cur_char != 'M') && |
|---|
| 66 | (cur_char != 'K') && |
|---|
| 67 | (cur_char != 'W') && |
|---|
| 68 | (cur_char != 'S') && |
|---|
| 69 | (cur_char != 'B') && |
|---|
| 70 | (cur_char != 'D') && |
|---|
| 71 | (cur_char != 'H') && |
|---|
| 72 | (cur_char != 'V') && |
|---|
| 73 | (cur_char != EOS)) |
|---|
| 74 | { |
|---|
| 75 | pos += direction; // <=> pos++ if direction=FORWARD else pos-- |
|---|
| 76 | if (pos < 0) return EOS; // reached front end ? |
|---|
| 77 | cur_char = sequence[pos]; |
|---|
| 78 | if (cur_char > 'Z') cur_char = cur_char - ('a' - 'A'); // convert to upper case |
|---|
| 79 | }; |
|---|
| 80 | |
|---|
| 81 | if (cur_char != EOS) delivered++; |
|---|
| 82 | |
|---|
| 83 | return cur_char; |
|---|
| 84 | } |
|---|