bpp-phyl  2.2.0
AbstractSubstitutionModel.h
Go to the documentation of this file.
1 //
2 // File: AbstractSubstitutionModel.h
3 // Created by: Julien Dutheil
4 // Created on: Tue May 27 10:31:49 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 #ifndef _ABSTRACTSUBSTITUTIONMODEL_H_
41 #define _ABSTRACTSUBSTITUTIONMODEL_H_
42 
43 #include "SubstitutionModel.h"
44 
45 #include <Bpp/Numeric/AbstractParameterAliasable.h>
46 #include <Bpp/Numeric/VectorTools.h>
47 
48 #include <memory>
49 
50 namespace bpp
51 {
79  public virtual SubstitutionModel,
80  public virtual AbstractParameterAliasable
81 {
82 protected:
86  const Alphabet* alphabet_;
87 
91  std::auto_ptr<StateMap> stateMap_;
92 
96  size_t size_;
97 
103  double rate_;
104 
108  RowMatrix<double> generator_;
109 
113  Vdouble freq_;
114 
122  RowMatrix<double> exchangeability_;
123 
127  mutable RowMatrix<double> pijt_;
128  mutable RowMatrix<double> dpijt_;
129  mutable RowMatrix<double> d2pijt_;
130 
135 
139  Vdouble eigenValues_;
140 
144  Vdouble iEigenValues_;
145 
151 
155  RowMatrix<double> rightEigenVectors_;
156 
161 
166  RowMatrix<double> leftEigenVectors_;
167 
172  std::vector< RowMatrix<double> > vPowGen_;
173 
177  mutable RowMatrix<double> tmpMat_;
178 
179 public:
180  AbstractSubstitutionModel(const Alphabet* alpha, StateMap* stateMap, const std::string& prefix);
181 
183  AbstractParameterAliasable(model),
184  alphabet_(model.alphabet_),
185  stateMap_(model.stateMap_->clone()),
186  size_(model.size_),
187  rate_(model.rate_),
188  generator_(model.generator_),
189  freq_(model.freq_),
191  pijt_(model.pijt_),
192  dpijt_(model.dpijt_),
193  d2pijt_(model.d2pijt_),
195  eigenValues_(model.eigenValues_),
201  vPowGen_(model.vPowGen_),
202  tmpMat_(model.tmpMat_)
203  {}
204 
206  {
207  AbstractParameterAliasable::operator=(model);
208  alphabet_ = model.alphabet_;
209  stateMap_.reset(model.stateMap_->clone());
210  size_ = model.size_;
211  rate_ = model.rate_;
212  generator_ = model.generator_;
213  freq_ = model.freq_;
215  pijt_ = model.pijt_;
216  dpijt_ = model.dpijt_;
217  d2pijt_ = model.d2pijt_;
219  eigenValues_ = model.eigenValues_;
225  vPowGen_ = model.vPowGen_;
226  tmpMat_ = model.tmpMat_;
227  return *this;
228  }
229 
231 
232 #ifndef NO_VIRTUAL_COV
233  virtual AbstractSubstitutionModel* clone() const = 0;
234 #endif
235 
236 public:
237  const Alphabet* getAlphabet() const { return alphabet_; }
238 
239  const StateMap& getStateMap() const { return *stateMap_; }
240 
241  const std::vector<int>& getAlphabetStates() const { return stateMap_->getAlphabetStates(); }
242 
243  std::string getAlphabetStateAsChar(size_t index) const { return stateMap_->getAlphabetStateAsChar(index); }
244 
245  int getAlphabetStateAsInt(size_t index) const { return stateMap_->getAlphabetStateAsInt(index); }
246 
247  std::vector<size_t> getModelStates(int code) const { return stateMap_->getModelStates(code); }
248 
249  std::vector<size_t> getModelStates(const std::string& code) const { return stateMap_->getModelStates(code); }
250 
251  virtual const Vdouble& getFrequencies() const { return freq_; }
252 
253  const Matrix<double>& getGenerator() const { return generator_; }
254 
255  const Matrix<double>& getExchangeabilityMatrix() const { return exchangeability_; }
256 
257  double Sij(size_t i, size_t j) const { return exchangeability_(i, j); }
258 
259  virtual const Matrix<double>& getPij_t(double t) const;
260  virtual const Matrix<double>& getdPij_dt(double t) const;
261  virtual const Matrix<double>& getd2Pij_dt2(double t) const;
262 
263  const Vdouble& getEigenValues() const { return eigenValues_; }
264 
265  const Vdouble& getIEigenValues() const { return iEigenValues_; }
266 
267  bool isDiagonalizable() const { return isDiagonalizable_; }
268 
269  bool isNonSingular() const { return isNonSingular_; }
270 
271  const Matrix<double>& getRowLeftEigenVectors() const { return leftEigenVectors_; }
272 
273  const Matrix<double>& getColumnRightEigenVectors() const { return rightEigenVectors_; }
274 
275  virtual double freq(size_t i) const { return freq_[i]; }
276 
277  virtual double Qij(size_t i, size_t j) const { return generator_(i, j); }
278 
279  virtual double Pij_t (size_t i, size_t j, double t) const { return getPij_t(t) (i, j); }
280  virtual double dPij_dt (size_t i, size_t j, double t) const { return getdPij_dt(t) (i, j); }
281  virtual double d2Pij_dt2(size_t i, size_t j, double t) const { return getd2Pij_dt2(t) (i, j); }
282 
283  double getInitValue(size_t i, int state) const throw (IndexOutOfBoundsException, BadIntException);
284 
285  void setFreqFromData(const SequenceContainer& data, double pseudoCount = 0);
286 
287  virtual void setFreq(std::map<int, double>&);
288 
290 
292 
298  virtual void fireParameterChanged(const ParameterList& parameters)
299  {
300  AbstractParameterAliasable::fireParameterChanged(parameters);
301  if ((parameters.size()!=1) || (parameters[0].getName()!=getNamespace()+"rate"))
302  updateMatrices();
303  }
304 
305  void addRateParameter();
306 
307 protected:
324  virtual void updateMatrices();
325 
326 public:
327  double getScale() const;
328 
329  /*
330  * @brief Multiplies the current generator by the given scale.
331  *
332  * @param scale the scale by which the generator is multiplied.
333  *
334  */
335 
336  void setScale(double scale);
337 
338  virtual double getRate() const;
339 
340  virtual void setRate(double rate);
341 
342 
344 
345 };
346 
347 
374  public virtual ReversibleSubstitutionModel
375 {
376 public:
377  AbstractReversibleSubstitutionModel(const Alphabet* alpha, StateMap* stateMap, const std::string& prefix) :
378  AbstractParameterAliasable(prefix),
379  AbstractSubstitutionModel(alpha, stateMap, prefix)
380  {
381  isDiagonalizable_ = true;
382  isNonSingular_ = true;
383  }
384 
386 
387 #ifndef NO_VIRTUAL_COV
388  virtual AbstractReversibleSubstitutionModel* clone() const = 0;
389 #endif
390 
391 protected:
392 
414  virtual void updateMatrices();
415 };
416 
417 } //end of namespace bpp.
418 
419 #endif //_ABSTRACTSUBSTITUTIONMODEL_H_
420 
virtual double d2Pij_dt2(size_t i, size_t j, double t) const
virtual void updateMatrices()
Diagonalize the matrix, and fill the eigenValues_, iEigenValues_, leftEigenVectors_ and rightEigenVe...
Interface for all substitution models.
bool enableEigenDecomposition()
Tell if eigenValues and Vectors must be computed.
RowMatrix< double > exchangeability_
The exchangeability matrix of the model, defined as . When the model is reversible, this matrix is symetric.
virtual void fireParameterChanged(const ParameterList &parameters)
Tells the model that a parameter value has changed.
AbstractSubstitutionModel(const AbstractSubstitutionModel &model)
virtual double getRate() const
Get the rate.
bool isNonSingular_
boolean value for non-singularity of rightEigenVectors_
Partial implementation of the SubstitutionModel interface for models that are set for matching the bi...
virtual const Matrix< double > & getPij_t(double t) const
RowMatrix< double > pijt_
These ones are for bookkeeping:
RowMatrix< double > rightEigenVectors_
The matrix made of right eigen vectors (by column).
Vdouble eigenValues_
The vector of eigen values.
int getAlphabetStateAsInt(size_t index) const
const Matrix< double > & getRowLeftEigenVectors() const
Vdouble freq_
The vector of equilibrium frequencies.
double getInitValue(size_t i, int state) const
std::string getAlphabetStateAsChar(size_t index) const
virtual double Qij(size_t i, size_t j) const
std::vector< size_t > getModelStates(const std::string &code) const
Get the state in the model corresponding to a particular state in the alphabet.
virtual void updateMatrices()
Compute and diagonalize the matrix, and fill the eigenValues_, leftEigenVectors_ and rightEigenVecto...
AbstractSubstitutionModel & operator=(const AbstractSubstitutionModel &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.
virtual AbstractSubstitutionModel * clone() const =0
Partial implementation of the SubstitutionModel interface.
void setFreqFromData(const SequenceContainer &data, double pseudoCount=0)
Set equilibrium frequencies equal to the frequencies estimated from the data.
AbstractReversibleSubstitutionModel(const Alphabet *alpha, StateMap *stateMap, const std::string &prefix)
Partial implementation of the ReversibleSubstitutionModel interface.
virtual AbstractReversibleSubstitutionModel * clone() const =0
virtual double freq(size_t i) const
virtual double Pij_t(size_t i, size_t j, double t) const
Interface for reversible substitution models.
void setScale(double scale)
Multiplies the current generator by the given scale.
void enableEigenDecomposition(bool yn)
Set if eigenValues and Vectors must be computed.
const Alphabet * alphabet_
The alphabet relevant to this model.
virtual std::string getName() const =0
Get the name of the model.
virtual const Matrix< double > & getdPij_dt(double t) const
virtual const Matrix< double > & getd2Pij_dt2(double t) const
std::auto_ptr< StateMap > stateMap_
The map of model states with alphabet states.
const std::vector< int > & getAlphabetStates() const
bool eigenDecompose_
Tell if the eigen decomposition should be performed.
RowMatrix< double > leftEigenVectors_
The matrix made of left eigen vectors (by row) if rightEigenVectors_ is non-singular.
virtual double dPij_dt(size_t i, size_t j, double t) const
double Sij(size_t i, size_t j) const
RowMatrix< double > generator_
The generator matrix of the model.
const Matrix< double > & getExchangeabilityMatrix() const
double rate_
The rate of the model (default: 1). The generator (and all its vectorial components) is independent o...
std::vector< size_t > getModelStates(int code) const
Get the state in the model corresponding to a particular state in the alphabet.
std::vector< RowMatrix< double > > vPowGen_
vector of the powers of generator_ for Taylor development (if rightEigenVectors_ is singular)...
virtual void setRate(double rate)
Set the rate of the model (must be positive).
const Matrix< double > & getColumnRightEigenVectors() const
size_t size_
The size of the generator, i.e. the number of states.
const Matrix< double > & getGenerator() const
virtual const Vdouble & getFrequencies() const
Map the states of a given alphabet which have a model state.
Definition: StateMap.h:58
RowMatrix< double > tmpMat_
For computational issues.
bool isDiagonalizable_
boolean value for diagonalizability in R of the generator_
Vdouble iEigenValues_
The vector of the imaginary part of the eigen values.
AbstractSubstitutionModel(const Alphabet *alpha, StateMap *stateMap, const std::string &prefix)
virtual void setFreq(std::map< int, double > &)
Set equilibrium frequencies.