bpp-phyl3  3.0.0
RegisterRatesSubstitutionModel.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 
8 
9 using namespace bpp;
10 using namespace std;
11 
13  unique_ptr<SubstitutionModelInterface> originalModel,
15  bool isNormalized) :
16  AbstractParameterAliasable("FromRegister."),
17  AbstractWrappedModel("FromRegister."),
18  AbstractWrappedTransitionModel("FromRegister."),
19  AbstractWrappedSubstitutionModel("FromRegister."),
20  AbstractSubstitutionModel(originalModel->getAlphabet(), originalModel->getStateMap(), "FromRegister."),
21  originalModel_(originalModel->clone()),
22  registerName_(reg.getName()),
23  vRegStates_(),
24  nbTypes_(reg.getNumberOfSubstitutionTypes()),
25  vRates_(reg.getNumberOfSubstitutionTypes())
26 {
27 // getSubstitutionModel().enableEigenDecomposition(false);
28 
29  // record register types
30  isScalable_ = isNormalized;
31 
32 
33  vRegStates_.resize(nbTypes_);
34  for (auto& vreg : vRegStates_)
35  {
36  vreg.resize(getNumberOfStates());
37  }
38 
39  for (size_t i = 0; i < getNumberOfStates(); i++)
40  {
41  for (size_t j = 0; j < getNumberOfStates(); j++)
42  {
43  if (i != j)
44  {
45  size_t nR = reg.getType(i, j);
46  if (nR != 0)
47  vRegStates_[nR - 1][i].push_back((unsigned int)j);
48  }
49  }
50  }
51 
53  // changed (see updateMatrices for vRates_ update).
54  // rates for all register types
55  for (size_t i = 1; i <= nbTypes_; i++)
56  {
57  addParameter_(new Parameter("FromRegister.rho_" + reg.getTypeName(i), 1, Parameter::R_PLUS_STAR));
58  }
59 
63 }
64 
65 /******************************************************************************/
66 
68 {
69  for (size_t i = 0; i < vRates_.size(); ++i)
70  {
71  vRates_[i] = getParameter_(i).getValue();
72  }
73 
75 
76  gen = substitutionModel().generator();
77 
78  for (size_t t = 0; t < nbTypes_; ++t)
79  {
80  double rate = vRates_[t];
81  const VVuint& v = vRegStates_[t];
82 
83  for (size_t i = 0; i < getNumberOfStates(); ++i)
84  {
85  const Vuint& v2 = v[i];
86  for (const auto& j : v2)
87  {
88  gen(i, j) *= rate;
89  }
90  }
91  }
92 
93  setDiagonal();
94 
96 }
void addParameters_(const ParameterList &parameters)
void addParameter_(Parameter *parameter)
std::string getNamespace() const override
const ParameterList & getParameters() const override
Parameter & getParameter_(const std::string &name)
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.
bool isScalable_
If the model is scalable (ie generator can be normalized automatically).
virtual void updateMatrices_()
Diagonalize the matrix, and fill the eigenValues_, iEigenValues_, leftEigenVectors_ and rightEigenVe...
Abstract class of Wrapping model class, where all methods are redirected from model().
const BranchModelInterface & model() const override
virtual double getValue() const
static const std::shared_ptr< IntervalConstraint > R_PLUS_STAR
virtual void setNamespace(const std::string &prefix)=0
VVVuint vRegStates_
Vector of register state -> vector of from states -> vector of to states (for acceleration purpose)
Vdouble vRates_
vector of the rates of the register types
RegisterRatesSubstitutionModel(std::unique_ptr< SubstitutionModelInterface > originalModel, const SubstitutionRegisterInterface &reg, bool isNormalized=false)
Constructor.
size_t getNumberOfStates() const override
Get the number of states.
void updateMatrices_() override
Diagonalize the matrix, and fill the eigenValues_, iEigenValues_, leftEigenVectors_ and rightEigenVe...
const SubstitutionModelInterface & substitutionModel() const override
From AbstractWrappedSubstitutionModel.
virtual const Matrix< double > & generator() const =0
The SubstitutionRegister interface.
virtual std::string getTypeName(size_t type) const =0
Get the name of a given substitution type.
virtual size_t getType(size_t fromState, size_t toState) const =0
Get the substitution type far a given pair of model states.
Defines the basic types of data flow nodes.
std::vector< Vuint > VVuint
std::vector< unsigned int > Vuint