bpp-phyl3  3.0.0
YNGP_M10.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
8 #include <Bpp/Text/TextTools.h>
9 
10 #include "../MixtureOfASubstitutionModel.h"
11 #include "YN98.h"
12 #include "YNGP_M10.h"
13 
14 using namespace bpp;
15 using namespace std;
16 
17 /******************************************************************************/
18 
20  std::shared_ptr<const GeneticCode> gc,
21  std::unique_ptr<CodonFrequencySetInterface> codonFreqs,
22  unsigned int nbBeta,
23  unsigned int nbGamma) :
24  AbstractParameterAliasable("YNGP_M10."),
25  AbstractWrappedModel("YNGP_M10."),
26  AbstractWrappedTransitionModel("YNGP_M10."),
28  AbstractBiblioTransitionModel("YNGP_M10."),
29  YNGP_M("YNGP_M10."),
30  nBeta_(nbBeta),
31  nGamma_(nbGamma)
32 {
33  if (nbBeta <= 0)
34  throw Exception("Bad number of classes for beta distribution of model YNGP_M10: " + TextTools::toString(nbBeta));
35  if (nbGamma <= 0)
36  throw Exception("Bad number of classes for gamma distribution of model YNGP_M10: " + TextTools::toString(nbGamma));
37 
38  // build the submodel
39 
40  auto pbdd = make_unique<BetaDiscreteDistribution>(nbBeta, 2, 2);
41  auto pgdd = make_unique<GammaDiscreteDistribution>(nbGamma, 1, 1, 0.05, 0.05, false, 1);
42 
43  vector<unique_ptr<DiscreteDistributionInterface>> v_distr;
44  v_distr.push_back(std::move(pbdd));
45  v_distr.push_back(std::move(pgdd));
46  vector<double> prob;
47  prob.push_back(0.5);
48  prob.push_back(0.5);
49 
50  auto pmodd = make_unique<MixtureOfDiscreteDistributions>(v_distr, prob);
51 
52  map<string, unique_ptr<DiscreteDistributionInterface>> mpdd;
53  mpdd["omega"] = std::move(pmodd);
54 
55  auto yn98 = make_unique<YN98>(gc, std::move(codonFreqs));
56 
57  mixedModelPtr_.reset(new MixtureOfASubstitutionModel(gc->getSourceAlphabet(), std::move(yn98), mpdd));
58  mixedSubModelPtr_ = dynamic_cast<const MixtureOfASubstitutionModel*>(&mixedModel());
59 
60  vector<int> supportedChars = mixedModelPtr_->getAlphabetStates();
61 
62  // mapping the parameters
63 
64  ParameterList pl = mixedModelPtr_->getParameters();
65  for (size_t i = 0; i < pl.size(); ++i)
66  {
68  }
69 
70  vector<string> v = dynamic_cast<const YN98&>(mixedModelPtr_->nModel(0)).frequencySet().getParameters().getParameterNames();
71 
72  for (auto& vi : v)
73  {
74  mapParNamesFromPmodel_[vi] = vi.substr(5);
75  }
76 
77  mapParNamesFromPmodel_["YN98.kappa"] = "kappa";
78  mapParNamesFromPmodel_["YN98.omega_Mixture.theta1"] = "p0";
79  mapParNamesFromPmodel_["YN98.omega_Mixture.1_Beta.alpha"] = "p";
80  mapParNamesFromPmodel_["YN98.omega_Mixture.1_Beta.beta"] = "q";
81  mapParNamesFromPmodel_["YN98.omega_Mixture.2_Gamma.alpha"] = "alpha";
82  mapParNamesFromPmodel_["YN98.omega_Mixture.2_Gamma.beta"] = "beta";
83 
84  // specific parameters
85 
86  string st;
87  for (auto it : mapParNamesFromPmodel_)
88  {
89  st = mixedModelPtr_->getParameterNameWithoutNamespace(it.first);
90  addParameter_(new Parameter("YNGP_M10." + it.second, mixedModelPtr_->getParameterValue(st),
91  mixedModelPtr_->parameter(st).hasConstraint() ? std::shared_ptr<ConstraintInterface>(mixedModelPtr_->parameter(st).getConstraint()->clone()) : 0));
92  }
93 
94  // look for synonymous codons
95  for (synfrom_ = 1; synfrom_ < supportedChars.size(); synfrom_++)
96  {
97  for (synto_ = 0; synto_ < synfrom_; synto_++)
98  {
99  if ((gc->areSynonymous(supportedChars[synfrom_], supportedChars[synto_]))
102  break;
103  }
104  if (synto_ < synfrom_)
105  break;
106  }
107 
108  if (synto_ == gc->getSourceAlphabet()->getSize())
109  throw Exception("Impossible to find synonymous codons");
110 
111  // update Matrices
112  computeFrequencies(false);
113  updateMatrices_();
114 }
115 
117 {
119 
120  // homogeneization of the synonymous substittion rates
121 
122  Vdouble vd;
123 
124  for (unsigned int i = 0; i < mixedModelPtr_->getNumberOfModels(); ++i)
125  {
126  vd.push_back(1 / mixedSubModelPtr_->subNModel(i).Qij(synfrom_, synto_));
127  }
128 
129  mixedModelPtr_->setVRates(vd);
130 }
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
YNGP_M10(std::shared_ptr< const GeneticCode > gc, std::unique_ptr< CodonFrequencySetInterface > codonFreqs, unsigned int nbBeta, unsigned int nbGamma)
Constructor that requires the number of classes of the BetaDiscreteDistribution and the GammaDiscrete...
Definition: YNGP_M10.cpp:19
void updateMatrices_() override
Definition: YNGP_M10.cpp:116
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
std::string toString(T t)
Defines the basic types of data flow nodes.
std::vector< double > Vdouble