faif
Chain.h
1 // dna single stranded chain
2 // @author Robert Nowak
3 
4 #ifndef CHAIN_H
5 #define CHAIN_H
6 
7 #include <cassert>
8 #include <ostream>
9 #include <limits>
10 #include <string>
11 #include <vector>
12 #include <algorithm>
13 #include <iterator>
14 
15 #include "Nucleotide.h"
16 
17 namespace faif {
18  namespace dna {
19 
20  /** \brief The DNA strand (single).
21 
22  Sequence of nucleotides,
23  the begin of sequence = 5' end of molecule, the end of sequence = 3' end of molecule
24  */
25  class Chain {
26  /** support type, collects nucleotides */
27  typedef std::vector<Nucleotide> NucleotideChain;
28  //support (shorter code)
29  typedef NucleotideChain::iterator NucleotideIter;
30  //support (shorter code)
31  typedef NucleotideChain::const_iterator ConstNucleotideIter;
32  public:
33  class const_iterator;
34 
35  /** iterator for nucleotide */
36  class iterator : public NucleotideIter {
37  friend class const_iterator;
38  public:
39  iterator(Chain& ch, NucleotideIter it) : NucleotideIter(it), chain_(ch) {}
40  Chain& getChain() { return chain_; }
41 
42  /** comparison take into account the chain */
43  bool operator==(const iterator& it) const {
44  if(&chain_ != &it.chain_)
45  return false;
46  return static_cast<NucleotideIter>(*this) == static_cast<NucleotideIter>(it);
47  }
48  private:
49  iterator& operator=(const iterator&); //zabronione przypisanie
50  Chain& chain_;
51  };
52 
53  /** const iterator for nucleotide */
54  class const_iterator : public ConstNucleotideIter {
55  public:
56  const_iterator(const Chain& ch, ConstNucleotideIter it) : ConstNucleotideIter(it), chain_(ch) {}
57  const_iterator(const iterator& it) : ConstNucleotideIter(it), chain_(it.chain_) {}
58  const Chain& getChain() const { return chain_; }
59 
60  /** comparison take into account the chain */
61  bool operator==(const const_iterator& it) const {
62  if(&chain_ != &it.chain_)
63  return false;
64  return static_cast<ConstNucleotideIter>(*this) == static_cast<ConstNucleotideIter>(it);
65  }
66  private:
67  const_iterator& operator=(const const_iterator&); //zabronione przypisanie
68  const Chain& chain_;
69  };
70 
71  explicit Chain(){}
72  // Chain(const NucleotideChain& n) : nucleos_(n) {}
73  Chain(const Chain& ch) : nucleos_(ch.nucleos_) {}
74  Chain(const std::string& seq) {
75  for(std::string::const_iterator it = seq.begin(); it != seq.end(); ++it)
76  operator+=( create(*it) );
77  }
78  /** copy constructor from range */
80  std::copy( begin, end, std::back_inserter(nucleos_) );
81  }
82  ~Chain(){}
83 
84  Chain& operator=(const Chain& ch) {
85  nucleos_=ch.nucleos_;
86  return *this;
87  }
88 
89  /** insert a new nucleotide at the and */
90  Chain& operator+=(const Nucleotide& val) {
91  nucleos_.push_back(val);
92  return *this;
93  }
94 
95  Chain& operator+=(const Chain& ch) {
96  std::copy(ch.nucleos_.begin(), ch.nucleos_.end(), std::back_insert_iterator<NucleotideChain>(nucleos_) );
97  return *this;
98  }
99 
100  /** comparison of chains */
101  bool operator==(const Chain& ch)const { return nucleos_ == ch.nucleos_; }
102  /** comparison of chains */
103  bool operator!=(const Chain& ch)const { return nucleos_ != ch.nucleos_; }
104 
105  /** accessor for index, throws out_of_range */
106  const Nucleotide& operator[](int index) const { return nucleos_.at(index); }
107 
108  /** accessor for index, throws out_of_range */
109  Nucleotide& operator[](int index) {
110  return const_cast<Nucleotide&>(static_cast<const Chain*>(this)->operator[](index) );
111  }
112 
113  /** accessor - length */
114  int getLength() const { return static_cast<int>(nucleos_.size() ); }
115 
116  /** mutator - iterator */
117  iterator begin() { return iterator(*this,nucleos_.begin() ); }
118  /** mutator - const iterator */
119  const_iterator begin() const { return const_iterator(*this,nucleos_.begin()); }
120  /** mutator - iterator */
121  iterator end() { return iterator(*this,nucleos_.end()); }
122  /** mutator - const iterator */
123  const_iterator end() const { return const_iterator(*this,nucleos_.end()); }
124  /** the string for chain */
125  std::string getString()const {
126  std::string str;
127  for(NucleotideChain::const_iterator it = nucleos_.begin(); it != nucleos_.end(); ++it) {
128  str += static_cast<char>(it->get() );
129  }
130  return str;
131  }
132  /** creates new complementary chain (always the 5' end on index 0) */
134  Chain complChain;
135  for(NucleotideChain::const_reverse_iterator it = nucleos_.rbegin(); it != nucleos_.rend(); ++it)
136  complChain += it->complementary();
137  return complChain;
138  }
139  private:
140  NucleotideChain nucleos_;
141  };
142 
143  /** creator (support): sub-chain, using index).
144  Returns the largest chain starting from given index and having length less or equal given length. */
145  inline Chain createSubChain(const Chain& chain, int start, int length) {
146  if(start >= chain.getLength() )
147  start = chain.getLength();
148 
149  int finish = start + length;
150  if( start + length >= chain.getLength() )
151  finish = chain.getLength();
152 
153  Chain::const_iterator beg = chain.begin();
154  beg += start;
155  Chain::const_iterator end = chain.begin();
156  end += finish;
157  return Chain(beg, end);
158  }
159 
160 
161  /** stream operator (support for debugging) */
162  inline std::ostream& operator<<(std::ostream& os, const Chain& chain) {
163  os << chain.getString();
164  return os;
165  }
166 
167  } //namespace dna
168 } //namespace faif
169 
170 #endif //CHAIN_H
Definition: Chain.h:54
const_iterator begin() const
Definition: Chain.h:119
Definition: Chain.h:17
Chain createSubChain(const Chain &chain, int start, int length)
Definition: Chain.h:145
the DNA nucleotide
Definition: Nucleotide.h:21
bool operator==(const const_iterator &it) const
Definition: Chain.h:61
Nucleotide & operator[](int index)
Definition: Chain.h:109
int getLength() const
Definition: Chain.h:114
iterator begin()
Definition: Chain.h:117
iterator end()
Definition: Chain.h:121
bool operator!=(const Chain &ch) const
Definition: Chain.h:103
The DNA strand (single).
Definition: Chain.h:25
Definition: Chain.h:36
Chain & operator+=(const Nucleotide &val)
Definition: Chain.h:90
std::string getString() const
Definition: Chain.h:125
Chain complementary() const
Definition: Chain.h:133
bool operator==(const iterator &it) const
Definition: Chain.h:43
bool operator==(const Chain &ch) const
Definition: Chain.h:101
Chain(const const_iterator &begin, const const_iterator &end)
Definition: Chain.h:79
std::ostream & operator<<(std::ostream &os, const Chain &chain)
Definition: Chain.h:162
const_iterator end() const
Definition: Chain.h:123
const Nucleotide & operator[](int index) const
Definition: Chain.h:106
Nucleotide create(const char &n)
Definition: Nucleotide.h:60