bpp-phyl3 3.0.0
JCprot.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: The Bio++ Development Group
2//
3// SPDX-License-Identifier: CECILL-2.1
4
5#include "JCprot.h"
6
7// From bpp-seq:
9
10using namespace bpp;
11
12#include <cmath>
13#include <map>
14
15using namespace std;
16
17/******************************************************************************/
18
19JCprot::JCprot(std::shared_ptr<const ProteicAlphabet> alpha) :
21 AbstractReversibleProteinSubstitutionModel(alpha, make_shared<CanonicalStateMap>(alpha, false), "JC69."),
22 exp_(), p_(size_, size_), freqSet_(nullptr), withFreq_(false)
23{
24 freqSet_.reset(new FixedProteinFrequencySet(alpha));
26}
27
29 std::shared_ptr<const ProteicAlphabet> alpha,
30 std::unique_ptr<ProteinFrequencySetInterface> freqSet,
31 bool initFreqs) :
33 AbstractReversibleProteinSubstitutionModel(alpha, make_shared<CanonicalStateMap>(alpha, false), "JC69+F."),
34 exp_(), p_(size_, size_), freqSet_(std::move(freqSet)), withFreq_(true)
35{
36 freqSet_->setNamespace("JC69+F." + freqSet_->getNamespace());
37 if (initFreqs)
38 freqSet_->setFrequencies(freq_);
39 else
40 freq_ = freqSet_->getFrequencies();
41 addParameters_(freqSet_->getParameters());
42
44}
45
46/******************************************************************************/
47
49{
50 for (unsigned int i = 0; i < 20; ++i)
51 {
52 for (unsigned int j = 0; j < 20; ++j)
53 {
54 exchangeability_(i, j) = (i == j) ? -20. : 20. / 19.;
55 }
56 }
57
58 if (!withFreq_)
59 {
60 // Frequencies:
61 for (unsigned int i = 0; i < 20; ++i)
62 {
63 freq_[i] = 1. / 20.;
64 }
65
66 // Generator:
67 for (unsigned int i = 0; i < 20; ++i)
68 {
69 for (unsigned int j = 0; j < 20; ++j)
70 {
71 generator_(i, j) = (i == j) ? -1. : 1. / 19.;
72 }
73 }
74
75 // Eigen values:
76 eigenValues_[0] = 0;
77 for (unsigned int i = 1; i < 20; ++i)
78 {
79 eigenValues_[i] = -20. / 19.;
80 }
81
82 // Eigen vectors:
83 for (unsigned int i = 0; i < 20; ++i)
84 {
85 leftEigenVectors_(0, i) = 1. / 20.;
86 }
87 for (unsigned int i = 1; i < 20; ++i)
88 {
89 for (unsigned int j = 0; j < 20; ++j)
90 {
91 leftEigenVectors_(i, j) = -1. / 20.;
92 }
93 }
94 for (unsigned int i = 0; i < 19; ++i)
95 {
96 leftEigenVectors_(19 - i, i) = 19. / 20.;
97 }
98
99 for (unsigned int i = 0; i < 20; ++i)
100 {
101 rightEigenVectors_(i, 0) = 1.;
102 }
103 for (unsigned int i = 1; i < 20; ++i)
104 {
105 rightEigenVectors_(19, i) = -1.;
106 }
107 for (unsigned int i = 0; i < 19; ++i)
108 {
109 for (unsigned int j = 1; j < 20; ++j)
110 {
111 rightEigenVectors_(i, j) = 0.;
112 }
113 }
114 for (unsigned int i = 1; i < 20; ++i)
115 {
116 rightEigenVectors_(19 - i, i) = 1.;
117 }
118 }
119 else
121}
122
123/******************************************************************************/
124
125double JCprot::Pij_t(size_t i, size_t j, double d) const
126{
127 if (!withFreq_)
128 {
129 if (i == j)
130 return 1. / 20. + 19. / 20. * exp(-rate_ * 20. / 19. * d);
131 else
132 return 1. / 20. - 1. / 20. * exp(-rate_ * 20. / 19. * d);
133 }
134 else
135 return AbstractSubstitutionModel::Pij_t(i, j, d);
136}
137
138/******************************************************************************/
139
140double JCprot::dPij_dt(size_t i, size_t j, double d) const
141{
142 if (!withFreq_)
143 {
144 if (i == j)
145 return -rate_* exp(-rate_ * 20. / 19. * d);
146 else
147 return rate_ * 1. / 19. * exp(-rate_ * 20. / 19. * d);
148 }
149 else
151}
152
153/******************************************************************************/
154
155double JCprot::d2Pij_dt2(size_t i, size_t j, double d) const
156{
157 if (!withFreq_)
158 {
159 if (i == j)
160 return rate_ * rate_ * 20. / 19. * exp(-rate_ * 20. / 19. * d);
161 else
162 return -rate_ * rate_ * 20. / 361. * exp(-rate_ * 20. / 19. * d);
163 }
164 else
166}
167
168/******************************************************************************/
169
170const Matrix<double>& JCprot::getPij_t(double d) const
171{
172 if (!withFreq_)
173 {
174 exp_ = exp(-rate_ * 20. / 19. * d);
175 for (unsigned int i = 0; i < size_; i++)
176 {
177 for (unsigned int j = 0; j < size_; j++)
178 {
179 p_(i, j) = (i == j) ? 1. / 20. + 19. / 20. * exp_ : 1. / 20. - 1. / 20. * exp_;
180 }
181 }
182 return p_;
183 }
184 else
186}
187
188const Matrix<double>& JCprot::getdPij_dt(double d) const
189{
190 if (!withFreq_)
191 {
192 exp_ = exp(-rate_ * 20. / 19. * d);
193 for (unsigned int i = 0; i < size_; i++)
194 {
195 for (unsigned int j = 0; j < size_; j++)
196 {
197 p_(i, j) = rate_ * ((i == j) ? -exp_ : 1. / 19. * exp_);
198 }
199 }
200 return p_;
201 }
202 else
204}
205
207{
208 if (!withFreq_)
209 {
210 exp_ = exp( rate_ * -20. / 19. * d);
211 for (unsigned int i = 0; i < size_; i++)
212 {
213 for (unsigned int j = 0; j < size_; j++)
214 {
215 p_(i, j) = rate_ * rate_ * ((i == j) ? 20. / 19. * exp_ : -20. / 361. * exp_);
216 }
217 }
218 return p_;
219 }
220 else
222}
223
224/******************************************************************************/
225
226void JCprot::setFreqFromData(const SequenceDataInterface& data, double pseudoCount)
227{
228 map<int, double> counts;
229 SequenceContainerTools::getFrequencies(data, counts, pseudoCount);
230 for (auto i : counts)
231 {
232 freq_[(size_t)i.first] = i.second;
233 }
234
235 freqSet_->setFrequencies(freq_);
236 // Update parameters and re-compute generator and eigen values:
237 matchParametersValues(freqSet_->getParameters());
238}
239
240/******************************************************************************/
void addParameters_(const ParameterList &parameters)
bool matchParametersValues(const ParameterList &parameters) override
Specialisation abstract class for reversible protein substitution model.
virtual void updateMatrices_() override
Compute and diagonalize the matrix, and fill the eigenValues_, leftEigenVectors_ and rightEigenVecto...
RowMatrix< double > generator_
The generator matrix of the model.
Vdouble eigenValues_
The vector of eigen values.
RowMatrix< double > exchangeability_
The exchangeability matrix of the model, defined as . When the model is reversible,...
const Matrix< double > & getdPij_dt(double t) const
RowMatrix< double > leftEigenVectors_
The matrix made of left eigen vectors (by row) if rightEigenVectors_ is non-singular.
const Matrix< double > & getPij_t(double t) const
RowMatrix< double > rightEigenVectors_
The matrix made of right eigen vectors (by column).
const Matrix< double > & getd2Pij_dt2(double t) const
virtual double d2Pij_dt2(size_t i, size_t j, double t) const override
size_t size_
The number of states.
Vdouble freq_
The vector of equilibrium frequencies.
double rate_
The rate of the model (default: 1). The generator (and all its vectorial components) is independent o...
virtual double Pij_t(size_t i, size_t j, double t) const override
virtual double dPij_dt(size_t i, size_t j, double t) const override
This class implements a state map where all resolved states are modeled.
Definition: StateMap.h:168
FrequencySet useful for homogeneous and stationary models, protein implementation.
RowMatrix< double > p_
Definition: JCprot.h:104
const Matrix< double > & getPij_t(double d) const override
Definition: JCprot.cpp:170
double exp_
Definition: JCprot.h:103
const Matrix< double > & getd2Pij_dt2(double d) const override
Definition: JCprot.cpp:206
double Pij_t(size_t i, size_t j, double d) const override
Definition: JCprot.cpp:125
double dPij_dt(size_t i, size_t j, double d) const override
Definition: JCprot.cpp:140
double d2Pij_dt2(size_t i, size_t j, double d) const override
Definition: JCprot.cpp:155
JCprot(std::shared_ptr< const ProteicAlphabet > alpha)
Build a simple JC69 model, with original equilibrium frequencies.
Definition: JCprot.cpp:19
void setFreqFromData(const SequenceDataInterface &data, double pseudoCount=0) override
Set equilibrium frequencies equal to the frequencies estimated from the data.
Definition: JCprot.cpp:226
std::unique_ptr< ProteinFrequencySetInterface > freqSet_
Definition: JCprot.h:105
void updateMatrices_() override
Definition: JCprot.cpp:48
const Matrix< double > & getdPij_dt(double d) const override
Definition: JCprot.cpp:188
bool withFreq_
Definition: JCprot.h:106
static void getFrequencies(const SequenceContainerInterface &sc, std::map< int, double > &f, double pseudoCount=0)
Defines the basic types of data flow nodes.
ExtendedFloat exp(const ExtendedFloat &ef)