bpp-seq3  3.0.0
CaseMaskedAlphabet.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include <Bpp/Text/TextTools.h>
6 
7 #include "CaseMaskedAlphabet.h"
8 
9 using namespace bpp;
10 
11 // From the STL:
12 #include <vector>
13 #include <iostream>
14 
15 using namespace std;
16 
17 CaseMaskedAlphabet::CaseMaskedAlphabet(std::shared_ptr<const LetterAlphabet> nocaseAlphabet) :
18  LetterAlphabet(true),
19  nocaseAlphabet_(nocaseAlphabet)
20 {
21  vector<string> chars = nocaseAlphabet_->getSupportedChars();
22  for (size_t i = 0; i < chars.size(); ++i)
23  {
24  AlphabetState* state = nocaseAlphabet_->getState(chars[i]).clone();
25  registerState(state);
26  char c = *chars[i].c_str();
27  if (isalpha(c))
28  {
29  if (isupper(c))
30  {
31  registerState(new AlphabetState(state->getNum() + 100, TextTools::toLower(state->getLetter()), string("Masked ") + state->getName()));
32  }
33  }
34  }
35 }
36 
38 {
39  if (!isIntInAlphabet(state))
40  throw BadIntException(state, "CaseMaskedAlphabet::getMaskedEquivalentState. Unsupported state code.", this);
41  if (state >= 100)
42  return state;
43  else
44  {
45  state += 100;
46  if (!isIntInAlphabet(state))
47  throw BadIntException(state, "CaseMaskedAlphabet::getMaskedEquivalentState. State has masked equivalent.", this);
48  return state;
49  }
50 }
51 
52 const string CaseMaskedAlphabet::getMaskedEquivalentState(const string& state) const
53 {
54  if (!isCharInAlphabet(state))
55  throw BadCharException(state, "CaseMaskedAlphabet::getMaskedEquivalentState. Unsupported state code.", this);
56  int code = charToInt(state);
57  if (code >= 100)
58  return state;
59  else
60  {
61  code += 100;
62  if (!isIntInAlphabet(code))
63  throw BadIntException(code, "CaseMaskedAlphabet::getMaskedEquivalentState. State has masked equivalent.", this);
64  return intToChar(code);
65  }
66 }
std::string intToChar(int state) const
Give the string description of a state given its int description.
bool isIntInAlphabet(int state) const
Tell if a state (specified by its int 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 & getName() const
Get the name of the state.
Definition: AlphabetState.h:76
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.
An alphabet exception thrown when trying to specify a bad int to the alphabet.
std::shared_ptr< const LetterAlphabet > nocaseAlphabet_
int getMaskedEquivalentState(int state) const
Get the masked state equivalent to the input one.
CaseMaskedAlphabet(std::shared_ptr< const LetterAlphabet > nocaseAlphabet)
Specialized partial implementation of Alphabet using single letters.
void registerState(AlphabetState *st)
Add a state to the Alphabet.
bool isCharInAlphabet(char state) const
int charToInt(const std::string &state) const
Give the int description of a state given its string description.
std::string toLower(const std::string &s)
This alphabet is used to deal NumericAlphabet.