Debian Codegraph @

atac-driver/libatac/fasta-accessor.H

raw symbols SCIP
1 #ifndef FASTA_ACCESSOR_H
2 #define FASTA_ACCESSOR_H
3
4 #include "bio++.H"
5
6
7 // Define this to do bounds checking
8 //
9 #if 1
10 #define SANITY(NAM, POS) \
11 if ((POS) > _len) { \
12 fprintf(stderr, "%s-- position "uint32FMT" larger than length "uint32FMT"\n", \
13 NAM, (POS), _len); \
14 assert((POS) <= _len); \
15 }
16 #else
17 #define SANITY(NAM, POS)
18 #endif
19
20
21 // Externally, we show the coordinate in the forward strand.
22 // Internally, we represent the coordinate on the physical string.
23 // The user is required to maintain the range that the
24 // reverse-complement string is valid over. It is not possible to
25 // randomly access sequence outside the range, but it is possible to
26 // iterate over it (but then you cannot get the coordinate of where
27 // you are at!)
28
29
30 class FastAAccessor {
31 private:
32 public:
33 char *_seq;
34 uint32 _pos;
35 uint32 _len;
36
37 uint32 _rcBase;
38 uint32 _rcLen;
39
40 bool _doRevComp;
41
42 private:
43 void FastAAccessorInit(char *S, uint32 length, bool revcomp) {
44 _seq = S;
45 _pos = 0;
46 _len = length;
47 if (length == 0)
48 _len = (uint32)strlen(S);
49
50 _rcBase = 0;
51 _rcLen = _len;
52
53 _doRevComp = revcomp;
54
55 if (_doRevComp)
56 _pos = _len-1;
57 };
58
59 public:
60 FastAAccessor(seqInCore *S, bool revcomp=false) {
61 FastAAccessorInit(S->sequence(), S->sequenceLength(), revcomp);
62 };
63
64 FastAAccessor(char *S, uint32 length=0, bool revcomp=false) {
65 FastAAccessorInit(S, length, revcomp);
66 };
67
68 private:
69
70 // Given a range in the forward string, we can reverse-complement
71 // just that range. This amounts to translating the forward string
72 // to make the beginning of the range be the origin, then
73 // reverseing the range, then translating the sequence back to the
74 // original origin.
75 //
76 uint32 rc(uint32 p) const {
77 return(_rcBase + _rcLen - (p - _rcBase) - 1);
78 };
79
80 public:
81
82 // For iterating over reverse complement regions of a forward
83 // sequence.
84 //
85 // e.g., (500, 250) would be:
86 //
87 // |-----|-----|XXXXX|-----|
88 // 0 250 500 750 1000
89 //
90 // Set both to zero (also the default) to unset the range
91 //
92 // The physical location (_pos) doesn't change, but this will
93 // change the value of the corresponding forward coordinate, but
94 // not the meaning.
95 //
96 bool setRange(uint32 base=0, uint32 length=0) {
97 if ((base < _len) && (base + length <= _len)) {
98 if ((base == 0) && (length == 0)) {
99 _rcBase = 0;
100 _rcLen = _len;
101 } else {
102 _rcBase = base;
103 _rcLen = length;
104 }
105 return(true);
106 } else {
107 fprintf(stderr, "FastAAccessor::setRange()-- base="uint32FMT" and length="uint32FMT" exceed sequence length of "uint32FMT"\n",
108 base, length, _len);
109 assert(0);
110 return(false);
111 }
112 };
113
114
115 // True if this physical location is valid.
116 //
117 bool isValid(void) {
118 return(_pos < _len);
119 };
120
121
122 char operator[](uint32 p) const {
123 if ((_rcBase <= p) && (p < _rcBase + _rcLen)) {
124 if (_doRevComp) return(complementSymbol[_seq[rc(p)]]);
125 else return(_seq[p]);
126 } else {
127 fprintf(stderr, "operator[]-- Tried to access to "uint32FMT", but range is "uint32FMT"-"uint32FMT"\n",
128 p, _rcBase, _rcBase + _rcLen);
129 assert(0);
130 return(0);
131 }
132 };
133
134
135
136 // Set the accessor to some position.
137 //
138 bool setPosition(uint32 p) {
139 if ((_rcBase <= p) && (p < _rcBase + _rcLen)) {
140 if (_doRevComp) _pos = rc(p);
141 else _pos = p;
142 return(true);
143 } else {
144 fprintf(stderr, "setPosition()-- Tried to set to "uint32FMT", but range is "uint32FMT"-"uint32FMT".\n",
145 p, _rcBase, _rcBase + _rcLen);
146 assert(0);
147 return(false);
148 }
149 };
150
151 uint32 getPosition(void) {
152 if (_doRevComp) return(rc(_pos));
153 else return(_pos);
154 };
155
156 uint32 getRangeBegin(void) { return(_rcBase); };
157 uint32 getRangeEnd(void) { return(_rcBase + _rcLen); };
158 uint32 getRangeLength(void) { return(_rcLen); };
159
160 bool extendLeft(int32 x) {
161 _rcLen += x;
162 if (_doRevComp == false)
163 _rcBase -= x;
164
165 if ((_rcBase > _len) || (_rcBase + _rcLen > _len)) {
166 fprintf(stderr, "FastAAccessor::extendLeft()-- extend by "int32FMT" makes invalid: length is "uint32FMT", new range is "uint32FMT"-"uint32FMT"\n",
167 x, _len, _rcBase, _rcBase + _rcLen);
168 assert(0);
169 return(false);
170 }
171 return(true);
172 };
173
174 bool extendRight(int32 x) {
175 _rcLen += x;
176 if (_doRevComp == true)
177 _rcBase -= x;
178
179 if ((_rcBase > _len) || (_rcBase + _rcLen > _len)) {
180 fprintf(stderr, "FastAAccessor::extendRight()-- extend by "int32FMT" makes invalid: length is "uint32FMT", new range is "uint32FMT"-"uint32FMT"\n",
181 x, _len, _rcBase, _rcBase + _rcLen);
182 assert(0);
183 return(false);
184 }
185 return(true);
186 };
187
188 char operator*(void) const {
189 SANITY("FastAAccessor::operator*()", _pos);
190 if (_doRevComp) return(complementSymbol[_seq[_pos]]);
191 else return(_seq[_pos]);
192 };
193
194 char get(void) const {
195 SANITY("FastAAccessor::get()", _pos);
196 if (_doRevComp) return(complementSymbol[_seq[_pos]]);
197 else return(_seq[_pos]);
198 };
199
200 FastAAccessor &operator--(void) {
201 if (_doRevComp) _pos++;
202 else _pos--;
203 return(*this);
204 };
205
206 FastAAccessor &operator++(void) {
207 if (_doRevComp) _pos--;
208 else _pos++;
209 return(*this);
210 };
211 };
212
213 #endif // FASTA_ACCESSOR_H