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
7using namespace bpp;
8using namespace std;
9
10/******************************************************************************/
11
12CodonSameAARateSubstitutionModel::CodonSameAARateSubstitutionModel(
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 if (pFreq_)
33 pFreq_->setNamespace("SameAARate." + pFreq_->getNamespace());
34
35 addParameters_(pAAmodel_->getParameters());
36 addParameters_(pCodonModel_->getParameters());
37
38 if (pFreq_)
39 addParameters_(pFreq_->getParameters());
40
41 compute_();
43}
44
46{
47 pAAmodel_->matchParametersValues(parameters);
48 pCodonModel_->matchParametersValues(parameters);
49
50 if (pFreq_)
51 pFreq_->matchParametersValues(parameters);
52
53 compute_();
55}
56
58{
59 const auto& freq = pFreq_ ? pFreq_->getFrequencies() : pCodonModel_->getFrequencies();
60
61 const auto& gen = pCodonModel_->generator();
62
63 std::fill(phi_.begin(), phi_.end(), 0);
64
65 for (size_t i = 0; i < stateMap_->getNumberOfModelStates(); i++)
66 {
67 if (!pgencode_->isStop((int)i))
68 phi_[pAAmodel_->getModelStates(pgencode_->translate((int)i))[0]] += freq[i];
69 }
70
71
72 for (size_t ai = 0; ai < 20; ai++)
73 {
74 std::fill(X_.getRow(ai).begin(), X_.getRow(ai).end(), 0);
75 }
76
77 for (size_t i = 0; i < stateMap_->getNumberOfModelStates(); i++)
78 {
79 if (pgencode_->isStop((int)i))
80 continue;
81 auto ai = pAAmodel_->getModelStates(pgencode_->translate((int)i))[0];
82
83 auto& X_i = X_.getRow(ai);
84
85 for (size_t j = 0; j < stateMap_->getNumberOfModelStates(); j++)
86 {
87 if (pgencode_->isStop((int)j))
88 continue;
89 auto aj = pAAmodel_->getModelStates(pgencode_->translate((int)j))[0];
90 X_i[aj] += gen(i, j) * freq[i];
91 }
92 }
93
94 for (size_t ai = 0; ai < 20; ai++)
95 {
96 auto& X_i = X_.getRow(ai);
97 for (size_t aj = 0; aj < 20; aj++)
98 {
99 if (X_i[aj] != 0)
100 X_i[aj] = phi_[ai] * pAAmodel_->generator()(ai, aj) / X_i[aj];
101 }
102 }
103
104
105 for (size_t i = 0; i < stateMap_->getNumberOfModelStates(); i++)
106 {
107 if (pgencode_->isStop((int)i))
108 continue;
109 auto ai = pAAmodel_->getModelStates(pgencode_->translate((int)i))[0];
110
111 const auto& X_i = X_.row(ai);
112
113 for (size_t j = 0; j < stateMap_->getNumberOfModelStates(); j++)
114 {
115 if (pgencode_->isStop((int)j))
116 continue;
117 auto aj = pAAmodel_->getModelStates(pgencode_->translate((int)j))[0];
118 generator_(i, j) = gen(i, j) * X_i[aj];
119 }
120 }
121
122 // Set diagonal
123
124 setDiagonal();
125}
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
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.
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.