bpp-seq3  3.0.0
AbstractAlphabet.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_SEQ_ALPHABET_ABSTRACTALPHABET_H
6 #define BPP_SEQ_ALPHABET_ABSTRACTALPHABET_H
7 
8 #include <Bpp/Exceptions.h>
9 
10 #include "Alphabet.h"
11 #include "AlphabetState.h"
12 
13 // From the STL:
14 #include <string>
15 #include <vector>
16 #include <map>
17 
18 namespace bpp
19 {
31  public Alphabet
32 {
33 private:
37  std::vector<AlphabetState*> alphabet_;
38 
43  std::map<std::string, size_t> letters_;
44  std::map<int, size_t> nums_;
53  void updateMaps_(size_t pos, const AlphabetState& st);
54 
55 protected:
63  mutable std::vector<std::string> charList_;
64  mutable std::vector<int> intList_;
67 public:
69 
71  {
72  for (size_t i = 0; i < alph.alphabet_.size(); ++i)
73  {
74  alphabet_.push_back(alph.alphabet_[i]->clone());
75  }
76  }
77 
79  {
80  for (size_t i = 0; i < alphabet_.size(); ++i)
81  {
82  delete alphabet_[i];
83  }
84 
85  for (size_t i = 0; i < alph.alphabet_.size(); ++i)
86  {
87  alphabet_.push_back(alph.alphabet_[i]->clone());
88  }
89 
90  letters_ = alph.letters_;
91  nums_ = alph.nums_;
92  charList_ = alph.charList_;
93  intList_ = alph.intList_;
94 
95  return *this;
96  }
97 
98  virtual AbstractAlphabet* clone() const = 0;
99 
101  {
102  for (size_t i = 0; i < alphabet_.size(); ++i)
103  {
104  delete alphabet_[i];
105  }
106  }
107 
108 public:
114  size_t getNumberOfStates() const { return alphabet_.size(); }
115  unsigned int getNumberOfChars() const { return static_cast<unsigned int>(alphabet_.size()); }
116  std::string getName(const std::string& state) const;
117  std::string getName(int state) const;
118  int charToInt(const std::string& state) const;
119  std::string intToChar(int state) const;
120  bool isIntInAlphabet(int state) const;
121  bool isCharInAlphabet(const std::string& state) const;
122  bool isResolvedIn(int state1, int state2) const;
123  std::vector<int> getAlias(int state) const;
124  std::vector<std::string> getAlias(const std::string& state) const;
125  int getGeneric(const std::vector<int>& states) const;
126  std::string getGeneric(const std::vector<std::string>& states) const;
127  const std::vector<int>& getSupportedInts() const;
128  const std::vector<std::string>& getSupportedChars() const;
129  const std::vector<std::string>& getResolvedChars() const;
130  int getGapCharacterCode() const { return -1; }
131  bool isGap(int state) const { return state == -1; }
132  bool isGap(const std::string& state) const { return charToInt(state) == -1; }
148  virtual AlphabetState& getStateAt(size_t stateIndex);
149 
159  virtual const AlphabetState& getStateAt(size_t stateIndex) const;
160 
170  const AlphabetState& getState(const std::string& letter) const;
171 
172  AlphabetState& getState(const std::string& letter);
173 
183  const AlphabetState& getState(int num) const;
184 
185  AlphabetState& getState(int num);
186 
187  int getIntCodeAt(size_t stateIndex) const
188  {
189  return getStateAt(stateIndex).getNum();
190  }
191 
192  const std::string& getCharCodeAt(size_t stateIndex) const
193  {
194  return getStateAt(stateIndex).getLetter();
195  }
196 
197  size_t getStateIndex(int state) const;
198 
199  size_t getStateIndex(const std::string& state) const;
202 protected:
209  virtual void registerState(AlphabetState* st);
210 
219  virtual void setState(size_t pos, AlphabetState* st);
220 
226  void resize(size_t size) { alphabet_.resize(size); }
227 
231  void remap()
232  {
233  letters_.clear();
234  nums_.clear();
235  for (size_t i = 0; i < alphabet_.size(); ++i)
236  {
237  updateMaps_(i, *alphabet_[i]);
238  }
239  }
240 
241  unsigned int getStateCodingSize() const { return 1; }
242 
243  bool equals(const Alphabet& alphabet) const
244  {
245  return getAlphabetType() == alphabet.getAlphabetType();
246  }
247 };
248 } // end of namespace bpp.
249 #endif // BPP_SEQ_ALPHABET_ABSTRACTALPHABET_H
A partial implementation of the Alphabet interface.
bool equals(const Alphabet &alphabet) const
Comparison of alphabets.
AbstractAlphabet(const AbstractAlphabet &alph)
int getIntCodeAt(size_t stateIndex) const
std::vector< AlphabetState * > alphabet_
Alphabet: vector of AlphabetState.
std::string getName(const std::string &state) const
Get the complete name of a state given its string description.
const std::vector< int > & getSupportedInts() const
virtual AbstractAlphabet * clone() const =0
void resize(size_t size)
Resize the private alphabet_ vector.
void updateMaps_(size_t pos, const AlphabetState &st)
Update the private maps letters_ and nums_ when adding a state.
virtual AlphabetState & getStateAt(size_t stateIndex)
Get a state at a position in the alphabet_ vector.
const std::vector< std::string > & getResolvedChars() const
unsigned int getNumberOfChars() const
Get the number of supported characters in this alphabet, including generic characters (e....
AbstractAlphabet & operator=(const AbstractAlphabet &alph)
const AlphabetState & getState(const std::string &letter) const
Get a state by its letter.
virtual void setState(size_t pos, AlphabetState *st)
Set a state in the Alphabet.
std::string intToChar(int state) const
Give the string description of a state given its int description.
const std::vector< std::string > & getSupportedChars() const
std::map< int, size_t > nums_
bool isGap(const std::string &state) const
size_t getNumberOfStates() const
This is a convenient alias for getNumberOfChars(), returning a size_t instead of unsigned int.
size_t getStateIndex(int state) const
virtual void registerState(AlphabetState *st)
Add a state to the Alphabet.
bool isGap(int state) const
int charToInt(const std::string &state) const
Give the int description of a state given its string description.
int getGapCharacterCode() const
unsigned int getStateCodingSize() const
Get the size of the string coding a state.
std::vector< std::string > charList_
std::vector< int > intList_
bool isResolvedIn(int state1, int state2) const
Tells if a given (potentially unresolved) state can be resolved in another resolved state.
int getGeneric(const std::vector< int > &states) const
Get the generic state that match a set of states.
std::map< std::string, size_t > letters_
void remap()
Re-update the maps using the alphabet_ vector content.
std::vector< int > getAlias(int state) const
Get all resolved states that match a generic state.
const std::string & getCharCodeAt(size_t stateIndex) const
bool isIntInAlphabet(int state) const
Tell if a state (specified by its int description) is allowed by the the alphabet.
bool isCharInAlphabet(const std::string &state) const
Tell if a state (specified by its string description) is allowed by the the alphabet.
This is the base class to describe states in an Alphabet.
Definition: AlphabetState.h:22
const std::string & getLetter() const
Get the letter(s) corresponding to the state.
Definition: AlphabetState.h:63
int getNum() const
Get the state's number.
Definition: AlphabetState.h:47
The Alphabet interface.
Definition: Alphabet.h:99
virtual std::string getAlphabetType() const =0
Identification method.
This alphabet is used to deal NumericAlphabet.