bpp-seq3  3.0.0
LetterAlphabet.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_LETTERALPHABET_H
6 #define BPP_SEQ_ALPHABET_LETTERALPHABET_H
7 
8 
9 // From the STL
10 #include <string>
11 #include <vector>
12 #include <iostream>
13 
14 // From Seq
15 #include "AbstractAlphabet.h"
16 #include "AlphabetExceptions.h"
17 
18 namespace bpp
19 {
26  public AbstractAlphabet
27 {
28 private:
29  static const int LETTER_UNDEF_VALUE;
30  std::vector<int> letters_;
32 
33 public:
34  LetterAlphabet(bool caseSensitive = false) : letters_(256, LETTER_UNDEF_VALUE), caseSensitive_(caseSensitive) {}
35 
37 
39  {
41  letters_ = bia.letters_;
43 
44  return *this;
45  }
46 
47  virtual LetterAlphabet* clone() const = 0;
48 
49  virtual ~LetterAlphabet() {}
50 
51 public:
52  bool isCharInAlphabet(char state) const
53  {
54  return letters_[static_cast<size_t>(state)] != LETTER_UNDEF_VALUE;
55  }
56  bool isCharInAlphabet(const std::string& state) const
57  {
58  return isCharInAlphabet(state[0]);
59  }
60  int charToInt(const std::string& state) const
61  {
62  if (!isCharInAlphabet(state))
63  throw BadCharException(state, "LetterAlphabet::charToInt: Unknown state", this);
64  return letters_[static_cast<unsigned int>(state[0])];
65  }
66 
67 protected:
69  {
71  if (caseSensitive_)
72  {
73  letters_[static_cast<size_t>(st->getLetter()[0])] = st->getNum();
74  }
75  else
76  {
77  letters_[static_cast<size_t>(tolower(st->getLetter()[0]))] = st->getNum();
78  letters_[static_cast<size_t>(toupper(st->getLetter()[0]))] = st->getNum();
79  }
80  }
81 
82  void setState(size_t pos, AlphabetState* st)
83  {
85  if (caseSensitive_)
86  {
87  letters_[static_cast<size_t>(st->getLetter()[0])] = st->getNum();
88  }
89  else
90  {
91  letters_[static_cast<size_t>(tolower(st->getLetter()[0]))] = st->getNum();
92  letters_[static_cast<size_t>(toupper(st->getLetter()[0]))] = st->getNum();
93  }
94  }
95 };
96 }
97 #endif // BPP_SEQ_ALPHABET_LETTERALPHABET_H
A partial implementation of the Alphabet interface.
AbstractAlphabet & operator=(const AbstractAlphabet &alph)
virtual void setState(size_t pos, AlphabetState *st)
Set a state in the Alphabet.
virtual void registerState(AlphabetState *st)
Add a state to 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
An alphabet exception thrown when trying to specify a bad char to the alphabet.
Specialized partial implementation of Alphabet using single letters.
void registerState(AlphabetState *st)
Add a state to the Alphabet.
LetterAlphabet & operator=(const LetterAlphabet &bia)
static const int LETTER_UNDEF_VALUE
std::vector< int > letters_
bool isCharInAlphabet(const std::string &state) const
Tell if a state (specified by its string description) is allowed by the the alphabet.
void setState(size_t pos, AlphabetState *st)
Set a state in the Alphabet.
virtual ~LetterAlphabet()
bool isCharInAlphabet(char state) const
LetterAlphabet(bool caseSensitive=false)
virtual LetterAlphabet * clone() const =0
LetterAlphabet(const LetterAlphabet &bia)
int charToInt(const std::string &state) const
Give the int description of a state given its string description.
This alphabet is used to deal NumericAlphabet.