bpp-phyl  2.1.0
 All Classes Namespaces Files Functions Variables Friends Pages
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 
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_(-1),
57  synto_(-1)
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);
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 
75 
76  map<string, DiscreteDistribution*> mpdd;
77  mpdd["omega"] = pmodd;
78 
79  pmixmodel_.reset(new MixtureOfASubstitutionModel(gc->getSourceAlphabet(), new YN98(gc, codonFreqs), mpdd));
80  delete pbdd;
81 
82  // mapping the parameters
83 
84  ParameterList pl = pmixmodel_->getParameters();
85  for (size_t i = 0; i < pl.size(); i++)
86  {
88  }
89 
90  vector<std::string> v = dynamic_cast<YN98*>(pmixmodel_->getNModel(0))->getFrequenciesSet()->getParameters().getParameterNames();
91 
92  for (size_t i = 0; i < v.size(); i++)
93  {
94  mapParNamesFromPmodel_[v[i]] = getParameterNameWithoutNamespace("YNGKP_M8." + v[i].substr(5));
95  }
96 
97  mapParNamesFromPmodel_["YN98.kappa"] = "kappa";
98  mapParNamesFromPmodel_["YN98.omega_Mixture.theta1"] = "p0";
99  mapParNamesFromPmodel_["YN98.omega_Mixture.1_Beta.alpha"] = "p";
100  mapParNamesFromPmodel_["YN98.omega_Mixture.1_Beta.beta"] = "q";
101  mapParNamesFromPmodel_["YN98.omega_Mixture.2_Simple.V1"] = "omegas";
102 
103  // specific parameters
104 
105  string st;
106  for (map<string, string>::iterator it = mapParNamesFromPmodel_.begin(); it != mapParNamesFromPmodel_.end(); it++)
107  {
108  st = pmixmodel_->getParameterNameWithoutNamespace(it->first);
109  if (it->second != "omegas")
110  addParameter_(new Parameter("YNGKP_M8." + it->second, pmixmodel_->getParameterValue(st),
111  pmixmodel_->getParameter(st).hasConstraint() ? pmixmodel_->getParameter(st).getConstraint()->clone() : 0, true));
112  }
113 
114  addParameter_(new Parameter("YNGKP_M8.omegas", 2., new IntervalConstraint(1, 1, false), true));
115 
116  // look for synonymous codons
117  for (synfrom_ = 1; synfrom_ < (int)gc->getSourceAlphabet()->getSize(); synfrom_++)
118  {
119  for (synto_ = 0; synto_ < synfrom_; synto_++)
120  {
121  if ((gc->areSynonymous(synfrom_, synto_))
122  && (pmixmodel_->getNModel(0)->Qij(synfrom_, synto_) != 0)
123  && (pmixmodel_->getNModel(1)->Qij(synfrom_, synto_) != 0))
124  break;
125  }
126  if (synto_ < synfrom_)
127  break;
128  }
129 
130  if (synto_ == (int)gc->getSourceAlphabet()->getSize())
131  throw Exception("Impossible to find synonymous codons");
132 
133  // update Matrices
134  updateMatrices();
135 }
136 
138  pmixmodel_(new MixtureOfASubstitutionModel(*mod2.pmixmodel_)),
139  synfrom_(mod2.synfrom_),
140  synto_(mod2.synto_)
141 {}
142 
144 {
146 
148  synfrom_ = mod2.synfrom_;
149  synto_ = mod2.synto_;
150 
151  return *this;
152 }
153 
155 
157 {
159 
160  // homogeneization of the synonymous substittion rates
161 
162  Vdouble vd;
163 
164  for (unsigned int i = 0; i < pmixmodel_->getNumberOfModels(); i++)
165  {
166  vd.push_back(1 / pmixmodel_->getNModel(i)->Qij(synfrom_, synto_));
167  }
168 
169  pmixmodel_->setVRates(vd);
170 }
171