bpp-phyl3  3.0.0
AbstractKroneckerCodonSubstitutionModel.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) :
18  gCode->getSourceAlphabet(),
19  std::shared_ptr<const StateMapInterface>(new CanonicalStateMap(gCode->getSourceAlphabet(), false)),
20  prefix),
21  gCode_(gCode)
22 {
24 
25  size_t i;
26  for (i = 0; i < 3; i++)
27  {
28  VSubMod_.push_back(std::move(pmod));
29  VnestedPrefix_.push_back(VSubMod_[i]->getNamespace());
30  }
31 
32  VSubMod_[0]->setNamespace(prefix + "123_" + VnestedPrefix_[0]);
33  VSubMod_[0]->enableEigenDecomposition(0);
35 
37 }
38 
40  std::shared_ptr<const GeneticCode> gCode,
41  std::unique_ptr<NucleotideSubstitutionModelInterface> pmod,
42  const std::vector<std::set< size_t>>& vPos,
43  const std::string& prefix) :
46  gCode->getSourceAlphabet(),
47  std::shared_ptr<const StateMapInterface>(new CanonicalStateMap(gCode->getSourceAlphabet(), false)),
48  prefix),
49  gCode_(gCode)
50 {
52 
53  shared_ptr<NucleotideSubstitutionModelInterface> pmod2 = std::move(pmod);
54  size_t i;
55  for (i = 0; i < 3; i++)
56  {
57  VSubMod_.push_back(pmod2);
58  VnestedPrefix_.push_back(pmod2->getNamespace());
59  }
60  pmod2->setNamespace(prefix + "123_" + VnestedPrefix_[0]);
61  pmod2->enableEigenDecomposition(0);
62  addParameters_(pmod2->getParameters());
63 
66 }
67 
69  std::shared_ptr<const GeneticCode> gCode,
70  std::unique_ptr<NucleotideSubstitutionModelInterface> pmod1,
71  std::unique_ptr<NucleotideSubstitutionModelInterface> pmod2,
72  std::unique_ptr<NucleotideSubstitutionModelInterface> pmod3,
73  const string& prefix) :
76  gCode->getSourceAlphabet(),
77  shared_ptr<const StateMapInterface>(new CanonicalStateMap(gCode->getSourceAlphabet(), false)),
78  prefix),
79  gCode_(gCode)
80 {
82 
83  VSubMod_.push_back(std::move(pmod1));
84  VnestedPrefix_.push_back(VSubMod_[0]->getNamespace());
85  VSubMod_[0]->setNamespace(prefix + "1_" + VnestedPrefix_[0]);
86  VSubMod_[0]->enableEigenDecomposition(0);
88 
89  VSubMod_.push_back(std::move(pmod2));
90  VnestedPrefix_.push_back(VSubMod_[1]->getNamespace());
91  VSubMod_[1]->setNamespace(prefix + "2_" + VnestedPrefix_[1]);
92  VSubMod_[1]->enableEigenDecomposition(0);
94 
95  VSubMod_.push_back(std::move(pmod3));
96  VnestedPrefix_.push_back(VSubMod_[2]->getNamespace());
97  VSubMod_[2]->setNamespace(prefix + "3_" + VnestedPrefix_[2]);
98  VSubMod_[2]->enableEigenDecomposition(0);
100 
101  initGenerators_();
102 }
103 
105  std::shared_ptr<const GeneticCode> gCode,
106  std::unique_ptr<NucleotideSubstitutionModelInterface> pmod1,
107  std::unique_ptr<NucleotideSubstitutionModelInterface> pmod2,
108  std::unique_ptr<NucleotideSubstitutionModelInterface> pmod3,
109  const std::vector<std::set< size_t>>& vPos,
110  const std::string& prefix) :
113  gCode->getSourceAlphabet(),
114  shared_ptr<const StateMapInterface>(new CanonicalStateMap(gCode->getSourceAlphabet(), false)),
115  prefix),
116  gCode_(gCode)
117 {
119 
120  VSubMod_.push_back(std::move(pmod1));
121  VnestedPrefix_.push_back(VSubMod_[0]->getNamespace());
122  VSubMod_[0]->setNamespace(prefix + "1_" + VnestedPrefix_[0]);
123  VSubMod_[0]->enableEigenDecomposition(0);
125 
126  VSubMod_.push_back(std::move(pmod2));
127  VnestedPrefix_.push_back(VSubMod_[1]->getNamespace());
128  VSubMod_[1]->setNamespace(prefix + "2_" + VnestedPrefix_[1]);
129  VSubMod_[1]->enableEigenDecomposition(0);
131 
132  VSubMod_.push_back(std::move(pmod3));
133  VnestedPrefix_.push_back(VSubMod_[2]->getNamespace());
134  VSubMod_[2]->setNamespace(prefix + "3_" + VnestedPrefix_[2]);
135  VSubMod_[2]->enableEigenDecomposition(0);
137 
138  initGenerators_();
139  setChangingPositions(vPos);
140 }
141 
142 
144 {
145  size_t i, j;
146  size_t salph = getNumberOfStates();
147 
148  for (i = 0; i < salph; i++)
149  {
150  for (j = 0; j < salph; j++)
151  {
152  if (gCode_->isStop(static_cast<int>(i)) || gCode_->isStop(static_cast<int>(j)))
153  {
154  generator_(i, j) = 0;
155  }
156  else
157  generator_(i, j) *= getCodonsMulRate(i, j);
158  }
159  }
160 }
AbstractKroneckerCodonSubstitutionModel(std::shared_ptr< const GeneticCode > gCode, std::unique_ptr< NucleotideSubstitutionModelInterface > pmod, const std::string &st)
Build a new AbstractKroneckerCodonSubstitutionModel object from a pointer to a NucleotideSubstitution...
void completeMatrices_() override
Method inherited from AbstractWordSubstitutionModel.
virtual double getCodonsMulRate(size_t i, size_t j) const override
Method inherited from CodonSubstitutionModel.
void setChangingPositions(const std::vector< std::set< size_t >> &vPos)
void addParameters_(const ParameterList &parameters)
RowMatrix< double > generator_
The generator matrix of the model.
size_t getNumberOfStates() const override
Get the number of states.
std::vector< std::shared_ptr< SubstitutionModelInterface > > VSubMod_
This class implements a state map where all resolved states are modeled.
Definition: StateMap.h:168
virtual std::string getNamespace() const=0
virtual const ParameterList & getParameters() const=0
Map the states of a given alphabet which have a model state.
Definition: StateMap.h:25
virtual bool enableEigenDecomposition()=0
Tell if eigenValues and Vectors must be computed.
Defines the basic types of data flow nodes.