bpp-phyl  2.2.0
LaplaceSubstitutionCount.cpp
Go to the documentation of this file.
1 //
2 // File: LaplaceSubstitutionCount.cpp
3 // Created by: Julien Dutheil
4 // Created on: Wed Apr 5 11:21 2006
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (November 16, 2004, 2005, 2006)
9 
10  This software is a computer program whose purpose is to provide classes
11  for phylogenetic data analysis.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided only
21  with a limited warranty and the software's author, the holder of the
22  economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards their
32  requirements in conditions enabling the security of their systems and/or
33  data to be ensured and, more generally, to use and operate it in the
34  same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38  */
39 
41 
42 #include <Bpp/Numeric/Matrix/MatrixTools.h>
43 
44 using namespace bpp;
45 
46 /******************************************************************************/
47 
48 void LaplaceSubstitutionCount::computeCounts(double length) const
49 {
50  RowMatrix<double> Q = model_->getGenerator();
51  // L is the diagonal matrix with all substitution rates.
52  size_t s = Q.getNumberOfRows();
53  RowMatrix<double> QL(s, s);
54  for (size_t i = 0; i < s; i++)
55  {
56  for (size_t j = 0; j < s; j++)
57  {
58  QL(i, j) = ((i == j) ? 0. : Q(i, j));
59  }
60  }
61 
62  MatrixTools::fill(m_, 0.);
63  RowMatrix<double> M2(s, s);
64  RowMatrix<double> M3(s, s);
65  RowMatrix<double> M4(s, s);
66  RowMatrix<double> M5(s, s);
67  for (size_t n = 1; n < cutOff_; ++n)
68  {
69  MatrixTools::fill(M2, 0.);
70  for (size_t p = 0; p < n; ++p)
71  {
72  MatrixTools::pow(Q, p, M3); // Q^p -> M5
73  MatrixTools::mult(M3, QL, M4); // Q^p . QL -> M4
74  MatrixTools::pow(Q, n - p - 1, M3); // Q^(n-p-1) -> M3
75  MatrixTools::mult(M4, M3, M5); // Q^p . QL . Q^(n-p-1) -> M5
76  MatrixTools::add(M2, M5);
77  }
78  MatrixTools::scale(M2, pow(length, static_cast<double>(n)) / static_cast<double>(NumTools::fact(n)));
79  MatrixTools::add(m_, M2);
80  }
81 
82  // Now we must divide by pijt:
83  RowMatrix<double> P = model_->getPij_t(length);
84  for (size_t i = 0; i < s; i++)
85  {
86  for (size_t j = 0; j < s; j++)
87  {
88  m_(i, j) /= P(i, j);
89  }
90  }
91 }
92 
93 /******************************************************************************/
94 
95 double LaplaceSubstitutionCount::getNumberOfSubstitutions(size_t initialState, size_t finalState, double length, size_t type) const
96 {
97  if (length == currentLength_)
98  return m_(initialState, finalState);
99  if (length < 0.000001)
100  return initialState == finalState ? 0. : 1.; // Limit case!
101  // Else we need to recompute M:
102  computeCounts(length);
103 
104  currentLength_ = length;
105  return m_(initialState, finalState);
106 }
107 
108 /******************************************************************************/
109 
110 Matrix<double>* LaplaceSubstitutionCount::getAllNumbersOfSubstitutions(double length, size_t type) const
111 {
112  if (length == currentLength_)
113  return new RowMatrix<double>(m_);
114  if (length < 0.000001) // Limit case!
115  {
116  size_t s = model_->getAlphabet()->getSize();
117  for (size_t i = 0; i < s; i++)
118  {
119  for (size_t j = 0; j < s; j++)
120  {
121  m_(i, j) = i == j ? 0. : 1.;
122  }
123  }
124  }
125  else
126  {
127  // Else we need to recompute M:
128  computeCounts(length);
129  }
130 
131  currentLength_ = length;
132 
133  return new RowMatrix<double>(m_);
134 }
135 
136 /******************************************************************************/
137 
139 {
140  model_ = model;
141  size_t n = model->getAlphabet()->getSize();
142  m_.resize(n, n);
143  // Recompute counts:
145 }
146 
147 /******************************************************************************/
148 
void setSubstitutionModel(const SubstitutionModel *model)
Set the substitution model associated with this count, if relevent.
Interface for all substitution models.
void computeCounts(double length) const
virtual const Alphabet * getAlphabet() const =0
virtual const Matrix< double > & getPij_t(double t) const =0
virtual const Matrix< double > & getGenerator() const =0
Matrix< double > * getAllNumbersOfSubstitutions(double length, size_t type=1) const
Get the numbers of susbstitutions on a branch, for each initial and final states, and given the branc...
const SubstitutionModel * model_
double getNumberOfSubstitutions(size_t initialState, size_t finalState, double length, size_t type=1) const
Get the number of susbstitutions on a branch, given the initial and final states, and the branch leng...