bpp-phyl  2.1.0
Bpp/Phyl/Simulation/MutationProcess.h
Go to the documentation of this file.
00001 //
00002 // File: MutationProcess.h
00003 // Created by: Julien Dutheil
00004 // Created on: Wed Mar 12 16:11:44 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 _MUTATIONPROCESS_H_
00041 #define _MUTATIONPROCESS_H_
00042 
00043 #include "../Model/SubstitutionModel.h"
00044 #include "../Mapping/SubstitutionRegister.h"
00045 
00046 #include <Bpp/Numeric/VectorTools.h>
00047 
00048 namespace bpp
00049 {
00050 
00056 class MutationPath
00057 {
00058   private:
00059 
00060     const Alphabet* alphabet_;
00061 
00065     std::vector<int> states_;
00066 
00071     std::vector<double> times_;
00072     
00076     int initialState_;
00077 
00082     double totalTime_;
00083 
00084   public:
00085 
00093     MutationPath(const Alphabet* alphabet, int initialState, double time) :
00094       alphabet_(alphabet), states_(), times_(), initialState_(initialState), totalTime_(time) {};
00095     
00096     MutationPath(const MutationPath& path) :
00097       alphabet_(path.alphabet_), states_(path.states_), times_(path.times_), initialState_(path.initialState_), totalTime_(path.totalTime_) {};
00098 
00099     MutationPath& operator=(const MutationPath& path) {
00100       alphabet_     = path.alphabet_;
00101       states_       = path.states_;
00102       times_        = path.times_;
00103       initialState_ = path.initialState_;
00104       totalTime_    = path.totalTime_;
00105       return *this;
00106     }
00107 
00108     virtual ~MutationPath() {};
00109 
00110   public:
00111   
00115     const Alphabet* getAlphabet() const { return alphabet_; }
00116     
00123     void addEvent(int state, double time) {
00124       states_.push_back(state);
00125       times_.push_back(time);
00126     }
00127 
00133     int getInitialState() const { return initialState_; }
00134 
00140     double getTotalTime() const { return totalTime_; }
00141     
00147     size_t getNumberOfEvents() const { return states_.size(); }
00148 
00154     template<class Scalar>
00155     void getEventCounts(Matrix<Scalar>& counts) const {
00156       if (counts.getNumberOfRows()    != alphabet_->getSize()
00157        || counts.getNumberOfColumns() != alphabet_->getSize())
00158         throw Exception("MutationPath::getEventCounts. Incorrect input matrix, does not match alphabet size.");
00159       int currentState = initialState_;
00160       for (size_t i = 0; i < states_.size(); ++i) {
00161         int newState = states_[i];
00162         counts(currentState, newState)++;
00163         currentState = newState;
00164       }
00165     }
00166 
00173     template<class Scalar>
00174     void getEventCounts(std::vector<Scalar>& counts, const SubstitutionRegister& reg) const {
00175       if (counts.size() != reg.getNumberOfSubstitutionTypes())
00176         throw Exception("MutationPath::getEventCounts. Incorrect input vector, does not match alphabet size.");
00177       int currentState = initialState_;
00178       for (size_t i = 0; i < states_.size(); ++i) {
00179         int newState = states_[i];
00180         size_t type = reg.getType(currentState, newState);
00181         if (type > 0) counts[type - 1]++;
00182         currentState = newState;
00183       }
00184     }
00185 
00191     int getFinalState() const {
00192       if (states_.size() == 0) return initialState_;
00193       else return states_[states_.size() - 1];
00194     }
00195 };
00196 
00204 class MutationProcess
00205 {
00206   public:
00207     MutationProcess() {};
00208     virtual ~MutationProcess() {};
00209   
00210   public:
00211     
00217     virtual int mutate(int state) const = 0;
00218 
00225     virtual int mutate(int state, unsigned int n) const = 0;
00226   
00233     virtual double getTimeBeforeNextMutationEvent(int state) const = 0;
00234     
00243     virtual int evolve(int initialState, double time) const = 0;
00244   
00254     virtual MutationPath detailedEvolve(int initialState, double time) const = 0;
00255 
00261     virtual const SubstitutionModel* getSubstitutionModel() const = 0;
00262 };
00263 
00277 class AbstractMutationProcess :
00278   public virtual MutationProcess
00279 {
00280   protected:
00281     
00285     const SubstitutionModel* model_;
00286   
00290     size_t size_;
00291   
00298     VVdouble repartition_;
00299   
00300   public:
00301     AbstractMutationProcess(const SubstitutionModel* model) :
00302       model_(model), size_(), repartition_()
00303     {}
00304 
00305     AbstractMutationProcess(const AbstractMutationProcess& amp) :
00306       model_(amp.model_), size_(amp.size_), repartition_(amp.repartition_)
00307     {}
00308 
00309     AbstractMutationProcess& operator=(const AbstractMutationProcess& amp)
00310     {
00311       model_       = amp.model_;
00312       size_        = amp.size_;
00313       repartition_ = amp.repartition_;
00314       return *this;
00315     }
00316 
00317     virtual ~AbstractMutationProcess() {}
00318   
00319   public:
00320     int mutate(int state) const;
00321     int mutate(int state, unsigned int n) const;
00322     double getTimeBeforeNextMutationEvent(int state) const;
00323     int evolve(int initialState, double time) const;
00324     MutationPath detailedEvolve(int initialState, double time) const;
00325     const SubstitutionModel* getSubstitutionModel() const { return model_; }
00326 };
00327 
00341 class SimpleMutationProcess : public AbstractMutationProcess
00342 {
00343   public: // Constructor and destructor:
00344     
00350     SimpleMutationProcess(const SubstitutionModel* model);
00351   
00352     virtual ~SimpleMutationProcess();
00353 
00361     int evolve(int initialState, double time) const;
00362 };
00363 
00368 class SelfMutationProcess : public AbstractMutationProcess
00369 {
00370     public:
00371       SelfMutationProcess(int alphabetSize);
00372   
00373       virtual ~SelfMutationProcess();
00374 };
00375 
00376 } //end of namespace bpp.
00377 
00378 #endif  //_MUTATIONPROCESS_H_
00379 
 All Classes Namespaces Files Functions Variables Typedefs Friends