LocARNA-1.9.2
src/LocARNA/basepairs.hh
00001 #ifndef LOCARNA_BASEPAIRS_HH
00002 #define LOCARNA_BASEPAIRS_HH
00003 
00004 #ifdef HAVE_CONFIG_H
00005 #include <config.h>
00006 #endif
00007 
00008 #include <iosfwd>
00009 
00010 #include <vector>
00011 #include <set>
00012 #include <assert.h>
00013 
00014 #include "params.hh"
00015 #include "sparse_matrix.hh"
00016 
00017 namespace LocARNA {
00018 
00019     class RnaData;
00020     class Sequence;
00021 
00040     class BasePairs__Arc {
00041     private:
00042         size_t idx_;
00043         size_t left_;
00044         size_t right_;
00045 
00046     public:
00054         BasePairs__Arc(size_t idx, size_t left, size_t right)
00055             : idx_(idx), left_(left), right_(right) {}
00056 
00057         /*
00058          * @brief Virtual destructor
00059          */
00060         virtual ~BasePairs__Arc();
00061 
00067         size_t
00068         left() const {
00069             return left_;
00070         }
00071 
00077         size_t
00078         right() const {
00079             return right_;
00080         }
00081 
00087         size_t
00088         idx() const {
00089             return idx_;
00090         }
00091     };
00092 
00093     // ============================================================
00109     class BasePairs {
00110     private:
00111         const RnaData *rna_data_;
00112         double min_prob_;
00113         double len_;
00114 
00115     public:
00116         typedef size_t size_type; 
00117 
00118         typedef BasePairs__Arc Arc; 
00119 
00131         class LeftAdjEntry : public Arc {
00132         public:
00138             explicit LeftAdjEntry(const Arc &a) : Arc(a) {}
00139         };
00140 
00145         class RightAdjEntry : public Arc {
00146         public:
00152             explicit RightAdjEntry(const Arc &a) : Arc(a) {}
00153         };
00154 
00156         typedef std::vector<Arc> arc_vec_t;
00157 
00158         /* types for data structures for the access of an arc in the structure,
00159            by its right end, its left end, or left and right end
00160 
00161            the access structure is implemented as a hash map,
00162         */
00163 
00165         typedef std::vector<LeftAdjEntry> LeftAdjList;
00166 
00168         typedef std::vector<RightAdjEntry> RightAdjList;
00169 
00171         typedef SparseMatrix<int> arc_matrix_t;
00172 
00174         typedef std::pair<size_type, size_type> bpair_t;
00175 
00177         typedef std::set<bpair_t> bpair_set_t;
00178 
00179     private:
00180         std::vector<LeftAdjList> left_;
00181         std::vector<RightAdjList> right_;
00182 
00183         arc_vec_t arc_vec_;
00184         arc_matrix_t arcs_;
00185 
00194         void
00195         resize(size_type seq_len);
00196 
00198         void
00199         generateBPLists(const RnaData &rna_data);
00200 
00202         void
00203         sortAdjLists();
00204 
00205     public:
00216         BasePairs(const RnaData *rna_data, double min_prob)
00217             : rna_data_(rna_data),
00218               min_prob_(min_prob),
00219               len_(get_length_from_rna_data()),
00220               left_(),
00221               right_(),
00222               arc_vec_(),
00223               arcs_(-1) {
00224             generateBPLists(*rna_data_);
00225         }
00226 
00233         BasePairs(size_type len, const bpair_set_t &bps)
00234             : rna_data_(0),
00235               min_prob_(1.0),
00236               len_(len),
00237               left_(),
00238               right_(),
00239               arc_vec_(),
00240               arcs_(-1) {
00241             resize(seqlen());
00242             for (bpair_set_t::const_iterator it = bps.begin(); bps.end() != it;
00243                  ++it) {
00244                 register_arc(it->first, it->second);
00245             }
00246             sortAdjLists();
00247         }
00248 
00249         // /**
00250         //  * @brief Copy constructor
00251         //  */
00252         // BasePairs(const BasePairs &bps);
00253 
00254         // /**
00255         //  * @brief Assignment operator
00256         //  */
00257         // BasePairs &
00258         // operator =(const BasePairs &bps);
00259 
00264         void
00265         register_arc(int i, int j);
00266 
00268         const LeftAdjList &
00269         left_adjlist(int i) const {
00270             // std::cout<<"size of left adjlist of "<<i<<"is
00271             // "<<left_[i].size()<<std::endl;
00272             return left_[i];
00273         }
00274 
00276         const RightAdjList &
00277         right_adjlist(int i) const {
00278             return right_[i];
00279         }
00280 
00282         const Arc &
00283         arc(int i, int j) const {
00284             return arc_vec_[arcs_(i, j)];
00285         }
00286 
00291         const Arc &
00292         arc(size_type idx) const {
00293             assert(idx < arc_vec_.size());
00294             return arc_vec_[idx];
00295         }
00296 
00298         bool
00299         exists_arc(int i, int j) const {
00300             return -1 != arcs_(i, j);
00301         }
00302 
00304         size_type
00305         num_bps() const {
00306             return arc_vec_.size();
00307         }
00308 
00310         size_type
00311         seqlen() const;
00312 
00313         double
00314         prob_min() const; 
00315 
00316         // /**
00317         //  * @brief Access to corresponding RnaData object
00318         //  *
00319         //  * @return reference to RnaData object
00320         //  */
00321         // const RnaData &
00322         // get_rna_data() const {
00323         //     assert(rna_data_!=NULL);
00324         //     return *rna_data_;
00325         // }
00326 
00327     private:
00328         // return length from rna data
00329         // pre: rna data available
00330         size_type
00331         get_length_from_rna_data() const;
00332     };
00333 
00334     std::ostream &
00335     operator<<(std::ostream &out, const BasePairs::Arc &arc);
00336 }
00337 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends