bpp-seq3  3.0.0
BinaryAlphabet.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 "AlphabetState.h"
6 #include "BinaryAlphabet.h"
7 #include "AlphabetExceptions.h"
8 
9 // From Utils:
10 #include <Bpp/Text/TextTools.h>
11 
12 using namespace bpp;
13 
15 {
16  // Alphabet content definition
17  registerState(new AlphabetState(-1, "-", "Gap"));
18  for (int i = 0; i < 2; i++)
19  {
21  }
22  registerState(new AlphabetState(2, "?", "Unresolved state"));
23 }
24 
25 
26 /******************************************************************************/
27 bool BinaryAlphabet::isResolvedIn(int state1, int state2) const
28 {
29  if (state1 < 0 || !isIntInAlphabet(state1))
30  throw BadIntException(state1, "BinaryAlphabet::isResolvedIn(int, int): Specified base " + intToChar(state1) + " is unknown.", this);
31 
32  if (state2 < 0 || !isIntInAlphabet(state2))
33  throw BadIntException(state2, "BinaryAlphabet::isResolvedIn(int, int): Specified base " + intToChar(state2) + " is unknown.", this);
34 
35  if (isUnresolved(state2))
36  throw BadIntException(state2, "BinaryAlphabet::isResolvedIn(int, int): Unresolved base " + intToChar(state2), this);
37 
38  return (state1 == 2) || (state1 == state2);
39 }
40 
41 /******************************************************************************/
42 
43 std::vector<int> BinaryAlphabet::getAlias(int state) const
44 {
45  if (!isIntInAlphabet(state))
46  throw BadIntException(state, "BinaryAlphabet::getAlias(int): Specified base unknown.", this);
47  std::vector<int> v;
48  switch (state)
49  {
50  case -1:
51  v.push_back(-1);
52  break;
53  case 0:
54  v.push_back(0);
55  break;
56  case 1:
57  v.push_back(1);
58  break;
59  case 2:
60  v.push_back(0);
61  v.push_back(1);
62  break;
63  }
64 
65  return v;
66 }
67 
68 /******************************************************************************/
69 
70 std::vector<std::string> BinaryAlphabet::getAlias(const std::string& state) const
71 {
72  if (!isCharInAlphabet(state))
73  throw BadCharException(state, "BinaryAlphabet::getAlias(char): Specified base unknown.", this);
74 
75  std::vector<std::string> v(1);
76  if (state == "?")
77  {
78  v.push_back("0");
79  v.push_back("1");
80  }
81  else
82  v.push_back(state);
83 
84  return v;
85 }
std::string intToChar(int state) const
Give the string description of a state given its int description.
virtual void registerState(AlphabetState *st)
Add a state to the Alphabet.
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
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.
bool isUnresolved(int state) const
bool isResolvedIn(int state1, int state2) const
Tells if a given (potentially unresolved) state can be resolved in another resolved state.
std::vector< int > getAlias(int state) const
Get all resolved states that match a generic state.
std::string toString(T t)
This alphabet is used to deal NumericAlphabet.