bpp-phyl  2.2.0
YNGKP_M2.cpp
Go to the documentation of this file.
1 //
2 // File: YNGKP_M2.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_M2.h"
40 #include "YN98.h"
41 
42 #include <Bpp/Numeric/NumConstants.h>
43 #include <Bpp/Numeric/Prob/SimpleDiscreteDistribution.h>
44 
45 using namespace bpp;
46 
47 using namespace std;
48 
49 /******************************************************************************/
50 
51 YNGKP_M2::YNGKP_M2(const GeneticCode* gc, FrequenciesSet* codonFreqs) :
53  pmixmodel_(0),
54  synfrom_(),
55  synto_()
56 {
57  // build the submodel
58 
59  vector<double> v1, v2;
60  v1.push_back(0.5); v1.push_back(1); v1.push_back(2);
61  v2.push_back(0.333333); v2.push_back(0.333333); v2.push_back(0.333334);
62 
63  SimpleDiscreteDistribution* psdd = new SimpleDiscreteDistribution(v1, v2);
64 
65  map<string, DiscreteDistribution*> mpdd;
66  mpdd["omega"] = psdd;
67 
68  YN98* yn98 = new YN98(gc, codonFreqs);
69  pmixmodel_.reset(new MixtureOfASubstitutionModel(gc->getSourceAlphabet(), yn98, mpdd));
70  delete psdd;
71  vector<int> supportedChars = yn98->getAlphabetStates();
72 
73  // mapping the parameters
74 
75  ParameterList pl = pmixmodel_->getParameters();
76  for (size_t i = 0; i < pl.size(); i++)
77  {
78  lParPmodel_.addParameter(Parameter(pl[i]));
79  }
80 
81  vector<std::string> v = dynamic_cast<YN98*>(pmixmodel_->getNModel(0))->getFrequenciesSet()->getParameters().getParameterNames();
82 
83  for (size_t i = 0; i < v.size(); i++)
84  {
85  mapParNamesFromPmodel_[v[i]] = getParameterNameWithoutNamespace("YNGKP_M2." + v[i].substr(5));
86  }
87 
88  mapParNamesFromPmodel_["YN98.kappa"] = "kappa";
89  mapParNamesFromPmodel_["YN98.omega_Simple.V1"] = "omega0";
90  mapParNamesFromPmodel_["YN98.omega_Simple.theta1"] = "theta1";
91  mapParNamesFromPmodel_["YN98.omega_Simple.V3"] = "omega2";
92  mapParNamesFromPmodel_["YN98.omega_Simple.theta2"] = "theta2";
93 
94  // specific parameters
95 
96  string st;
97  for (map<string, string>::iterator it = mapParNamesFromPmodel_.begin(); it != mapParNamesFromPmodel_.end(); it++)
98  {
99  st = pmixmodel_->getParameterNameWithoutNamespace(it->first);
100  if (it->second.substr(0, 5) != "omega")
101  addParameter_(new Parameter("YNGKP_M2." + it->second, pmixmodel_->getParameterValue(st),
102  pmixmodel_->getParameter(st).hasConstraint() ? pmixmodel_->getParameter(st).getConstraint()->clone() : 0, true));
103  }
104 
105  addParameter_(new Parameter("YNGKP_M2.omega0", 0.5, new IntervalConstraint(NumConstants::MILLI(), 1, true, false), true));
106 
107  addParameter_(new Parameter("YNGKP_M2.omega2", 2, new IntervalConstraint(1, 999, false, false, NumConstants::MILLI()), true));
108 
109  // look for synonymous codons
110  for (synfrom_ = 1; synfrom_ < supportedChars.size(); ++synfrom_)
111  {
112  for (synto_ = 0; synto_ < synfrom_; ++synto_)
113  {
114  if (gc->areSynonymous(supportedChars[synfrom_], supportedChars[synto_])
115  && (pmixmodel_->getNModel(0)->Qij(synfrom_, synto_) != 0)
116  && (pmixmodel_->getNModel(1)->Qij(synfrom_, synto_) != 0))
117  break;
118  }
119  if (synto_ < synfrom_)
120  break;
121  }
122 
123  if (synto_ == supportedChars.size())
124  throw Exception("Impossible to find synonymous codons");
125 
126  // update Matrices
127 
128  updateMatrices();
129 }
130 
132  pmixmodel_(new MixtureOfASubstitutionModel(*mod2.pmixmodel_)),
133  synfrom_(mod2.synfrom_),
134  synto_(mod2.synto_)
135 {}
136 
138 {
140 
142  synfrom_ = mod2.synfrom_;
143  synto_ = mod2.synto_;
144 
145  return *this;
146 }
147 
149 
151 {
153 
154  // homogeneization of the synonymous substittion rates
155 
156  Vdouble vd;
157 
158  vd.push_back(1 / pmixmodel_->getNModel(0)->Qij(synfrom_, synto_));
159  vd.push_back(1 / pmixmodel_->getNModel(1)->Qij(synfrom_, synto_));
160  vd.push_back(1 / pmixmodel_->getNModel(2)->Qij(synfrom_, synto_));
161 
162  pmixmodel_->setVRates(vd);
163 }
164 
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.
std::auto_ptr< MixtureOfASubstitutionModel > pmixmodel_
Definition: YNGKP_M2.h:80
STL namespace.
size_t synto_
Definition: YNGKP_M2.h:86
Parametrize a set of state frequencies.
void updateMatrices()
Definition: YNGKP_M2.cpp:150
const std::vector< int > & getAlphabetStates() const
YNGKP_M2(const GeneticCode *gc, FrequenciesSet *codonFreqs)
Definition: YNGKP_M2.cpp:51
size_t synfrom_
indexes of 2 codons between which the substitution is synonymous, to set a basis to the homogeneizati...
Definition: YNGKP_M2.h:86
AbstractBiblioMixedSubstitutionModel & operator=(const AbstractBiblioMixedSubstitutionModel &model)
The Yang and Nielsen (1998) substitution model for codons.
Definition: YN98.h:88
Substitution models defined as a mixture of nested substitution models.
The Yang et al (2000) M2 substitution model for codons, with the more realistic modification in Wong ...
Definition: YNGKP_M2.h:75
virtual ~YNGKP_M2()
Definition: YNGKP_M2.cpp:148
const FrequenciesSet * getFrequenciesSet() const
If the model owns a FrequenciesSet, returns a pointer to it, otherwise return 0.
Definition: YNGKP_M2.h:114
YNGKP_M2 & operator=(const YNGKP_M2 &)
Definition: YNGKP_M2.cpp:137