IntaRNA 3.4.1
RNA-RNA interaction prediction | C++ API
Loading...
Searching...
No Matches
IndexRange.h
Go to the documentation of this file.
1
2#ifndef INTARNA_INDEXRANGE_H_
3#define INTARNA_INDEXRANGE_H_
4
5
6#include "IntaRNA/general.h"
8
9#include <stdexcept>
10
11#include <boost/regex.hpp>
12
13namespace IntaRNA {
14
21
22public:
23
26 static const size_t LAST_INDEX;
27
29 static const size_t NA_INDEX;
30
32 size_t from;
33
35 size_t to;
36
38 static constexpr const char* regexString = R"((0|-?[123456789]\d*)-(0|-?[123456789]\d*))";
39
41 static const boost::regex regex;
42
43public:
44
50 IndexRange(const size_t from = 0,
51 const size_t to = NA_INDEX)
52 : from(from), to(to)
53 {
54 }
55
65 IndexRange(const std::string & stringEncoding, const RnaSequence * seq )
66 : from(0), to(NA_INDEX)
67 {
68 fromString(stringEncoding,seq);
69 }
70
74 virtual ~IndexRange() {}
75
80 bool isAscending() const
81 {
82 return from <= to;
83 }
84
89 bool isDescending() const
90 {
91 return from >= to;
92 }
93
102 IndexRange operator + ( const int shift ) const {
103 if (shift == 0) {
104 return *this;
105 }
106 if (shift > 0) {
107 return IndexRange(from+shift, to+shift);
108 }
109 if (to < std::abs(shift)) {
110 return IndexRange( NA_INDEX, NA_INDEX);
111 }
112 return IndexRange( from - std::min(from, (size_t)std::abs(shift)), to + shift );
113 }
114
123 IndexRange operator - ( const int shift ) const {
124 // forward implementation using inverted shift
125 return this->operator+(-shift);
126 }
127
133 const bool operator < ( const IndexRange &r ) const {
134 return ( from < r.from || (from==r.from && to<r.to) );
135 }
136
142 const bool operator == ( const IndexRange &r ) const {
143 return ( from == r.from && to == r.to );
144 }
145
151 const bool operator != ( const IndexRange &r ) const {
152 return !( this->operator ==(r) );
153 }
154
155
162 friend std::ostream& operator<<(std::ostream& out, const IndexRange& range)
163 {
164 // has to be in accordance to this.regex
165 return (out <<range.from<<"-"<<range.to);
166 }
167
173 void
174 fromString( const std::string & stringEncoding, const RnaSequence * seq )
175 {
176 if( ! boost::regex_match(stringEncoding, regex, boost::match_perl) ) {
177 throw std::runtime_error("IndexRange::fromString("+stringEncoding+") uses no valid index range string encoding");
178 }
179 // find split position assuming second index to be negative
180 size_t splitPos = stringEncoding.find("--");
181 if (splitPos == std::string::npos) {
182 // last index should not be negative, thus, split at last '-'
183 splitPos = stringEncoding.find_last_of('-');
184 }
185 // parse interval boundaries
186 if (seq == NULL) {
187 from = boost::lexical_cast<size_t>(stringEncoding.substr(0,splitPos));
188 to = boost::lexical_cast<size_t>(stringEncoding.substr(splitPos+1));
189 } else {
190 // correct for shifted indexing within the input sequence
191 from = seq->getIndex(boost::lexical_cast<long>(stringEncoding.substr(0,splitPos)));
192 to = seq->getIndex(boost::lexical_cast<long>(stringEncoding.substr(splitPos+1)));
193 }
194 }
195
202 std::vector<IndexRange>
203 overlappingWindows(const size_t windowWidth, const size_t windowOverlap) const;
204
205
206};
207
208} // namespace
209
210#endif /* INDEXRANGE_H_ */
Definition IndexRange.h:20
bool isAscending() const
Definition IndexRange.h:80
const bool operator!=(const IndexRange &r) const
Definition IndexRange.h:151
IndexRange operator+(const int shift) const
Definition IndexRange.h:102
bool isDescending() const
Definition IndexRange.h:89
std::vector< IndexRange > overlappingWindows(const size_t windowWidth, const size_t windowOverlap) const
IndexRange operator-(const int shift) const
Definition IndexRange.h:123
IndexRange(const std::string &stringEncoding, const RnaSequence *seq)
Definition IndexRange.h:65
size_t from
the start of the index range
Definition IndexRange.h:32
void fromString(const std::string &stringEncoding, const RnaSequence *seq)
Definition IndexRange.h:174
size_t to
the end of the index range
Definition IndexRange.h:35
const bool operator==(const IndexRange &r) const
Definition IndexRange.h:142
IndexRange(const size_t from=0, const size_t to=NA_INDEX)
Definition IndexRange.h:50
virtual ~IndexRange()
Definition IndexRange.h:74
friend std::ostream & operator<<(std::ostream &out, const IndexRange &range)
Definition IndexRange.h:162
const bool operator<(const IndexRange &r) const
Definition IndexRange.h:133
static constexpr const char * regexString
regular expression that matches valid IndexRange string encodings
Definition IndexRange.h:38
static const size_t LAST_INDEX
Definition IndexRange.h:26
static const boost::regex regex
regular expression that matches valid IndexRange string encodings
Definition IndexRange.h:41
static const size_t NA_INDEX
placeholder for not-defined values
Definition IndexRange.h:29
Definition RnaSequence.h:29
size_t getIndex(const long i) const
Definition RnaSequence.h:398
Definition Accessibility.h:13