bpp-seq3  3.0.0
AllelicAlphabet.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_SEQ_ALPHABET_ALLELICALPHABET_H
6 #define BPP_SEQ_ALPHABET_ALLELICALPHABET_H
7 
8 #include "AbstractAlphabet.h"
9 
10 // From the STL:
11 #include <string>
12 #include <vector>
13 
14 #include "../Transliterator.h"
16 
17 namespace bpp
18 {
50  public AbstractAlphabet
51 {
52 protected:
53  std::shared_ptr<const Alphabet> alph_;
54 
58  unsigned int nbAlleles_;
59 
64 
65 public:
66  // Constructor and destructor.
70  AllelicAlphabet(std::shared_ptr<const Alphabet> alph, unsigned int nbAlleles);
71 
73  AbstractAlphabet(bia),
74  alph_(bia.alph_),
77  {}
78 
79  AllelicAlphabet* clone() const override
80  {
81  return new AllelicAlphabet(*this);
82  }
83 
84  virtual ~AllelicAlphabet() {}
85 
86 public:
92  int charToInt(const std::string& state) const override
93  {
94  if (state.size() != getStateCodingSize())
95  throw BadCharException(state, "AllelicAlphabet::charToInt", this);
96  else
97  return AbstractAlphabet::charToInt(state);
98  }
99 
100  unsigned int getSize() const override
101  {
102  return getNumberOfChars() - 2;
103  }
104 
105  unsigned int getNumberOfTypes() const override
106  {
107  return getNumberOfChars() - 1;
108  }
109 
113  bool isResolvedIn(int state1, int state2) const override;
114 
119  unsigned int getNbAlleles() const
120  {
121  return nbAlleles_;
122  }
123 
127  std::shared_ptr<const Alphabet> getStateAlphabet() const
128  {
129  return alph_;
130  }
131 
132  const Alphabet& stateAlphabet() const
133  {
134  return *alph_;
135  }
136 
137  std::string getAlphabetType() const override;
138 
139  int getUnknownCharacterCode() const override
140  {
141  return nbUnknown_;
142  }
143 
144  bool isUnresolved(int state) const override { return state == getUnknownCharacterCode(); }
145 
146  bool isUnresolved(const std::string& state) const override { return charToInt(state) == getUnknownCharacterCode(); }
147 
148  std::vector<int> getAlias(int state) const override;
149 
150  std::vector<std::string> getAlias(const std::string& state) const override;
151 
152  int getGeneric(const std::vector<int>& states) const override
153  {
154  return states[0];
155  }
156 
157  std::string getGeneric(const std::vector<std::string>& states) const override
158  {
159  return states[0];
160  }
161 
163  {
164 private:
165  std::shared_ptr<const AllelicAlphabet> alph_;
166 
167 public:
168  AllelicTransliterator(std::shared_ptr<const AllelicAlphabet> alph) :
170  {}
171 
172  std::shared_ptr<const Alphabet> getSourceAlphabet() const override
173  {
174  return alph_->getStateAlphabet();
175  }
176  std::shared_ptr<const Alphabet> getTargetAlphabet() const override { return alph_; }
177 
178  std::string translate(const std::string& state) const override
179  {
180  return getTargetAlphabet()->intToChar(getSourceAlphabet()->charToInt(state));
181  }
182 
187  int translate(int state) const override
188  {
189  return state;
190  }
191 
192  std::unique_ptr<Sequence> translate(const SequenceInterface& sequence) const override
193  {
194  return AbstractTransliterator::translate(sequence);
195  }
196  };
197 
208  std::unique_ptr<ProbabilisticSequence> convertFromStateAlphabet(const CoreSequenceInterface& sequence) const;
209 
215  void computeLikelihoods(const Vdouble& counts, Vdouble& likelihoods) const;
216 
217 private:
223  // bool containsUnresolved(const std::string& state) const;
224  // bool containsGap(const std::string& state) const;
225 
232  unsigned int getStateCodingSize() const override
233  {
234  auto x = 2 * ((unsigned int)alph_->getStateCodingSize() + (unsigned int)std::to_string(nbAlleles_).size());
235  return x;
236  }
237 
239 };
240 } // end of namespace bpp.
241 
242 #endif // BPP_SEQ_ALPHABET_ALLELICALPHABET_H
A partial implementation of the Alphabet interface.
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.
Partial implementation of the Transliterator interface.
int translate(int state) const override=0
Translate a given state coded as a int from source alphabet to target alphabet.
AllelicTransliterator(std::shared_ptr< const AllelicAlphabet > alph)
std::unique_ptr< Sequence > translate(const SequenceInterface &sequence) const override
Translate a whole sequence from source alphabet to target alphabet.
int translate(int state) const override
States of the original alphabet are the first ones of the allelic alphabet.
std::shared_ptr< const Alphabet > getTargetAlphabet() const override
Get the target alphabet.
std::shared_ptr< const Alphabet > getSourceAlphabet() const override
Get the source alphabet.
std::string translate(const std::string &state) const override
Translate a given state coded as a string from source alphabet to target alphabet.
std::shared_ptr< const AllelicAlphabet > alph_
The base class for allelic alphabets.
unsigned int nbAlleles_
the number of alleles.
AllelicAlphabet * clone() const override
const Alphabet & stateAlphabet() const
unsigned int getNumberOfTypes() const override
Get the number of distinct states in alphabet (e.g. return 15 for DNA alphabet). This is the number o...
std::unique_ptr< ProbabilisticSequence > convertFromStateAlphabet(const CoreSequenceInterface &sequence) const
Convert a CoreSequence in StateAlphabet to a ProbabilisticSequence of the likelihoods of the di-allel...
std::shared_ptr< const Alphabet > getStateAlphabet() const
Returns Base Alphabet.
void computeLikelihoods(const Vdouble &counts, Vdouble &likelihoods) const
Fills the vector of the likelihoods of a vector of counts in the states alphabet, given the di-allele...
std::string getGeneric(const std::vector< std::string > &states) const override
Get the generic state that match a set of states.
std::string getAlphabetType() const override
Identification method.
int getUnknownCharacterCode() const override
int nbUnknown_
the unknown state number
std::shared_ptr< const Alphabet > alph_
std::vector< int > getAlias(int state) const override
Get all resolved states that match a generic state.
unsigned int getStateCodingSize() const override
Get the size of the string coding a state.
AllelicAlphabet(std::shared_ptr< const Alphabet > alph, unsigned int nbAlleles)
Builds a new word alphabet from an Alphabet.
bool isUnresolved(int state) const override
AllelicAlphabet(const AllelicAlphabet &bia)
unsigned int getNbAlleles() const
Returns the number of alleles.
int charToInt(const std::string &state) const override
Give the int description of a state given its string description.
bool isResolvedIn(int state1, int state2) const override
Tells if a given (potentially unresolved) state can be resolved in another resolved state.
bool isUnresolved(const std::string &state) const override
unsigned int getSize() const override
Get the number of resolved states in the alphabet (e.g. return 4 for DNA alphabet)....
int getGeneric(const std::vector< int > &states) const override
Get the generic state that match a set of states.
The Alphabet interface.
Definition: Alphabet.h:99
An alphabet exception thrown when trying to specify a bad char to the alphabet.
The core sequence interface.
Definition: CoreSequence.h:28
The sequence interface.
Definition: Sequence.h:34
This alphabet is used to deal NumericAlphabet.
std::vector< double > Vdouble