bpp-phyl3  3.0.0
CodonSameAARateSubstitutionModel.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
6 
7 using namespace bpp;
8 using namespace std;
9 
10 /******************************************************************************/
11 
13  std::unique_ptr<ProteinSubstitutionModelInterface> pAAmodel,
14  std::unique_ptr<CodonSubstitutionModelInterface> pCodonModel,
15  std::unique_ptr<CodonFrequencySetInterface> pFreq,
16  std::shared_ptr<const GeneticCode> pgencode) :
17  AbstractParameterAliasable("SameAARate."),
18  AbstractSubstitutionModel(pCodonModel->getAlphabet(), pCodonModel->getStateMap(), "SameAARate."),
19  pAAmodel_(std::move(pAAmodel)),
20  pCodonModel_(std::move(pCodonModel)),
21  pFreq_(std::move(pFreq)),
22  pgencode_(pgencode),
23  X_(20, 20),
24  phi_(20)
25 {
26  pCodonModel_->enableEigenDecomposition(true);
27  pAAmodel_->enableEigenDecomposition(true);
28 
29  pAAmodel_->setNamespace("SameAARate." + pAAmodel_->getNamespace());
30  pCodonModel_->setNamespace("SameAARate." + pCodonModel_->getNamespace());
31 
32  // TODO (jdutheil on 30/12/22): if we want this, we need to use shared_ptr for FrequencySets
33  if (pFreq_ && pFreq_.get() != &(dynamic_cast<const CoreCodonSubstitutionModelInterface*>(pCodonModel_.get())->codonFrequencySet()))
34  pFreq_->setNamespace("SameAARate." + pFreq_->getNamespace());
35 
36  addParameters_(pAAmodel_->getParameters());
37  addParameters_(pCodonModel_->getParameters());
38 
39  // TODO (jdutheil on 30/12/22): if we want this, we need to use shared_ptr for FrequencySets
40  if (pFreq_ && pFreq_.get() != &(dynamic_cast<const CoreCodonSubstitutionModelInterface*>(pCodonModel_.get())->codonFrequencySet()))
41  addParameters_(pFreq_->getParameters());
42 
43  compute_();
45 }
46 
48 {
49  pAAmodel_->matchParametersValues(parameters);
50  pCodonModel_->matchParametersValues(parameters);
51 
52  if (pFreq_)
53  pFreq_->matchParametersValues(parameters);
54 
55  compute_();
57 }
58 
60 {
61  const auto& freq = pFreq_ ? pFreq_->getFrequencies() : pCodonModel_->getFrequencies();
62 
63  const auto& gen = pCodonModel_->generator();
64 
65  std::fill(phi_.begin(), phi_.end(), 0);
66 
67  for (size_t i = 0; i < stateMap_->getNumberOfModelStates(); i++)
68  {
69  if (!pgencode_->isStop((int)i))
70  phi_[pAAmodel_->getModelStates(pgencode_->translate((int)i))[0]] += freq[i];
71  }
72 
73 
74  for (size_t ai = 0; ai < 20; ai++)
75  {
76  std::fill(X_.getRow(ai).begin(), X_.getRow(ai).end(), 0);
77  }
78 
79  for (size_t i = 0; i < stateMap_->getNumberOfModelStates(); i++)
80  {
81  if (pgencode_->isStop((int)i))
82  continue;
83  auto ai = pAAmodel_->getModelStates(pgencode_->translate((int)i))[0];
84 
85  auto& X_i = X_.getRow(ai);
86 
87  for (size_t j = 0; j < stateMap_->getNumberOfModelStates(); j++)
88  {
89  if (pgencode_->isStop((int)j))
90  continue;
91  auto aj = pAAmodel_->getModelStates(pgencode_->translate((int)j))[0];
92  X_i[aj] += gen(i, j) * freq[i];
93  }
94  }
95 
96  for (size_t ai = 0; ai < 20; ai++)
97  {
98  auto& X_i = X_.getRow(ai);
99  for (size_t aj = 0; aj < 20; aj++)
100  {
101  if (X_i[aj] != 0)
102  X_i[aj] = phi_[ai] * pAAmodel_->generator()(ai, aj) / X_i[aj];
103  }
104  }
105 
106 
107  for (size_t i = 0; i < stateMap_->getNumberOfModelStates(); i++)
108  {
109  if (pgencode_->isStop((int)i))
110  continue;
111  auto ai = pAAmodel_->getModelStates(pgencode_->translate((int)i))[0];
112 
113  const auto& X_i = X_.row(ai);
114 
115  for (size_t j = 0; j < stateMap_->getNumberOfModelStates(); j++)
116  {
117  if (pgencode_->isStop((int)j))
118  continue;
119  auto aj = pAAmodel_->getModelStates(pgencode_->translate((int)j))[0];
120  generator_(i, j) = gen(i, j) * X_i[aj];
121  }
122  }
123 
124  // Set diagonal
125 
126  setDiagonal();
127 }
void addParameters_(const ParameterList &parameters)
RowMatrix< double > generator_
The generator matrix of the model.
void setDiagonal()
set the diagonal of the generator such that sum on each line equals 0.
virtual void updateMatrices_()
Diagonalize the matrix, and fill the eigenValues_, iEigenValues_, leftEigenVectors_ and rightEigenVe...
std::shared_ptr< const StateMapInterface > stateMap_
The map of model states with alphabet states.
virtual double freq(size_t i) const override
CodonSameAARateSubstitutionModel(std::unique_ptr< ProteinSubstitutionModelInterface > pAAmodel, std::unique_ptr< CodonSubstitutionModelInterface > pCodonModel, std::unique_ptr< CodonFrequencySetInterface > pFreq, std::shared_ptr< const GeneticCode > pgencode)
Build a new CodonSameAARateSubstitutionModel object from a pointer to NucleotideSubstitutionModel.
std::unique_ptr< CodonFrequencySetInterface > pFreq_
Protein Model which will be used to get similar AA rates. Its possible parameters are not copied in t...
std::unique_ptr< CodonSubstitutionModelInterface > pCodonModel_
Codon Model which will be copied. Its possible parameters are not copied in this object.
std::shared_ptr< const GeneticCode > pgencode_
RowMatrix< double > X_
20 x 20 Matrix of the denominator of the multiplicators
std::unique_ptr< ProteinSubstitutionModelInterface > pAAmodel_
Protein Model which will be used to get similar AA rates.
void fireParameterChanged(const ParameterList &parameters) override
Tells the model that a parameter value has changed.
virtual const CodonFrequencySetInterface & codonFrequencySet() const =0
const std::vector< double > & getRow(size_t i) const
std::vector< double > row(size_t i) const
Defines the basic types of data flow nodes.