faif
SecStruct.h
1 // collections of connections between DNA pairs, represent secondary structures
2 // @author Robert Nowak
3 
4 #ifndef SECSTRUCT_H
5 #define SECSTRUCT_H
6 
7 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
8 //visual studio 8.0 - the + - operators for iterators and conversion iterator to int
9 #pragma warning(disable:4244)
10 #endif
11 
12 #include <set>
13 #include <algorithm>
14 #include <numeric>
15 #include <iterator>
16 #include <boost/bind.hpp>
17 #include <boost/noncopyable.hpp>
18 
19 #include "Chain.h"
20 #include "EnergyNucleo.h"
21 
22 namespace faif {
23  namespace dna {
24 
25  /** \brief The pair of nucleotides which are join by Watson-Crick interaction
26 
27  The pair of iterators to nucleotides which are join.
28  The nucleotides are from the same chain or from the different chains
29  */
30  struct ConnectPair : public std::pair<Chain::const_iterator, Chain::const_iterator> {
31 
32  /** c-tor */
34  : std::pair<Chain::const_iterator, Chain::const_iterator>(f,s) {}
35 
36  /** the comparison, the view order is enforced, e.g. (0,15) < (0,14) < (1,16) */
37  bool operator<(const ConnectPair& pair) const {
38  if(first != pair.first)
39  return first < pair.first;
40  return pair.second < second;
41  }
42  };
43 
44  /** support - to debuggiog */
45  inline std::ostream& operator<<(std::ostream& os, const ConnectPair& p) {
46  os << '(' << p.first - p.first.getChain().begin()
47  << ',' << p.second - p.second.getChain().begin() << ')';
48  return os;
49  }
50 
51  /** \brief the secondary structure
52 
53  The collection of ConnectPair, represents the secondary structure.
54  */
55  class SecStruct {
56  public:
57  /** the folded pairs collection - secondary structure */
58  typedef std::set<ConnectPair> Foldings;
59 
60  /** constructor */
62 
63  /** copy constructor */
64  SecStruct(const SecStruct& s) : foldings_(s.foldings_) {}
65 
66  /** destructor */
68 
69  /** assign operator */
71  foldings_ = s.foldings_;
72  return *this;
73  }
74 
75  /** adds the pair to secondary structure */
76  void addPair(const ConnectPair& p) { foldings_.insert(p); }
77 
78  /** append the collection of pairs to secondary structure */
79  void append(const SecStruct& s) { foldings_.insert(s.foldings_.begin(), s.foldings_.end() ); }
80 
81  /** identity */
82  bool operator==(const SecStruct& s) const { return foldings_ == s.foldings_; }
83 
84  /** comparison */
85  bool operator<(const SecStruct& s) const { return foldings_ < s.foldings_; }
86 
87  /** number of folded pairs */
88  int size() const { return static_cast<int>( foldings_.size() ); }
89 
90  /** calculates the energy of secondary structure. Summarizes all pairs. */
91  EnergyValue energy(const EnergyNucleo& energy_matrix) const;
92 
93  /** accessor - returns the pair collection */
94  const Foldings& getFoldings() const { return foldings_; }
95 
96  /** support - to debugging */
97  friend std::ostream& operator<<(std::ostream& os, const SecStruct& sec_struct);
98  private:
99  //Foldings forbidden
100  Foldings foldings_;
101  };
102 
103  /** secondary structure collection */
104  typedef std::set<SecStruct> SecStructures;
105 
106  namespace {
107  //helping functor - energy for single connection
108  struct EnergyFunctor {
109  EnergyFunctor(const EnergyNucleo& energy_matrix) : energy_(energy_matrix) {}
110  EnergyValue operator()(EnergyValue sum, const ConnectPair& p) {
111  return sum + energy_.getEnergy( *(p.first), *(p.second) );
112  }
113  const EnergyNucleo& energy_;
114  private:
115  //forbidden, operator= (warning w msvc)
116  EnergyFunctor& operator=(const EnergyFunctor&);
117  };
118  } //namespace
119 
120  inline EnergyValue SecStruct::energy(const EnergyNucleo& energy_matrix) const {
121  EnergyFunctor functor(energy_matrix);
122  return std::accumulate( foldings_.begin(), foldings_.end(), 0, functor );
123  }
124 
125  /** support - to debugging */
126  inline std::ostream& operator<<(std::ostream& os,const SecStruct& sec_struct) {
127  std::copy(sec_struct.foldings_.begin(), sec_struct.foldings_.end(), std::ostream_iterator<ConnectPair>(os,"") );
128  return os;
129  }
130 
131 
132 
133  } //namespace
134 } //namespace
135 
136 #endif //SECSTRUCT_H
Definition: Chain.h:54
Definition: Chain.h:17
The pair of nucleotides which are join by Watson-Crick interaction.
Definition: SecStruct.h:30
STL namespace.
int EnergyValue
Definition: EnergyNucleo.h:15
The DNA strand (single).
Definition: Chain.h:25
std::set< SecStruct > SecStructures
Definition: SecStruct.h:104
bool operator<(const ConnectPair &pair) const
Definition: SecStruct.h:37
the maps between pair of nucleotides and its energy
Definition: EnergyNucleo.h:37
SecStruct()
Definition: SecStruct.h:61
void append(const SecStruct &s)
Definition: SecStruct.h:79
SecStruct & operator=(const SecStruct &s)
Definition: SecStruct.h:70
bool operator<(const SecStruct &s) const
Definition: SecStruct.h:85
void addPair(const ConnectPair &p)
Definition: SecStruct.h:76
std::ostream & operator<<(std::ostream &os, const Chain &chain)
Definition: Chain.h:162
int size() const
Definition: SecStruct.h:88
std::set< ConnectPair > Foldings
Definition: SecStruct.h:58
~SecStruct()
Definition: SecStruct.h:67
bool operator==(const SecStruct &s) const
Definition: SecStruct.h:82
ConnectPair(const Chain::const_iterator &f, const Chain::const_iterator &s)
Definition: SecStruct.h:33
const Foldings & getFoldings() const
Definition: SecStruct.h:94
the secondary structure
Definition: SecStruct.h:55
EnergyValue energy(const EnergyNucleo &energy_matrix) const
Definition: SecStruct.h:120
SecStruct(const SecStruct &s)
Definition: SecStruct.h:64