bpp-seq3  3.0.0
CodonFromProteicAlphabetIndex2.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_ALPHABETINDEX_CODONFROMPROTEICALPHABETINDEX2_H
6 #define BPP_SEQ_ALPHABETINDEX_CODONFROMPROTEICALPHABETINDEX2_H
7 
9 
10 #include "../Alphabet/CodonAlphabet.h"
11 #include "../GeneticCode/GeneticCode.h"
12 #include "AlphabetIndex2.h"
13 
14 namespace bpp
15 {
16 /*
17  * @brief AlphabetIndex2 for codon based on ProteicAlphabetIndex2.
18  *
19  */
20 
22  public AlphabetIndex2
23 {
24 private:
25  std::shared_ptr<const CodonAlphabet> alpha_;
26  std::shared_ptr<const GeneticCode> gencode_;
27 
29 
31 
32 public:
33  CodonFromProteicAlphabetIndex2(std::shared_ptr<const GeneticCode> gencode, std::shared_ptr<const AlphabetIndex2> protalphindex) :
35  alpha_(AlphabetTools::DNA_CODON_ALPHABET),
36  gencode_(gencode),
37  vIndex_(64, 64),
38  isSymmetric_(protalphindex->isSymmetric())
39  {
40  if (!AlphabetTools::isProteicAlphabet(protalphindex->alphabet()))
41  throw Exception("CodonFromProteicAlphabetIndex2: Not a Proteic Alphabet for CodonAlphabetIndex2.");
42  fillIndex_(protalphindex);
43  }
44 
46  alpha_(cfp.alpha_),
47  gencode_(cfp.gencode_),
48  vIndex_(cfp.vIndex_),
50  {}
51 
53  {
54  alpha_ = cfp.alpha_;
55  gencode_ = cfp.gencode_;
56  vIndex_ = cfp.vIndex_;
58 
59  return *this;
60  }
61 
63 
64  CodonFromProteicAlphabetIndex2* clone() const override { return new CodonFromProteicAlphabetIndex2(*this); }
65 
66 public:
67  double getIndex(int state1, int state2) const override
68  {
69  return vIndex_(getAlphabet()->getStateIndex(state1) - 1, getAlphabet()->getStateIndex(state2) - 1);
70  }
71 
72  double getIndex(const std::string& state1, const std::string& state2) const override
73  {
74  return vIndex_(getAlphabet()->getStateIndex(state1) - 1, getAlphabet()->getStateIndex(state2) - 1);
75  }
76 
77  std::shared_ptr<const Alphabet> getAlphabet() const override
78  {
79  return alpha_;
80  }
81 
82  const Alphabet& alphabet() const override
83  {
84  return *alpha_;
85  }
86 
87  const Matrix<double>& getIndexMatrix() const override
88  {
89  return vIndex_;
90  }
91 
92  bool isSymmetric() const override
93  {
94  return isSymmetric_;
95  }
96 
97 private:
98  void fillIndex_(std::shared_ptr<const AlphabetIndex2>& protAlphIndex_)
99  {
100  for (int i = 0; i < 64; i++)
101  {
102  for (int j = 0; j < 64; j++)
103  {
104  vIndex_(getAlphabet()->getStateIndex(i) - 1, getAlphabet()->getStateIndex(j) - 1) =
105  gencode_->isStop(i) ? 0 :
106  gencode_->isStop(j) ? 0 :
107  protAlphIndex_->getIndex(gencode_->translate(i), gencode_->translate(j));
108  }
109  }
110  }
111 };
112 } // end of namespace bpp.
113 #endif // BPP_SEQ_ALPHABETINDEX_CODONFROMPROTEICALPHABETINDEX2_H
Two dimensionnal alphabet index interface.
Utilitary functions dealing with alphabets.
Definition: AlphabetTools.h:32
static bool isProteicAlphabet(const Alphabet &alphabet)
The Alphabet interface.
Definition: Alphabet.h:99
double getIndex(int state1, int state2) const override
Get the index associated to a pair of states.
std::shared_ptr< const GeneticCode > gencode_
CodonFromProteicAlphabetIndex2 * clone() const override
CodonFromProteicAlphabetIndex2(std::shared_ptr< const GeneticCode > gencode, std::shared_ptr< const AlphabetIndex2 > protalphindex)
std::shared_ptr< const CodonAlphabet > alpha_
const Matrix< double > & getIndexMatrix() const override
void fillIndex_(std::shared_ptr< const AlphabetIndex2 > &protAlphIndex_)
CodonFromProteicAlphabetIndex2(const CodonFromProteicAlphabetIndex2 &cfp)
double getIndex(const std::string &state1, const std::string &state2) const override
Get the index associated to a pair of states.
std::shared_ptr< const Alphabet > getAlphabet() const override
Get the alphabet associated to this index.
CodonFromProteicAlphabetIndex2 & operator=(const CodonFromProteicAlphabetIndex2 &cfp)
const Alphabet & alphabet() const override
Get the alphabet associated to this index.
This alphabet is used to deal NumericAlphabet.