bpp-phyl  2.2.0
AbstractCodonSubstitutionModel.cpp
Go to the documentation of this file.
1 //
2 // File: CodonSubstitutionModel.cpp
3 // Created by: Laurent Gueguen
4 // Created on: Feb 2009
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 
40 
41 using namespace bpp;
42 
43 using namespace std;
44 
45 /******************************************************************************/
46 
48  const GeneticCode* gCode,
50  const std::string& prefix,
51  bool paramRates) :
52  AbstractParameterAliasable(prefix),
53  AbstractWordSubstitutionModel(gCode->getSourceAlphabet(), new CanonicalStateMap(gCode->getSourceAlphabet(), false), prefix),
54  hasParametrizedRates_(paramRates),
55  gCode_(gCode)
56 {
58 
59  size_t i;
60  for (i = 0; i < 3; i++)
61  {
62  VSubMod_.push_back(pmod);
63  VnestedPrefix_.push_back(pmod->getNamespace());
64  }
65 
66  pmod->setNamespace(prefix + "123_" + VnestedPrefix_[0]);
67  pmod->enableEigenDecomposition(0);
68  addParameters_(pmod->getParameters());
69 
70  Vrate_.resize(3);
71  for (i = 0; i < 3; i++)
72  {
73  Vrate_[i] = 1.0 / 3;
74  }
75 
76 
78  {
79  // relative rates
80  for (i = 0; i < 2; i++)
81  {
82  addParameter_(new Parameter(prefix + "relrate" + TextTools::toString(i + 1), 1.0 / static_cast<double>(3 - i), &Parameter::PROP_CONSTRAINT_EX));
83  }
84  }
85 }
86 
88  const GeneticCode* gCode,
92  const std::string& prefix,
93  bool paramRates) :
94  AbstractParameterAliasable(prefix),
95  AbstractWordSubstitutionModel(gCode->getSourceAlphabet(), new CanonicalStateMap(gCode->getSourceAlphabet(), false), prefix),
96  hasParametrizedRates_(paramRates),
97  gCode_(gCode)
98 {
100 
101  if ((pmod1 == pmod2) || (pmod2 == pmod3) || (pmod1 == pmod3))
102  {
103  for (size_t i = 0; i < 3; ++i)
104  {
105  VSubMod_.push_back(pmod1);
106  VnestedPrefix_.push_back(pmod1->getNamespace());
107  }
108 
109  pmod1->setNamespace(prefix + "123_" + VnestedPrefix_[0]);
110  pmod1->enableEigenDecomposition(0);
111  addParameters_(pmod1->getParameters());
112  }
113  else
114  {
115  VSubMod_.push_back(pmod1);
116  VnestedPrefix_.push_back(pmod1->getNamespace());
117  VSubMod_[0]->setNamespace(prefix + "1_" + VnestedPrefix_[0]);
118  VSubMod_[0]->enableEigenDecomposition(0);
119  addParameters_(pmod1->getParameters());
120 
121  VSubMod_.push_back(pmod2);
122  VnestedPrefix_.push_back(pmod2->getNamespace());
123  VSubMod_[1]->setNamespace(prefix + "2_" + VnestedPrefix_[1]);
124  VSubMod_[1]->enableEigenDecomposition(0);
125  addParameters_(pmod2->getParameters());
126 
127  VSubMod_.push_back(pmod3);
128  VnestedPrefix_.push_back(pmod3->getNamespace());
129  VSubMod_[2]->setNamespace(prefix + "3_" + VnestedPrefix_[2]);
130  VSubMod_[2]->enableEigenDecomposition(0);
131  addParameters_(pmod3->getParameters());
132  }
133 
134  Vrate_.resize(3);
135  for (size_t i = 0; i < 3; ++i)
136  {
137  Vrate_[i] = 1.0 / 3;
138  }
139 
141  {
142  // relative rates
143  for (int i = 0; i < 2; ++i)
144  {
145  addParameter_(new Parameter(prefix + "relrate" + TextTools::toString(i + 1), 1.0 / (3.0 - i), &Parameter::PROP_CONSTRAINT_EX));
146  }
147  }
148 }
149 
151 {
153  {
154  size_t i, nbmod = VSubMod_.size();
155  double x;
156  size_t k;
157  for (k = nbmod - 1; k > 0; k--)
158  {
159  x = 1.0;
160  for (i = 0; i < k; i++)
161  {
162  x *= 1 - getParameterValue("relrate" + TextTools::toString(i + 1));
163  }
164  if (k != nbmod - 1)
165  x *= getParameterValue("relrate" + TextTools::toString(k + 1));
166  Vrate_[k] = x;
167  }
168  }
169 
171 }
172 
174 {
175  size_t i, j;
176  size_t salph = getNumberOfStates();
177 
178  for (i = 0; i < salph; i++)
179  {
180  for (j = 0; j < salph; j++)
181  {
182  if (gCode_->isStop(static_cast<int>(i)) || gCode_->isStop(static_cast<int>(j)))
183  {
184  generator_(i, j) = 0;
185  }
186  else
187  generator_(i, j) *= getCodonsMulRate(i, j);
188  }
189  }
190 }
191 
192 
bool enableEigenDecomposition()
Tell if eigenValues and Vectors must be computed.
AbstractCodonSubstitutionModel(const GeneticCode *gCode, NucleotideSubstitutionModel *pmod, const std::string &st, bool paramRates=false)
Build a new AbstractCodonSubstitutionModel object from a pointer to a NucleotideSubstitutionModel.
This class implements a state map where all resolved states are modeled.
Definition: StateMap.h:161
STL namespace.
virtual size_t getNumberOfStates() const
Get the number of states.
virtual void enableEigenDecomposition(bool yn)=0
Set if eigenValues and Vectors must be computed.
bool hasParametrizedRates_
boolean for the parametrization of the position relative rates. Default : false.
void updateMatrices()
Diagonalize the matrix, and fill the eigenValues_, iEigenValues_, leftEigenVectors_ and rightEigenVe...
Abstract Basal class for words of substitution models.
RowMatrix< double > generator_
The generator matrix of the model.
Specialisation interface for nucleotide substitution model.
void completeMatrices()
Method inherited from AbstractWordSubstitutionModel.
void updateMatrices()
Diagonalize the matrix, and fill the eigenValues_, iEigenValues_, leftEigenVectors_ and rightEigenVe...
virtual double getCodonsMulRate(size_t i, size_t j) const
Method inherited from CodonSubstitutionModel.
std::vector< SubstitutionModel * > VSubMod_