bpp-seq  2.2.0
AbstractAlphabet.h
Go to the documentation of this file.
1 //
2 // File: AbstractAlphabet.h
3 // Authors: Guillaume Deuchst
4 // Julien Dutheil
5 // Sylvain Gaillard
6 // Created on: Tue Jul 22 2003
7 //
8 
9 /*
10  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
11 
12  This software is a computer program whose purpose is to provide classes
13  for sequences analysis.
14 
15  This software is governed by the CeCILL license under French law and
16  abiding by the rules of distribution of free software. You can use,
17  modify and/ or redistribute the software under the terms of the CeCILL
18  license as circulated by CEA, CNRS and INRIA at the following URL
19  "http://www.cecill.info".
20 
21  As a counterpart to the access to the source code and rights to copy,
22  modify and redistribute granted by the license, users are provided only
23  with a limited warranty and the software's author, the holder of the
24  economic rights, and the successive licensors have only limited
25  liability.
26 
27  In this respect, the user's attention is drawn to the risks associated
28  with loading, using, modifying and/or developing or reproducing the
29  software by the user in light of its specific status of free software,
30  that may mean that it is complicated to manipulate, and that also
31  therefore means that it is reserved for developers and experienced
32  professionals having in-depth computer knowledge. Users are therefore
33  encouraged to load and test the software's suitability as regards their
34  requirements in conditions enabling the security of their systems and/or
35  data to be ensured and, more generally, to use and operate it in the
36  same conditions as regards security.
37 
38  The fact that you are presently reading this means that you have had
39  knowledge of the CeCILL license and that you accept its terms.
40 */
41 
42 #ifndef _ABSTRACTALPHABET_H_
43 #define _ABSTRACTALPHABET_H_
44 
45 #include "Alphabet.h"
46 #include "AlphabetState.h"
47 #include <Bpp/Exceptions.h>
48 
49 // From the STL:
50 #include <string>
51 #include <vector>
52 #include <map>
53 
54 namespace bpp
55 {
56 
68  public Alphabet
69  {
70  private:
71 
75  std::vector<AlphabetState*> alphabet_;
80  std::map<std::string, size_t> letters_;
81  std::map<int, size_t> nums_;
89  void updateMaps_(size_t pos, const AlphabetState& st);
90 
91  protected:
99  mutable std::vector<std::string> charList_;
100  mutable std::vector<int> intList_;
103  public:
104 
106 
108  {
109  for (size_t i = 0; i < alph.alphabet_.size(); ++i)
110  alphabet_.push_back(new AlphabetState(*alph.alphabet_[i]));
111  }
112 
114  {
115  for (size_t i = 0 ; i < alphabet_.size() ; ++i)
116  delete alphabet_[i];
117 
118  for (size_t i = 0; i < alph.alphabet_.size(); ++i)
119  alphabet_.push_back(new AlphabetState(*alph.alphabet_[i]));
120 
121  letters_ = alph.letters_;
122  nums_ = alph.nums_;
123  charList_ = alph.charList_;
124  intList_ = alph.intList_;
125 
126  return *this;
127  }
128 
129 #ifndef NO_VIRTUAL_COV
130  virtual AbstractAlphabet* clone() const = 0;
131 #endif
132 
134  {
135  for (size_t i = 0 ; i < alphabet_.size() ; ++i)
136  delete alphabet_[i];
137  }
138 
139  public:
145  size_t getNumberOfStates() const { return alphabet_.size(); }
146  unsigned int getNumberOfChars() const { return static_cast<unsigned int>(alphabet_.size()); }
147  std::string getName(const std::string& state) const throw (BadCharException);
148  std::string getName(int state) const throw (BadIntException);
149  int charToInt(const std::string& state) const throw (BadCharException);
150  std::string intToChar(int state) const throw (BadIntException);
151  bool isIntInAlphabet(int state) const;
152  bool isCharInAlphabet(const std::string& state) const;
153  std::vector<int> getAlias(int state) const throw (BadIntException);
154  std::vector<std::string> getAlias(const std::string& state) const throw (BadCharException);
155  int getGeneric(const std::vector<int>& states) const throw (BadIntException);
156  std::string getGeneric(const std::vector<std::string>& states) const throw (AlphabetException);
157  const std::vector<int>& getSupportedInts() const;
158  const std::vector<std::string>& getSupportedChars() const;
159  int getGapCharacterCode() const { return -1; }
160  bool isGap(int state) const { return state == -1; }
161  bool isGap(const std::string& state) const { return charToInt(state) == -1; }
177  virtual AlphabetState& getStateAt(size_t stateIndex) throw (IndexOutOfBoundsException);
178 
188  virtual const AlphabetState& getStateAt(size_t stateIndex) const throw (IndexOutOfBoundsException);
189 
199  const AlphabetState& getState(const std::string& letter) const throw (BadCharException);
200 
201  AlphabetState& getState(const std::string& letter) throw (BadCharException);
202 
212  const AlphabetState& getState(int num) const throw (BadIntException);
213 
214  AlphabetState& getState(int num) throw (BadIntException);
215 
216  int getIntCodeAt(size_t stateIndex) const throw (IndexOutOfBoundsException) {
217  return getStateAt(stateIndex).getNum();
218  }
219 
220  const std::string& getCharCodeAt(size_t stateIndex) const throw (IndexOutOfBoundsException) {
221  return getStateAt(stateIndex).getLetter();
222  }
223 
224  size_t getStateIndex(int state) const throw (BadIntException);
225 
226  size_t getStateIndex(const std::string& state) const throw (BadCharException);
229  protected:
236  virtual void registerState(AlphabetState* st) throw (Exception);
237 
246  virtual void setState(size_t pos, AlphabetState* st) throw (Exception, IndexOutOfBoundsException);
247 
253  void resize(size_t size) { alphabet_.resize(size); }
254 
258  void remap() {
259  letters_.clear();
260  nums_.clear();
261  for (size_t i = 0; i < alphabet_.size(); ++i) {
262  updateMaps_(i, *alphabet_[i]);
263  }
264  }
265 
266  unsigned int getStateCodingSize() const { return 1; }
267 
268  bool equals(const Alphabet& alphabet) const {
269  return getAlphabetType() == alphabet.getAlphabetType();
270  }
271  };
272 
273 } //end of namespace bpp.
274 
275 #endif // _ABSTRACTALPHABET_H_
276 
This is the base class to describe states in an Alphabet.
Definition: AlphabetState.h:54
AbstractAlphabet(const AbstractAlphabet &alph)
An alphabet exception thrown when trying to specify a bad char to the alphabet.
int getNum() const
Get the state&#39;s number.
Definition: AlphabetState.h:83
This alphabet is used to deal NumericAlphabet.
void updateMaps_(size_t pos, const AlphabetState &st)
Update the private maps letters_ and nums_ when adding a state.
The Alphabet interface.
Definition: Alphabet.h:130
std::map< std::string, size_t > letters_
int charToInt(const std::string &state) const
Give the int description of a state given its string description.
bool isIntInAlphabet(int state) const
Tell if a state (specified by its int description) is allowed by the the alphabet.
const std::vector< std::string > & getSupportedChars() const
bool isGap(int state) const
AbstractAlphabet & operator=(const AbstractAlphabet &alph)
const AlphabetState & getState(const std::string &letter) const
Get a state by its letter.
int getIntCodeAt(size_t stateIndex) const
const std::string & getCharCodeAt(size_t stateIndex) const
size_t getNumberOfStates() const
This is a convenient alias for getNumberOfChars(), returning a size_t instead of unsigned int...
virtual AbstractAlphabet * clone() const =0
std::vector< std::string > charList_
virtual void setState(size_t pos, AlphabetState *st)
Set a state in the Alphabet.
The alphabet exception base class.
std::vector< AlphabetState * > alphabet_
Alphabet: vector of AlphabetState.
A partial implementation of the Alphabet interface.
std::vector< int > getAlias(int state) const
Get all resolved states that match a generic state.
std::map< int, size_t > nums_
const std::string & getLetter() const
Get the letter(s) corresponding to the state.
Definition: AlphabetState.h:99
int getGapCharacterCode() const
bool isGap(const std::string &state) const
std::string getName(const std::string &state) const
Get the complete name of a state given its string description.
bool isCharInAlphabet(const std::string &state) const
Tell if a state (specified by its string description) is allowed by the the alphabet.
bool equals(const Alphabet &alphabet) const
Comparison of alphabets.
void resize(size_t size)
Resize the private alphabet_ vector.
void remap()
Re-update the maps using the alphabet_ vector content.
int getGeneric(const std::vector< int > &states) const
Get the generic state that match a set of states.
std::vector< int > intList_
An alphabet exception thrown when trying to specify a bad int to the alphabet.
virtual AlphabetState & getStateAt(size_t stateIndex)
Get a state at a position in the alphabet_ vector.
virtual void registerState(AlphabetState *st)
Add a state to the Alphabet.
const std::vector< int > & getSupportedInts() const
unsigned int getStateCodingSize() const
Get the size of the string coding a state.
std::string intToChar(int state) const
Give the string description of a state given its int description.
unsigned int getNumberOfChars() const
Get the number of supported characters in this alphabet, including generic characters (e...
size_t getStateIndex(int state) const
virtual std::string getAlphabetType() const =0
Identification method.