bpp-phyl  2.1.0
Bpp/Phyl/Model/MarkovModulatedSubstitutionModel.h
Go to the documentation of this file.
00001 //
00002 // File: MarkovModulatedSubstitutionModel.h
00003 // Created by: Julien Dutheil
00004 // Created on: Sat Aug 05 08:21 2006
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 _MARKOVMODULATEDSUBSTITUTIONMODEL_H_
00041 #define _MARKOVMODULATEDSUBSTITUTIONMODEL_H_
00042 
00043 #include "SubstitutionModel.h"
00044 
00045 #include <Bpp/Numeric/AbstractParameterAliasable.h>
00046 #include <Bpp/Numeric/Matrix/MatrixTools.h>
00047 
00048 namespace bpp
00049 {
00050 
00075   class MarkovModulatedSubstitutionModel:
00076     public virtual ReversibleSubstitutionModel,
00077     public AbstractParameterAliasable
00078   {
00079 
00080   protected:
00081     ReversibleSubstitutionModel* model_;
00082     size_t nbStates_; //Number of states in model
00083     size_t nbRates_; //Number of rate classes
00084 
00091     RowMatrix<double> rates_;                //All rates values
00092     RowMatrix<double> ratesExchangeability_; //All rates transitions
00093     Vdouble           ratesFreq_;            //All rates equilibrium frequencies
00095     RowMatrix<double> ratesGenerator_;       //All rates transitions
00096     
00100     std::vector<int> chars_;
00101 
00105     RowMatrix<double> generator_;
00106 
00110     RowMatrix<double> exchangeability_;
00111 
00115     RowMatrix<double> leftEigenVectors_;
00116 
00120     RowMatrix<double> rightEigenVectors_;
00121 
00125     Vdouble eigenValues_;
00126 
00131     Vdouble iEigenValues_;
00132 
00136     bool eigenDecompose_;
00137 
00141     mutable RowMatrix<double> pijt_;
00142     mutable RowMatrix<double> dpijt_;
00143     mutable RowMatrix<double> d2pijt_;
00144 
00148     Vdouble freq_;
00149 
00150     bool normalizeRateChanges_;
00151 
00152     std::string nestedPrefix_;
00153 
00154   public:
00164     MarkovModulatedSubstitutionModel(ReversibleSubstitutionModel* model, bool normalizeRateChanges, const std::string& prefix) :
00165       AbstractParameterAliasable(prefix),
00166       model_(model), nbStates_(model->getNumberOfStates()), nbRates_(0), rates_(), ratesExchangeability_(),
00167       ratesFreq_(), ratesGenerator_(), chars_(), generator_(), exchangeability_(),
00168       leftEigenVectors_(), rightEigenVectors_(), eigenValues_(), iEigenValues_(), eigenDecompose_(true), 
00169       pijt_(), dpijt_(), d2pijt_(), freq_(),
00170       normalizeRateChanges_(normalizeRateChanges),
00171       nestedPrefix_("model_" + model->getNamespace())
00172     {
00173       model_->setNamespace(prefix + nestedPrefix_);
00174       addParameters_(model_->getIndependentParameters());
00175     }
00176     
00177     MarkovModulatedSubstitutionModel(const MarkovModulatedSubstitutionModel& model);
00178     MarkovModulatedSubstitutionModel& operator=(const MarkovModulatedSubstitutionModel& model);
00179 
00180     virtual ~MarkovModulatedSubstitutionModel() { delete model_; }
00181 
00182 #ifndef NO_VIRTUAL_COV
00183     MarkovModulatedSubstitutionModel*
00184 #else
00185     Clonable*
00186 #endif
00187     clone() const = 0;
00188 
00189   public:
00190     
00191     const Alphabet* getAlphabet() const { return model_->getAlphabet(); }
00192 
00193     size_t getNumberOfStates() const { return nbStates_ * nbRates_; }
00194 
00195     const Vdouble& getFrequencies() const { return freq_; }
00196     
00197     const Matrix<double>& getExchangeabilityMatrix() const { return exchangeability_; }
00198     
00199     const Matrix<double>& getGenerator() const { return generator_; }
00200     
00201     const Matrix<double>& getPij_t(double t) const;
00202     const Matrix<double>& getdPij_dt(double t) const;
00203     const Matrix<double>& getd2Pij_dt2(double t) const;
00204     
00205     const Vdouble& getEigenValues() const { return eigenValues_; }
00206     const Vdouble& getIEigenValues() const { return iEigenValues_; }
00207 
00208     bool isDiagonalizable() const { return true; }
00209     bool isNonSingular() const { return true; }
00210 
00211     const Matrix<double>& getRowLeftEigenVectors() const { return leftEigenVectors_; }
00212     const Matrix<double>& getColumnRightEigenVectors() const { return rightEigenVectors_; }
00213     
00214     double freq(size_t i) const { return freq_[i]; }
00215     double Sij(size_t i, size_t j) const { return exchangeability_(i, j); }
00216     double Qij(size_t i, size_t j) const { return generator_(i, j); }
00217     
00218     double Pij_t    (size_t i, size_t j, double t) const { return getPij_t(t)(i, j); }
00219     double dPij_dt  (size_t i, size_t j, double t) const { return getdPij_dt(t)(i, j); }
00220     double d2Pij_dt2(size_t i, size_t j, double t) const { return getd2Pij_dt2(t)(i, j); }
00221     
00222     double getInitValue(size_t i, int state) const throw (IndexOutOfBoundsException, BadIntException);
00223     
00224     void setFreqFromData(const SequenceContainer& data, double pseudoCount = 0)
00225     {
00226       model_->setFreqFromData(data, pseudoCount);
00227       updateMatrices();
00228     }
00229 
00230     const std::vector<int>& getAlphabetChars() const
00231     {
00232       return chars_;
00233     }
00234 
00235     int getAlphabetChar(size_t i) const
00236     {
00237       return chars_[i]; 
00238     }
00239    
00240     std::vector<size_t> getModelStates(int i) const
00241     {
00242       std::vector<size_t> states(nbRates_ * nbStates_);
00243       std::vector<size_t> nestedStates = model_->getModelStates(i);
00244       for(size_t j = 0; j < nbRates_; j++)
00245         for(size_t k = 0; k < nestedStates.size(); k++)
00246           states.push_back(j * nbRates_ + states[k]);
00247       return states;
00248     }
00249 
00250     const ReversibleSubstitutionModel* getNestedModel() const { return model_; }
00251 
00259     size_t getRate(size_t i) const
00260     {
00261       return i / nbStates_; 
00262     }
00263 
00264     double getRate() const { return model_->getRate(); }
00265 
00266     void setRate(double rate) { model_->setRate(rate); }
00267 
00268     double getScale() const
00269     {
00270       std::vector<double> v;
00271       MatrixTools::diag(generator_, v);
00272       return -VectorTools::scalar<double, double>(v, freq_);
00273     }
00274 
00275     void setScale(double scale) {
00276       model_->setScale(scale);
00277       updateMatrices();
00278     } 
00279 
00280     void enableEigenDecomposition(bool yn) { eigenDecompose_ = yn; }
00281 
00282     bool enableEigenDecomposition() { return eigenDecompose_; }
00283   
00289     virtual void fireParameterChanged(const ParameterList& parameters)
00290     {
00291       AbstractParameterAliasable::fireParameterChanged(parameters);
00292       model_->matchParametersValues(parameters);
00293       updateRatesModel();
00294       updateMatrices();
00295     }
00296    
00297     void setNamespace(const std::string& prefix);
00298 
00299   protected:
00300     
00301     virtual void updateMatrices();
00302 
00309     virtual void updateRatesModel() = 0;
00310 
00311   };
00312 
00313 } //end of namespace bpp.
00314 
00315 #endif //_MARKOVMODULATEDSUBSTITUTIONMODEL_H_
00316 
 All Classes Namespaces Files Functions Variables Typedefs Friends