bpp-phyl  2.2.0
JCprot.cpp
Go to the documentation of this file.
1 //
2 // File: JCprot.cpp
3 // Created by: Julien Dutheil
4 // Created on: Tue May 27 16:04:36 2003
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
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 
40 #include "JCprot.h"
41 
42 //From bpp-seq:
43 #include <Bpp/Seq/Container/SequenceContainerTools.h>
44 
45 using namespace bpp;
46 
47 #include <cmath>
48 #include <map>
49 
50 using namespace std;
51 
52 /******************************************************************************/
53 
54 JCprot::JCprot(const ProteicAlphabet* alpha) :
55  AbstractParameterAliasable("JC69."),
56  AbstractReversibleSubstitutionModel(alpha, new CanonicalStateMap(alpha, false), "JC69."),
57  exp_(), p_(size_, size_), freqSet_(0)
58 {
61 }
62 
63 JCprot::JCprot(const ProteicAlphabet* alpha, ProteinFrequenciesSet* freqSet, bool initFreqs) :
64  AbstractParameterAliasable("JC69+F."),
65  AbstractReversibleSubstitutionModel(alpha, new CanonicalStateMap(alpha, false), "JC69+F."),
66  exp_(), p_(size_, size_), freqSet_(freqSet)
67 {
68  freqSet_->setNamespace("JC69+F."+freqSet_->getNamespace());
69  if (initFreqs) freqSet_->setFrequencies(freq_);
70  else freq_ = freqSet_->getFrequencies();
71  addParameters_(freqSet_->getParameters());
72  updateMatrices();
73 }
74 
75 
76 /******************************************************************************/
77 
79 {
80  // Frequencies:
81  for (unsigned int i = 0; i < 20; i++) freq_[i] = 1. / 20.;
82 
83  // Generator:
84  for (unsigned int i = 0; i < 20; i++)
85  {
86  for (unsigned int j = 0; j < 20; j++)
87  {
88  generator_(i, j) = (i == j) ? -1. : 1./19.;
89  exchangeability_(i, j) = generator_(i, j) * 20.;
90  }
91  }
92 
93  // Eigen values:
94  eigenValues_[0] = 0;
95  for (unsigned int i = 1; i < 20; i++) eigenValues_[i] = -20. / 19.;
96 
97  // Eigen vectors:
98  for (unsigned int i = 0; i < 20; i++) leftEigenVectors_(0,i) = 1./20.;
99  for (unsigned int i = 1; i < 20; i++)
100  for (unsigned int j = 0; j < 20; j++)
101  leftEigenVectors_(i,j) = -1./20.;
102  for (unsigned int i = 0; i < 19; i++) leftEigenVectors_(19-i,i) = 19./20.;
103 
104  for (unsigned int i = 0; i < 20; i++) rightEigenVectors_(i,0) = 1.;
105  for (unsigned int i = 1; i < 20; i++) rightEigenVectors_(19,i) = -1.;
106  for (unsigned int i = 0; i < 19; i++)
107  for (unsigned int j = 1; j < 20; j++)
108  rightEigenVectors_(i,j) = 0.;
109  for (unsigned int i = 1; i < 20; i++) rightEigenVectors_(19-i,i) = 1.;
110 
111 }
112 
113 /******************************************************************************/
114 
115 double JCprot::Pij_t(size_t i, size_t j, double d) const
116 {
117  if(i == j) return 1./20. + 19./20. * exp(- rate_ * 20./19. * d);
118  else return 1./20. - 1./20. * exp(- rate_ * 20./19. * d);
119 }
120 
121 /******************************************************************************/
122 
123 double JCprot::dPij_dt(size_t i, size_t j, double d) const
124 {
125  if(i == j) return - rate_ * exp(- rate_ * 20./19. * d);
126  else return rate_ * 1./19. * exp(- rate_ * 20./19. * d);
127 }
128 
129 /******************************************************************************/
130 
131 double JCprot::d2Pij_dt2(size_t i, size_t j, double d) const
132 {
133  if(i == j) return rate_ * rate_ * 20./19. * exp(- rate_ * 20./19. * d);
134  else return - rate_ * rate_ * 20./361. * exp(- rate_ * 20./19. * d);
135 }
136 
137 /******************************************************************************/
138 
139 const Matrix<double>& JCprot::getPij_t(double d) const
140 {
141  exp_ = exp(- rate_ * 20./19. * d);
142  for(unsigned int i = 0; i < size_; i++)
143  {
144  for(unsigned int j = 0; j < size_; j++)
145  {
146  p_(i,j) = (i==j) ? 1./20. + 19./20. * exp_ : 1./20. - 1./20. * exp_;
147  }
148  }
149  return p_;
150 }
151 
152 const Matrix<double>& JCprot::getdPij_dt(double d) const
153 {
154  exp_ = exp(- rate_ * 20./19. * d);
155  for(unsigned int i = 0; i < size_; i++)
156  {
157  for(unsigned int j = 0; j < size_; j++)
158  {
159  p_(i,j) = rate_ * ((i==j) ? - exp_ : 1./19. * exp_);
160  }
161  }
162  return p_;
163 }
164 
165 const Matrix<double>& JCprot::getd2Pij_dt2(double d) const
166 {
167  exp_ = exp( rate_ * - 20./19. * d);
168  for(unsigned int i = 0; i < size_; i++)
169  {
170  for(unsigned int j = 0; j < size_; j++)
171  {
172  p_(i,j) = rate_ * rate_ * ((i==j) ? 20./19. * exp_ : - 20./361. * exp_);
173  }
174  }
175  return p_;
176 }
177 
178 /******************************************************************************/
179 
180 void JCprot::setFreqFromData(const SequenceContainer& data, double pseudoCount)
181 {
182  map<int, int> counts;
183  SequenceContainerTools::getCounts(data, counts);
184  double t = 0;
185  for (int i = 0; i < static_cast<int>(size_); i++)
186  {
187  t += (counts[i] + pseudoCount);
188  }
189  for (size_t i = 0; i < size_; ++i) freq_[i] = (static_cast<double>(counts[static_cast<int>(i)]) + pseudoCount) / t;
191  //Update parameters and re-compute generator and eigen values:
192  matchParametersValues(freqSet_->getParameters());
193 }
194 
195 /******************************************************************************/
196 
RowMatrix< double > exchangeability_
The exchangeability matrix of the model, defined as . When the model is reversible, this matrix is symetric.
const Matrix< double > & getPij_t(double d) const
Definition: JCprot.cpp:139
This class implements a state map where all resolved states are modeled.
Definition: StateMap.h:161
RowMatrix< double > rightEigenVectors_
The matrix made of right eigen vectors (by column).
const Matrix< double > & getdPij_dt(double d) const
Definition: JCprot.cpp:152
Vdouble eigenValues_
The vector of eigen values.
double d2Pij_dt2(size_t i, size_t j, double d) const
Definition: JCprot.cpp:131
virtual void setFrequencies(const std::vector< double > &frequencies)=0
Set the parameters in order to match a given set of frequencies.
FrequenciesSet useful for homogeneous and stationary models, protein implementation.
STL namespace.
virtual const std::vector< double > getFrequencies() const =0
Vdouble freq_
The vector of equilibrium frequencies.
double exp_
Definition: JCprot.h:139
Partial implementation of the ReversibleSubstitutionModel interface.
JCprot(const ProteicAlphabet *alpha)
Build a simple JC69 model, with original equilibrium frequencies.
Definition: JCprot.cpp:54
const Matrix< double > & getd2Pij_dt2(double d) const
Definition: JCprot.cpp:165
Parametrize a set of state frequencies for proteins.
RowMatrix< double > leftEigenVectors_
The matrix made of left eigen vectors (by row) if rightEigenVectors_ is non-singular.
RowMatrix< double > generator_
The generator matrix of the model.
RowMatrix< double > p_
Definition: JCprot.h:140
void setFreqFromData(const SequenceContainer &data, double pseudoCount=0)
Set equilibrium frequencies equal to the frequencies estimated from the data.
Definition: JCprot.cpp:180
double rate_
The rate of the model (default: 1). The generator (and all its vectorial components) is independent o...
double dPij_dt(size_t i, size_t j, double d) const
Definition: JCprot.cpp:123
void updateMatrices()
Definition: JCprot.cpp:78
size_t size_
The size of the generator, i.e. the number of states.
double Pij_t(size_t i, size_t j, double d) const
Definition: JCprot.cpp:115
ProteinFrequenciesSet * freqSet_
Definition: JCprot.h:141