bpp-phyl  2.1.0
Bpp/Phyl/Model/AbstractSubstitutionModel.h
Go to the documentation of this file.
00001 //
00002 // File: AbstractSubstitutionModel.h
00003 // Created by: Julien Dutheil
00004 // Created on: Tue May 27 10:31:49 2003
00005 //
00006 
00007 /*
00008    Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
00009 
00010    This software is a computer program whose purpose is to provide classes
00011    for phylogenetic data analysis.
00012 
00013    This software is governed by the CeCILL  license under French law and
00014    abiding by the rules of distribution of free software.  You can  use,
00015    modify and/ or redistribute the software under the terms of the CeCILL
00016    license as circulated by CEA, CNRS and INRIA at the following URL
00017    "http://www.cecill.info".
00018 
00019    As a counterpart to the access to the source code and  rights to copy,
00020    modify and redistribute granted by the license, users are provided only
00021    with a limited warranty  and the software's author,  the holder of the
00022    economic rights,  and the successive licensors  have only  limited
00023    liability.
00024 
00025    In this respect, the user's attention is drawn to the risks associated
00026    with loading,  using,  modifying and/or developing or reproducing the
00027    software by the user in light of its specific status of free software,
00028    that may mean  that it is complicated to manipulate,  and  that  also
00029    therefore means  that it is reserved for developers  and  experienced
00030    professionals having in-depth computer knowledge. Users are therefore
00031    encouraged to load and test the software's suitability as regards their
00032    requirements in conditions enabling the security of their systems and/or
00033    data to be ensured and,  more generally, to use and operate it in the
00034    same conditions as regards security.
00035 
00036    The fact that you are presently reading this means that you have had
00037    knowledge of the CeCILL license and that you accept its terms.
00038  */
00039 
00040 #ifndef _ABSTRACTSUBSTITUTIONMODEL_H_
00041 #define _ABSTRACTSUBSTITUTIONMODEL_H_
00042 
00043 #include "SubstitutionModel.h"
00044 
00045 #include <Bpp/Numeric/AbstractParameterAliasable.h>
00046 #include <Bpp/Numeric/VectorTools.h>
00047 
00048 namespace bpp
00049 {
00076 class AbstractSubstitutionModel :
00077   public virtual SubstitutionModel,
00078   public virtual AbstractParameterAliasable
00079 {
00080 protected:
00084   const Alphabet* alphabet_;
00085 
00089   size_t size_;
00090 
00097   double rate_;
00098 
00102   std::vector<int> chars_;
00103 
00107   RowMatrix<double> generator_;
00108 
00113   Vdouble freq_;
00114 
00122   RowMatrix<double> exchangeability_;
00123 
00127   mutable RowMatrix<double> pijt_;
00128   mutable RowMatrix<double> dpijt_;
00129   mutable RowMatrix<double> d2pijt_;
00130 
00134   bool eigenDecompose_;
00135 
00139   Vdouble eigenValues_;
00140 
00144   Vdouble iEigenValues_;
00145 
00150   bool isDiagonalizable_;
00151 
00155   RowMatrix<double> rightEigenVectors_;
00156 
00161   bool isNonSingular_;
00162 
00168   RowMatrix<double> leftEigenVectors_;
00169 
00175   std::vector< RowMatrix<double> > vPowGen_;
00176 
00182   mutable RowMatrix<double> tmpMat_;
00183   
00184 public:
00185   AbstractSubstitutionModel(const Alphabet* alpha, const std::string& prefix);
00186 
00187   AbstractSubstitutionModel(const AbstractSubstitutionModel& model) :
00188     AbstractParameterAliasable(model),
00189     alphabet_(model.alphabet_),
00190     size_(model.size_),
00191     rate_(model.rate_),
00192     chars_(model.chars_),
00193     generator_(model.generator_),
00194     freq_(model.freq_),
00195     exchangeability_(model.exchangeability_),
00196     pijt_(model.pijt_),
00197     dpijt_(model.dpijt_),
00198     d2pijt_(model.d2pijt_),
00199     eigenDecompose_(model.eigenDecompose_),
00200     eigenValues_(model.eigenValues_),
00201     iEigenValues_(model.iEigenValues_),
00202     isDiagonalizable_(model.isDiagonalizable_),
00203     rightEigenVectors_(model.rightEigenVectors_),
00204     isNonSingular_(model.isNonSingular_),
00205     leftEigenVectors_(model.leftEigenVectors_),
00206     vPowGen_(model.vPowGen_),
00207     tmpMat_(model.tmpMat_)
00208   {}
00209 
00210   AbstractSubstitutionModel& operator=(const AbstractSubstitutionModel& model)
00211   {
00212     AbstractParameterAliasable::operator=(model);
00213     alphabet_          = model.alphabet_;
00214     size_              = model.size_;
00215     rate_              = model.rate_;
00216     chars_             = model.chars_;
00217     generator_         = model.generator_;
00218     freq_              = model.freq_;
00219     exchangeability_   = model.exchangeability_;
00220     pijt_              = model.pijt_;
00221     dpijt_             = model.dpijt_;
00222     d2pijt_            = model.d2pijt_;
00223     eigenDecompose_    = model.eigenDecompose_;
00224     eigenValues_       = model.eigenValues_;
00225     iEigenValues_      = model.iEigenValues_;
00226     isDiagonalizable_  = model.isDiagonalizable_;
00227     rightEigenVectors_ = model.rightEigenVectors_;
00228     isNonSingular_     = model.isNonSingular_;
00229     leftEigenVectors_  = model.leftEigenVectors_;
00230     vPowGen_           = model.vPowGen_;
00231     tmpMat_            = model.tmpMat_;
00232     return *this;
00233   }
00234   
00235   virtual ~AbstractSubstitutionModel() {}
00236 
00237 #ifndef NO_VIRTUAL_COV
00238   virtual AbstractSubstitutionModel* clone() const = 0;
00239 #endif
00240 
00241 public:
00242   const Alphabet* getAlphabet() const { return alphabet_; }
00243 
00244   const std::vector<int>& getAlphabetChars() const { return chars_; }
00245 
00246   int getAlphabetChar(size_t i) const { return chars_[i]; }
00247 
00248   std::vector<size_t> getModelStates(int i) const { return VectorTools::whichAll(chars_, i); }
00249 
00250   virtual const Vdouble& getFrequencies() const { return freq_; }
00251 
00252   const Matrix<double>& getGenerator() const { return generator_; }
00253 
00254   const Matrix<double>& getExchangeabilityMatrix() const { return exchangeability_; }
00255 
00256   double Sij(size_t i, size_t j) const { return exchangeability_(i, j); }
00257 
00258   virtual const Matrix<double>& getPij_t(double t) const;
00259   virtual const Matrix<double>& getdPij_dt(double t) const;
00260   virtual const Matrix<double>& getd2Pij_dt2(double t) const;
00261 
00262   const Vdouble& getEigenValues() const { return eigenValues_; }
00263 
00264   const Vdouble& getIEigenValues() const { return iEigenValues_; }
00265 
00266   bool isDiagonalizable() const { return isDiagonalizable_; }
00267   
00268   bool isNonSingular() const { return isNonSingular_; }
00269 
00270   const Matrix<double>& getRowLeftEigenVectors() const { return leftEigenVectors_; }
00271 
00272   const Matrix<double>& getColumnRightEigenVectors() const { return rightEigenVectors_; }
00273 
00274   virtual double freq(size_t i) const { return freq_[i]; }
00275 
00276   virtual double Qij(size_t i, size_t j) const { return generator_(i, j); }
00277 
00278   virtual double Pij_t    (size_t i, size_t j, double t) const { return getPij_t(t) (i, j); }
00279   virtual double dPij_dt  (size_t i, size_t j, double t) const { return getdPij_dt(t) (i, j); }
00280   virtual double d2Pij_dt2(size_t i, size_t j, double t) const { return getd2Pij_dt2(t) (i, j); }
00281 
00282   double getInitValue(size_t i, int state) const throw (IndexOutOfBoundsException, BadIntException);
00283 
00284   void setFreqFromData(const SequenceContainer& data, double pseudoCount = 0);
00285 
00286   virtual void setFreq(std::map<int, double>&);
00287 
00288   void enableEigenDecomposition(bool yn) { eigenDecompose_ = yn; }
00289 
00290   bool enableEigenDecomposition() { return eigenDecompose_; }
00291 
00297   virtual void fireParameterChanged(const ParameterList& parameters)
00298   {
00299     AbstractParameterAliasable::fireParameterChanged(parameters);
00300     if ((parameters.size()!=1) || (parameters[0].getName()!=getNamespace()+"rate"))
00301       updateMatrices();
00302   }
00303 
00304   void addRateParameter();
00305 
00306 protected:
00323   virtual void updateMatrices();
00324 
00325 public:
00326   double getScale() const;
00327 
00328   void setScale(double scale);
00329 
00330   virtual double getRate() const;
00331 
00332   virtual void setRate(double rate);
00333 
00334 
00335   friend class AbstractBiblioSubstitutionModel;
00336 
00337 };
00338 
00339 
00364 class AbstractReversibleSubstitutionModel :
00365   public virtual AbstractSubstitutionModel,
00366   public virtual ReversibleSubstitutionModel
00367 {
00368 public:
00369   AbstractReversibleSubstitutionModel(const Alphabet* alpha, const std::string& prefix) :
00370     AbstractParameterAliasable(prefix),
00371     AbstractSubstitutionModel(alpha, prefix)
00372   {
00373     isDiagonalizable_ = true;
00374     isNonSingular_    = true;
00375   }
00376 
00377   virtual ~AbstractReversibleSubstitutionModel() {}
00378 
00379 #ifndef NO_VIRTUAL_COV
00380   virtual AbstractReversibleSubstitutionModel* clone() const = 0;
00381 #endif
00382 
00383 protected:
00384 
00406   virtual void updateMatrices();
00407 };
00408 
00409 } //end of namespace bpp.
00410 
00411 #endif  //_ABSTRACTSUBSTITUTIONMODEL_H_
00412 
 All Classes Namespaces Files Functions Variables Typedefs Friends