~ubuntu-branches/ubuntu/raring/libbpp-phyl/raring

« back to all changes in this revision

Viewing changes to src/Bpp/Phyl/Model/HKY85.h

  • Committer: Bazaar Package Importer
  • Author(s): Julien Dutheil
  • Date: 2011-06-09 11:00:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110609110000-yvx78svv6w7xxgph
Tags: upstream-2.0.2
Import upstream version 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// File: HKY85.h
 
3
// Created by: Julien Dutheil
 
4
// Created on: Thu Jan 22 16:17:39 2004
 
5
//
 
6
 
 
7
/*
 
8
Copyright or © or Copr. CNRS, (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
#ifndef _HKY85_H_
 
41
#define _HKY85_H_
 
42
 
 
43
#include "NucleotideSubstitutionModel.h"
 
44
#include "AbstractSubstitutionModel.h"
 
45
 
 
46
#include <Bpp/Numeric/Constraints.h>
 
47
 
 
48
// From SeqLib:
 
49
#include <Bpp/Seq/Alphabet/NucleicAlphabet.h>
 
50
 
 
51
namespace bpp
 
52
{
 
53
 
 
54
/**
 
55
 * @brief The Hasegawa M, Kishino H and Yano T (1985) substitution model for nucleotides.
 
56
 *
 
57
 * This model is similar to K80 model,
 
58
 * but allows distinct equilibrium frequencies between A, C, G and T.
 
59
 * \f[
 
60
 * \begin{pmatrix}
 
61
 * \cdots & r & \kappa r & r \\ 
 
62
 * r & \cdots & r & \kappa r \\ 
 
63
 * \kappa r & r & \cdots & r \\ 
 
64
 * r & \kappa r & r & \cdots \\ 
 
65
 * \end{pmatrix}
 
66
 * \f]
 
67
 * \f[
 
68
 * \pi = \left(\pi_A, \pi_C, \pi_G, \pi_T\right)
 
69
 * \f]
 
70
 * This models hence includes five parameters, the transition / transversion
 
71
 * relative rate \f$\kappa\f$ and four frequencies \f$\pi_A, \pi_C, \pi_G, \pi_T\f$.
 
72
 * These four frequencies are not independent parameters, since they have the constraint to
 
73
 * sum to 1.
 
74
 * We use instead a different parametrization to remove this constraint:
 
75
 * \f[
 
76
 * \begin{cases}
 
77
 * \theta = \pi_C + \pi_G\\
 
78
 * \theta_1 = \frac{\pi_A}{1 - \theta} = \frac{\pi_A}{\pi_A + \pi_T}\\
 
79
 * \theta_2 = \frac{\pi_G}{\theta} = \frac{\pi_G}{\pi_C + \pi_G}\\
 
80
 * \end{cases}
 
81
 * \Longleftrightarrow
 
82
 * \begin{cases}
 
83
 * \pi_A = \theta_1 (1 - \theta)\\
 
84
 * \pi_C = (1 - \theta_2) \theta\\
 
85
 * \pi_G = \theta_2 \theta\\
 
86
 * \pi_T = (1 - \theta_1)(1 - \theta).
 
87
 * \end{cases}
 
88
 * \f]
 
89
 * These parameters can also be measured from the data and not optimized.
 
90
 *
 
91
 * Normalization: \f$r\f$ is set so that \f$\sum_i Q_{i,i}\pi_i = -1\f$:
 
92
 * \f[
 
93
 * S = \frac{1}{P}\begin{pmatrix}
 
94
 * \frac{-\pi_T-\kappa\pi_G-\pi_C}{\pi_A} & 1 & \kappa & 1 \\ 
 
95
 * 1 & \frac{-\kappa\pi_T-\pi_G-\pi_A}{\pi_C} & 1 & \kappa \\ 
 
96
 * \kappa & 1 & \frac{-\pi_T-\pi_C-\kappa\pi_A}{\pi_G} & 1 \\ 
 
97
 * 1 & \kappa & 1 & \frac{-\pi_G-\kappa\pi_C-\pi_A}{\pi_T} \\ 
 
98
 * \end{pmatrix}
 
99
 * \f]
 
100
 * with \f$P=2\left(\pi_A \pi_C + \pi_C \pi_G + \pi_A \pi_T + \pi_G \pi_T + \kappa \left(\pi_C \pi_T + \pi_A \pi_G\right)\right)\f$.
 
101
 *
 
102
 * The normalized generator is obtained by taking the dot product of \f$S\f$ and \f$\pi\f$:
 
103
 * \f[
 
104
 * Q = S . \pi = \frac{1}{P}\begin{pmatrix}
 
105
 * -\pi_T-\kappa\pi_G-\pi_C & \pi_C & \kappa\pi_G & \pi_T \\ 
 
106
 * \pi_A & -\kappa\pi_T-\pi_G-\pi_A & \pi_G & \kappa\pi_T \\ 
 
107
 * \kappa\pi_A & \pi_C & -\pi_T-\pi_C-\kappa\pi_A & \pi_T \\ 
 
108
 * \pi_A & \kappa\pi_C & \pi_G & -\pi_G-\kappa\pi_C-\pi_A \\ 
 
109
 * \end{pmatrix}
 
110
 * \f]
 
111
 *  
 
112
 * The eigen values are \f$\left(0, -\frac{\pi_R + \kappa\pi_Y}{P}, -\frac{\pi_Y + \kappa\pi_R}{P}, -\frac{1}{P}\right)\f$,
 
113
 * with \f$\pi_R=\pi_A+\pi_G\f$ and \f$\pi_Y=\pi_C+\pi_T\f$.
 
114
 * The left eigen vectors are, by row:
 
115
 * \f[
 
116
 * U = \begin{pmatrix}
 
117
 *                    \pi_A &               \pi_C &                    \pi_G & \pi_T \\
 
118
 *                        0 & \frac{\pi_T}{\pi_Y} &                        0 & -\frac{\pi_T}{\pi_Y} \\
 
119
 *      \frac{\pi_G}{\pi_R} &                   0 &     -\frac{\pi_G}{\pi_R} & 0 \\
 
120
 * \frac{\pi_A\pi_Y}{\pi_R} &              -\pi_C & \frac{\pi_G\pi_Y}{\pi_R} & -\pi_T \\
 
121
 * \end{pmatrix}
 
122
 * \f]
 
123
 * and the right eigen vectors are, by column:
 
124
 * \f[
 
125
 * U^-1 = \begin{pmatrix}
 
126
 * 1 &  0 &  1 &  1 \\
 
127
 * 1 &  1 &  0 & -\frac{\pi_R}{\pi_Y} \\
 
128
 * 1 &  0 & \frac{\pi_A}{\pi_G} &  1 \\
 
129
 * 1 & -\frac{\pi_C}{\pi_T} &  0 & -\frac{\pi_R}{\pi_Y} \\
 
130
 * \end{pmatrix}
 
131
 * \f]
 
132
 *
 
133
 * In addition, a rate_ factor defines the mean rate of the model.
 
134
 *
 
135
 * The probabilities of changes are computed analytically using the formulas:
 
136
 * \f{multline*}
 
137
 * P_{i,j}(t) = \\
 
138
 * \begin{pmatrix}
 
139
 * \frac{\pi_G}{\pi_R}A + \frac{\pi_A\pi_Y}{\pi_R}B + \pi_A & \pi_C - \pi_CB & -\frac{\pi_G}{\pi_R}A + \frac{\pi_G\pi_Y}{\pi_R}B + \pi_G & \pi_T - \pi_TB \\
 
140
 * \pi_A - \pi_AB & \frac{\pi_T}{\pi_Y}A + \frac{\pi_C\pi_R}{\pi_Y}B + \pi_C & \pi_G - \pi_GB & -\frac{\pi_T}{\pi_Y}A + \frac{\pi_T\pi_R}{\pi_Y}B + \pi_T \\
 
141
 * -\frac{\pi_A}{\pi_R}A + \frac{\pi_A\pi_Y}{\pi_R}B + \pi_A & \pi_C - \pi_CB & \frac{\pi_A}{\pi_R}A + \frac{\pi_G\pi_Y}{\pi_R}B + \pi_G & \pi_T - \pi_TB \\
 
142
 * \pi_A - \pi_AB & -\frac{\pi_C}{\pi_Y}A + \frac{\pi_C\pi_R}{\pi_Y}B + \pi_C & \pi_G - \pi_GB & \frac{\pi_C}{\pi_Y}A + \frac{\pi_R\pi_T}{\pi_Y}B + \pi_T \\
 
143
 * \end{pmatrix}
 
144
 * \f}
 
145
 * with \f$A=e^{-\frac{rate\_*(\pi_Y+\kappa\pi_R)t}{P}}\f$ and \f$B = e^{-\frac{rate\_*t}{P}}\f$. 
 
146
 *
 
147
 * First and second order derivatives are also computed analytically using the formulas:
 
148
 * \f{multline*}
 
149
 * \frac{\partial P_{i,j}(t)}{\partial t} = rate\_ * \\
 
150
 * \frac{1}{P}
 
151
 * \begin{pmatrix}
 
152
 * -\frac{\pi_G(\pi_Y+\kappa\pi_R)}{\pi_R}A - \frac{\pi_A\pi_Y}{\pi_R}B & \pi_CB & \frac{\pi_G(\pi_Y+\kappa\pi_R)}{\pi_R}A - \frac{\pi_G\pi_Y}{\pi_R}B & \pi_TB \\
 
153
 * \pi_AB & -\frac{\pi_T(\pi_R+\kappa\pi_Y)}{\pi_Y}A - \frac{\pi_C\pi_R}{\pi_Y}B & \pi_GB & \frac{\pi_T(\pi_R+\kappa\pi_Y)}{\pi_Y}A - \frac{\pi_T\pi_R}{\pi_Y}B \\
 
154
 * \frac{\pi_A(\pi_Y+\kappa\pi_R)}{\pi_R}A - \frac{\pi_A\pi_Y}{\pi_R}B & \pi_CB & -\frac{\pi_A(\pi_Y+\kappa\pi_R)}{\pi_R}A - \frac{\pi_G\pi_Y}{\pi_R}B & \pi_TB \\
 
155
 * \pi_AB & \frac{\pi_C(\pi_R+\kappa\pi_Y)}{\pi_Y}A - \frac{\pi_C\pi_R}{\pi_Y}B & \pi_GB & -\frac{\pi_C(\pi_R+\kappa\pi_Y)}{\pi_Y}A - \frac{\pi_R\pi_T}{\pi_Y}B \\
 
156
 * \end{pmatrix}
 
157
 * \f}
 
158
 * \f{multline*}
 
159
 * \frac{\partial^2 P_{i,j}(t)}{\partial t^2} = rate\_^2 * \\
 
160
 * \frac{1}{P^2}
 
161
 * \begin{pmatrix}
 
162
 * \frac{\pi_G{(\pi_Y+\kappa\pi_R)}^2}{\pi_R}A + \frac{\pi_A\pi_Y}{\pi_R}B & -\pi_CB & -\frac{\pi_G{(\pi_Y+\kappa\pi_R)}^2}{\pi_R}A + \frac{\pi_G\pi_Y}{\pi_R}B & -\pi_TB \\
 
163
 * -\pi_AB & \frac{\pi_T{(\pi_R+\kappa\pi_Y)}^2}{\pi_Y}A + \frac{\pi_C\pi_R}{\pi_Y}B & -\pi_GB & -\frac{\pi_T{(\pi_R+\kappa\pi_Y)}^2}{\pi_Y}A + \frac{\pi_T\pi_R}{\pi_Y}B \\
 
164
 * -\frac{\pi_A{(\pi_Y+\kappa\pi_R)}^2}{\pi_R}A + \frac{\pi_A\pi_Y}{\pi_R}B & -\pi_CB & \frac{\pi_A{(\pi_Y+\kappa\pi_R)}^2}{\pi_R}A + \frac{\pi_G\pi_Y}{\pi_R}B & -\pi_TB \\
 
165
 * -\pi_AB & -\frac{\pi_C{(\pi_R+\kappa\pi_Y)}^2}{\pi_Y}A + \frac{\pi_C\pi_R}{\pi_Y}B & -\pi_GB & \frac{\pi_C{(\pi_R+\kappa\pi_Y)}^2}{\pi_Y}A + \frac{\pi_R\pi_T}{\pi_Y}B \\
 
166
  * \end{pmatrix}
 
167
 * \f}
 
168
 *
 
169
 * The parameters are named \c "kappa", \c "theta", \c "theta1", and \c "theta2"
 
170
 * and their values may be retrieve with the command 
 
171
 * \code
 
172
 * getParameterValue("kappa")
 
173
 * \endcode
 
174
 * for instance.
 
175
 *
 
176
 * Reference:
 
177
 * - Hasegawa M, Kishino H and Yano T (1985), Molecular_ Biology And Evolution_ 22(2) 160-74. 
 
178
 */
 
