faif
FoldedChain.h
1 // DNA single stranded chain with second structures
2 // @author Robert Nowak
3 
4 #ifndef FOLDED_CHAIN_H
5 #define FOLDED_CHAIN_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 #endif
13 
14 #include "Chain.h"
15 #include "SecStruct.h"
16 #include "FoldedMatrix.h"
17 
18 #include <boost/scoped_ptr.hpp>
19 
20 namespace faif {
21  namespace dna {
22 
23  /** matrix with energy */
24  class FoldedMatrix;
25  /** startegy to calculate matrix */
26  class FoldedMatrixStrategy;
27 
28  /** \brief DNA strand with secondary structure
29 
30  Single DNA strand with secondary structures calculated by given algorithm.
31  */
32  class FoldedChain : boost::noncopyable {
33  public:
34  explicit FoldedChain(const Chain& chain, const EnergyNucleo& energy, unsigned int max_foldings = 100);
35 
36  ~FoldedChain(){}
37 
38  /** accessor */
39  const Chain& getChain() const { return chain_; }
40 
41  /** the second structure energy. Calculated on the first time, when method is called. */
43  return lazyCreate()->getSecStructEnergy();
44  }
45 
46  /** single second structure, algorithm search graph in depth */
48  return lazyCreate()->findInDepth();
49  }
50 
51  /** collection of second structures, algorithm search graph in width,
52  the max_foldings parameters restricts the number of structures */
53  const SecStructures& getStructures() const {
54  return lazyCreate()->getStructures();
55  }
56 
57  /** print the matrix (for testing and debugging) */
58  std::ostream& printMatrix(std::ostream& os, int print_width = 4) const {
59  if( proxy_.get() != 0L)
60  proxy_->printMatrix(os, print_width);
61  else
62  os << "energy matrix not created" << std::endl;
63  return os;
64  }
65 
66 
67  /** print the secondary structures (for testing and debugging) */
68  std::ostream& printStructures(std::ostream& os, int print_width = 4) const {
69  if( proxy_.get() != 0L)
70  proxy_->printStructures(os, print_width);
71  else
72  os << "energy matrix not created" << std::endl;
73  return os;
74  }
75 
76  /** the support class - holding the matrix with energy and secondary structures */
77  typedef boost::scoped_ptr<FoldedMatrix> FoldedMatrixPtr;
78  /** the strategy to access for FoldedMatrix (one or two DNA chains) */
79  typedef boost::scoped_ptr<FoldedMatrixStrategy> FoldedMatrixStrategyPtr;
80  private:
81  /** the analyzed DNA chain */
82  Chain chain_;
83  /** the energy of nucleotide pairs */
84  const EnergyNucleo& energy_;
85 
86  /** maximum number of returned second structures */
87  unsigned int max_foldings_;
88 
89  /** strategy for FoldedMatrix to index single chain DNA */
90  FoldedMatrixStrategyPtr strategy_;
91 
92  /** obiekt, ktory przechowuje obliczone wartosci */
93  mutable FoldedMatrixPtr proxy_;
94 
95  /** creates the proxy if necessary */
96  FoldedMatrixPtr& lazyCreate() const {
97  if( proxy_.get() == 0L )
98  proxy_.reset( new FoldedMatrix(*strategy_, max_foldings_ ) );
99  return proxy_;
100  }
101  };
102 
103  namespace {
104 
105  /** strategy to calculate FoldedMatrix, when one chain (single chain) is calculated */
106  class SingleMatrixStrategy : public FoldedMatrixStrategy {
107  public:
108  SingleMatrixStrategy( const EnergyNucleo& energy, const Chain& chain )
109  : FoldedMatrixStrategy(energy), chain_(chain) {}
110  virtual ~SingleMatrixStrategy() {}
111 
112  /** return the iterator for nuleotide of i-th index in matrix */
113  virtual Chain::const_iterator getNucleotide(int index) const {
114  Chain::const_iterator it = chain_.begin();
115  it += index;
116  return it;
117  }
118 
119  /** returns the split index (the first index belonging to the second chain) */
120  virtual int getSplitIndex() const { return chain_.getLength(); }
121 
122  /** returns the last valid index */
123  virtual int getLength() const { return chain_.getLength(); }
124 
125  private:
126  const Chain& chain_;
127  };
128 
129  } //namespace
130 
131  inline FoldedChain::FoldedChain(const Chain& chain, const EnergyNucleo& energy, unsigned int max_foldings)
132  : chain_(chain),
133  energy_(energy),
134  max_foldings_(max_foldings),
135  strategy_( new SingleMatrixStrategy(energy_, chain_) ),
136  proxy_(0L)
137  { }
138 
139 
140  /** print the chain sequence and one secondary structure */
141  inline std::ostream& operator<<(std::ostream& os, const FoldedChain& folded) {
142  os << folded.getChain().getString();
143  os << folded.findInDepth();
144  return os;
145  }
146 
147 
148 
149 
150  } //namespace dna
151 } //namespace faif
152 
153 #endif //CHAIN_H
boost::scoped_ptr< FoldedMatrixStrategy > FoldedMatrixStrategyPtr
Definition: FoldedChain.h:79
Definition: Chain.h:54
SecStruct findInDepth() const
Definition: FoldedChain.h:47
Definition: Chain.h:17
DNA strand with secondary structure.
Definition: FoldedChain.h:32
const Chain & getChain() const
Definition: FoldedChain.h:39
int EnergyValue
Definition: EnergyNucleo.h:15
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
const SecStructures & getStructures() const
Definition: FoldedChain.h:53
EnergyValue getSecStructEnergy() const
Definition: FoldedChain.h:42
std::ostream & printStructures(std::ostream &os, int print_width=4) const
Definition: FoldedChain.h:68
std::ostream & operator<<(std::ostream &os, const Chain &chain)
Definition: Chain.h:162
std::ostream & printMatrix(std::ostream &os, int print_width=4) const
Definition: FoldedChain.h:58
boost::scoped_ptr< FoldedMatrix > FoldedMatrixPtr
Definition: FoldedChain.h:77
Definition: FoldedMatrix.h:76
the secondary structure
Definition: SecStruct.h:55