bpp-seq  2.2.0
LetterAlphabet.h
Go to the documentation of this file.
1 //
2 // File: LetterAlphabet.h
3 // Author: Sylvain Gaillard
4 // Created: 11/09/2009 14:31:05
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (September 11, 2009)
9 
10  This software is a computer program whose purpose is to provide classes
11  for sequences analysis.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided only
21  with a limited warranty and the software's author, the holder of the
22  economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards their
32  requirements in conditions enabling the security of their systems and/or
33  data to be ensured and, more generally, to use and operate it in the
34  same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #ifndef _LETTERALPHABET_
41 #define _LETTERALPHABET_
42 
43 // From the STL
44 #include <string>
45 #include <vector>
46 #include <iostream>
47 
48 // From Seq
49 #include "AbstractAlphabet.h"
50 
51 namespace bpp {
58  public AbstractAlphabet
59  {
60  private:
61  static const int LETTER_UNDEF_VALUE;
62  std::vector<int> letters_;
64 
65  public:
66  LetterAlphabet(bool caseSensitive = false): letters_(256, LETTER_UNDEF_VALUE), caseSensitive_(caseSensitive) {}
67 
69 
71  {
73  letters_=bia.letters_;
75 
76  return *this;
77 
78  }
79 
80 #ifndef NO_VIRTUAL_COV
81  virtual LetterAlphabet* clone() const = 0;
82 #endif
83 
84  virtual ~LetterAlphabet() {}
85 
86  public:
87  bool isCharInAlphabet(char state) const {
88  return letters_[static_cast<unsigned int>(state)] != LETTER_UNDEF_VALUE;
89  }
90  bool isCharInAlphabet(const std::string& state) const {
91  return isCharInAlphabet(state[0]);
92  }
93  int charToInt(const std::string &state) const throw (BadCharException) {
94  if (!isCharInAlphabet(state))
95  throw BadCharException(state, "LetterAlphabet::charToInt: Unknown state", this);
96  return letters_[static_cast<unsigned int>(state[0])];
97  }
98 
99  protected:
100  void registerState(AlphabetState* st) throw (Exception) {
102  if (caseSensitive_) {
103  letters_[static_cast<unsigned int>(st->getLetter()[0])] = st->getNum();
104  } else {
105  letters_[static_cast<unsigned int>(tolower(st->getLetter()[0]))] = st->getNum();
106  letters_[static_cast<unsigned int>(toupper(st->getLetter()[0]))] = st->getNum();
107  }
108  }
109 
110  void setState(size_t pos, AlphabetState* st) throw (Exception, IndexOutOfBoundsException) {
112  if (caseSensitive_) {
113  letters_[static_cast<unsigned int>(st->getLetter()[0])] = st->getNum();
114  } else {
115  letters_[static_cast<unsigned int>(tolower(st->getLetter()[0]))] = st->getNum();
116  letters_[static_cast<unsigned int>(toupper(st->getLetter()[0]))] = st->getNum();
117  }
118  }
119 
120  };
121 }
122 
123 #endif // _LETTERALPHABET_
LetterAlphabet & operator=(const LetterAlphabet &bia)
This is the base class to describe states in an Alphabet.
Definition: AlphabetState.h:54
An alphabet exception thrown when trying to specify a bad char to the alphabet.
LetterAlphabet(const LetterAlphabet &bia)
std::vector< int > letters_
This alphabet is used to deal NumericAlphabet.
virtual LetterAlphabet * clone() const =0
AbstractAlphabet & operator=(const AbstractAlphabet &alph)
Specialized partial implementation of Alphabet using single letters.
bool isCharInAlphabet(char state) const
void registerState(AlphabetState *st)
Add a state to the Alphabet.
static const int LETTER_UNDEF_VALUE
virtual void setState(size_t pos, AlphabetState *st)
Set a state in the Alphabet.
void setState(size_t pos, AlphabetState *st)
Set a state in the Alphabet.
A partial implementation of the Alphabet interface.
virtual ~LetterAlphabet()
LetterAlphabet(bool caseSensitive=false)
virtual void registerState(AlphabetState *st)
Add a state to the Alphabet.
bool isCharInAlphabet(const std::string &state) const
Tell if a state (specified by its string description) is allowed by the the alphabet.
int charToInt(const std::string &state) const
Give the int description of a state given its string description.