bpp-phyl3  3.0.0
AbstractCodonSubstitutionModel.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::shared_ptr<const GeneticCode> gCode,
14  std::unique_ptr<NucleotideSubstitutionModelInterface> pmod,
15  const string& prefix,
16  bool paramRates) :
19  gCode->getSourceAlphabet(),
20  std::shared_ptr<const StateMapInterface>(new CanonicalStateMap(gCode->getSourceAlphabet(), false)),
21  prefix),
22  hasParametrizedRates_(paramRates),
23  gCode_(gCode)
24 {
26 
27  shared_ptr<NucleotideSubstitutionModelInterface> pmod2 = std::move(pmod);
28  for (size_t i = 0; i < 3; ++i)
29  {
30  VSubMod_.push_back(pmod2);
31  VnestedPrefix_.push_back(pmod2->getNamespace());
32  }
33 
34  pmod2->setNamespace(prefix + "123_" + VnestedPrefix_[0]);
35  pmod2->enableEigenDecomposition(0);
36 
37  addParameters_(pmod2->getParameters());
38 
39  Vrate_.resize(3);
40  for (size_t i = 0; i < 3; ++i)
41  {
42  Vrate_[i] = 1.0 / 3;
43  }
44 
45 
47  {
48  // relative rates
49  for (size_t i = 0; i < 2; ++i)
50  {
51  addParameter_(new Parameter(prefix + "relrate" + TextTools::toString(i + 1), 1.0 / double(3 - i), Parameter::PROP_CONSTRAINT_EX));
52  }
53  }
54 }
55 
57  std::shared_ptr<const GeneticCode> gCode,
58  std::unique_ptr<NucleotideSubstitutionModelInterface> pmod1,
59  std::unique_ptr<NucleotideSubstitutionModelInterface> pmod2,
60  std::unique_ptr<NucleotideSubstitutionModelInterface> pmod3,
61  const std::string& prefix,
62  bool paramRates) :
65  gCode->getSourceAlphabet(),
66  shared_ptr<const StateMapInterface>(new CanonicalStateMap(gCode->getSourceAlphabet(), false)),
67  prefix),
68  hasParametrizedRates_(paramRates),
69  gCode_(gCode)
70 {
72 
73  VSubMod_.push_back(std::move(pmod1));
74  VnestedPrefix_.push_back(VSubMod_[0]->getNamespace());
75  VSubMod_[0]->setNamespace(prefix + "1_" + VnestedPrefix_[0]);
76  VSubMod_[0]->enableEigenDecomposition(0);
78 
79  VSubMod_.push_back(std::move(pmod2));
80  VnestedPrefix_.push_back(VSubMod_[1]->getNamespace());
81  VSubMod_[1]->setNamespace(prefix + "2_" + VnestedPrefix_[1]);
82  VSubMod_[1]->enableEigenDecomposition(0);
84 
85  VSubMod_.push_back(std::move(pmod3));
86  VnestedPrefix_.push_back(VSubMod_[2]->getNamespace());
87  VSubMod_[2]->setNamespace(prefix + "3_" + VnestedPrefix_[2]);
88  VSubMod_[2]->enableEigenDecomposition(0);
90 
91  Vrate_.resize(3);
92  for (size_t i = 0; i < 3; ++i)
93  {
94  Vrate_[i] = 1.0 / 3;
95  }
96 
98  {
99  // relative rates
100  for (int i = 0; i < 2; ++i)
101  {
102  addParameter_(new Parameter(prefix + "relrate" + TextTools::toString(i + 1), 1.0 / double(3 - i), Parameter::PROP_CONSTRAINT_EX));
103  }
104  }
105 }
106 
108 {
110  {
111  size_t i, nbmod = VSubMod_.size();
112  double x;
113  size_t k;
114  for (k = nbmod - 1; k > 0; k--)
115  {
116  x = 1.0;
117  for (i = 0; i < k; i++)
118  {
119  x *= 1 - getParameterValue("relrate" + TextTools::toString(i + 1));
120  }
121  if (k != nbmod - 1)
122  x *= getParameterValue("relrate" + TextTools::toString(k + 1));
123  Vrate_[k] = x;
124  }
125  }
126 
128 }
129 
131 {
132  size_t i, j;
133  size_t salph = getNumberOfStates();
134 
135  for (i = 0; i < salph; i++)
136  {
137  for (j = 0; j < salph; j++)
138  {
139  if (gCode_->isStop(getAlphabetStateAsInt(i)) || gCode_->isStop(getAlphabetStateAsInt(j)))
140  {
141  generator_(i, j) = 0;
142  }
143  else
144  generator_(i, j) *= getCodonsMulRate(i, j);
145  }
146  }
147 }
AbstractCodonSubstitutionModel(std::shared_ptr< const GeneticCode > gCode, std::unique_ptr< NucleotideSubstitutionModelInterface > pmod, const std::string &st, bool paramRates=false)
Build a new AbstractCodonSubstitutionModel object from a pointer to a NucleotideSubstitutionModel.
std::shared_ptr< const GeneticCode > gCode_
void completeMatrices_() override
Method inherited from AbstractWordSubstitutionModel.
void updateMatrices_() override
Diagonalize the matrix, and fill the eigenValues_, iEigenValues_, leftEigenVectors_ and rightEigenVe...
bool hasParametrizedRates_
boolean for the parametrization of the position relative rates. Default : false.
void addParameters_(const ParameterList &parameters)
void addParameter_(Parameter *parameter)
RowMatrix< double > generator_
The generator matrix of the model.
bool enableEigenDecomposition()
Tell if eigenValues and Vectors must be computed.
int getAlphabetStateAsInt(size_t index) const override
size_t getNumberOfStates() const override
Get the number of states.
Abstract Basal class for words of substitution models.
void updateMatrices_()
Diagonalize the matrix, and fill the eigenValues_, iEigenValues_, leftEigenVectors_ and rightEigenVe...
std::vector< std::shared_ptr< SubstitutionModelInterface > > VSubMod_
This class implements a state map where all resolved states are modeled.
Definition: StateMap.h:168
virtual double getCodonsMulRate(size_t, size_t) const =0
Returns the multiplicative rate specific to two codons specified by their number. The respective gene...
static const std::shared_ptr< IntervalConstraint > PROP_CONSTRAINT_EX
virtual std::string getNamespace() const=0
virtual double getParameterValue(const std::string &name) const=0
virtual const ParameterList & getParameters() const=0
Map the states of a given alphabet which have a model state.
Definition: StateMap.h:25
std::string toString(T t)
Defines the basic types of data flow nodes.