179
class HKY85:
 
180
  public virtual NucleotideSubstitutionModel,
 
181
  public AbstractReversibleSubstitutionModel
 
182
{
 
183
        private:
 
184
                double kappa_, k1_, k2_, r_, piA_, piC_, piG_, piT_, piY_, piR_, theta_, theta1_, theta2_;
 
185
    mutable double exp1_, exp21_, exp22_, l_;
 
186
    mutable RowMatrix<double> p_;
 
187
 
 
188
        public:
 
189
                HKY85(
 
190
                        const NucleicAlphabet * alpha,
 
191
                        double kappa = 1.,
 
192
                        double piA = 0.25,
 
193
                        double piC = 0.25,
 
194
                        double piG = 0.25,
 
195
                        double piT = 0.25);
 
196
        
 
197
                virtual ~HKY85() {}
 
198
 
 
199
#ifndef NO_VIRTUAL_COV
 
200
    HKY85*
 
201
#else
 
202
    Clonable*
 
203
#endif
 
204
    clone() const { return new HKY85(*this); }
 
205
 
 
206
  public:
 
207
                double Pij_t    (int i, int j, double d) const;
 
208
                double dPij_dt  (int i, int j, double d) const;
 
209
                double d2Pij_dt2(int i, int j, double d) const;
 
210
                const Matrix<double> & getPij_t    (double d) const;
 
211
                const Matrix<double> & getdPij_dt  (double d) const;
 
212
                const Matrix<double> & getd2Pij_dt2(double d) const;
 
213
 
 
214
    std::string getName() const { return "HKY85"; }
 
215
 
 
216
  /**
 
217
   * @brief This method is redefined to actualize the corresponding parameters piA, piT, piG and piC too.
 
218
   */
 
219
  void setFreq(std::map<int, double>& freqs);
 
220
        
 
221
  void updateMatrices();
 
222
};
 
223
 
 
224
} //end of namespace bpp.
 
225
 
 
226
#endif  //_HKY85_H_
 
227