bpp-seq3  3.0.0
NumericAlphabet.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 #include <iostream>
7 #include <typeinfo>
8 
9 #include "NumericAlphabet.h" // class's header file
10 
11 using namespace std;
12 using namespace bpp;
13 
14 /****************************************************************************************/
15 
16 NumericAlphabet::NumericAlphabet(const UniformDiscreteDistribution& pd) :
17  AbstractAlphabet(), pdd_(pd.clone()), values_()
18 {
19  // Alphabet size definition
20  size_t size = pdd_->getNumberOfCategories();
21 
22  Vdouble vd = pdd_->getCategories();
23 
24  for (size_t i = 0; i < size; ++i)
25  {
26  registerState(new AlphabetNumericState(static_cast<int>(i), vd[i], TextTools::toString(vd[i]), TextTools::toString(vd[i])));
27  }
28 }
29 
31  AbstractAlphabet(na),
32  pdd_(na.pdd_->clone()),
33  values_(na.values_)
34 {}
35 
37 {
39  pdd_ = na.pdd_->clone();
40  return *this;
41 }
42 
43 /****************************************************************************************/
44 
45 void NumericAlphabet::setState(size_t stateIndex, AlphabetState* st)
46 {
47  try
48  {
49  AbstractAlphabet::setState(stateIndex, st);
50  double x = dynamic_cast<AlphabetNumericState*>(st)->getValue();
51  if (values_.find(x) == values_.end())
52  values_[x] = stateIndex;
53  }
54  catch (std::bad_cast&)
55  {
56  throw Exception("NumericAlphabet::setState. Incorrect alphabet type.");
57  }
58 }
59 
61 {
62  try
63  {
65  double x = dynamic_cast<AlphabetNumericState*>(st)->getValue();
66  if (values_.find(x) == values_.end())
67  values_[x] = getSize();
68  }
69  catch (std::bad_cast&)
70  {
71  throw Exception("NumericAlphabet::registerState. Incorrect alphabet type.");
72  }
73 }
74 
75 vector<int> NumericAlphabet::getAlias(int state) const
76 {
77  vector<int> v(1);
78  v[0] = state;
79  return v;
80 }
81 
82 vector<string> NumericAlphabet::getAlias(const string& state) const
83 {
84  vector<string> v(1);
85  v[0] = state;
86  return v;
87 }
88 
89 /****************************************************************************************/
90 
91 bool NumericAlphabet::isGap(int state) const
92 {
93  return false;
94 }
95 
96 bool NumericAlphabet::containsGap(const string& state) const
97 {
98  return false;
99 }
100 
101 bool NumericAlphabet::isUnresolved(const string& state) const
102 {
103  return false;
104 }
105 
106 bool NumericAlphabet::isUnresolved(int state) const
107 {
108  return false;
109 }
110 
111 unsigned int NumericAlphabet::getSize() const
112 {
113  return static_cast<unsigned int>(values_.size());
114 }
115 
117 {
118  return static_cast<unsigned int>(values_.size());
119 }
120 
122 {
124  values_.clear();
125  for (size_t pos = 0; pos < getSize(); pos++)
126  {
127  double x = getStateAt(pos).getValue();
128  if (values_.find(x) == values_.end())
129  values_[x] = pos;
130  }
131 }
132 
133 /************************************************************/
134 
136 {
137  return (pdd_->getUpperBound() - pdd_->getLowerBound()) / static_cast<double>(pdd_->getNumberOfCategories());
138 }
139 
140 double NumericAlphabet::intToValue(int state) const
141 {
142  return static_cast<const AlphabetNumericState& >(getState(state)).getValue();
143 }
144 
145 size_t NumericAlphabet::getValueIndex(double value) const
146 {
147  map<double, size_t>::const_iterator it = values_.find(pdd_->getValueCategory(value));
148  return it->second;
149 }
150 
152 {
153  return static_cast<AlphabetNumericState&>(AbstractAlphabet::getStateAt(stateIndex));
154 }
155 
156 const AlphabetNumericState& NumericAlphabet::getStateAt(size_t stateIndex) const
157 {
158  return static_cast<const AlphabetNumericState&>(AbstractAlphabet::getStateAt(stateIndex));
159 }
A partial implementation of the Alphabet interface.
virtual AlphabetState & getStateAt(size_t stateIndex)
Get a state at a position in the alphabet_ vector.
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.
virtual void registerState(AlphabetState *st)
Add a state to the Alphabet.
void remap()
Re-update the maps using the alphabet_ vector content.
double getValueCategory(double value) const
States that do have a double value.
double getValue() const
Get the state value.
This is the base class to describe states in an Alphabet.
Definition: AlphabetState.h:22
double intToValue(int state) const
Returns the value for the character number.
NumericAlphabet & operator=(const NumericAlphabet &)
double getDelta() const
Returns the difference between successive values.
bool containsGap(const std::string &state) const
unsigned int getSize() const
Get the number of resolved states in the alphabet (e.g. return 4 for DNA alphabet)....
const UniformDiscreteDistribution * pdd_
AlphabetNumericState & getStateAt(size_t stateIndex)
Get a state at a position in the alphabet_ vector.
std::map< double, size_t > values_
void setState(size_t pos, AlphabetState *st)
Set a state in the Alphabet.
void registerState(AlphabetState *st)
Add a state to the Alphabet.
void remap()
Re-update the maps.
bool isUnresolved(int state) const
size_t getValueIndex(double value) const
Returns the CategoryIndex of the category to which the value belongs.
unsigned int getNumberOfTypes() const
Get the number of distinct states in alphabet (e.g. return 15 for DNA alphabet). This is the number o...
std::vector< int > getAlias(int state) const
Get all resolved states that match a generic state.
NumericAlphabet(const UniformDiscreteDistribution &)
bool isGap(int state) const
UniformDiscreteDistribution * clone() const
std::string toString(T t)
This alphabet is used to deal NumericAlphabet.
std::vector< double > Vdouble