bpp-phyl  2.2.0
YNGKP_M3.cpp
Go to the documentation of this file.
1 //
2 // File: YNGKP_M3.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_M3.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_M3::YNGKP_M3(const GeneticCode* gc, FrequenciesSet* codonFreqs, unsigned int nbOmega) :
53  pmixmodel_(0),
54  synfrom_(),
55  synto_()
56 {
57  if (nbOmega < 1)
58  throw Exception("At least one omega is necessary in the YNGKP_M3 model");
59 
60  // build the submodel
61 
62  vector<double> v1, v2;
63  v1.push_back(0.5);
64  for (unsigned int i = 1; i < nbOmega; i++)
65  {
66  v1.push_back(0.5 + 0.5 * i);
67  }
68 
69  for (unsigned int i = 0; i < nbOmega; i++)
70  {
71  v2.push_back(1. / nbOmega);
72  }
73 
74  SimpleDiscreteDistribution* psdd = new SimpleDiscreteDistribution(v1, v2, NumConstants::MILLI());
75 
76  map<string, DiscreteDistribution*> mpdd;
77  mpdd["omega"] = psdd;
78 
79  YN98* yn98 = new YN98(gc, codonFreqs);
80  pmixmodel_.reset(new MixtureOfASubstitutionModel(gc->getSourceAlphabet(), yn98, mpdd));
81  delete psdd;
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  for (size_t i = 0; i < v.size(); ++i)
94  {
95  mapParNamesFromPmodel_[v[i]] = getParameterNameWithoutNamespace("YNGKP_M3." + v[i].substr(5));
96  }
97 
98  mapParNamesFromPmodel_["YN98.kappa"] = "kappa";
99 
100  for (size_t i = 1; i < nbOmega; ++i)
101  {
102  mapParNamesFromPmodel_["YN98.omega_Simple.theta" + TextTools::toString(i)] = "theta" + TextTools::toString(i);
103  }
104 
105 
106  mapParNamesFromPmodel_["YN98.omega_Simple.V1"] = "omega0";
107  for (size_t i = 1; i < nbOmega; ++i)
108  {
109  mapParNamesFromPmodel_["YN98.omega_Simple.V" + TextTools::toString(i + 1)] = "delta" + TextTools::toString(i);
110  }
111 
112  // specific parameters
113 
114  string st;
115  for (map<string, string>::iterator it = mapParNamesFromPmodel_.begin(); it != mapParNamesFromPmodel_.end(); it++)
116  {
117  st = pmixmodel_->getParameterNameWithoutNamespace(it->first);
118  if (it->second.substr(0, 5) != "delta")
119  addParameter_(new Parameter("YNGKP_M3." + it->second, pmixmodel_->getParameterValue(st),
120  pmixmodel_->getParameter(st).hasConstraint() ? pmixmodel_->getParameter(st).getConstraint()->clone() : 0, true));
121  }
122 
123  for (size_t i = 1; i < nbOmega; ++i)
124  {
125  addParameter_(new Parameter("YNGKP_M3.delta" + TextTools::toString(i), 0.5, new IntervalConstraint(NumConstants::MILLI(), 999, true, true, NumConstants::MILLI()), true));
126  }
127 
128  // look for synonymous codons
129  for (synfrom_ = 1; synfrom_ < supportedChars.size(); synfrom_++)
130  {
131  for (synto_ = 0; synto_ < synfrom_; synto_++)
132  {
133  if (gc->areSynonymous(supportedChars[synfrom_], supportedChars[synto_])
134  && (pmixmodel_->getNModel(0)->Qij(synfrom_, synto_) != 0)
135  && (pmixmodel_->getNModel(1)->Qij(synfrom_, synto_) != 0))
136  break;
137  }
138  if (synto_ < synfrom_)
139  break;
140  }
141 
142  if (synto_ == supportedChars.size())
143  throw Exception("Impossible to find synonymous codons");
144 
145  // update Matrices
146  updateMatrices();
147 }
148 
150  pmixmodel_(new MixtureOfASubstitutionModel(*mod2.pmixmodel_)),
151  synfrom_(mod2.synfrom_),
152  synto_(mod2.synto_)
153 {}
154 
156 {
158 
160  synfrom_ = mod2.synfrom_;
161  synto_ = mod2.synto_;
162 
163  return *this;
164 }
165 
167 
169 {
170  for (unsigned int i = 0; i < lParPmodel_.size(); i++)
171  {
172  if (mapParNamesFromPmodel_.find(lParPmodel_[i].getName()) != mapParNamesFromPmodel_.end())
173  {
174  if (lParPmodel_[i].getName()[18] == 'V')
175  {
176  size_t ind = TextTools::to<size_t>(lParPmodel_[i].getName().substr(19));
177  double x = getParameterValue("omega0");
178  for (unsigned j = 1; j < ind; j++)
179  {
180  x += getParameterValue("delta" + TextTools::toString(j));
181  }
182  lParPmodel_[i].setValue(x);
183  }
184  else
185  {
186  lParPmodel_[i].setValue(getParameter(getParameterNameWithoutNamespace(mapParNamesFromPmodel_[lParPmodel_[i].getName()])).getValue());
187  }
188  }
189  }
190 
191  pmixmodel_->matchParametersValues(lParPmodel_);
192 
193  // homogeneization of the synonymous substitution rates
194 
195 
196  Vdouble vd;
197 
198  for (unsigned int i = 0; i < pmixmodel_->getNumberOfModels(); i++)
199  {
200  vd.push_back(1 / pmixmodel_->getNModel(i)->Qij(synfrom_, synto_));
201  }
202 
203  pmixmodel_->setVRates(vd);
204 }
205 
206 
std::map< std::string, std::string > mapParNamesFromPmodel_
Tools to make the link between the Parameters of the object and those of pmixmodel_.
size_t synfrom_
indexes of 2 codons between which the substitution is synonymous, to set a basis to the homogeneizati...
Definition: YNGKP_M3.h:87
Abstract class for mixture models based on the bibliography.
std::string getName() const
Get the name of the model.
Definition: YNGKP_M3.h:108
size_t synto_
Definition: YNGKP_M3.h:87
STL namespace.
Parametrize a set of state frequencies.
const std::vector< int > & getAlphabetStates() const
std::auto_ptr< MixtureOfASubstitutionModel > pmixmodel_
Definition: YNGKP_M3.h:80
AbstractBiblioMixedSubstitutionModel & operator=(const AbstractBiblioMixedSubstitutionModel &model)
The Yang and Nielsen (1998) substitution model for codons.
Definition: YN98.h:88
const FrequenciesSet * getFrequenciesSet() const
If the model owns a FrequenciesSet, returns a pointer to it, otherwise return 0.
Definition: YNGKP_M3.h:115
void updateMatrices()
Definition: YNGKP_M3.cpp:168
The Yang et al (2000) M3 substitution model for codons.
Definition: YNGKP_M3.h:75
Substitution models defined as a mixture of nested substitution models.
YNGKP_M3 & operator=(const YNGKP_M3 &)
Definition: YNGKP_M3.cpp:155
YNGKP_M3(const GeneticCode *gc, FrequenciesSet *codonFreqs, unsigned int nclass=3)
Definition: YNGKP_M3.cpp:51