bpp-phyl  2.2.0
MutationProcess.h
Go to the documentation of this file.
1 //
2 // File: MutationProcess.h
3 // Created by: Julien Dutheil
4 // Created on: Wed Mar 12 16:11:44 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 _MUTATIONPROCESS_H_
41 #define _MUTATIONPROCESS_H_
42 
43 #include "../Model/SubstitutionModel.h"
44 #include "../Mapping/SubstitutionRegister.h"
45 
46 #include <Bpp/Numeric/VectorTools.h>
47 
48 namespace bpp
49 {
50 
57 {
58  private:
59 
60  const Alphabet* alphabet_;
61 
65  std::vector<size_t> states_;
66 
71  std::vector<double> times_;
72 
76  size_t initialState_;
77 
82  double totalTime_;
83 
84  public:
85 
93  MutationPath(const Alphabet* alphabet, size_t initialState, double time) :
94  alphabet_(alphabet), states_(), times_(), initialState_(initialState), totalTime_(time) {};
95 
96  MutationPath(const MutationPath& path) :
98 
100  alphabet_ = path.alphabet_;
101  states_ = path.states_;
102  times_ = path.times_;
104  totalTime_ = path.totalTime_;
105  return *this;
106  }
107 
108  virtual ~MutationPath() {};
109 
110  public:
111 
115  const Alphabet* getAlphabet() const { return alphabet_; }
116 
123  void addEvent(size_t state, double time) {
124  states_.push_back(state);
125  times_.push_back(time);
126  }
127 
133  size_t getInitialState() const { return initialState_; }
134 
140  double getTotalTime() const { return totalTime_; }
141 
147  size_t getNumberOfEvents() const { return states_.size(); }
148 
154  template<class Scalar>
155  void getEventCounts(Matrix<Scalar>& counts) const {
156  if (counts.getNumberOfRows() != alphabet_->getSize()
157  || counts.getNumberOfColumns() != alphabet_->getSize())
158  throw Exception("MutationPath::getEventCounts. Incorrect input matrix, does not match alphabet size.");
159  size_t currentState = initialState_;
160  for (size_t i = 0; i < states_.size(); ++i) {
161  size_t newState = states_[i];
162  counts(currentState, newState)++;
163  currentState = newState;
164  }
165  }
166 
173  template<class Scalar>
174  void getEventCounts(std::vector<Scalar>& counts, const SubstitutionRegister& reg) const {
175  if (counts.size() != reg.getNumberOfSubstitutionTypes())
176  throw Exception("MutationPath::getEventCounts. Incorrect input vector, does not match alphabet size.");
177  size_t currentState = initialState_;
178  for (size_t i = 0; i < states_.size(); ++i) {
179  size_t newState = states_[i];
180  size_t type = reg.getType(currentState, newState);
181  if (type > 0) counts[type - 1]++;
182  currentState = newState;
183  }
184  }
185 
191  size_t getFinalState() const {
192  if (states_.size() == 0) return initialState_;
193  else return states_[states_.size() - 1];
194  }
195 };
196 
205 {
206  public:
208  virtual ~MutationProcess() {};
209 
210  public:
211 
217  virtual size_t mutate(size_t state) const = 0;
218 
225  virtual size_t mutate(size_t state, unsigned int n) const = 0;
226 
233  virtual double getTimeBeforeNextMutationEvent(size_t state) const = 0;
234 
243  virtual size_t evolve(size_t initialState, double time) const = 0;
244 
254  virtual MutationPath detailedEvolve(size_t initialState, double time) const = 0;
255 
261  virtual const SubstitutionModel* getSubstitutionModel() const = 0;
262 };
263 
278  public virtual MutationProcess
279 {
280  protected:
281 
286 
290  size_t size_;
291 
298  VVdouble repartition_;
299 
300  public:
302  model_(model), size_(), repartition_()
303  {}
304 
307  {}
308 
310  {
311  model_ = amp.model_;
312  size_ = amp.size_;
314  return *this;
315  }
316 
318 
319  public:
320  size_t mutate(size_t state) const;
321  size_t mutate(size_t state, unsigned int n) const;
322  double getTimeBeforeNextMutationEvent(size_t state) const;
323  size_t evolve(size_t initialState, double time) const;
324  MutationPath detailedEvolve(size_t initialState, double time) const;
325  const SubstitutionModel* getSubstitutionModel() const { return model_; }
326 };
327 
342 {
343  public: // Constructor and destructor:
344 
351 
352  virtual ~SimpleMutationProcess();
353 
361  size_t evolve(size_t initialState, double time) const;
362 };
363 
369 {
370  public:
371  SelfMutationProcess(size_t alphabetSize);
372 
373  virtual ~SelfMutationProcess();
374 };
375 
376 } //end of namespace bpp.
377 
378 #endif //_MUTATIONPROCESS_H_
379 
void getEventCounts(std::vector< Scalar > &counts, const SubstitutionRegister &reg) const
Retrieve the number of substitution events per type of substitution, defined by a SubstitutionRegiste...
virtual double getTimeBeforeNextMutationEvent(size_t state) const =0
Get the time before next mutation event.
size_t initialState_
The initial state.
Interface for all substitution models.
virtual size_t evolve(size_t initialState, double time) const =0
Simulation a character evolution during a specified time according to the given substitution model an...
VVdouble repartition_
The repartition function for states probabilities.
std::vector< size_t > states_
The states taken, without initial state.
MutationPath detailedEvolve(size_t initialState, double time) const
Simulation a character evolution during a specified time according to the given substitution model an...
This class is used by MutationProcess to store detailed results of simulations.
The SubstitutionRegister interface.
const Alphabet * getAlphabet() const
size_t mutate(size_t state) const
Mutate a character in state i.
MutationPath(const Alphabet *alphabet, size_t initialState, double time)
Builds a new MutationPath object with initial state &#39;initialState&#39; and total time &#39;time&#39;...
SelfMutationProcess(size_t alphabetSize)
size_t size_
The number of states allowed for the character to mutate.
size_t getNumberOfEvents() const
Retrieve the number of substitution events.
Partial implmentation of the MutationProcess interface.
MutationPath & operator=(const MutationPath &path)
Interface for simulations.
MutationPath(const MutationPath &path)
SimpleMutationProcess(const SubstitutionModel *model)
Build a new SimpleMutationProcess object.
std::vector< double > times_
Times between states. The first element in array is the time between the initial state and the first ...
virtual MutationPath detailedEvolve(size_t initialState, double time) const =0
Simulation a character evolution during a specified time according to the given substitution model an...
double getTimeBeforeNextMutationEvent(size_t state) const
Get the time before next mutation event.
AbstractMutationProcess(const AbstractMutationProcess &amp)
void getEventCounts(Matrix< Scalar > &counts) const
Retrieve the number of substitution events per type of substitution.
virtual size_t getNumberOfSubstitutionTypes() const =0
AbstractMutationProcess(const SubstitutionModel *model)
virtual size_t getType(size_t fromState, size_t toState) const =0
Get the substitution type far a given pair of model states.
size_t evolve(size_t initialState, double time) const
Simulation a character evolution during a specified time according to the given substitution model an...
This class is mainly for testing purpose. It allow "self" mutation of the kind i->i;.
double totalTime_
Total time of evolution. Typically, this is a branch length.
virtual const SubstitutionModel * getSubstitutionModel() const =0
Get the substitution model associated to the mutation process.
AbstractMutationProcess & operator=(const AbstractMutationProcess &amp)
const SubstitutionModel * getSubstitutionModel() const
Get the substitution model associated to the mutation process.
const Alphabet * alphabet_
size_t getInitialState() const
Retrieve the initial state.
size_t evolve(size_t initialState, double time) const
Method redefinition for better performance.
size_t getFinalState() const
Retrieve the final state of this path.
double getTotalTime() const
Retrieve the total time of evolution.
Generally used mutation process model.
virtual size_t mutate(size_t state) const =0
Mutate a character in state i.
const SubstitutionModel * model_
The substitution model to use:
void addEvent(size_t state, double time)
Add a new mutation event.