faif
FoldedPair.h
1 // DNA two single stranded chain with second structures
2 // @author Robert Nowak
3 
4 #ifndef FOLDED_PAIR_H
5 #define FOLDED_PAIR_H
6 
7 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
8 //visual studio 8.0 - konwersja pomiedzy unsigned int a size_t
9 #pragma warning(disable:4267)
10 //visual studio 8.0 - arytmetyka dla iteratorow przy konwersji na inta
11 #pragma warning(disable:4244)
12 
13 #endif
14 
15 #include "Chain.h"
16 #include "SecStruct.h"
17 #include "FoldedMatrix.h"
18 
19 #include <boost/scoped_ptr.hpp>
20 
21 namespace faif {
22  namespace dna {
23 
24  /** matrix with energy */
25  class FoldedMatrix;
26  /** startegy to calculate matrix */
27  class FoldedMatrixStrategy;
28 
29  /** \brief Two DNA chains with secondary structures.
30 
31  Two DNA chains with secondary structures
32  (created between them as well as the self-secondary structures)
33  */
34  class FoldedPair : boost::noncopyable {
35  public:
36  explicit FoldedPair(const Chain& first_chain, const Chain& second_chain,
37  const EnergyNucleo& energy, unsigned int max_foldings = 100);
38 
39  ~FoldedPair() {}
40 
41  /** accessor */
42  const Chain& getFirstChain() const { return first_; }
43 
44  /** accessor */
45  const Chain& getSecondChain() const { return second_; }
46 
47  /** the second structure energy. Calculated on the first time, when method is called. */
49  return lazyCreate()->getSecStructEnergy();
50  }
51 
52  /** single second structure, algorithm search graph in depth */
54  return lazyCreate()->findInDepth();
55  }
56 
57  /** collection of second structures, algorithm search graph in width,
58  the max_foldings parameters restricts the number of structures */
59  const SecStructures& getStructures() const {
60  return lazyCreate()->getStructures();
61  }
62 
63  /** print the matrix (for testing and debugging) */
64  std::ostream& printMatrix(std::ostream& os, int print_width = 4) const {
65  if( proxy_.get() != 0L)
66  proxy_->printMatrix(os, print_width);
67  else
68  os << "energy matrix not created" << std::endl;
69  return os;
70  }
71 
72  /** print the secondary structures (for testing and debugging) */
73  std::ostream& printStructures(std::ostream& os, int print_width = 4) const {
74  if( proxy_.get() != 0L)
75  proxy_->printStructures(os, print_width);
76  else
77  os << "energy matrix not created" << std::endl;
78  return os;
79  }
80 
81  /** the support class - holding the matrix with energy and secondary structures */
82  typedef boost::scoped_ptr<FoldedMatrix> FoldedMatrixPtr;
83  /** the strategy to access for FoldedMatrix (one or two DNA chains) */
84  typedef boost::scoped_ptr<FoldedMatrixStrategy> FoldedMatrixStrategyPtr;
85  private:
86  /** the analyzed DNA chains */
87  Chain first_;
88  Chain second_;
89  /** the energy of nucleotide pairs */
90  const EnergyNucleo& energy_;
91 
92  /** maximum number of returned second structures */
93  unsigned int max_foldings_;
94 
95  /** strategy for FoldedMatrix to index two DNA chains */
96  FoldedMatrixStrategyPtr strategy_;
97 
98  /** obiekt, ktory przechowuje obliczone wartosci */
99  mutable FoldedMatrixPtr proxy_;
100 
101  /** creates the proxy if necessary */
102  FoldedMatrixPtr& lazyCreate() const {
103  if( proxy_.get() == 0L )
104  proxy_.reset( new FoldedMatrix(*strategy_, max_foldings_) );
105  return proxy_;
106  }
107  };
108 
109  namespace {
110 
111  /** strategy to calculate FoldedMatrix, when one chain (single chain) is calculated */
112  class DoubleMatrixStrategy : public FoldedMatrixStrategy {
113  public:
114  DoubleMatrixStrategy( const EnergyNucleo& energy, const Chain& first, const Chain& second )
115  : FoldedMatrixStrategy(energy),
116  first_(first),
117  second_(second),
118  splitIdx_(first_.getLength() )
119  {}
120 
121  virtual ~DoubleMatrixStrategy() {}
122 
123  /** return the iterator for nuleotide of i-th index in matrix */
124  virtual Chain::const_iterator getNucleotide(int index) const {
125 
126  if(index < splitIdx_) {
127  Chain::const_iterator it = first_.begin();
128  it += index;
129  return it;
130  }
131  else {
132  index -= splitIdx_;
133  Chain::const_iterator it = second_.begin();
134  it += index;
135  return it;
136  }
137  }
138 
139  /** returns the split index (the first index belonging to the second chain) */
140  virtual int getSplitIndex() const { return splitIdx_; }
141 
142  /** returns the last valid index */
143  virtual int getLength() const { return first_.getLength() + second_.getLength(); }
144  private:
145  const Chain& first_;
146  const Chain& second_;
147  int splitIdx_;
148  };
149 
150  } //namespace
151 
152  inline FoldedPair::FoldedPair(const Chain& first_chain, const Chain& second_chain,
153  const EnergyNucleo& energy, unsigned int max_foldings)
154  : first_(first_chain),
155  second_(second_chain),
156  energy_(energy),
157  max_foldings_(max_foldings),
158  strategy_( new DoubleMatrixStrategy(energy_, first_, second_) ),
159  proxy_(0L)
160  { }
161 
162 
163 
164  //print the chain sequences and one secondary structure
165  inline std::ostream& operator<<(std::ostream& os, const FoldedPair& folded) {
166  os << folded.getFirstChain().getString() << std::endl;
167  os << folded.getSecondChain().getString() << std::endl;
168  os << folded.findInDepth();
169  return os;
170  }
171 
172  } //namespace dna
173 } //namespace faif
174 
175 #endif //FOLDER_PAIR_H
Definition: Chain.h:54
Definition: Chain.h:17
const Chain & getSecondChain() const
Definition: FoldedPair.h:45
const Chain & getFirstChain() const
Definition: FoldedPair.h:42
Two DNA chains with secondary structures.
Definition: FoldedPair.h:34
std::ostream & printMatrix(std::ostream &os, int print_width=4) const
Definition: FoldedPair.h:64
int EnergyValue
Definition: EnergyNucleo.h:15
EnergyValue getSecStructEnergy() const
Definition: FoldedPair.h:48
The DNA strand (single).
Definition: Chain.h:25
std::set< SecStruct > SecStructures
Definition: SecStruct.h:104
Definition: FoldedMatrix.h:52
the maps between pair of nucleotides and its energy
Definition: EnergyNucleo.h:37
std::string getString() const
Definition: Chain.h:125
boost::scoped_ptr< FoldedMatrixStrategy > FoldedMatrixStrategyPtr
Definition: FoldedPair.h:84
std::ostream & operator<<(std::ostream &os, const Chain &chain)
Definition: Chain.h:162
std::ostream & printStructures(std::ostream &os, int print_width=4) const
Definition: FoldedPair.h:73
boost::scoped_ptr< FoldedMatrix > FoldedMatrixPtr
Definition: FoldedPair.h:82
const SecStructures & getStructures() const
Definition: FoldedPair.h:59
SecStruct findInDepth() const
Definition: FoldedPair.h:53
Definition: FoldedMatrix.h:76
the secondary structure
Definition: SecStruct.h:55