source: tags/arb-6.0/PRIMER_DESIGN/PRD_SequenceIterator.cxx

Last change on this file was 6366, checked in by westram, 14 years ago
  • fixed whitespace (scripted)
  • 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{
14    sequence      = sequence_;
15    max_length    = max_length_;
16    stop_position = stop_pos_;
17    direction     = (direction_ < 0) ? BACKWARD : FORWARD;
18    pos           = start_pos_ - direction; // -direction because nextBase() starts with pos+direction
19    delivered     = 0;
20}
21
22SequenceIterator::SequenceIterator (const char *sequence_)
23{
24    sequence      = sequence_;
25    max_length    = IGNORE;
26    stop_position = IGNORE;
27    direction     = FORWARD;
28    pos           = 0;
29    delivered     = 0;
30}
31
32
33//
34// restart at new position
35//
36void SequenceIterator::restart (PRD_Sequence_Pos start_pos_, PRD_Sequence_Pos stop_pos_, int max_length_, int direction_)
37{
38    max_length    = max_length_;
39    stop_position = stop_pos_;
40    direction     = (direction_ < 0) ? BACKWARD : FORWARD;
41    pos           = start_pos_ - direction; // -direction because nextBase() starts with pos+direction
42    delivered     = 0;
43}
44
45
46//
47// get next base if not at the end of sequence or already delivered max_length bases
48//
49unsigned char SequenceIterator::nextBase ()
50{
51    if (((max_length != IGNORE) && (delivered >= max_length)) ||
52        ((stop_position != IGNORE) && (pos == stop_position))) return EOS;
53
54    char cur_char;
55
56    pos += direction;                                                 // <=> pos++ if direction=FORWARD else pos--
57    if (pos < 0) return EOS;                                          // reached front end ?
58    cur_char = sequence[pos];
59    if (cur_char > 'Z') cur_char = cur_char - ('a' - 'A');            // convert to upper case
60
61    while ((cur_char != 'A') &&
62           (cur_char != 'T') &&
63           (cur_char != 'U') &&
64           (cur_char != 'C') &&
65           (cur_char != 'G') &&
66           (cur_char != 'N') &&
67           (cur_char != 'R') &&
68           (cur_char != 'Y') &&
69           (cur_char != 'M') &&
70           (cur_char != 'K') &&
71           (cur_char != 'W') &&
72           (cur_char != 'S') &&
73           (cur_char != 'B') &&
74           (cur_char != 'D') &&
75           (cur_char != 'H') &&
76           (cur_char != 'V') &&
77           (cur_char != EOS))
78    {
79        pos += direction;                                               // <=> pos++ if direction=FORWARD else pos--
80        if (pos < 0) return EOS;                                        // reached front end ?
81        cur_char = sequence[pos];
82        if (cur_char > 'Z') cur_char = cur_char - ('a' - 'A');          // convert to upper case
83    };
84
85    if (cur_char != EOS) delivered++;
86
87    return cur_char;
88}
Note: See TracBrowser for help on using the repository browser.