faif
Nucleotide.h
1 // dna nucleotide
2 // @author Robert Nowak
3 
4 #ifndef NUCLEOTIDE_H
5 #define NUCLEOTIDE_H
6 
7 #include <ostream>
8 #include "ExceptionsDna.h"
9 
10 namespace faif {
11 
12  namespace dna {
13 
14  /** the nucleotide value (enum) */
15  enum NucleotideValue { ADENINE = 'A', CYTOSINE = 'C', GUANINE = 'G' , THYMINE = 'T', ANY_NUCLEOTIDE = 'N'};
16 
17  /** \brief the DNA nucleotide
18 
19  There are four values: A, T, G, C and the N (denotes any nucleotide)
20  */
21  class Nucleotide {
22  private:
23  NucleotideValue val_;
24  public:
25  /** c-tor */
26  explicit Nucleotide(const NucleotideValue& val = ANY_NUCLEOTIDE) : val_(val) {}
27  Nucleotide(const Nucleotide& n) : val_(n.val_) {}
28 
29  Nucleotide& operator=(const Nucleotide& n) { val_ = n.val_;return *this; }
30  ~Nucleotide(){}
31 
32  /** accessor */
33  NucleotideValue get() const { return val_; }
34 
35  /** comparison */
36  bool operator==(const Nucleotide& n)const{ return val_ == n.val_; }
37  /** comparison */
38  bool operator!=(const Nucleotide& n)const{ return val_ != n.val_; }
39  /** order */
40  bool operator<(const Nucleotide& n) const { return val_ < n.val_; }
41 
42  /** return the complementary nucleotide */
44  switch(val_) {
45  case ADENINE:
46  return Nucleotide(THYMINE);
47  case CYTOSINE:
48  return Nucleotide(GUANINE);
49  case GUANINE:
50  return Nucleotide(CYTOSINE);
51  case THYMINE:
52  return Nucleotide(ADENINE);
53  default:
54  throw NucleotideBadCharException(static_cast<char>(val_) );
55  }
56  }
57  };
58 
59  /** creates from the char representing nucleotide */
60  inline Nucleotide create(const char& n) {
61  switch(n) {
62  case 'A':
63  case 'a':
64  return Nucleotide(ADENINE);
65  case 'C':
66  case 'c':
67  return Nucleotide(CYTOSINE);
68  case 'G':
69  case 'g':
70  return Nucleotide(GUANINE);
71  case 'T':
72  case 't':
73  return Nucleotide(THYMINE);
74  }
76  }
77 
78  /** stream operator, support, for debugging */
79  inline std::ostream& operator<<(std::ostream& os, const Nucleotide& n) {
80  os << static_cast<char>(n.get());
81  return os;
82  }
83 
84  } //namespace dna
85 } //namespace faif
86 
87 #endif //NUCLEOTIDE_H
Definition: Chain.h:17
the DNA nucleotide
Definition: Nucleotide.h:21
the exception thrown when unknown nucleotide (bad letter) occures
Definition: ExceptionsDna.h:17
NucleotideValue
Definition: Nucleotide.h:15
Nucleotide complementary() const
Definition: Nucleotide.h:43
NucleotideValue get() const
Definition: Nucleotide.h:33
Nucleotide(const NucleotideValue &val=ANY_NUCLEOTIDE)
Definition: Nucleotide.h:26
std::ostream & operator<<(std::ostream &os, const Chain &chain)
Definition: Chain.h:162
bool operator==(const Nucleotide &n) const
Definition: Nucleotide.h:36
bool operator!=(const Nucleotide &n) const
Definition: Nucleotide.h:38
Nucleotide create(const char &n)
Definition: Nucleotide.h:60
bool operator<(const Nucleotide &n) const
Definition: Nucleotide.h:40