bpp-phyl  2.2.0
YNGKP_M8.cpp
Go to the documentation of this file.
1 //
2 // File: YNGKP_M8.cpp
3 // Created by: Laurent Gueguen
4 // Created on: May 2010
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
9  This software is a computer program whose purpose is to provide classes
10  for phylogenetic data analysis.
11 
12  This software is governed by the CeCILL license under French law and
13  abiding by the rules of distribution of free software. You can use,
14  modify and/ or redistribute the software under the terms of the CeCILL
15  license as circulated by CEA, CNRS and INRIA at the following URL
16  "http://www.cecill.info".
17 
18  As a counterpart to the access to the source code and rights to copy,
19  modify and redistribute granted by the license, users are provided only
20  with a limited warranty and the software's author, the holder of the
21  economic rights, and the successive licensors have only limited
22  liability.
23 
24  In this respect, the user's attention is drawn to the risks associated
25  with loading, using, modifying and/or developing or reproducing the
26  software by the user in light of its specific status of free software,
27  that may mean that it is complicated to manipulate, and that also
28  therefore means that it is reserved for developers and experienced
29  professionals having in-depth computer knowledge. Users are therefore
30  encouraged to load and test the software's suitability as regards their
31  requirements in conditions enabling the security of their systems and/or
32  data to be ensured and, more generally, to use and operate it in the
33  same conditions as regards security.
34 
35  The fact that you are presently reading this means that you have had
36  knowledge of the CeCILL license and that you accept its terms.
37  */
38 
39 #include "YNGKP_M8.h"
40 #include "YN98.h"
41 
42 #include <Bpp/Numeric/Prob/MixtureOfDiscreteDistributions.h>
43 #include <Bpp/Numeric/Prob/SimpleDiscreteDistribution.h>
44 #include <Bpp/Numeric/Prob/BetaDiscreteDistribution.h>
45 #include <Bpp/Text/TextTools.h>
46 
47 using namespace bpp;
48 
49 using namespace std;
50 
51 /******************************************************************************/
52 
53 YNGKP_M8::YNGKP_M8(const GeneticCode* gc, FrequenciesSet* codonFreqs, unsigned int nclass) :
55  pmixmodel_(0),
56  synfrom_(),
57  synto_()
58 {
59  if (nclass <= 0)
60  throw Exception("Bad number of classes for model YNGKP_M8: " + TextTools::toString(nclass));
61 
62  // build the submodel
63 
64  BetaDiscreteDistribution* pbdd = new BetaDiscreteDistribution(nclass, 2, 2);
65 
66  vector<double> val; val.push_back(2);
67  vector<double> prob; prob.push_back(1);
68  SimpleDiscreteDistribution* psdd = new SimpleDiscreteDistribution(val, prob);
69 
70  vector<DiscreteDistribution*> v_distr;
71  v_distr.push_back(pbdd); v_distr.push_back(psdd);
72  prob.clear(); prob.push_back(0.5); prob.push_back(0.5);
73 
74  MixtureOfDiscreteDistributions* pmodd = new MixtureOfDiscreteDistributions(v_distr, prob);
75 
76  map<string, DiscreteDistribution*> mpdd;
77  mpdd["omega"] = pmodd;
78 
79  YN98* yn98 = new YN98(gc, codonFreqs);
80  pmixmodel_.reset(new MixtureOfASubstitutionModel(gc->getSourceAlphabet(), yn98, mpdd));
81  delete pbdd;
82  vector<int> supportedChars = yn98->getAlphabetStates();
83 
84  // mapping the parameters
85 
86  ParameterList pl = pmixmodel_->getParameters();
87  for (size_t i = 0; i < pl.size(); i++)
88  {
89  lParPmodel_.addParameter(Parameter(pl[i]));
90  }
91 
92  vector<std::string> v = dynamic_cast<YN98*>(pmixmodel_->getNModel(0))->getFrequenciesSet()->getParameters().getParameterNames();
93 
94  for (size_t i = 0; i < v.size(); i++)
95  {
96  mapParNamesFromPmodel_[v[i]] = getParameterNameWithoutNamespace("YNGKP_M8." + v[i].substr(5));
97  }
98 
99  mapParNamesFromPmodel_["YN98.kappa"] = "kappa";
100  mapParNamesFromPmodel_["YN98.omega_Mixture.theta1"] = "p0";
101  mapParNamesFromPmodel_["YN98.omega_Mixture.1_Beta.alpha"] = "p";
102  mapParNamesFromPmodel_["YN98.omega_Mixture.1_Beta.beta"] = "q";
103  mapParNamesFromPmodel_["YN98.omega_Mixture.2_Simple.V1"] = "omegas";
104 
105  // specific parameters
106 
107  string st;
108  for (map<string, string>::iterator it = mapParNamesFromPmodel_.begin(); it != mapParNamesFromPmodel_.end(); it++)
109  {
110  st = pmixmodel_->getParameterNameWithoutNamespace(it->first);
111  if (it->second != "omegas")
112  addParameter_(new Parameter("YNGKP_M8." + it->second, pmixmodel_->getParameterValue(st),
113  pmixmodel_->getParameter(st).hasConstraint() ? pmixmodel_->getParameter(st).getConstraint()->clone() : 0, true));
114  }
115 
116  addParameter_(new Parameter("YNGKP_M8.omegas", 2., new IntervalConstraint(1, 1, false), true));
117 
118  // look for synonymous codons
119  for (synfrom_ = 1; synfrom_ < supportedChars.size(); synfrom_++)
120  {
121  for (synto_ = 0; synto_ < synfrom_; synto_++)
122  {
123  if ((gc->areSynonymous(supportedChars[synfrom_], supportedChars[synto_]))
124  && (pmixmodel_->getNModel(0)->Qij(synfrom_, synto_) != 0)
125  && (pmixmodel_->getNModel(1)->Qij(synfrom_, synto_) != 0))
126  break;
127  }
128  if (synto_ < synfrom_)
129  break;
130  }
131 
132  if (synto_ == gc->getSourceAlphabet()->getSize())
133  throw Exception("Impossible to find synonymous codons");
134 
135  // update Matrices
136  updateMatrices();
137 }
138 
140  pmixmodel_(new MixtureOfASubstitutionModel(*mod2.pmixmodel_)),
141  synfrom_(mod2.synfrom_),
142  synto_(mod2.synto_)
143 {}
144 
146 {
148 
150  synfrom_ = mod2.synfrom_;
151  synto_ = mod2.synto_;
152 
153  return *this;
154 }
155 
157 
159 {
161 
162  // homogeneization of the synonymous substittion rates
163 
164  Vdouble vd;
165 
166  for (unsigned int i = 0; i < pmixmodel_->getNumberOfModels(); i++)
167  {
168  vd.push_back(1 / pmixmodel_->getNModel(i)->Qij(synfrom_, synto_));
169  }
170 
171  pmixmodel_->setVRates(vd);
172 }
173 
std::map< std::string, std::string > mapParNamesFromPmodel_
Tools to make the link between the Parameters of the object and those of pmixmodel_.
Abstract class for mixture models based on the bibliography.
STL namespace.
Parametrize a set of state frequencies.
const std::vector< int > & getAlphabetStates() const
size_t synfrom_
indexes of 2 codons between which the substitution is synonymous, to set a basis to the homogeneizati...
Definition: YNGKP_M8.h:84
AbstractBiblioMixedSubstitutionModel & operator=(const AbstractBiblioMixedSubstitutionModel &model)
YNGKP_M8 & operator=(const YNGKP_M8 &)
Definition: YNGKP_M8.cpp:145
const FrequenciesSet * getFrequenciesSet() const
If the model owns a FrequenciesSet, returns a pointer to it, otherwise return 0.
Definition: YNGKP_M8.h:118
The Yang and Nielsen (1998) substitution model for codons.
Definition: YN98.h:88
The Yang et al (2000) M8 substitution model for codons.
Definition: YNGKP_M8.h:73
Substitution models defined as a mixture of nested substitution models.
size_t synto_
Definition: YNGKP_M8.h:84
void updateMatrices()
Definition: YNGKP_M8.cpp:158
std::auto_ptr< MixtureOfASubstitutionModel > pmixmodel_
Definition: YNGKP_M8.h:78
YNGKP_M8(const GeneticCode *gc, FrequenciesSet *codonFreqs, unsigned int nbclass)
Definition: YNGKP_M8.cpp:53