bpp-seq3  3.0.0
LexicalAlphabet.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 "LexicalAlphabet.h"
8 #include "AlphabetExceptions.h"
9 
10 using namespace bpp;
11 
12 // From the STL:
13 #include <iostream>
14 
15 using namespace std;
16 
17 LexicalAlphabet::LexicalAlphabet(const vector<std::string>& vocab) :
19 {
20  if (vocab.size() == 0)
21  throw Exception("LexicalAlphabet::LexicalAlphabet: not constructible from empty vocabulary.");
22 
23  size_t t = vocab[0].size();
24 
25  string s = "";
26  for (size_t i = 0; i < t; i++)
27  {
28  s += "-";
29  }
30 
31  registerState(new AlphabetState(-1, s, "gap"));
32 
33 
34  for (size_t i = 0; i < vocab.size(); ++i)
35  {
36  if (t != vocab[i].size())
37  throw Exception("LexicalAlphabet: several lengths in vocabulary.");
38 
39  try
40  {
41  string s2 = getName(vocab[i]);
42  throw Exception("LexicalAlphabet : " + vocab[i] + " defined twice.");
43  }
44  catch (BadCharException& e)
45  {}
46 
47  registerState(new AlphabetState(static_cast<int>(i), vocab[i], vocab[i]));
48  }
49 
50  s = "";
51  for (size_t i = 0; i < t; i++)
52  {
53  s += "?";
54  }
55 
56  registerState(new AlphabetState(static_cast<int>(vocab.size()), s, "Unresolved word"));
57 }
58 
59 
60 /******************************************************************************/
61 
63 {
64  string s = "Lexicon(";
65 
66  for (size_t i = 1; i < getNumberOfStates() - 1; i++)
67  {
68  if (i != 1)
69  s += ",";
70 
71  s += getStateAt(i).getLetter();
72  }
73 
74  s += ")";
75 
76  return s;
77 }
A partial implementation of the Alphabet interface.
std::string getName(const std::string &state) const
Get the complete name of a state given its string description.
virtual AlphabetState & getStateAt(size_t stateIndex)
Get a state at a position in the alphabet_ vector.
size_t getNumberOfStates() const
This is a convenient alias for getNumberOfChars(), returning a size_t instead of unsigned int.
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
An alphabet exception thrown when trying to specify a bad char to the alphabet.
std::string getAlphabetType() const
Identification method.
LexicalAlphabet(const std::vector< std::string > &vocab)
Builds a new word alphabet from a vector of given words.
This alphabet is used to deal NumericAlphabet.