bpp-phyl3 3.0.0
YNGP_M3.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_M3.h"
11
12using namespace bpp;
13using namespace std;
14
15/******************************************************************************/
16
17YNGP_M3::YNGP_M3(
18 shared_ptr<const GeneticCode> gc,
19 unique_ptr<CodonFrequencySetInterface> codonFreqs,
20 unsigned int nbOmega) :
22 AbstractWrappedModel("YNGP_M3."),
26 YNGP_M("YNGP_M3.")
27{
28 if (nbOmega < 1)
29 throw Exception("At least one omega is necessary in the YNGP_M3 model");
30
31 // build the submodel
32
33 vector<double> v1, v2;
34 v1.push_back(0.5);
35 for (unsigned int i = 1; i < nbOmega; ++i)
36 {
37 v1.push_back(0.5 + 0.5 * i);
38 }
39
40 for (unsigned int i = 0; i < nbOmega; ++i)
41 {
42 v2.push_back(1. / nbOmega);
43 }
44
45 auto psdd = make_unique<SimpleDiscreteDistribution>(v1, v2, 0.002);
46
47 map<string, unique_ptr<DiscreteDistributionInterface>> mpdd;
48 mpdd["omega"] = std::move(psdd);
49
50 auto yn98 = make_unique<YN98>(gc, std::move(codonFreqs));
51
52 mixedModelPtr_.reset(new MixtureOfASubstitutionModel(gc->getSourceAlphabet(), std::move(yn98), mpdd));
54
55 vector<int> supportedChars = mixedSubModelPtr_->getAlphabetStates();
56
57 // mapping the parameters
58
59 ParameterList pl = mixedModelPtr_->getParameters();
60 for (size_t i = 0; i < pl.size(); ++i)
61 {
63 }
64
65 vector<std::string> v = dynamic_cast<const YN98&>(mixedModelPtr_->nModel(0)).frequencySet().getParameters().getParameterNames();
66 for (auto& vi : v)
67 {
68 mapParNamesFromPmodel_[vi] = vi.substr(5);
69 }
70
71 mapParNamesFromPmodel_["YN98.kappa"] = "kappa";
72
73 for (size_t i = 1; i < nbOmega; ++i)
74 {
75 mapParNamesFromPmodel_["YN98.omega_Simple.theta" + TextTools::toString(i)] = "theta" + TextTools::toString(i);
76 }
77
78
79 mapParNamesFromPmodel_["YN98.omega_Simple.V1"] = "omega0";
80 for (size_t i = 1; i < nbOmega; ++i)
81 {
82 mapParNamesFromPmodel_["YN98.omega_Simple.V" + TextTools::toString(i + 1)] = "delta" + TextTools::toString(i);
83 }
84
85 // specific parameters
86
87 string st;
88 for (auto& it : mapParNamesFromPmodel_)
89 {
90 st = mixedModelPtr_->getParameterNameWithoutNamespace(it.first);
91 if (it.second.substr(0, 5) != "delta")
92 addParameter_(new Parameter("YNGP_M3." + it.second, mixedModelPtr_->getParameterValue(st),
93 mixedModelPtr_->parameter(st).hasConstraint() ? shared_ptr<ConstraintInterface>(mixedModelPtr_->parameter(st).getConstraint()->clone()) : 0));
94 }
95
96 for (size_t i = 1; i < nbOmega; ++i)
97 {
98 addParameter_(new Parameter("YNGP_M3.delta" + TextTools::toString(i), 0.5, make_shared<IntervalConstraint>(0.002, 999, true, true, 0.002)));
99 }
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_ == supportedChars.size())
116 throw Exception("Impossible to find synonymous codons");
117
118 // update Matrices
119 computeFrequencies(false);
121}
122
124{
125 for (unsigned int i = 0; i < lParPmodel_.size(); ++i)
126 {
127 if (mapParNamesFromPmodel_.find(lParPmodel_[i].getName()) != mapParNamesFromPmodel_.end())
128 {
129 const string& np = lParPmodel_[i].getName();
130 if (np.size() > 19 && np[18] == 'V')
131 {
132 size_t ind = TextTools::to<size_t>(np.substr(19));
133 double x = getParameterValue("omega0");
134 for (unsigned j = 1; j < ind; j++)
135 {
136 x += getParameterValue("delta" + TextTools::toString(j));
137 }
138
139 const auto parConst = lParPmodel_[i].getConstraint();
140 lParPmodel_[i].setValue(parConst ? (parConst->isCorrect(x) ? x : parConst->getAcceptedLimit(x)) : x);
141 }
142 else
144 }
145 }
146
147 mixedModelPtr_->matchParametersValues(lParPmodel_);
148
149 // homogeneization of the synonymous substitution rates
150
151 Vdouble vd;
152
153 for (unsigned int i = 0; i < mixedModelPtr_->getNumberOfModels(); ++i)
154 {
155 vd.push_back(1 / mixedSubModelPtr_->subNModel(i).Qij(synfrom_, synto_));
156 }
157
158 mixedModelPtr_->setVRates(vd);
159}
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 std::string getParameterNameWithoutNamespace(const std::string &name) const=0
virtual double getParameterValue(const std::string &name) const=0
virtual const ParameterList & getParameters() const=0
virtual const Parameter & parameter(const std::string &name) 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_M3.cpp:123
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