bpp-phyl3  3.0.0
NonHomogeneousSequenceSimulator.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 _NONHOMOGENEOUSSEQUENCESIMULATOR_H_
6 #define _NONHOMOGENEOUSSEQUENCESIMULATOR_H_
7 
8 #include "../../Simulation/DetailedSiteSimulator.h"
9 #include "../../Simulation/SequenceSimulator.h"
10 #include "../../Tree/TreeTemplate.h"
11 #include "../../Tree/NodeTemplate.h"
12 #include "../../Model/SubstitutionModel.h"
13 #include "../../Likelihood/ParametrizablePhyloTree.h"
14 
17 
18 // From bpp-seq:
20 #include <Bpp/Seq/Site.h>
22 
23 // From the STL:
24 #include <map>
25 #include <vector>
26 
27 #include "../Model/SubstitutionModelSet.h"
28 
29 namespace bpp
30 {
31 class SimData
32 {
33 public:
34  size_t state;
35  std::vector<size_t> states;
37  std::shared_ptr<const TransitionModelInterface> model;
38 
39 public:
40  SimData() : state(), states(), cumpxy(), model(0) {}
41  SimData(const SimData& sd) : state(sd.state), states(sd.states), cumpxy(), model(sd.model) {}
43  {
44  state = sd.state;
45  states = sd.states;
46  cumpxy = sd.cumpxy;
47  model = sd.model;
48  return *this;
49  }
50 };
51 
53 
64  public virtual SequenceSimulatorInterface
65 {
66 private:
67  std::shared_ptr<const SubstitutionModelSet> modelSet_;
68  mutable std::shared_ptr<const Alphabet> alphabet_;
69  std::vector<int> supportedStates_;
70  std::shared_ptr<const DiscreteDistributionInterface> rate_;
71  std::shared_ptr<const Tree> templateTree_;
73  mutable std::shared_ptr<const ParametrizablePhyloTree> phyloTree_;
74 
79  std::vector<SNode*> leaves_;
80 
81  std::vector<std::string> seqNames_;
82 
83  size_t nbNodes_;
84  size_t nbClasses_;
85  size_t nbStates_;
86 
88 
89  // Should we output internal sequences as well?
91 
98 public:
100  std::shared_ptr<const SubstitutionModelSet> modelSet,
101  std::shared_ptr<const DiscreteDistributionInterface> rate,
102  std::shared_ptr<const Tree> tree
103  );
104 
106  std::shared_ptr<const TransitionModelInterface> model,
107  std::shared_ptr<const DiscreteDistributionInterface> rate,
108  std::shared_ptr<const Tree> tree
109  );
110 
112 
114 
116 
118  {
119  return new NonHomogeneousSequenceSimulator(*this);
120  }
121 
122 private:
128  void init();
129 
130 public:
136  std::unique_ptr<Site> simulateSite() const override;
137 
138  std::unique_ptr<Site> simulateSite(size_t ancestralStateIndex) const override;
139 
140  std::unique_ptr<Site> simulateSite(size_t ancestralStateIndex, double rate) const override;
141 
142  std::unique_ptr<Site> simulateSite(double rate) const override;
143 
144  std::vector<std::string> getSequenceNames() const override { return seqNames_; }
145 
153  std::unique_ptr<SiteSimulationResult> dSimulateSite() const override;
154 
155  std::unique_ptr<SiteSimulationResult> dSimulateSite(size_t ancestralStateIndex) const override;
156 
157  std::unique_ptr<SiteSimulationResult> dSimulateSite(size_t ancestralStateIndex, double rate) const override;
158 
159  std::unique_ptr<SiteSimulationResult> dSimulateSite(double rate) const override;
167  std::unique_ptr<SiteContainerInterface> simulate(size_t numberOfSites) const override;
168 
169  const SiteSimulatorInterface& siteSimulator(size_t pos) const override
170  {
171  throw Exception("NonHomogeneousSequenceSimulator::siteSimulator: not implemented.");
172  }
180  std::shared_ptr<const Alphabet> getAlphabet() const override { return alphabet_; }
181 
182  const Alphabet& alphabet() const override { return *alphabet_; }
190  virtual std::unique_ptr<Site> simulateSite(size_t ancestralStateIndex, size_t rateClass) const;
191  virtual std::unique_ptr<SiteSimulationResult> dSimulateSite(size_t ancestralStateIndex, size_t rateClass) const;
199  std::shared_ptr<const SubstitutionModelSet> getSubstitutionModelSet() const { return modelSet_; }
200 
207 
213  std::shared_ptr<const DiscreteDistributionInterface> getRateDistribution() const { return rate_; }
214 
221 
227  std::shared_ptr<const Tree> getTree() const { return templateTree_; }
228 
234  const Tree& tree() const { return *templateTree_; }
235 
243  void enableContinuousRates(bool yn) { continuousRates_ = yn; }
244 
251  void outputInternalSequences(bool yn) override;
252  void outputInternalSites(bool yn) override
253  {
254  throw Exception("NonHomogeneousSequenceSimulator::outputInternalSites: not implemented.");
255  }
256 
257 protected:
264  size_t evolve(const SNode* node, size_t initialStateIndex, size_t rateClass) const;
265 
272  size_t evolve(const SNode* node, size_t initialStateIndex, double rate) const;
273 
279  void multipleEvolve(
280  const SNode* node,
281  const std::vector<size_t>& initialStateIndices,
282  const std::vector<size_t>& rateClasses,
283  std::vector<size_t>& finalStates) const;
284 
285  std::unique_ptr<SiteContainerInterface> multipleEvolve(
286  const std::vector<size_t>& initialStates,
287  const std::vector<size_t>& rateClasses) const;
288 
289  void dEvolve(size_t initialState, double rate, RASiteSimulationResult& rassr) const;
290 
300  void evolveInternal(SNode* node, size_t rateClass) const;
304  void evolveInternal(SNode* node, double rate) const;
308  void multipleEvolveInternal(SNode* node, const std::vector<size_t>& rateClasses) const;
309 
313  void dEvolveInternal(SNode* node, double rate, RASiteSimulationResult& rassr) const;
315 };
316 } // end of namespace bpp.
317 
318 #endif // _NONHOMOGENEOUSSEQUENCESIMULATOR_H_
This interface adds the dSimulate method to the SiteSimulator interface.
The NodeTemplate class.
Definition: NodeTemplate.h:39
(Legacy) Site and sequences simulation under non-homogeneous models.
NonHomogeneousSequenceSimulator * clone() const override
NonHomogeneousSequenceSimulator & operator=(const NonHomogeneousSequenceSimulator &nhss)=default
const SubstitutionModelSet & substitutionModelSet() const
Get the substitution model associated to this instance.
std::shared_ptr< const Tree > getTree() const
Get the tree associated to this instance.
const DiscreteDistributionInterface & rateDistribution() const
Get the rate distribution associated to this instance.
std::vector< std::string > getSequenceNames() const override
std::shared_ptr< const SubstitutionModelSet > modelSet_
std::unique_ptr< SiteContainerInterface > simulate(size_t numberOfSites) const override
std::shared_ptr< const DiscreteDistributionInterface > rate_
const SiteSimulatorInterface & siteSimulator(size_t pos) const override
void multipleEvolve(const SNode *node, const std::vector< size_t > &initialStateIndices, const std::vector< size_t > &rateClasses, std::vector< size_t > &finalStates) const
The same as the evolve(initialState, rateClass) function, but for several sites at a time.
std::unique_ptr< Site > simulateSite() const override
std::shared_ptr< const Alphabet > getAlphabet() const override
void outputInternalSequences(bool yn) override
Sets whether we will output the internal sequences or not.
void multipleEvolveInternal(SNode *node, const std::vector< size_t > &rateClasses) const
const Tree & tree() const
Get the tree associated to this instance.
std::shared_ptr< const DiscreteDistributionInterface > getRateDistribution() const
Get the rate distribution associated to this instance.
std::vector< SNode * > leaves_
This stores once for all all leaves in a given order. This order will be used during site creation.
void dEvolve(size_t initialState, double rate, RASiteSimulationResult &rassr) const
void dEvolveInternal(SNode *node, double rate, RASiteSimulationResult &rassr) const
NonHomogeneousSequenceSimulator(std::shared_ptr< const SubstitutionModelSet > modelSet, std::shared_ptr< const DiscreteDistributionInterface > rate, std::shared_ptr< const Tree > tree)
std::shared_ptr< const SubstitutionModelSet > getSubstitutionModelSet() const
Get the substitution model associated to this instance.
size_t evolve(const SNode *node, size_t initialStateIndex, size_t rateClass) const
Evolve from an initial state along a branch, knowing the evolutionary rate class.
std::unique_ptr< SiteSimulationResult > dSimulateSite() const override
Get a detailed simulation result for one site.
NonHomogeneousSequenceSimulator(const NonHomogeneousSequenceSimulator &nhss)=default
void enableContinuousRates(bool yn)
Enable the use of continuous rates instead of discrete rates.
std::shared_ptr< const ParametrizablePhyloTree > phyloTree_
void evolveInternal(SNode *node, size_t rateClass) const
Data structure to store the result of a DetailedSiteSimulator.
The SequenceSimulator interface. SequenceSimulator classes can simulate whole datasets.
SimData(const SimData &sd)
std::vector< size_t > states
std::shared_ptr< const TransitionModelInterface > model
SimData & operator=(const SimData &sd)
The SiteSimulator interface. SiteSimulator classes can simulate single sites.
Definition: SiteSimulator.h:24
Substitution models manager for non-homogeneous / non-reversible models of evolution.
The phylogenetic tree class.
Definition: TreeTemplate.h:59
Interface for phylogenetic tree objects.
Definition: Tree.h:115
Defines the basic types of data flow nodes.
NodeTemplate< SimData > SNode
std::vector< VVdouble > VVVdouble