source: branches/stable/PRIMER_DESIGN/PRD_SequenceIterator.cxx

Last change on this file was 16766, checked in by westram, 6 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.8 KB
Line 
1#include "PRD_SequenceIterator.hxx"
2#include <cstdio>
3
4using namespace std;
5
6const int SequenceIterator::FORWARD;
7const int SequenceIterator::BACKWARD;
8
9//
10// Constructor
11//
12SequenceIterator::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
21SequenceIterator::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//
34void 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//
46unsigned 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}
Note: See TracBrowser for help on using the repository browser.