bpp-phyl3  3.0.0
YNGP_M2.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
7 
8 #include "../MixtureOfASubstitutionModel.h"
9 #include "YN98.h"
10 #include "YNGP_M2.h"
11 
12 using namespace bpp;
13 using namespace std;
14 
15 /******************************************************************************/
16 
18  shared_ptr<const GeneticCode> gc,
19  unique_ptr<CodonFrequencySetInterface> codonFreqs) :
20  AbstractParameterAliasable("YNGP_M2."),
21  AbstractWrappedModel("YNGP_M2."),
25  YNGP_M("YNGP_M2.")
26 {
27  // build the submodel
28 
29  vector<double> v1, v2;
30  v1.push_back(0.5);
31  v1.push_back(1);
32  v1.push_back(2);
33  v2.push_back(0.333333);
34  v2.push_back(0.333333);
35  v2.push_back(0.333334);
36 
37  auto psdd = make_unique<SimpleDiscreteDistribution>(v1, v2);
38 
39  map<string, unique_ptr<DiscreteDistributionInterface>> mpdd;
40  mpdd["omega"] = std::move(psdd);
41 
42  auto yn98 = make_unique<YN98>(gc, std::move(codonFreqs));
43 
44  mixedModelPtr_.reset(new MixtureOfASubstitutionModel(gc->getSourceAlphabet(), std::move(yn98), mpdd));
45  mixedSubModelPtr_ = dynamic_cast<const MixtureOfASubstitutionModel*>(&mixedModel());
46 
47  vector<int> supportedChars = mixedModelPtr_->getAlphabetStates();
48 
49  // mapping the parameters
50 
51  ParameterList pl = mixedModelPtr_->getParameters();
52  for (size_t i = 0; i < pl.size(); ++i)
53  {
55  }
56 
57  vector<std::string> v = dynamic_cast<const YN98&>(mixedModelPtr_->nModel(0)).frequencySet().getParameters().getParameterNames();
58 
59  for (auto& vi : v)
60  {
61  mapParNamesFromPmodel_[vi] = vi.substr(5);
62  }
63 
64  mapParNamesFromPmodel_["YN98.kappa"] = "kappa";
65  mapParNamesFromPmodel_["YN98.omega_Simple.V1"] = "omega0";
66  mapParNamesFromPmodel_["YN98.omega_Simple.theta1"] = "theta1";
67  mapParNamesFromPmodel_["YN98.omega_Simple.V3"] = "omega2";
68  mapParNamesFromPmodel_["YN98.omega_Simple.theta2"] = "theta2";
69 
70  // specific parameters
71 
72  string st;
73  for (auto it : mapParNamesFromPmodel_)
74  {
75  st = mixedModelPtr_->getParameterNameWithoutNamespace(it.first);
76  if (it.second.substr(0, 5) != "omega")
77  addParameter_(new Parameter("YNGP_M2." + it.second, mixedModelPtr_->getParameterValue(st),
78  mixedModelPtr_->parameter(st).hasConstraint() ? std::shared_ptr<ConstraintInterface>(mixedModelPtr_->parameter(st).getConstraint()->clone()) : 0));
79  }
80 
81  addParameter_(new Parameter("YNGP_M2.omega0", 0.5, std::make_shared<IntervalConstraint>(0.002, 1, true, false)));
82 
83  addParameter_(new Parameter("YNGP_M2.omega2", 2, std::make_shared<IntervalConstraint>(1, 999, false, false, 0.002)));
84 
85  // look for synonymous codons
86  for (synfrom_ = 1; synfrom_ < supportedChars.size(); ++synfrom_)
87  {
88  for (synto_ = 0; synto_ < synfrom_; ++synto_)
89  {
90  if (gc->areSynonymous(supportedChars[synfrom_], supportedChars[synto_])
93  break;
94  }
95  if (synto_ < synfrom_)
96  break;
97  }
98 
99  if (synto_ == supportedChars.size())
100  throw Exception("Impossible to find synonymous codons");
101 
102  // update Matrices
103 
104  computeFrequencies(false);
105  updateMatrices_();
106 }
107 
109 {
111 
112  // homogeneization of the synonymous substittion rates
113 
114  Vdouble vd;
115 
116  vd.push_back(1 / mixedSubModelPtr_->subNModel(0).Qij(synfrom_, synto_));
117  vd.push_back(1 / mixedSubModelPtr_->subNModel(1).Qij(synfrom_, synto_));
118  vd.push_back(1 / mixedSubModelPtr_->subNModel(2).Qij(synfrom_, synto_));
119 
120  mixedModelPtr_->setVRates(vd);
121 }
const MixedTransitionModelInterface & mixedModel() const
std::unique_ptr< MixedTransitionModelInterface > mixedModelPtr_
const FrequencySetInterface & frequencySet() const override
Partial implementation of the SubstitutionModel interface for models that are set for matching the bi...
std::map< std::string, std::string > mapParNamesFromPmodel_
Tools to make the link between the Parameters of the object and those of pmixmodel_.
void addParameter_(Parameter *parameter)
Abstract class of Wrapping model class, where all methods are redirected from model().
const SubstitutionModelInterface & subNModel(size_t i) const
size_t size() const
virtual std::vector< std::string > getParameterNames() const
virtual void addParameter(const Parameter &param)
virtual const ParameterList & getParameters() const=0
virtual double Qij(size_t i, size_t j) const =0
A method for computing all necessary matrices.
virtual bool computeFrequencies() const =0
The Yang and Nielsen (1998) substitution model for codons.
Definition: YN98.h:57
void updateMatrices_() override
Definition: YNGP_M2.cpp:108
YNGP_M2(std::shared_ptr< const GeneticCode > gc, std::unique_ptr< CodonFrequencySetInterface > codonFreqs)
Definition: YNGP_M2.cpp:17
Abstract generic class for The Yang et al (2000) M substitution models for codons....
Definition: YNGP_M.h:32
size_t synto_
Definition: YNGP_M.h:43
const MixtureOfASubstitutionModel * mixedSubModelPtr_
Definition: YNGP_M.h:37
size_t synfrom_
indexes of 2 codons states between which the substitution is synonymous, to set a basis to the homoge...
Definition: YNGP_M.h:43
Defines the basic types of data flow nodes.
std::vector< double > Vdouble