bpp-phyl  2.2.0
SubstitutionModel.h
Go to the documentation of this file.
1 //
2 // File: SubstitutionModel.h
3 // Created by: Julien Dutheil
4 // Created on: Mon May 26 14:52:34 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 _SUBSTITUTIONMODEL_H_
41 #define _SUBSTITUTIONMODEL_H_
42 
44 #include "StateMap.h"
45 
46 // From bpp-core:
47 #include <Bpp/Exceptions.h>
48 #include <Bpp/Numeric/Parameter.h>
49 #include <Bpp/Numeric/ParameterList.h>
50 #include <Bpp/Numeric/ParameterAliasable.h>
51 #include <Bpp/Numeric/VectorTools.h>
52 #include <Bpp/Numeric/Matrix/Matrix.h>
53 
54 // From bpp-seq:
55 #include <Bpp/Seq/Alphabet/Alphabet.h>
56 #include <Bpp/Seq/Container/SequenceContainer.h>
57 
58 // From the STL:
59 #include <cstdlib>
60 #include <map>
61 #include <string>
62 
63 namespace bpp
64 {
65 class SubstitutionModel;
66 
73  public Exception
74 {
75 protected:
77 
78 public:
79  SubstitutionModelException(const std::string& text, const SubstitutionModel* sm = 0);
80 
82  Exception(sme), model_(sme.model_) {}
83 
85  {
86  Exception::operator=(sme);
87  model_ = sme.model_;
88  return *this;
89  }
90 
91  ~SubstitutionModelException() throw ();
92 
93 public:
99  virtual const SubstitutionModel* getSubstitutionModel() const { return model_; }
100 };
101 
200  public virtual ParameterAliasable
201 {
202 public:
204  virtual ~SubstitutionModel() {}
205 
206 #ifndef NO_VIRTUAL_COV
207  SubstitutionModel* clone() const = 0;
208 #endif
209 
210 public:
216  virtual std::string getName() const = 0;
217 
223  virtual const std::vector<int>& getAlphabetStates() const = 0;
224 
228  virtual const StateMap& getStateMap() const = 0;
229 
236  virtual std::vector<size_t> getModelStates(int code) const = 0;
237 
244  virtual std::vector<size_t> getModelStates(const std::string& code) const = 0;
245 
250  virtual int getAlphabetStateAsInt(size_t index) const = 0;
251 
256  virtual std::string getAlphabetStateAsChar(size_t index) const = 0;
257 
262  virtual double freq(size_t i) const = 0;
263 
269  virtual double Qij(size_t i, size_t j) const = 0;
270 
275  virtual double Pij_t(size_t i, size_t j, double t) const = 0;
276 
282  virtual double dPij_dt(size_t i, size_t j, double t) const = 0;
283 
289  virtual double d2Pij_dt2(size_t i, size_t j, double t) const = 0;
290 
295  virtual const Vdouble& getFrequencies() const = 0;
296 
312  virtual const Matrix<double>& getGenerator() const = 0;
313 
319  virtual const Matrix<double>& getExchangeabilityMatrix() const = 0;
320 
327  virtual double Sij(size_t i, size_t j) const = 0;
332  virtual const Matrix<double>& getPij_t(double t) const = 0;
333 
339  virtual const Matrix<double>& getdPij_dt(double t) const = 0;
340 
346  virtual const Matrix<double>& getd2Pij_dt2(double t) const = 0;
347 
351  virtual void enableEigenDecomposition(bool yn) = 0;
352 
356  virtual bool enableEigenDecomposition() = 0;
357 
361  virtual const Vdouble& getEigenValues() const = 0;
362 
366  virtual const Vdouble& getIEigenValues() const = 0;
367 
371  virtual bool isDiagonalizable() const = 0;
372 
376  virtual bool isNonSingular() const = 0;
377 
382  virtual const Matrix<double>& getRowLeftEigenVectors() const = 0;
383 
388  virtual const Matrix<double>& getColumnRightEigenVectors() const = 0;
389 
393  virtual const Alphabet* getAlphabet() const = 0;
394 
403  virtual size_t getNumberOfStates() const = 0;
404 
418  virtual double getInitValue(size_t i, int state) const throw (IndexOutOfBoundsException, BadIntException) = 0;
419 
428  virtual double getScale() const = 0;
429 
438  virtual void setScale(double scale) = 0;
439 
443  virtual double getRate() const = 0;
444 
450  virtual void setRate(double rate) = 0;
451 
452  virtual void addRateParameter() = 0;
453 
466  virtual void setFreqFromData(const SequenceContainer& data, double pseudoCount = 0) = 0;
467 
473  virtual void setFreq(std::map<int, double>& frequencies) {}
474 
479  virtual const FrequenciesSet* getFrequenciesSet() const {return NULL;}
480 };
481 
482 
495  public virtual SubstitutionModel
496 {
497 public:
500 
501 #ifndef NO_VIRTUAL_COV
502  ReversibleSubstitutionModel* clone() const = 0;
503 #endif
504 };
505 
506 } //end of namespace bpp.
507 
508 #endif //_SUBSTITUTIONMODEL_H_
509 
Interface for all substitution models.
virtual std::vector< size_t > getModelStates(int code) const =0
Get the state in the model corresponding to a particular state in the alphabet.
virtual int getAlphabetStateAsInt(size_t index) const =0
virtual const Matrix< double > & getExchangeabilityMatrix() const =0
SubstitutionModelException(const std::string &text, const SubstitutionModel *sm=0)
virtual double getInitValue(size_t i, int state) const =0
virtual const StateMap & getStateMap() const =0
SubstitutionModelException & operator=(const SubstitutionModelException &sme)
virtual const std::vector< int > & getAlphabetStates() const =0
const SubstitutionModel * model_
SubstitutionModelException(const SubstitutionModelException &sme)
virtual const FrequenciesSet * getFrequenciesSet() const
If the model owns a FrequenciesSet, returns a pointer to it, otherwise return 0.
virtual const Alphabet * getAlphabet() const =0
virtual double Sij(size_t i, size_t j) const =0
virtual void addRateParameter()=0
Parametrize a set of state frequencies.
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.
virtual void setRate(double rate)=0
Set the rate of the model (must be positive).
virtual const Vdouble & getFrequencies() const =0
virtual const Matrix< double > & getd2Pij_dt2(double t) const =0
virtual double d2Pij_dt2(size_t i, size_t j, double t) const =0
virtual const Vdouble & getEigenValues() const =0
virtual const Matrix< double > & getPij_t(double t) const =0
virtual const SubstitutionModel * getSubstitutionModel() const
Get the model that throws the exception.
virtual void setFreq(std::map< int, double > &frequencies)
Set equilibrium frequencies.
Interface for reversible substitution models.
virtual double Pij_t(size_t i, size_t j, double t) const =0
virtual std::string getName() const =0
Get the name of the model.
virtual double Qij(size_t i, size_t j) const =0
virtual const Matrix< double > & getGenerator() const =0
virtual bool isDiagonalizable() const =0
Exception that may be thrown by susbstitution models.
virtual std::string getAlphabetStateAsChar(size_t index) const =0
virtual double getRate() const =0
Get the rate.
virtual const Matrix< double > & getRowLeftEigenVectors() const =0
virtual double freq(size_t i) const =0
virtual double getScale() const =0
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 const Matrix< double > & getdPij_dt(double t) const =0
ReversibleSubstitutionModel * clone() const =0
virtual double dPij_dt(size_t i, size_t j, double t) const =0
virtual const Matrix< double > & getColumnRightEigenVectors() const =0
virtual bool isNonSingular() const =0
virtual size_t getNumberOfStates() const =0
Get the number of states.
SubstitutionModel * clone() const =0
Map the states of a given alphabet which have a model state.
Definition: StateMap.h:58
virtual const Vdouble & getIEigenValues() const =0
virtual bool enableEigenDecomposition()=0
Tell if eigenValues and Vectors must be computed.