bpp-seq  2.2.0
NumericAlphabet.cpp
Go to the documentation of this file.
1 //
2 // File: NumericAlphabet.cpp
3 // Created by: Laurent Gueguen
4 // Created on: March 2010
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
9 
10  This software is a computer program whose purpose is to provide
11  classes for sequences analysis.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided
21  only with a limited warranty and the software's author, the holder of
22  the economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards
32  their requirements in conditions enabling the security of their
33  systems and/or data to be ensured and, more generally, to use and
34  operate it in the same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38  */
39 
40 #include "NumericAlphabet.h" // class's header file
41 
42 #include <Bpp/Text/TextTools.h>
43 
44 #include <iostream>
45 #include <typeinfo>
46 
47 using namespace std;
48 using namespace bpp;
49 
50 /****************************************************************************************/
51 
52 NumericAlphabet::NumericAlphabet(const UniformDiscreteDistribution& pd) :
53  AbstractAlphabet(), pdd_(pd.clone()), values_()
54 {
55 
56  // Alphabet size definition
57  size_t size = pdd_->getNumberOfCategories();
58 
59  Vdouble vd = pdd_->getCategories();
60 
61  for (size_t i = 0; i < size; ++i){
62  registerState(new AlphabetNumericState(static_cast<int>(i), vd[i], TextTools::toString(vd[i]), TextTools::toString(vd[i])));
63  }
64 }
65 
67  AbstractAlphabet(na),
68  pdd_(na.pdd_->clone()),
69  values_(na.values_)
70 {}
71 
73 {
75  pdd_ = na.pdd_->clone();
76  return *this;
77 }
78 
79 /****************************************************************************************/
80 
81 void NumericAlphabet::setState(size_t stateIndex, AlphabetState* st) throw (Exception)
82 {
83  try {
84  AbstractAlphabet::setState(stateIndex, st);
85  double x = dynamic_cast<AlphabetNumericState*>(st)->getValue();
86  if (values_.find(x) == values_.end())
87  values_[x] = stateIndex;
88  } catch(std::bad_cast&) {
89  throw Exception("NumericAlphabet::setState. Incorrect alphabet type.");
90  }
91 }
92 
93 void NumericAlphabet::registerState(AlphabetState* st) throw (Exception)
94 {
95  try {
97  double x = dynamic_cast<AlphabetNumericState*>(st)->getValue();
98  if (values_.find(x) == values_.end())
99  values_[x] = getSize();
100  } catch(std::bad_cast&) {
101  throw Exception("NumericAlphabet::registerState. Incorrect alphabet type.");
102  }
103 }
104 
105 
106 vector<int> NumericAlphabet::getAlias(int state) const throw (BadIntException)
107 {
108  vector<int> v;
109  v.push_back(state);
110  return v;
111 }
112 
113 vector<string> NumericAlphabet::getAlias(const string& state) const throw (BadCharException)
114 {
115  vector<string> v;
116  v.push_back(state);
117  return v;
118 }
119 
120 /****************************************************************************************/
121 
122 bool NumericAlphabet::isGap(int state) const
123 {
124  return false;
125 }
126 
127 bool NumericAlphabet::containsGap(const string& state) const throw (BadCharException)
128 {
129  return false;
130 }
131 
132 bool NumericAlphabet::isUnresolved(const string& state) const
133 {
134  return false;
135 }
136 
137 bool NumericAlphabet::isUnresolved(int state) const
138 {
139  return false;
140 }
141 
142 unsigned int NumericAlphabet::getSize() const
143 {
144  return static_cast<unsigned int>(values_.size());
145 }
146 
148 {
149  return static_cast<unsigned int>(values_.size());
150 }
151 
153 {
155  values_.clear();
156  for (size_t pos = 0 ; pos < getSize() ; pos++) {
157  double x = getStateAt(pos).getValue();
158  if (values_.find(x) == values_.end())
159  values_[x] = pos;
160  }
161 }
162 
163 /************************************************************/
164 
166 {
167  return (pdd_->getUpperBound() - pdd_->getLowerBound()) / static_cast<double>(pdd_->getNumberOfCategories());
168 }
169 
170 double NumericAlphabet::intToValue(int state) const throw (BadIntException)
171 {
172  return static_cast<const AlphabetNumericState& >(getState(state)).getValue();
173 }
174 
175 size_t NumericAlphabet::getValueIndex(double value) const
176 {
177  map<double, size_t>::const_iterator it = values_.find(pdd_->getValueCategory(value));
178  return it->second;
179 }
180 
181 AlphabetNumericState& NumericAlphabet::getStateAt(size_t stateIndex) throw (IndexOutOfBoundsException)
182 {
183  return static_cast<AlphabetNumericState&>(AbstractAlphabet::getStateAt(stateIndex));
184 }
185 
186 const AlphabetNumericState& NumericAlphabet::getStateAt(size_t stateIndex) const throw (IndexOutOfBoundsException)
187 {
188  return static_cast<const AlphabetNumericState&>(AbstractAlphabet::getStateAt(stateIndex));
189 }
This is the base class to describe states in an Alphabet.
Definition: AlphabetState.h:54
bool isUnresolved(int state) const
An alphabet exception thrown when trying to specify a bad char to the alphabet.
unsigned int getNumberOfTypes() const
Get the number of distinct states in alphabet (e.g. return 15 for DNA alphabet). This is the number o...
bool isGap(int state) const
This alphabet is used to deal NumericAlphabet.
unsigned int getSize() const
Get the number of resolved states in the alphabet (e.g. return 4 for DNA alphabet). This is the method you&#39;ll need in most cases.
double intToValue(int state) const
Returns the value for the character number.
STL namespace.
std::map< double, size_t > values_
double getDelta() const
Returns the difference between successive values.
const UniformDiscreteDistribution * pdd_
bool containsGap(const std::string &state) const
AbstractAlphabet & operator=(const AbstractAlphabet &alph)
void remap()
Re-update the maps.
NumericAlphabet & operator=(const NumericAlphabet &)
NumericAlphabet(const UniformDiscreteDistribution &)
virtual void setState(size_t pos, AlphabetState *st)
Set a state in the Alphabet.
void setState(size_t pos, AlphabetState *st)
Set a state in the Alphabet.
void registerState(AlphabetState *st)
Add a state to the Alphabet.
A partial implementation of the Alphabet interface.
void remap()
Re-update the maps using the alphabet_ vector content.
AlphabetNumericState & getStateAt(size_t stateIndex)
Get a state at a position in the alphabet_ vector.
An alphabet exception thrown when trying to specify a bad int to the alphabet.
States that do have a double value.
virtual AlphabetState & getStateAt(size_t stateIndex)
Get a state at a position in the alphabet_ vector.
virtual void registerState(AlphabetState *st)
Add a state to the Alphabet.
size_t getValueIndex(double value) const
Returns the CategoryIndex of the category to which the value belongs.
std::vector< int > getAlias(int state) const
Get all resolved states that match a generic state.
double getValue() const
Get the state value.