bpp-phyl3  3.0.0
YNGP_M9.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_M9.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_M9."),
25  AbstractWrappedModel("YNGP_M9."),
29  YNGP_M("YNGP_M9."),
30  nBeta_(nbBeta),
31  nGamma_(nbGamma)
32 {
33  if (nbBeta <= 0)
34  throw Exception("Bad number of classes for beta distribution of model YNGP_M9: " + TextTools::toString(nbBeta));
35  if (nbGamma <= 0)
36  throw Exception("Bad number of classes for gamma distribution of model YNGP_M9: " + 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);
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  mixedModelPtr_.reset(new MixtureOfASubstitutionModel(gc->getSourceAlphabet(), std::move(yn98), mpdd));
57  mixedSubModelPtr_ = dynamic_cast<const MixtureOfASubstitutionModel*>(&mixedModel());
58 
59  vector<int> supportedChars = mixedModelPtr_->getAlphabetStates();
60 
61  // mapping the parameters
62 
63  ParameterList pl = mixedModelPtr_->getParameters();
64  for (size_t i = 0; i < pl.size(); ++i)
65  {
67  }
68 
69  vector<std::string> v = dynamic_cast<const YN98&>(mixedModelPtr_->nModel(0)).frequencySet().getParameters().getParameterNames();
70 
71  for (auto& vi : v)
72  {
73  mapParNamesFromPmodel_[vi] = vi.substr(5);
74  }
75 
76  mapParNamesFromPmodel_["YN98.kappa"] = "kappa";
77  mapParNamesFromPmodel_["YN98.omega_Mixture.theta1"] = "p0";
78  mapParNamesFromPmodel_["YN98.omega_Mixture.1_Beta.alpha"] = "p";
79  mapParNamesFromPmodel_["YN98.omega_Mixture.1_Beta.beta"] = "q";
80  mapParNamesFromPmodel_["YN98.omega_Mixture.2_Gamma.alpha"] = "alpha";
81  mapParNamesFromPmodel_["YN98.omega_Mixture.2_Gamma.beta"] = "beta";
82 
83  // specific parameters
84 
85  string st;
86  for (auto& it : mapParNamesFromPmodel_)
87  {
88  st = mixedModelPtr_->getParameterNameWithoutNamespace(it.first);
89  addParameter_(new Parameter("YNGP_M9." + it.second, mixedModelPtr_->getParameterValue(st),
90  mixedModelPtr_->parameter(st).hasConstraint() ? shared_ptr<ConstraintInterface>(mixedModelPtr_->parameter(st).getConstraint()->clone()) : 0));
91  }
92 
93  // look for synonymous codons
94  for (synfrom_ = 1; synfrom_ < supportedChars.size(); synfrom_++)
95  {
96  for (synto_ = 0; synto_ < synfrom_; synto_++)
97  {
98  if ((gc->areSynonymous(supportedChars[synfrom_], supportedChars[synto_]))
101  break;
102  }
103  if (synto_ < synfrom_)
104  break;
105  }
106 
107  if (synto_ == gc->getSourceAlphabet()->getSize())
108  throw Exception("Impossible to find synonymous codons");
109 
110  // update Matrices
111  computeFrequencies(false);
112  updateMatrices_();
113 }
114 
116 {
118 
119  // homogeneization of the synonymous substittion rates
120 
121  Vdouble vd;
122 
123  for (unsigned int i = 0; i < mixedModelPtr_->getNumberOfModels(); ++i)
124  {
125  vd.push_back(1 / mixedSubModelPtr_->subNModel(i).Qij(synfrom_, synto_));
126  }
127 
128  mixedModelPtr_->setVRates(vd);
129 }
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_M9(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_M9.cpp:19
void updateMatrices_() override
Definition: YNGP_M9.cpp:115
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