LocARNA-2.0.0
rna_structure.hh
1 #ifndef LOCARNA_RNA_STRUCTURE_HH
2 #define LOCARNA_RNA_STRUCTURE_HH
3 
4 #ifdef HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7 
8 #include <assert.h>
9 #include <set>
10 #include <string>
11 #include <ostream>
12 
13 namespace LocARNA {
14 
15  namespace BasePairFilter {
16  class Filter;
17  }
18 
32  class RnaStructure {
33  public:
35  typedef std::pair<size_t, size_t> bp_t;
36 
38  typedef std::set<bp_t, std::less<bp_t> > bps_t;
39 
43  RnaStructure(size_t length = 0) : length_(length), bps_() {}
44 
63  explicit RnaStructure(const std::string &structure);
64 
71  bool
72  operator==(const RnaStructure &s) const {
73  return this->length_ == s.length_ && this->bps_ == s.bps_;
74  }
75 
83  bool
84  contains(const bp_t &x) const {
85  return bps_.find(x) != bps_.end();
86  }
87 
90  size_t
91  length() const {
92  return length_;
93  }
94 
97  size_t
98  size() const {
99  return bps_.size();
100  }
101 
105  void
106  insert(const bp_t &bp) {
107  assert_valid_bp(bp);
108  bps_.insert(bp);
109  }
110 
114  void
115  remove(const bp_t &bp) {
116  assert_valid_bp(bp);
117  bps_.erase(bp);
118  }
119 
123  void
124  clear() {
125  bps_.clear();
126  }
127 
128  // This is not well supported by the current structure
129  // represenation. Therefore, we don't offer such functionality.
130  // /**
131  // * @brief Check whether a loop contains a position
132  // *
133  // * @param k position
134  // * @param x loop; (0,length+1) means external loop
135  // *
136  // * @return whether the loop enclosed by x contains k
137  // *
138  // * Definition: the loop enclosed by (i,j) contains k iff i<k<j
139  // * and there is no base pair (i',j') in the structure, where
140  // * i<i'<k<j'<j. Note that we don't require (i,j) to be element
141  // * of the structure.
142  // *
143  // * k can be member of more than one loop unless nested().
144  // */
145  // bp_t
146  // in_loop_of(size_t k, bp_t x) const;
147 
148  // support contant iteration over base pair set
149 
151  typedef bps_t::const_iterator const_iterator;
152 
159  begin() const {
160  return bps_.begin();
161  }
162 
169  end() const {
170  return bps_.end();
171  }
172 
187  std::string
188  to_string() const;
189 
197  static bool
198  empty(const bps_t &bps);
199 
205  bool
206  empty() const {
207  return empty(bps_);
208  }
209 
223  static bool
224  nested(const bps_t &bps);
225 
231  bool
232  nested() const {
233  return nested(bps_);
234  }
235 
250  static bool
251  crossing(const bps_t &bps);
252 
258  bool
259  crossing() const {
260  return crossing(bps_);
261  }
262 
263  // base pair filter operations
264 
269  void
271 
277  void
278  apply_bpfilter(const BasePairFilter::Filter &filter);
279 
280  private:
281  size_t length_;
282  bps_t bps_;
283 
284  static const char unpaired_symbol_ = '.';
285  static const std::string open_symbols_;
286  static const std::string close_symbols_;
287 
288  bool
289  parse(const std::string &s, bps_t &bps, char op, char cl);
290 
291  bool
292  parse(const std::string &s,
293  bps_t &bps,
294  const std::string &op_syms,
295  const std::string &cl_syms);
296 
297  void
298  assert_valid_bp(const bp_t &bp) {
299  assert(1 <= bp.first);
300  assert(bp.first < bp.second);
301  assert(bp.second <= length_);
302  }
303 
304  std::string
305  to_string(const bps_t &bps) const;
306 
307  }; // end class RnaStructure
308 
309  std::ostream &
310  operator<<(std::ostream &out, const RnaStructure &structure);
311 
312 } // end namespace LocARNA
313 
314 #endif
basic class for base pair filters (no filtering)
Definition: base_pair_filter.hh:17
An RNA secondary structure.
Definition: rna_structure.hh:32
std::string to_string() const
convert to dot-bracket string
Definition: rna_structure.cc:69
size_t length() const
sequence length
Definition: rna_structure.hh:91
bool crossing() const
Check for crossing structure / class CROSSING.
Definition: rna_structure.hh:259
bool operator==(const RnaStructure &s) const
Equality operator.
Definition: rna_structure.hh:72
void insert(const bp_t &bp)
insert base pair
Definition: rna_structure.hh:106
std::set< bp_t, std::less< bp_t > > bps_t
base pair set type
Definition: rna_structure.hh:38
bool nested() const
Check for nested structure / class NESTED.
Definition: rna_structure.hh:232
RnaStructure(size_t length=0)
construct empty
Definition: rna_structure.hh:43
bool contains(const bp_t &x) const
Base pair for membership test.
Definition: rna_structure.hh:84
size_t size() const
number of base pairs
Definition: rna_structure.hh:98
bps_t::const_iterator const_iterator
constant iterator over base pairs
Definition: rna_structure.hh:151
const_iterator end() const
end of base pair set
Definition: rna_structure.hh:169
std::pair< size_t, size_t > bp_t
base pair type
Definition: rna_structure.hh:35
const_iterator begin() const
begin of base pair set
Definition: rna_structure.hh:159
bool empty() const
Check for empty structure / class PLAIN.
Definition: rna_structure.hh:206
void clear()
clear structure set structure to empty
Definition: rna_structure.hh:124
void apply_bpfilter(const BasePairFilter::Filter &filter)
apply base pair filter
Definition: rna_structure.cc:175
void remove_lonely_pairs()
remove lonely base pairs
Definition: rna_structure.cc:162
void remove(const bp_t &bp)
remove base pair
Definition: rna_structure.hh:115
Definition: aligner.cc:15
std::ostream & operator<<(std::ostream &out, const AlignerRestriction &r)
Definition: aligner_restriction.hh:135