bpp-phyl  2.2.0
WordSubstitutionModel.cpp
Go to the documentation of this file.
1 //
2 // File: WordSubstitutionModel.cpp
3 // Created by: Laurent Gueguen
4 // Created on: Jan 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 
39 #include "WordSubstitutionModel.h"
41 
42 #include <Bpp/Numeric/Matrix/MatrixTools.h>
43 #include <Bpp/Numeric/VectorTools.h>
44 #include <Bpp/Numeric/Matrix/EigenValue.h>
45 
46 // From SeqLib:
47 #include <Bpp/Seq/Alphabet/WordAlphabet.h>
48 #include <Bpp/Seq/Container/SequenceContainerTools.h>
49 
50 using namespace bpp;
51 
52 // From the STL:
53 #include <cmath>
54 
55 using namespace std;
56 
57 /******************************************************************************/
58 
60  const std::vector<SubstitutionModel*>& modelVector,
61  const std::string& prefix) :
62  AbstractParameterAliasable((prefix == "") ? "Word." : prefix),
64  (prefix == "") ? "Word." : prefix)
65 {
66  size_t i, nbmod = VSubMod_.size();
67 
68  // relative rates
69  for (i = 0; i < nbmod - 1; i++)
70  {
71  addParameter_(new Parameter("Word.relrate" + TextTools::toString(i + 1), 1.0 / static_cast<int>(nbmod - i), &Parameter::PROP_CONSTRAINT_EX));
72  }
73 
75 }
76 
78  const Alphabet* alph,
79  StateMap* stateMap,
80  const std::string& prefix) :
81  AbstractParameterAliasable((prefix == "") ? "Word." : prefix),
82  AbstractWordSubstitutionModel(alph, stateMap, (prefix == "") ? "Word." : prefix)
83 {}
84 
86  SubstitutionModel* pmodel,
87  unsigned int num,
88  const std::string& prefix) :
89  AbstractParameterAliasable((prefix == "") ? "Word." : prefix),
91  num,
92  (prefix == "") ? "Word." : prefix)
93 {
94  size_t i;
95 
96  // relative rates
97  for (i = 0; i < num - 1; i++)
98  {
99  addParameter_(new Parameter("Word.relrate" + TextTools::toString(i + 1), 1.0 / static_cast<int>(num - i ), &Parameter::PROP_CONSTRAINT_EX));
100  }
101 
103 }
104 
106 {
107  size_t i, nbmod = VSubMod_.size();
108  double x, y;
109  x = 1.0;
110 
111  for (i = 0; i < nbmod - 1; i++)
112  {
113  y = getParameterValue("relrate" + TextTools::toString(i + 1));
114  Vrate_[i] = x * y;
115  x *= 1 - y;
116  }
117  Vrate_[nbmod - 1] = x;
118 
120 }
121 
123 {
124  size_t nbmod = VSubMod_.size();
125  size_t i, p, j, m;
126  size_t salph = getAlphabet()->getSize();
127 
128  // freq_ for this generator
129 
130  for (i = 0; i < salph; i++)
131  {
132  freq_[i] = 1;
133  j = i;
134  for (p = nbmod; p > 0; p--)
135  {
136  m = VSubMod_[p-1]->getNumberOfStates();
137  freq_[i] *= VSubMod_[p-1]->getFrequencies()[j % m];
138  j /= m;
139  }
140  }
141 }
142 
143 const RowMatrix<double>& WordSubstitutionModel::getPij_t(double d) const
144 {
145  vector<const Matrix<double>*> vM;
146  size_t nbmod = VSubMod_.size();
147  size_t i, j;
148 
149  for (i = 0; i < nbmod; i++)
150  {
151  vM.push_back(&VSubMod_[i]->getPij_t(d * Vrate_[i] * rate_));
152  }
153 
154  size_t t;
155  double x;
156  size_t i2, j2;
157  size_t nbStates = getNumberOfStates();
158  size_t p;
159 
160  for (i = 0; i < nbStates; i++)
161  {
162  for (j = 0; j < nbStates; j++)
163  {
164  x = 1.;
165  i2 = i;
166  j2 = j;
167  for (p = nbmod; p > 0; p--)
168  {
169  t = VSubMod_[p - 1]->getNumberOfStates();
170  x *= (*vM[p - 1])(i2 % t, j2 % t);
171  i2 /= t;
172  j2 /= t;
173  }
174  pijt_(i, j) = x;
175  }
176  }
177  return pijt_;
178 }
179 
180 const RowMatrix<double>& WordSubstitutionModel::getdPij_dt(double d) const
181 {
182  vector<const Matrix<double>*> vM, vdM;
183  size_t nbmod = VSubMod_.size();
184  size_t i, j;
185 
186  for (i = 0; i < nbmod; i++)
187  {
188  vM.push_back(&VSubMod_[i]->getPij_t(d * Vrate_[i] * rate_));
189  vdM.push_back(&VSubMod_[i]->getdPij_dt(d * Vrate_[i] * rate_));
190  }
191 
192  size_t t;
193  double x, r;
194  size_t i2, j2;
195  size_t nbStates = getNumberOfStates();
196  size_t p, q;
197 
198  for (i = 0; i < nbStates; i++)
199  {
200  for (j = 0; j < nbStates; j++)
201  {
202  r = 0;
203  for (q = 0; q < nbmod; q++)
204  {
205  i2 = i;
206  j2 = j;
207  x = 1;
208  for (p = nbmod; p > 0; p--)
209  {
210  t = VSubMod_[p - 1]->getNumberOfStates();
211  if (q != p - 1)
212  x *= (*vM[p - 1])(i2 % t, j2 % t);
213  else
214  x *= rate_ * Vrate_[p - 1] * (*vdM[p - 1])(i2 % t, j2 % t);
215  i2 /= t;
216  j2 /= t;
217  }
218  r += x;
219  }
220  dpijt_(i, j) = r;
221  }
222  }
223  return dpijt_;
224 }
225 
226 const RowMatrix<double>& WordSubstitutionModel::getd2Pij_dt2(double d) const
227 
228 {
229  vector<const Matrix<double>*> vM, vdM, vd2M;
230  size_t nbmod = VSubMod_.size();
231  size_t i, j;
232 
233  for (i = 0; i < nbmod; i++)
234  {
235  vM.push_back(&VSubMod_[i]->getPij_t(d * Vrate_[i] * rate_));
236  vdM.push_back(&VSubMod_[i]->getdPij_dt(d * Vrate_[i] * rate_));
237  vd2M.push_back(&VSubMod_[i]->getd2Pij_dt2(d * Vrate_[i] * rate_));
238  }
239 
240 
241  double r, x;
242  size_t p, b, q, t;
243 
244  size_t i2, j2;
245  size_t nbStates = getNumberOfStates();
246 
247 
248  for (i = 0; i < nbStates; i++)
249  {
250  for (j = 0; j < nbStates; j++)
251  {
252  r = 0;
253  for (q = 1; q < nbmod; q++)
254  {
255  for (b = 0; b < q; b++)
256  {
257  x = 1;
258  i2 = i;
259  j2 = j;
260  for (p = nbmod; p > 0; p--)
261  {
262  t = VSubMod_[p - 1]->getNumberOfStates();
263  if ((p - 1 == q) || (p - 1 == b))
264  x *= rate_ * Vrate_[p - 1] * (*vdM[p - 1])(i2 % t, j2 % t);
265  else
266  x *= (*vM[p - 1])(i2 % t, j2 % t);
267 
268  i2 /= t;
269  j2 /= t;
270  }
271  r += x;
272  }
273  }
274 
275  r *= 2;
276 
277  for (q = 0; q < nbmod; q++)
278  {
279  x = 1;
280  i2 = i;
281  j2 = j;
282  for (p = nbmod; p > 0; p--)
283  {
284  t = VSubMod_[p - 1]->getNumberOfStates();
285  if (q != p - 1)
286  x *= (*vM[p - 1])(i2 % t, j2 % t);
287  else
288  x *= rate_ * rate_ * Vrate_[p - 1] * Vrate_[p - 1] * (*vd2M[p - 1])(i2 % t, j2 % t);
289 
290  i2 /= t;
291  j2 /= t;
292  }
293  r += x;
294  }
295  d2pijt_(i, j) = r;
296  }
297  }
298  return d2pijt_;
299 }
300 
302 {
303  size_t nbmod = VSubMod_.size();
304  string s = "WordSubstitutionModel model: " + VSubMod_[0]->getName();
305  for (size_t i = 1; i < nbmod - 1; i++)
306  {
307  s += " " + VSubMod_[i]->getName();
308  }
309 
310  return s;
311 }
312 
313 
Interface for all substitution models.
RowMatrix< double > pijt_
These ones are for bookkeeping:
virtual const RowMatrix< double > & getPij_t(double d) const
STL namespace.
Vdouble freq_
The vector of equilibrium frequencies.
virtual size_t getNumberOfStates() const
Get the number of states.
virtual void completeMatrices()
Called by updateMatrices to handle specific modifications for inheriting classes. ...
virtual const RowMatrix< double > & getd2Pij_dt2(double d) const
Abstract Basal class for words of substitution models.
virtual const RowMatrix< double > & getdPij_dt(double d) const
virtual std::string getName() const
Get the name of the model.
double rate_
The rate of the model (default: 1). The generator (and all its vectorial components) is independent o...
virtual void updateMatrices()
Diagonalize the matrix, and fill the eigenValues_, iEigenValues_, leftEigenVectors_ and rightEigenVe...
WordSubstitutionModel(const std::vector< SubstitutionModel *> &modelVector, const std::string &prefix="")
Build a new WordSubstitutionModel object from a Vector of pointers to SubstitutionModels.
void updateMatrices()
Diagonalize the matrix, and fill the eigenValues_, iEigenValues_, leftEigenVectors_ and rightEigenVe...
Map the states of a given alphabet which have a model state.
Definition: StateMap.h:58
std::vector< SubstitutionModel * > VSubMod_