bpp-seq3  3.0.0
GeneticCode.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_GENETICCODE_GENETICCODE_H
6 #define BPP_SEQ_GENETICCODE_GENETICCODE_H
7 
8 #include <Bpp/Exceptions.h>
9 
10 #include "../Alphabet/AlphabetTools.h"
11 #include "../Alphabet/CodonAlphabet.h"
12 #include "../Alphabet/ProteicAlphabet.h"
13 #include "../Transliterator.h"
14 
15 namespace bpp
16 {
21  public virtual Exception
22 {
23 private:
24  std::string codon_;
25 
26 public:
27  // Class constructor
28  StopCodonException(const std::string& text, const std::string& codon);
29 
30  // Class destructor
31  virtual ~StopCodonException() {}
32 
33 public:
34  virtual const std::string& getCodon() const { return codon_; }
35 };
36 
47 class GeneticCode :
49  public virtual Clonable
50 {
51 protected:
52  std::shared_ptr<const CodonAlphabet> codonAlphabet_;
53  std::shared_ptr<const ProteicAlphabet> proteicAlphabet_;
54  std::map<int, int> tlnTable_;
55 
56 public:
57  GeneticCode(std::shared_ptr<const NucleicAlphabet> alphabet) :
59  codonAlphabet_(new CodonAlphabet(alphabet)),
60  proteicAlphabet_(AlphabetTools::PROTEIN_ALPHABET),
61  tlnTable_()
62  {}
63 
64  virtual ~GeneticCode() {}
65 
66  virtual GeneticCode* clone() const override = 0;
67 
68 public:
74  std::shared_ptr<const Alphabet> getSourceAlphabet() const override { return codonAlphabet_; }
75 
76  const Alphabet& sourceAlphabet() const override { return *codonAlphabet_; }
77 
78  std::shared_ptr<const Alphabet> getTargetAlphabet() const override { return proteicAlphabet_; }
79 
80  const Alphabet& targetAlphabet() const override { return *proteicAlphabet_; }
81 
82  int translate(int state) const override;
83 
84  std::string translate(const std::string& state) const override;
85 
86  std::unique_ptr<Sequence> translate(const SequenceInterface& sequence) const override
87  {
88  return AbstractTransliterator::translate(sequence);
89  }
92 public:
104  virtual std::shared_ptr<const CodonAlphabet> getCodonAlphabet() const { return codonAlphabet_; }
105 
111  virtual const CodonAlphabet& codonAlphabet() const { return *codonAlphabet_; }
112 
118  virtual std::shared_ptr<const ProteicAlphabet> getProteicAlphabet() const { return proteicAlphabet_; }
119 
125  virtual const ProteicAlphabet& proteicAlphabet() const { return *proteicAlphabet_; }
126 
130  virtual size_t getNumberOfStopCodons() const = 0;
131 
135  virtual std::vector<int> getStopCodonsAsInt() const = 0;
136 
140  virtual std::vector<std::string> getStopCodonsAsChar() const = 0;
141 
148  virtual bool isStop(int state) const = 0;
149 
156  virtual bool isStop(const std::string& state) const = 0;
157 
164  virtual bool isStart(int state) const
165  {
166  // Test:
167  codonAlphabet_->intToChar(state); // throw exception if invalid state!
168  return state == 14;
169  }
170 
177  virtual bool isStart(const std::string& state) const
178  {
179  return isStart(codonAlphabet_->charToInt(state));
180  }
181 
188  virtual bool isAltStart(int state) const = 0;
189 
196  virtual bool isAltStart(const std::string& state) const = 0;
197 
205  bool areSynonymous(int i, int j) const
206  {
207  return translate(i) == translate(j);
208  }
209 
217  bool areSynonymous(const std::string& i, const std::string& j) const
218  {
219  return translate(i) == translate(j);
220  }
221 
222  std::vector<int> getSynonymous(int aminoacid) const;
223 
224  std::vector<std::string> getSynonymous(const std::string& aminoacid) const;
225 
232  bool isFourFoldDegenerated(int codon) const;
233 
251  std::unique_ptr<Sequence> getCodingSequence(const SequenceInterface& sequence, bool lookForInitCodon = false, bool includeInitCodon = false) const;
253 };
254 } // end of namespace bpp.
255 #endif // BPP_SEQ_GENETICCODE_GENETICCODE_H
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.
Utilitary functions dealing with alphabets.
Definition: AlphabetTools.h:32
The Alphabet interface.
Definition: Alphabet.h:99
Codon alphabet class.
Definition: CodonAlphabet.h:31
Partial implementation of the Transliterator interface for genetic code object.
Definition: GeneticCode.h:50
const Alphabet & targetAlphabet() const override
Get the target alphabet.
Definition: GeneticCode.h:80
virtual bool isStop(const std::string &state) const =0
Tells is a particular codon is a stop codon.
bool isFourFoldDegenerated(int codon) const
Definition: GeneticCode.cpp:84
bool areSynonymous(const std::string &i, const std::string &j) const
Tell if two codons are synonymous, that is, if they encode the same amino-acid.
Definition: GeneticCode.h:217
std::unique_ptr< Sequence > getCodingSequence(const SequenceInterface &sequence, bool lookForInitCodon=false, bool includeInitCodon=false) const
Get the subsequence corresponding to the coding part of a given sequence.
virtual ~GeneticCode()
Definition: GeneticCode.h:64
virtual const CodonAlphabet & codonAlphabet() const
Alias for getSourceAlphabet return a pointer toward a CodonAlphabet.
Definition: GeneticCode.h:111
std::vector< int > getSynonymous(int aminoacid) const
Definition: GeneticCode.cpp:42
virtual bool isAltStart(int state) const =0
Tells is a particular codon is an alternative start codon.
std::unique_ptr< Sequence > translate(const SequenceInterface &sequence) const override
Translate a whole sequence from source alphabet to target alphabet.
Definition: GeneticCode.h:86
virtual size_t getNumberOfStopCodons() const =0
virtual std::shared_ptr< const CodonAlphabet > getCodonAlphabet() const
Alias for getSourceAlphabet return a pointer toward a CodonAlphabet.
Definition: GeneticCode.h:104
std::shared_ptr< const CodonAlphabet > codonAlphabet_
Definition: GeneticCode.h:52
int translate(int state) const override
Translate a given state coded as a int from source alphabet to target alphabet.
Definition: GeneticCode.cpp:20
virtual bool isStart(int state) const
Tells is a particular codon is a start codon.
Definition: GeneticCode.h:164
std::shared_ptr< const Alphabet > getSourceAlphabet() const override
Get the source alphabet.
Definition: GeneticCode.h:74
const Alphabet & sourceAlphabet() const override
Get the source alphabet.
Definition: GeneticCode.h:76
virtual bool isAltStart(const std::string &state) const =0
Tells is a particular codon is an alternative start codon.
bool areSynonymous(int i, int j) const
Tell if two codons are synonymous, that is, if they encode the same amino-acid.
Definition: GeneticCode.h:205
virtual const ProteicAlphabet & proteicAlphabet() const
Alias for getTargetAlphabet return a pointer toward a ProteicAlphabet.
Definition: GeneticCode.h:125
std::map< int, int > tlnTable_
Definition: GeneticCode.h:54
virtual std::vector< int > getStopCodonsAsInt() const =0
std::shared_ptr< const Alphabet > getTargetAlphabet() const override
Get the target alphabet.
Definition: GeneticCode.h:78
std::shared_ptr< const ProteicAlphabet > proteicAlphabet_
Definition: GeneticCode.h:53
virtual bool isStart(const std::string &state) const
Tells is a particular codon is a start codon.
Definition: GeneticCode.h:177
virtual std::vector< std::string > getStopCodonsAsChar() const =0
virtual GeneticCode * clone() const override=0
virtual std::shared_ptr< const ProteicAlphabet > getProteicAlphabet() const
Alias for getTargetAlphabet return a pointer toward a ProteicAlphabet.
Definition: GeneticCode.h:118
virtual bool isStop(int state) const =0
Tells is a particular codon is a stop codon.
GeneticCode(std::shared_ptr< const NucleicAlphabet > alphabet)
Definition: GeneticCode.h:57
This alphabet is used to deal with proteins.
The sequence interface.
Definition: Sequence.h:34
Exception thrown when a stop codon is found.
Definition: GeneticCode.h:22
virtual ~StopCodonException()
Definition: GeneticCode.h:31
virtual const std::string & getCodon() const
Definition: GeneticCode.h:34
StopCodonException(const std::string &text, const std::string &codon)
Definition: GeneticCode.cpp:14
This alphabet is used to deal NumericAlphabet.