LocARNA-1.9.2
src/LocARNA/rna_structure.hh
00001 #ifndef LOCARNA_RNA_STRUCTURE_HH
00002 #define LOCARNA_RNA_STRUCTURE_HH
00003 
00004 #ifdef HAVE_CONFIG_H
00005 #include <config.h>
00006 #endif
00007 
00008 #include <assert.h>
00009 #include <set>
00010 #include <string>
00011 #include <ostream>
00012 
00013 namespace LocARNA {
00014 
00015     namespace BasePairFilter {
00016         class Filter;
00017     }
00018 
00032     class RnaStructure {
00033     public:
00035         typedef std::pair<size_t, size_t> bp_t;
00036 
00038         typedef std::set<bp_t, std::less<bp_t> > bps_t;
00039 
00040     private:
00041         size_t length_;
00042         bps_t bps_;
00043 
00044         static const char unpaired_symbol_ = '.';
00045         static const std::string open_symbols_;
00046         static const std::string close_symbols_;
00047 
00048         bool
00049         parse(const std::string &s, bps_t &bps, char op, char cl);
00050 
00051         bool
00052         parse(const std::string &s,
00053               bps_t &bps,
00054               const std::string &op_syms,
00055               const std::string &cl_syms);
00056 
00057         void
00058         assert_valid_bp(const bp_t &bp) {
00059             assert(1 <= bp.first);
00060             assert(bp.first < bp.second);
00061             assert(bp.second <= length_);
00062         }
00063 
00064     public:
00068         RnaStructure() : length_(0), bps_() {}
00069 
00088         explicit RnaStructure(const std::string &structure);
00089 
00096         bool
00097         operator==(const RnaStructure &s) const {
00098             return this->length_ == s.length_ && this->bps_ == s.bps_;
00099         }
00100 
00108         bool
00109         contains(const bp_t &x) const {
00110             return bps_.find(x) != bps_.end();
00111         }
00112 
00115         size_t
00116         length() const {
00117             return length_;
00118         }
00119 
00122         size_t
00123         size() const {
00124             return bps_.size();
00125         }
00126 
00130         void
00131         insert(const bp_t &bp) {
00132             assert_valid_bp(bp);
00133             bps_.insert(bp);
00134         }
00135 
00139         void
00140         remove(const bp_t &bp) {
00141             assert_valid_bp(bp);
00142             bps_.erase(bp);
00143         }
00144 
00148         void
00149         clear() {
00150             bps_.clear();
00151         }
00152 
00153         // This is not well supported by the current structure
00154         // represenation. Therefore, we don't offer such functionality.
00155         // /**
00156         //  * @brief Check whether a loop contains a position
00157         //  *
00158         //  * @param k position
00159         //  * @param x loop; (0,length+1) means external loop
00160         //  *
00161         //  * @return whether the loop enclosed by x contains k
00162         //  *
00163         //  * Definition: the loop enclosed by (i,j) contains k iff i<k<j
00164         //  * and there is no base pair (i',j') in the structure, where
00165         //  * i<i'<k<j'<j. Note that we don't require (i,j) to be element
00166         //  * of the structure.
00167         //  *
00168         //  * k can be member of more than one loop unless nested().
00169         //  */
00170         // bp_t
00171         // in_loop_of(size_t k, bp_t x) const;
00172 
00173         // support contant iteration over base pair set
00174 
00176         typedef bps_t::const_iterator const_iterator;
00177 
00183         const_iterator
00184         begin() const {
00185             return bps_.begin();
00186         }
00187 
00193         const_iterator
00194         end() const {
00195             return bps_.end();
00196         }
00197 
00212         std::string
00213         to_string() const;
00214 
00215     private:
00216         std::string
00217         to_string(const bps_t &bps) const;
00218 
00219     public:
00227         static bool
00228         empty(const bps_t &bps);
00229 
00235         bool
00236         empty() const {
00237             return empty(bps_);
00238         }
00239 
00253         static bool
00254         nested(const bps_t &bps);
00255 
00261         bool
00262         nested() const {
00263             return nested(bps_);
00264         }
00265 
00280         static bool
00281         crossing(const bps_t &bps);
00282 
00288         bool
00289         crossing() const {
00290             return crossing(bps_);
00291         }
00292 
00293         // base pair filter operations
00294 
00299         void
00300         remove_lonely_pairs();
00301 
00307         void
00308         apply_bpfilter(const BasePairFilter::Filter &filter);
00309 
00310     }; // end class RnaStructure
00311 
00312     std::ostream &
00313     operator<<(std::ostream &out, const RnaStructure &structure);
00314 
00315 } // end namespace LocARNA
00316 
00317 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends