bpp-phyl3  3.0.0
CodonAdHocSubstitutionModel.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 
9 using namespace std;
10 
11 /******************************************************************************/
12 
14  std::shared_ptr<const GeneticCode> gCode,
15  std::unique_ptr<NucleotideSubstitutionModelInterface> pmod,
16  vector<std::unique_ptr<CoreCodonSubstitutionModelInterface>>& vpmodel,
17  const string& name) :
18  AbstractParameterAliasable(name + "."),
19  AbstractCodonSubstitutionModel(gCode, std::move(pmod), name + "."),
20  vModel_(),
21  name_(name),
22  freqSet_()
23 {
24  for (auto& model : vpmodel)
25  {
26  if (model)
27  {
28  model->setNamespace(name + ".");
29  if (model->hasCodonFrequencySet())
30  {
31  if (freqSet_)
32  throw Exception("CodonAdHocSubstitutionModel::CodonAdHocSubstitutionModel : two sub models with FrequencySet");
33 
34  freqSet_ = unique_ptr<CodonFrequencySetInterface>(model->codonFrequencySet().clone());
35  }
36  addParameters_(model->getParameters());
37  vModel_.push_back(std::move(model));
38  }
39  }
40  computeFrequencies(true);
42 }
43 
45  std::shared_ptr<const GeneticCode> gCode,
46  std::unique_ptr<NucleotideSubstitutionModelInterface> pmod1,
47  std::unique_ptr<NucleotideSubstitutionModelInterface> pmod2,
48  std::unique_ptr<NucleotideSubstitutionModelInterface> pmod3,
49  vector<std::unique_ptr<CoreCodonSubstitutionModelInterface>>& vpmodel,
50  const std::string& name) :
51  AbstractParameterAliasable(name + "."),
52  AbstractCodonSubstitutionModel(gCode, std::move(pmod1), std::move(pmod2), std::move(pmod3), name + "."),
53  vModel_(),
54  name_(name),
55  freqSet_()
56 {
57  for (auto& model : vpmodel)
58  {
59  if (model != NULL)
60  {
61  model->setNamespace(name + ".");
62  if (model->hasCodonFrequencySet())
63  {
64  if (freqSet_)
65  throw Exception("CodonAdHocSubstitutionModel::CodonAdHocSubstitutionModel : two sub models with FrequencySet");
66 
67  freqSet_ = unique_ptr<CodonFrequencySetInterface>(model->codonFrequencySet().clone());
68  }
69 
70  addParameters_(model->getParameters());
71  vModel_.push_back(std::move(model));
72  }
73  }
74 
75  computeFrequencies(true);
77 }
78 
82  vModel_(),
83  name_(model.name_),
84  freqSet_()
85 {
86  for (auto& mod : model.vModel_)
87  {
88  vModel_.emplace_back(mod->clone());
89  }
90 
91  for (auto& mod : vModel_)
92  {
93  if (mod->hasCodonFrequencySet())
94  {
95  if (freqSet_)
96  throw Exception("CodonAdHocSubstitutionModel::CodonAdHocSubstitutionModel : two sub models with FrequencySet");
97 
98  freqSet_ = unique_ptr<CodonFrequencySetInterface>(mod->codonFrequencySet().clone());
99  }
100  }
101 }
102 
104 {
107  name_ = model.name_;
108 
109  vModel_.clear();
110  freqSet_ = 0;
111 
112  for (auto& mod : model.vModel_)
113  {
114  vModel_.emplace_back(mod->clone());
115  }
116 
117  for (auto& mod : vModel_)
118  {
119  if (mod->hasCodonFrequencySet())
120  {
121  if (freqSet_)
122  throw Exception("CodonAdHocSubstitutionModel::CodonAdHocSubstitutionModel : two sub models with FrequencySet");
123 
124  freqSet_ = unique_ptr<CodonFrequencySetInterface>(mod->codonFrequencySet().clone());
125  }
126  }
127 
128  return *this;
129 }
130 
131 void CodonAdHocSubstitutionModel::setFreq(std::map<int, double>& frequencies)
132 {
133  for (auto& model : vModel_)
134  {
135  model->setFreq(frequencies);
136  matchParametersValues(model->getParameters());
137  }
138 }
139 
141 {
142  for (auto& model : vModel_)
143  {
144  model->matchParametersValues(parameters);
145  }
146 
147  // Beware: must be called at the end
149 }
150 
151 double CodonAdHocSubstitutionModel::getCodonsMulRate(size_t i, size_t j) const
152 {
153  double x(1);
154 
155  for (auto& model : vModel_)
156  {
157  x *= model->getCodonsMulRate(i, j);
158  }
159 
160  return x;
161 }
Abstract class for substitution models on codons.
AbstractCodonSubstitutionModel & operator=(const AbstractCodonSubstitutionModel &model)
void updateMatrices_() override
Diagonalize the matrix, and fill the eigenValues_, iEigenValues_, leftEigenVectors_ and rightEigenVe...
void setNamespace(const std::string &prefix) override
void addParameters_(const ParameterList &parameters)
AbstractParameterAliasable & operator=(const AbstractParameterAliasable &ap)
virtual void fireParameterChanged(const ParameterList &parameters) override
Tells the model that a parameter value has changed.
Class for substitution models of codons with several layers of codon models.
double getCodonsMulRate(size_t i, size_t j) const override
Returns the multiplicative rate specific to two codons specified by their number. The respective gene...
void fireParameterChanged(const ParameterList &parameterlist) override
Tells the model that a parameter value has changed.
CodonAdHocSubstitutionModel(std::shared_ptr< const GeneticCode > gCode, std::unique_ptr< NucleotideSubstitutionModelInterface > pmod, std::vector< std::unique_ptr< CoreCodonSubstitutionModelInterface >> &vpmodel, const std::string &name)
Build a new CodonAdHocSubstitutionModel object from a pointer to NucleotideSubstitutionModel.
std::vector< std::unique_ptr< CoreCodonSubstitutionModelInterface > > vModel_
void setFreq(std::map< int, double > &frequencies) override
Estimation of the parameters of the models so that the equilibrium frequencies match the given ones.
std::unique_ptr< CodonFrequencySetInterface > freqSet_
optional FrequencySet if model is defined through a FrequencySet.
CodonAdHocSubstitutionModel & operator=(const CodonAdHocSubstitutionModel &model)
virtual bool matchParametersValues(const ParameterList &parameters)=0
Defines the basic types of data flow nodes.