bpp-phyl  2.2.0
GTR.cpp
Go to the documentation of this file.
1 //
2 // File: GTR.cpp
3 // Created by: Julien Dutheil
4 // Created on: Tue Oct 25 10:21 2005
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 "GTR.h"
41 #include "../FrequenciesSet/NucleotideFrequenciesSet.h"
42 
43 #include <Bpp/Numeric/Matrix/MatrixTools.h>
44 
45 // From SeqLib:
46 #include <Bpp/Seq/Container/SequenceContainerTools.h>
47 
48 // From the STL:
49 #include <cmath>
50 
51 using namespace bpp;
52 using namespace std;
53 
54 /******************************************************************************/
55 
57  const NucleicAlphabet* alpha,
58  double a,
59  double b,
60  double c,
61  double d,
62  double e,
63  double piA,
64  double piC,
65  double piG,
66  double piT) :
67  AbstractParameterAliasable("GTR."),
68  AbstractReversibleSubstitutionModel(alpha, new CanonicalStateMap(alpha, false), "GTR."),
69  a_(a), b_(b), c_(c), d_(d), e_(e), piA_(piA), piC_(piC), piG_(piG), piT_(piT), theta_(piG + piC), theta1_(piA / (1. - theta_)), theta2_(piG / theta_), p_()
70 {
71  addParameter_(new Parameter("GTR.a", a, &Parameter::R_PLUS_STAR));
72  addParameter_(new Parameter("GTR.b", b, &Parameter::R_PLUS_STAR));
73  addParameter_(new Parameter("GTR.c", c, &Parameter::R_PLUS_STAR));
74  addParameter_(new Parameter("GTR.d", d, &Parameter::R_PLUS_STAR));
75  addParameter_(new Parameter("GTR.e", e, &Parameter::R_PLUS_STAR));
76  //jdutheil on 07/02/11: is this still needed? If yes, we should also change it in all models in order to facilitate parameter aliasing...
77  //Parameter thetaP("GTR.theta", theta_ , new IncludingInterval(0.001, 0.999), true); //Avoid numerical errors close to the bounds.
78  addParameter_(new Parameter("GTR.theta", theta_, &FrequenciesSet::FREQUENCE_CONSTRAINT_SMALL));
79  //Parameter theta1P("GTR.theta1", theta1_, new IncludingInterval(0.001, 0.999), true);
80  addParameter_(new Parameter("GTR.theta1", theta1_, &FrequenciesSet::FREQUENCE_CONSTRAINT_SMALL));
81  //Parameter theta2P("GTR.theta2", theta2_, new IncludingInterval(0.001, 0.999), true);
82  addParameter_(new Parameter("GTR.theta2", theta2_, &FrequenciesSet::FREQUENCE_CONSTRAINT_SMALL));
84 }
85 
86 /******************************************************************************/
87 
89 {
90  a_ = getParameterValue("a");
91  b_ = getParameterValue("b");
92  c_ = getParameterValue("c");
93  d_ = getParameterValue("d");
94  e_ = getParameterValue("e");
95  theta_ = getParameterValue("theta");
96  theta1_ = getParameterValue("theta1");
97  theta2_ = getParameterValue("theta2");
98  piA_ = theta1_ * (1. - theta_);
99  piC_ = (1. - theta2_) * theta_;
100  piG_ = theta2_ * theta_;
101  piT_ = (1. - theta1_) * (1. - theta_);
103 
104  freq_[0] = piA_;
105  freq_[1] = piC_;
106  freq_[2] = piG_;
107  freq_[3] = piT_;
108 
109  // Exchangeability matrix:
110  exchangeability_(0,0) = (-b_*piT_-piG_-d_*piC_)/(piA_ * p_);
111  exchangeability_(1,0) = d_/p_;
112  exchangeability_(0,1) = d_/p_;
113  exchangeability_(2,0) = 1/p_;
114  exchangeability_(0,2) = 1/p_;
115  exchangeability_(3,0) = b_/p_;
116  exchangeability_(0,3) = b_/p_;
117  exchangeability_(1,1) = (-a_*piT_-e_*piG_-d_*piA_)/(piC_ * p_);
118  exchangeability_(1,2) = e_/p_;
119  exchangeability_(2,1) = e_/p_;
120  exchangeability_(1,3) = a_/p_;
121  exchangeability_(3,1) = a_/p_;
122  exchangeability_(2,2) = (-c_*piT_-e_*piC_-piA_)/(piG_ * p_);
123  exchangeability_(2,3) = c_/p_;
124  exchangeability_(3,2) = c_/p_;
125  exchangeability_(3,3) = (-c_*piG_-a_*piC_-b_*piA_)/(piT_ * p_);
126 
128 }
129 
130 /******************************************************************************/
131 
132 void GTR::setFreq(map<int, double>& freqs)
133 {
134  piA_ = freqs[0];
135  piC_ = freqs[1];
136  piG_ = freqs[2];
137  piT_ = freqs[3];
138  vector<string> thetas(3);
139  thetas[0] = getNamespace() + "theta";
140  thetas[1] = getNamespace() + "theta1";
141  thetas[2] = getNamespace() + "theta2";
142  ParameterList pl = getParameters().subList(thetas);
143  pl[0].setValue(piC_ + piG_);
144  pl[1].setValue(piA_ / (piA_ + piT_));
145  pl[2].setValue(piG_ / (piC_ + piG_));
146  setParametersValues(pl);
147 }
148 
149 /******************************************************************************/
150 
double p_
Definition: GTR.h:143
double piG_
Definition: GTR.h:143
RowMatrix< double > exchangeability_
The exchangeability matrix of the model, defined as . When the model is reversible, this matrix is symetric.
double b_
Definition: GTR.h:143
double d_
Definition: GTR.h:143
This class implements a state map where all resolved states are modeled.
Definition: StateMap.h:161
STL namespace.
Vdouble freq_
The vector of equilibrium frequencies.
GTR(const NucleicAlphabet *alpha, double a=1., double b=1., double c=1., double d=1., double e=1., double piA=0.25, double piC=0.25, double piG=0.25, double piT=0.25)
Definition: GTR.cpp:56
double theta_
Definition: GTR.h:143
double c_
Definition: GTR.h:143
double piA_
Definition: GTR.h:143
virtual void updateMatrices()
Compute and diagonalize the matrix, and fill the eigenValues_, leftEigenVectors_ and rightEigenVecto...
Partial implementation of the ReversibleSubstitutionModel interface.
static IntervalConstraint FREQUENCE_CONSTRAINT_SMALL
double theta1_
Definition: GTR.h:143
double e_
Definition: GTR.h:143
void setFreq(std::map< int, double > &freqs)
This method is redefined to actualize the corresponding parameters piA, piT, piG and piC too...
Definition: GTR.cpp:132
double piT_
Definition: GTR.h:143
double a_
Definition: GTR.h:143
double theta2_
Definition: GTR.h:143
void updateMatrices()
Compute and diagonalize the matrix, and fill the eigenValues_, leftEigenVectors_ and rightEigenVecto...
Definition: GTR.cpp:88
double piC_
Definition: GTR.h:143