bpp-phyl  2.2.0
MarkovModulatedSubstitutionModel.h
Go to the documentation of this file.
1 //
2 // File: MarkovModulatedSubstitutionModel.h
3 // Created by: Julien Dutheil
4 // Created on: Sat Aug 05 08:21 2006
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 #ifndef _MARKOVMODULATEDSUBSTITUTIONMODEL_H_
41 #define _MARKOVMODULATEDSUBSTITUTIONMODEL_H_
42 
43 #include "SubstitutionModel.h"
44 
45 #include <Bpp/Numeric/AbstractParameterAliasable.h>
46 #include <Bpp/Numeric/Matrix/MatrixTools.h>
47 
48 namespace bpp
49 {
50 
76  public virtual ReversibleSubstitutionModel,
77  public AbstractParameterAliasable
78  {
79 
80  protected:
83  size_t nbStates_; //Number of states in model
84  size_t nbRates_; //Number of rate classes
85 
92  RowMatrix<double> rates_; //All rates values
93  RowMatrix<double> ratesExchangeability_; //All rates transitions
94  Vdouble ratesFreq_; //All rates equilibrium frequencies
96  RowMatrix<double> ratesGenerator_; //All rates transitions
97 
101  RowMatrix<double> generator_;
102 
106  RowMatrix<double> exchangeability_;
107 
111  RowMatrix<double> leftEigenVectors_;
112 
116  RowMatrix<double> rightEigenVectors_;
117 
121  Vdouble eigenValues_;
122 
127  Vdouble iEigenValues_;
128 
133 
137  mutable RowMatrix<double> pijt_;
138  mutable RowMatrix<double> dpijt_;
139  mutable RowMatrix<double> d2pijt_;
140 
144  Vdouble freq_;
145 
147 
148  std::string nestedPrefix_;
149 
150  public:
161  MarkovModulatedSubstitutionModel(ReversibleSubstitutionModel* model, unsigned int nbRates, bool normalizeRateChanges, const std::string& prefix) :
162  AbstractParameterAliasable(prefix),
163  model_(model), stateMap_(model->getStateMap(), nbRates), nbStates_(model->getNumberOfStates()),
164  nbRates_(nbRates), rates_(nbRates, nbRates), ratesExchangeability_(nbRates, nbRates),
165  ratesFreq_(nbRates), ratesGenerator_(nbRates, nbRates), generator_(), exchangeability_(),
167  pijt_(), dpijt_(), d2pijt_(), freq_(),
168  normalizeRateChanges_(normalizeRateChanges),
169  nestedPrefix_("model_" + model->getNamespace())
170  {
171  model_->setNamespace(prefix + nestedPrefix_);
172  addParameters_(model_->getIndependentParameters());
173  }
174 
177 
179 
180 #ifndef NO_VIRTUAL_COV
182 #else
183  Clonable*
184 #endif
185  clone() const = 0;
186 
187  public:
188 
189  const Alphabet* getAlphabet() const { return model_->getAlphabet(); }
190 
191  size_t getNumberOfStates() const { return nbStates_ * nbRates_; }
192 
193  const StateMap& getStateMap() const { return stateMap_; }
194 
195  const std::vector<int>& getAlphabetStates() const { return stateMap_.getAlphabetStates(); }
196 
197  std::string getAlphabetStateAsChar(size_t index) const { return stateMap_.getAlphabetStateAsChar(index); }
198 
199  int getAlphabetStateAsInt(size_t index) const { return stateMap_.getAlphabetStateAsInt(index); }
200 
201  std::vector<size_t> getModelStates(int code) const { return stateMap_.getModelStates(code); }
202 
203  std::vector<size_t> getModelStates(const std::string& code) const { return stateMap_.getModelStates(code); }
204 
205  const Vdouble& getFrequencies() const { return freq_; }
206 
207  const Matrix<double>& getExchangeabilityMatrix() const { return exchangeability_; }
208 
209  const Matrix<double>& getGenerator() const { return generator_; }
210 
211  const Matrix<double>& getPij_t(double t) const;
212  const Matrix<double>& getdPij_dt(double t) const;
213  const Matrix<double>& getd2Pij_dt2(double t) const;
214 
215  const Vdouble& getEigenValues() const { return eigenValues_; }
216  const Vdouble& getIEigenValues() const { return iEigenValues_; }
217 
218  bool isDiagonalizable() const { return true; }
219  bool isNonSingular() const { return true; }
220 
221  const Matrix<double>& getRowLeftEigenVectors() const { return leftEigenVectors_; }
222  const Matrix<double>& getColumnRightEigenVectors() const { return rightEigenVectors_; }
223 
224  double freq(size_t i) const { return freq_[i]; }
225  double Sij(size_t i, size_t j) const { return exchangeability_(i, j); }
226  double Qij(size_t i, size_t j) const { return generator_(i, j); }
227 
228  double Pij_t (size_t i, size_t j, double t) const { return getPij_t(t)(i, j); }
229  double dPij_dt (size_t i, size_t j, double t) const { return getdPij_dt(t)(i, j); }
230  double d2Pij_dt2(size_t i, size_t j, double t) const { return getd2Pij_dt2(t)(i, j); }
231 
232  double getInitValue(size_t i, int state) const throw (IndexOutOfBoundsException, BadIntException);
233 
234  void setFreqFromData(const SequenceContainer& data, double pseudoCount = 0)
235  {
236  model_->setFreqFromData(data, pseudoCount);
237  updateMatrices();
238  }
239 
240 
242 
250  size_t getRate(size_t i) const
251  {
252  return i / nbStates_;
253  }
254 
255  double getRate() const { return model_->getRate(); }
256 
257  void setRate(double rate) { model_->setRate(rate); }
258 
259  double getScale() const
260  {
261  std::vector<double> v;
262  MatrixTools::diag(generator_, v);
263  return -VectorTools::scalar<double, double>(v, freq_);
264  }
265 
266  void setScale(double scale) {
267  model_->setScale(scale);
268  updateMatrices();
269  }
270 
272 
274 
280  virtual void fireParameterChanged(const ParameterList& parameters)
281  {
282  AbstractParameterAliasable::fireParameterChanged(parameters);
283  model_->matchParametersValues(parameters);
285  updateMatrices();
286  }
287 
288  void setNamespace(const std::string& prefix);
289 
290  protected:
291 
292  virtual void updateMatrices();
293 
300  virtual void updateRatesModel() = 0;
301 
302  };
303 
304 } //end of namespace bpp.
305 
306 #endif //_MARKOVMODULATEDSUBSTITUTIONMODEL_H_
307 
void setRate(double rate)
Set the rate of the model (must be positive).
Vdouble freq_
The vector of equilibrium frequencies.
virtual const std::vector< int > & getAlphabetStates() const
Definition: StateMap.h:143
void enableEigenDecomposition(bool yn)
Set if eigenValues and Vectors must be computed.
size_t getRate(size_t i) const
Get the rate category corresponding to a particular state in the compound model.
const Matrix< double > & getd2Pij_dt2(double t) const
std::string getAlphabetStateAsChar(size_t index) const
bool enableEigenDecomposition()
Tell if eigenValues and Vectors must be computed.
Vdouble iEigenValues_
The vector of imaginary parts of the eigen values (zero in case of reversible pmodel).
virtual const Alphabet * getAlphabet() const =0
virtual std::vector< size_t > getModelStates(int code) const
Definition: StateMap.h:146
std::vector< size_t > getModelStates(const std::string &code) const
Get the state in the model corresponding to a particular state in the alphabet.
const Matrix< double > & getColumnRightEigenVectors() const
virtual void setScale(double scale)=0
Multiplies the current generator by the given scale.
virtual void setFreqFromData(const SequenceContainer &data, double pseudoCount=0)=0
Set equilibrium frequencies equal to the frequencies estimated from the data.
MarkovModulatedSubstitutionModel * clone() const =0
virtual void setRate(double rate)=0
Set the rate of the model (must be positive).
virtual void updateRatesModel()=0
Update the rates vector, generator and equilibrium frequencies.
double dPij_dt(size_t i, size_t j, double t) const
MarkovModulatedSubstitutionModel & operator=(const MarkovModulatedSubstitutionModel &model)
RowMatrix< double > generator_
The generator matrix of the model.
double getScale() const
Get the scalar product of diagonal elements of the generator and the frequencies vector. If the generator is normalized, then scale=1. Otherwise each element must be multiplied by 1/scale.
Interface for reversible substitution models.
Vdouble eigenValues_
The vector of real parts of eigen values.
RowMatrix< double > pijt_
These ones are for bookkeeping:
RowMatrix< double > leftEigenVectors_
The matrix made of left eigen vectors (by row).
const Matrix< double > & getdPij_dt(double t) const
const std::vector< int > & getAlphabetStates() const
bool eigenDecompose_
Tell if the eigen decomposition should be performed.
void setScale(double scale)
Multiplies the current generator by the given scale.
const ReversibleSubstitutionModel * getNestedModel() const
double Pij_t(size_t i, size_t j, double t) const
Partial implementation of the Markov-modulated class of substitution models.
virtual std::string getAlphabetStateAsChar(size_t index) const
Definition: StateMap.h:145
std::vector< size_t > getModelStates(int code) const
Get the state in the model corresponding to a particular state in the alphabet.
virtual double getRate() const =0
Get the rate.
MarkovModulatedSubstitutionModel(ReversibleSubstitutionModel *model, unsigned int nbRates, bool normalizeRateChanges, const std::string &prefix)
Build a new MarkovModulatedSubstitutionModel object.
size_t getNumberOfStates() const
Get the number of states.
virtual void fireParameterChanged(const ParameterList &parameters)
Tells the model that a parameter value has changed.
const Matrix< double > & getRowLeftEigenVectors() const
virtual int getAlphabetStateAsInt(size_t index) const
Definition: StateMap.h:144
This class implements a state map for Markov modulated models.
Definition: StateMap.h:185
Map the states of a given alphabet which have a model state.
Definition: StateMap.h:58
const Matrix< double > & getExchangeabilityMatrix() const
RowMatrix< double > rightEigenVectors_
The matrix made of right eigen vectors (by column).
void setFreqFromData(const SequenceContainer &data, double pseudoCount=0)
Set equilibrium frequencies equal to the frequencies estimated from the data.
const Matrix< double > & getPij_t(double t) const
RowMatrix< double > exchangeability_
The exchangeability matrix of the model.
double d2Pij_dt2(size_t i, size_t j, double t) const