bpp-seq  2.2.0
NucleicAlphabet.h
Go to the documentation of this file.
1 //
2 // File: NucleicAlphabet.h
3 // Authors: Guillaume Deuchst
4 // Julien Dutheil
5 // Sylvain Gaillard
6 // Created on: Tue Jul 22 2003
7 //
8 
9 /*
10  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
11 
12  This software is a computer program whose purpose is to provide classes
13  for sequences analysis.
14 
15  This software is governed by the CeCILL license under French law and
16  abiding by the rules of distribution of free software. You can use,
17  modify and/ or redistribute the software under the terms of the CeCILL
18  license as circulated by CEA, CNRS and INRIA at the following URL
19  "http://www.cecill.info".
20 
21  As a counterpart to the access to the source code and rights to copy,
22  modify and redistribute granted by the license, users are provided only
23  with a limited warranty and the software's author, the holder of the
24  economic rights, and the successive licensors have only limited
25  liability.
26 
27  In this respect, the user's attention is drawn to the risks associated
28  with loading, using, modifying and/or developing or reproducing the
29  software by the user in light of its specific status of free software,
30  that may mean that it is complicated to manipulate, and that also
31  therefore means that it is reserved for developers and experienced
32  professionals having in-depth computer knowledge. Users are therefore
33  encouraged to load and test the software's suitability as regards their
34  requirements in conditions enabling the security of their systems and/or
35  data to be ensured and, more generally, to use and operate it in the
36  same conditions as regards security.
37 
38  The fact that you are presently reading this means that you have had
39  knowledge of the CeCILL license and that you accept its terms.
40 */
41 
42 #ifndef _NUCLEICALPHABET_H_
43 #define _NUCLEICALPHABET_H_
44 
45 #include "LetterAlphabet.h"
46 #include "NucleicAlphabetState.h"
47 
48 #include <map>
49 #include <iostream>
50 #include <typeinfo>
51 
52 namespace bpp
53 {
54 
62  public LetterAlphabet
63  {
64  private:
65  std::map<int, size_t> binCodes_;
66  void updateBinMaps_(size_t pos, const NucleicAlphabetState& st) {
67  if (binCodes_.find(st.getBinaryCode()) == binCodes_.end())
68  binCodes_[st.getBinaryCode()] = pos;
69  }
70 
71  public:
73 
75 
77  {
79  binCodes_ = bia.binCodes_;
80  return *this;
81  }
82 
83 #ifndef NO_VIRTUAL_COV
84  virtual NucleicAlphabet* clone() const = 0;
85 #endif
86 
87  virtual ~NucleicAlphabet() {}
88 
89  protected:
94  void registerState(AlphabetState* st) throw (Exception) {
95  NucleicAlphabetState* nst = dynamic_cast<NucleicAlphabetState*>(st);
96  if (!nst)
97  throw Exception("NucleicAlphabet::registerState. Incorrect alphabet type.");
100  }
101 
102  void setState(size_t pos, AlphabetState* st) throw (Exception, IndexOutOfBoundsException) {
103  NucleicAlphabetState* nst = dynamic_cast<NucleicAlphabetState*>(st);
104  if (!nst)
105  throw Exception("NucleicAlphabet::setState. Incorrect alphabet type.");
106  LetterAlphabet::setState(pos, nst);
107  updateBinMaps_(pos, *nst);
108  }
109 
112  public:
117  const NucleicAlphabetState& getStateAt(size_t stateIndex) const
118  throw (IndexOutOfBoundsException) {
119  return dynamic_cast<const NucleicAlphabetState&>(
120  AbstractAlphabet::getStateAt(stateIndex)
121  );
122  }
123  NucleicAlphabetState& getStateAt(size_t stateIndex)
124  throw (IndexOutOfBoundsException) {
125  return dynamic_cast<NucleicAlphabetState&>(
126  AbstractAlphabet::getStateAt(stateIndex)
127  );
128  }
129  const NucleicAlphabetState& getState(const std::string& letter) const
130  throw (BadCharException) {
131  return dynamic_cast<const NucleicAlphabetState&>(
133  );
134  }
135  const NucleicAlphabetState& getState(int num) const
136  throw (BadIntException) {
137  return dynamic_cast<const NucleicAlphabetState&>(
139  );
140  }
157  throw (BadIntException) {
158  std::map<int, size_t>::const_iterator it = binCodes_.find(code);
159  if (it == binCodes_.end())
160  throw BadIntException(code, "NucleicAlphabet::getState(unsigned char): Binary code not in alphabet", this);
161  return getStateAt(it->second);
162  }
163 
185  int subtract(int s1, int s2) const throw (BadIntException) {
186  return getStateByBinCode(getState(s1).getBinaryCode() & ~ getState(s2).getBinaryCode()).getNum();
187  }
188 
209  std::string subtract(const std::string& s1, const std::string& s2) const throw (BadCharException) {
210  return intToChar(subtract(charToInt(s1), charToInt(s2)));
211  }
212 
234  int getOverlap(int s1, int s2) const throw (BadIntException) {
235  return getStateByBinCode(getState(s1).getBinaryCode() & getState(s2).getBinaryCode()).getNum();
236  }
237 
258  std::string getOverlap(const std::string& s1, const std::string& s2) const throw (BadCharException) {
259  return intToChar(getOverlap(charToInt(s1), charToInt(s2)));
260  }
261 
264  public:
265  // return 4 : A, C, G, T (or U)
266  unsigned int getSize() const { return 4; }
267 
268  // return 15 : gap isn't included, generic unresolved bases (N, X, ?, O, 0) count for one
269  unsigned int getNumberOfTypes() const { return 15; }
270 
271  int getUnknownCharacterCode() const { return 14; }
272 
273  bool isUnresolved(int state) const { return state > 3; }
274  bool isUnresolved(const std::string& state) const { return charToInt(state) > 3; }
275 
276  };
277 
278 } //end of namespace bpp.
279 
280 #endif // _NUCLEICALPHABET_H_
281 
LetterAlphabet & operator=(const LetterAlphabet &bia)
This is the base class to describe states in an Alphabet.
Definition: AlphabetState.h:54
int getUnknownCharacterCode() const
An alphabet exception thrown when trying to specify a bad char to the alphabet.
void updateBinMaps_(size_t pos, const NucleicAlphabetState &st)
NucleicAlphabet & operator=(const NucleicAlphabet &bia)
bool isUnresolved(const std::string &state) const
int getNum() const
Get the state&#39;s number.
Definition: AlphabetState.h:83
unsigned int getNumberOfTypes() const
Get the number of distinct states in alphabet (e.g. return 15 for DNA alphabet). This is the number o...
This alphabet is used to deal NumericAlphabet.
int getBinaryCode() const
Get the state&#39;s binary representation.
const NucleicAlphabetState & getStateAt(size_t stateIndex) const
Get a state at a position in the alphabet_ vector.
virtual NucleicAlphabet * clone() const =0
void registerState(AlphabetState *st)
Add a state to the Alphabet.
std::string subtract(const std::string &s1, const std::string &s2) const
Subtract states.
Specialized partial implementation of Alphabet using single letters.
void setState(size_t pos, AlphabetState *st)
Set a state in the Alphabet.
const AlphabetState & getState(const std::string &letter) const
Get a state by its letter.
NucleicAlphabetState & getStateAt(size_t stateIndex)
Get a state at a position in the alphabet_ vector.
std::string getOverlap(const std::string &s1, const std::string &s2) const
Get the overlap between to states.
void registerState(AlphabetState *st)
Add a state to the Alphabet.
bool isUnresolved(int state) const
NucleicAlphabet(const NucleicAlphabet &bia)
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.
This is the base class to describe states in a NucleicAlphabet.
std::map< int, size_t > binCodes_
int subtract(int s1, int s2) const
Subtract states.
void setState(size_t pos, AlphabetState *st)
Set a state in the Alphabet.
int getOverlap(int s1, int s2) const
Get the overlap between to states.
const NucleicAlphabetState & getState(const std::string &letter) const
Get a state by its letter.
An alphabet exception thrown when trying to specify a bad int to the alphabet.
virtual AlphabetState & getStateAt(size_t stateIndex)
Get a state at a position in the alphabet_ vector.
const NucleicAlphabetState & getStateByBinCode(int code) const
Get a state by its binary representation.
const NucleicAlphabetState & getState(int num) const
Get a state by its num.
The abstract base class for nucleic alphabets.
std::string intToChar(int state) const
Give the string description of a state given its int description.
unsigned int getNumberOfChars() const
Get the number of supported characters in this alphabet, including generic characters (e...
int charToInt(const std::string &state) const
Give the int description of a state given its string description.