bpp-phyl3  3.0.0
YNGP_M8.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_M8.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 nclass,
23  bool neutral) :
24  AbstractParameterAliasable(neutral ? "YNGP_M8a." : "YNGP_M8."),
25  AbstractWrappedModel(neutral ? "YNGP_M8a." : "YNGP_M8."),
26  AbstractWrappedTransitionModel(neutral ? "YNGP_M8a." : "YNGP_M8."),
27  AbstractTotallyWrappedTransitionModel(neutral ? "YNGP_M8a." : "YNGP_M8."),
28  AbstractBiblioTransitionModel(neutral ? "YNGP_M8a." : "YNGP_M8."),
29  YNGP_M(neutral ? "YNGP_M8a." : "YNGP_M8."),
30  neutral_(neutral)
31 {
32  if (nclass <= 0)
33  throw Exception("Bad number of classes for model " + getName() + ": " + TextTools::toString(nclass));
34 
35  // build the submodel
36 
37  auto pbdd = make_unique<BetaDiscreteDistribution>(nclass, 2, 2);
38 
39  vector<double> val = { neutral_ ? 1. : 2. };
40  vector<double> prob = { 1. };
41  auto psdd = make_unique<SimpleDiscreteDistribution>(val, std::move(prob));
42 
43  vector<unique_ptr<DiscreteDistributionInterface>> v_distr;
44  v_distr.push_back(std::move(pbdd));
45  v_distr.push_back(std::move(psdd));
46  prob.clear();
47  prob.push_back(0.5);
48  prob.push_back(0.5);
49 
50  // Distribution on omega = mixture (Beta + Simple of size 1)
51  auto pmodd = make_unique<MixtureOfDiscreteDistributions>(v_distr, prob);
52 
53  map<string, unique_ptr<DiscreteDistributionInterface>> mpdd;
54  mpdd["omega"] = std::move(pmodd);
55 
56  auto yn98 = make_unique<YN98>(gc, std::move(codonFreqs));
57 
58  mixedModelPtr_.reset(new MixtureOfASubstitutionModel(gc->getSourceAlphabet(), std::move(yn98), mpdd));
59  mixedSubModelPtr_ = dynamic_cast<const MixtureOfASubstitutionModel*>(&mixedModel());
60 
61  vector<int> supportedChars = mixedSubModelPtr_->getAlphabetStates();
62 
63  // mapping the parameters
64 
65  ParameterList pl = mixedModelPtr_->getParameters();
66  for (size_t i = 0; i < pl.size(); ++i)
67  {
68  if (neutral_ && pl[i].getName() == "YN98.omega_Mixture.2_Simple.V1")
69  continue;
71  }
72 
73  vector<std::string> v = dynamic_cast<const YN98&>(mixedModelPtr_->nModel(0)).frequencySet().getParameters().getParameterNames();
74 
75  for (auto& vi : v)
76  {
77  mapParNamesFromPmodel_[vi] = vi.substr(5);
78  }
79 
80  mapParNamesFromPmodel_["YN98.kappa"] = "kappa";
81  mapParNamesFromPmodel_["YN98.omega_Mixture.theta1"] = "p0";
82  mapParNamesFromPmodel_["YN98.omega_Mixture.1_Beta.alpha"] = "p";
83  mapParNamesFromPmodel_["YN98.omega_Mixture.1_Beta.beta"] = "q";
84  if (!neutral_)
85  mapParNamesFromPmodel_["YN98.omega_Mixture.2_Simple.V1"] = "omegas";
86 
87  // specific parameters
88 
89  string st;
90  for (auto& it : mapParNamesFromPmodel_)
91  {
92  st = mixedModelPtr_->getParameterNameWithoutNamespace(it.first);
93  if (it.second != "omegas")
94  addParameter_(new Parameter(getName() + "." + it.second, mixedModelPtr_->getParameterValue(st),
95  mixedModelPtr_->parameter(st).hasConstraint() ? std::shared_ptr<ConstraintInterface>(mixedModelPtr_->parameter(st).getConstraint()->clone()) : 0));
96  }
97 
98  if (!neutral_)
99  addParameter_(new Parameter("YNGP_M8.omegas", 2., make_shared<IntervalConstraint>(1, 1, false)));
100 
101  // look for synonymous codons
102  for (synfrom_ = 1; synfrom_ < supportedChars.size(); synfrom_++)
103  {
104  for (synto_ = 0; synto_ < synfrom_; synto_++)
105  {
106  if ((gc->areSynonymous(supportedChars[synfrom_], supportedChars[synto_]))
109  break;
110  }
111  if (synto_ < synfrom_)
112  break;
113  }
114 
115  if (synto_ == gc->getSourceAlphabet()->getSize())
116  throw Exception("Impossible to find synonymous codons");
117 
118  // update Matrices
119  computeFrequencies(false);
120  updateMatrices_();
121 }
122 
124 {
126 
127  // homogeneization of the synonymous substittion rates
128 
129  Vdouble vd;
130 
131  for (unsigned int i = 0; i < mixedModelPtr_->getNumberOfModels(); ++i)
132  {
133  vd.push_back(1 / mixedSubModelPtr_->subNModel(i).Qij(synfrom_, synto_));
134  }
135 
136  mixedModelPtr_->setVRates(vd);
137 }
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)
const std::vector< int > & getAlphabetStates() const override
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_M8(std::shared_ptr< const GeneticCode > gc, std::unique_ptr< CodonFrequencySetInterface > codonFreqs, unsigned int nbclass, bool neutral=false)
Constructor that requires the number of classes of the BetaDiscreteDistribution.
Definition: YNGP_M8.cpp:19
void updateMatrices_() override
Definition: YNGP_M8.cpp:123
bool neutral_
If parameter omega=1.
Definition: YNGP_M8.h:45
std::string getName() const override
Get the name of the model.
Definition: YNGP_M8.h:77
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