bpp-phyl3 3.0.0
MutationProcess.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: The Bio++ Development Group
2//
3// SPDX-License-Identifier: CECILL-2.1
4
5#ifndef BPP_PHYL_SIMULATION_MUTATIONPROCESS_H
6#define BPP_PHYL_SIMULATION_MUTATIONPROCESS_H
7
9
10#include "../Mapping/SubstitutionRegister.h"
11#include "../Model/SubstitutionModel.h"
12
13namespace bpp
14{
21{
22private:
23 std::shared_ptr<const Alphabet> alphabet_;
24
28 std::vector<size_t> states_;
29
34 std::vector<double> times_;
35
40
45 double totalTime_;
46
47public:
56 std::shared_ptr<const Alphabet> alphabet,
57 size_t initialState,
58 double time) :
59 alphabet_(alphabet), states_(), times_(), initialState_(initialState), totalTime_(time) {}
60
63
65 {
66 alphabet_ = path.alphabet_;
67 states_ = path.states_;
68 times_ = path.times_;
71 return *this;
72 }
73
74 virtual ~MutationPath() {}
75
76public:
80 std::shared_ptr<const Alphabet> getAlphabet() const { return alphabet_; }
81
85 const Alphabet& alphabet() const { return *alphabet_; }
86
93 void addEvent(size_t state, double time)
94 {
95 states_.push_back(state);
96 times_.push_back(time);
97 }
98
99 /*
100 * @brief Remove all mutations
101 *
102 */
103 void clear()
104 {
105 states_.clear();
106 times_.clear();
107 }
108
114 size_t getInitialState() const { return initialState_; }
115
121 double getTotalTime() const { return totalTime_; }
122
128 size_t getNumberOfEvents() const { return states_.size(); }
129
135 template<class Scalar>
136 void getEventCounts(Matrix<Scalar>& counts) const
137 {
138 if (counts.getNumberOfRows() != alphabet_->getSize()
139 || counts.getNumberOfColumns() != alphabet_->getSize())
140 throw Exception("MutationPath::getEventCounts. Incorrect input matrix, does not match alphabet size.");
141 size_t currentState = initialState_;
142 for (size_t i = 0; i < states_.size(); ++i)
143 {
144 size_t newState = states_[i];
145 counts(currentState, newState)++;
146 currentState = newState;
147 }
148 }
149
156 template<class Scalar>
157 void getEventCounts(std::vector<Scalar>& counts, const SubstitutionRegisterInterface& reg) const
158 {
159 if (counts.size() != reg.getNumberOfSubstitutionTypes())
160 throw Exception("MutationPath::getEventCounts. Incorrect input vector, does not match alphabet size.");
161 size_t currentState = initialState_;
162 for (size_t i = 0; i < states_.size(); ++i)
163 {
164 size_t newState = states_[i];
165 size_t type = reg.getType(currentState, newState);
166 if (type > 0) counts[type - 1]++;
167 currentState = newState;
168 }
169 }
170
176 size_t getFinalState() const
177 {
178 if (states_.size() == 0) return initialState_;
179 else return states_[states_.size() - 1];
180 }
181};
182
191{
192public:
194 virtual ~MutationProcess() {}
195
196public:
202 virtual size_t mutate(size_t state) const = 0;
203
210 virtual size_t mutate(size_t state, unsigned int n) const = 0;
211
218 virtual double getTimeBeforeNextMutationEvent(size_t state) const = 0;
219
228 virtual size_t evolve(size_t initialState, double time) const = 0;
229
239 virtual MutationPath detailedEvolve(size_t initialState, double time) const = 0;
240
252 virtual MutationPath detailedEvolve(size_t initialState, size_t finalState, double time) const = 0;
253
259 virtual std::shared_ptr<const SubstitutionModelInterface> getSubstitutionModel() const = 0;
260};
261
276 public virtual MutationProcess
277{
278protected:
282 std::shared_ptr<const SubstitutionModelInterface> model_;
283
287 size_t size_;
288
296
297public:
298 AbstractMutationProcess(std::shared_ptr<const SubstitutionModelInterface> model) :
299 model_(model), size_(), repartition_()
300 {}
301
302public:
303 size_t mutate(size_t state) const;
304 size_t mutate(size_t state, unsigned int n) const;
305 double getTimeBeforeNextMutationEvent(size_t state) const;
306 size_t evolve(size_t initialState, double time) const;
307 MutationPath detailedEvolve(size_t initialState, double time) const;
308 MutationPath detailedEvolve(size_t initialState, size_t finalState, double time) const;
309 std::shared_ptr<const SubstitutionModelInterface> getSubstitutionModel() const { return model_; }
310};
311
327{
328public:
329 // Constructor and destructor:
330
336 SimpleMutationProcess(std::shared_ptr<const SubstitutionModelInterface> model);
337
338 virtual ~SimpleMutationProcess();
339
347 size_t evolve(size_t initialState, double time) const;
348};
349
355{
356public:
357 SelfMutationProcess(size_t alphabetSize);
358
359 virtual ~SelfMutationProcess();
360};
361} // end of namespace bpp.
362#endif // BPP_PHYL_SIMULATION_MUTATIONPROCESS_H
Partial implementation of the MutationProcess interface.
size_t size_
The number of states allowed for the character to mutate.
size_t evolve(size_t initialState, double time) const
Simulation a character evolution during a specified time according to the given substitution model an...
std::shared_ptr< const SubstitutionModelInterface > model_
The substitution model to use:
size_t mutate(size_t state) const
Mutate a character in state i.
VVdouble repartition_
The repartition function for states probabilities.
double getTimeBeforeNextMutationEvent(size_t state) const
Get the time before next mutation event.
AbstractMutationProcess(std::shared_ptr< const SubstitutionModelInterface > model)
std::shared_ptr< const SubstitutionModelInterface > getSubstitutionModel() const
Get the substitution model associated to the mutation process.
MutationPath detailedEvolve(size_t initialState, double time) const
Simulation a character evolution during a specified time according to the given substitution model an...
virtual size_t getNumberOfColumns() const=0
virtual size_t getNumberOfRows() const=0
This class is used by MutationProcess to store detailed results of simulations.
double getTotalTime() const
Retrieve the total time of evolution.
std::shared_ptr< const Alphabet > getAlphabet() const
void getEventCounts(std::vector< Scalar > &counts, const SubstitutionRegisterInterface &reg) const
Retrieve the number of substitution events per type of substitution, defined by a SubstitutionRegiste...
void getEventCounts(Matrix< Scalar > &counts) const
Retrieve the number of substitution events per type of substitution.
size_t getInitialState() const
Retrieve the initial state.
std::vector< double > times_
Times between states. The first element in array is the time between the initial state and the first ...
double totalTime_
Total time of evolution. Typically, this is a branch length.
MutationPath(std::shared_ptr< const Alphabet > alphabet, size_t initialState, double time)
Builds a new MutationPath object with initial state 'initialState' and total time 'time'.
std::vector< size_t > states_
The states taken, without initial state.
virtual ~MutationPath()
size_t initialState_
The initial state.
const Alphabet & alphabet() const
MutationPath & operator=(const MutationPath &path)
size_t getFinalState() const
Retrieve the final state of this path.
void addEvent(size_t state, double time)
Add a new mutation event.
std::shared_ptr< const Alphabet > alphabet_
size_t getNumberOfEvents() const
Retrieve the number of substitution events.
MutationPath(const MutationPath &path)
Interface for simulations.
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...
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...
virtual size_t mutate(size_t state) const =0
Mutate a character in state i.
virtual double getTimeBeforeNextMutationEvent(size_t state) const =0
Get the time before next mutation event.
virtual MutationPath detailedEvolve(size_t initialState, size_t finalState, double time) const =0
Simulation a character evolution during a specified time according to the given substitution model an...
virtual std::shared_ptr< const SubstitutionModelInterface > getSubstitutionModel() const =0
Get the substitution model associated to the mutation process.
virtual size_t mutate(size_t state, unsigned int n) const =0
Mutate a character in state i n times.
This class is mainly for testing purpose. It allow "self" mutation of the kind i->i;.
SelfMutationProcess(size_t alphabetSize)
Generally used mutation process model.
size_t evolve(size_t initialState, double time) const
Method redefinition for better performance.
SimpleMutationProcess(std::shared_ptr< const SubstitutionModelInterface > model)
Build a new SimpleMutationProcess object.
The SubstitutionRegister interface.
virtual size_t getNumberOfSubstitutionTypes() const =0
virtual size_t getType(size_t fromState, size_t toState) const =0
Get the substitution type far a given pair of model states.
Defines the basic types of data flow nodes.
std::vector< Vdouble > VVdouble