bpp-phyl  2.2.0
NonHomogeneousSequenceSimulator.h
Go to the documentation of this file.
1 //
2 // File: NonHomogeneousSequenceSimulator.h
3 // Created by: Julien Dutheil
4 // Bastien Boussau
5 // Created on: Wed Aug 24 15:20 2005
6 //
7 
8 /*
9 Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
10 
11 This software is a computer program whose purpose is to provide classes
12 for phylogenetic data analysis.
13 
14 This software is governed by the CeCILL license under French law and
15 abiding by the rules of distribution of free software. You can use,
16 modify and/ or redistribute the software under the terms of the CeCILL
17 license as circulated by CEA, CNRS and INRIA at the following URL
18 "http://www.cecill.info".
19 
20 As a counterpart to the access to the source code and rights to copy,
21 modify and redistribute granted by the license, users are provided only
22 with a limited warranty and the software's author, the holder of the
23 economic rights, and the successive licensors have only limited
24 liability.
25 
26 In this respect, the user's attention is drawn to the risks associated
27 with loading, using, modifying and/or developing or reproducing the
28 software by the user in light of its specific status of free software,
29 that may mean that it is complicated to manipulate, and that also
30 therefore means that it is reserved for developers and experienced
31 professionals having in-depth computer knowledge. Users are therefore
32 encouraged to load and test the software's suitability as regards their
33 requirements in conditions enabling the security of their systems and/or
34 data to be ensured and, more generally, to use and operate it in the
35 same conditions as regards security.
36 
37 The fact that you are presently reading this means that you have had
38 knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 #ifndef _NONHOMOGENEOUSSEQUENCESIMULATOR_H_
42 #define _NONHOMOGENEOUSSEQUENCESIMULATOR_H_
43 
44 #include "DetailedSiteSimulator.h"
45 #include "SequenceSimulator.h"
46 #include "../TreeTemplate.h"
47 #include "../NodeTemplate.h"
48 #include "../Model/SubstitutionModel.h"
49 
50 #include <Bpp/Numeric/Random/RandomTools.h>
51 #include <Bpp/Numeric/Prob/DiscreteDistribution.h>
52 
53 // From SeqLib:
54 #include <Bpp/Seq/Alphabet/Alphabet.h>
55 #include <Bpp/Seq/Site.h>
56 #include <Bpp/Seq/Container/SiteContainer.h>
57 
58 // From the STL:
59 #include <map>
60 #include <vector>
61 
62 #include "../Model/SubstitutionModelSet.h"
63 
64 namespace bpp
65 {
66 
67 class SimData
68 {
69  public:
70  size_t state;
71  std::vector<size_t> states;
72  VVVdouble cumpxy;
74 
75  public:
76  SimData(): state(), states(), cumpxy(), model(0) {}
77  SimData(const SimData& sd): state(sd.state), states(sd.states), cumpxy(), model(sd.model) {}
79  {
80  state = sd.state;
81  states = sd.states;
82  cumpxy = sd.cumpxy;
83  model = sd.model;
84  return *this;
85  }
86 };
87 
89 
96  public DetailedSiteSimulator,
97  public virtual SequenceSimulator
98 {
99  private:
101  const Alphabet * alphabet_;
102  std::vector<int> supportedStates_;
103  const DiscreteDistribution* rate_;
107 
112  std::vector<SNode*> leaves_;
113 
114  std::vector<std::string> seqNames_;
115 
116  size_t nbNodes_;
117  size_t nbClasses_;
118  size_t nbStates_;
119 
121 
128  public:
130  const SubstitutionModelSet* modelSet,
131  const DiscreteDistribution* rate,
132  const Tree* tree
133  ) throw (Exception);
134 
136  const SubstitutionModel* model,
137  const DiscreteDistribution* rate,
138  const Tree* tree
139  );
140 
142  {
143  if (ownModelSet_ && modelSet_) delete modelSet_;
144  }
145 
147  modelSet_ (nhss.modelSet_),
148  alphabet_ (nhss.alphabet_),
150  rate_ (nhss.rate_),
152  tree_ (nhss.tree_),
153  ownModelSet_ (nhss.ownModelSet_),
154  leaves_ (nhss.leaves_),
155  seqNames_ (nhss.seqNames_),
156  nbNodes_ (nhss.nbNodes_),
157  nbClasses_ (nhss.nbClasses_),
158  nbStates_ (nhss.nbStates_),
160  {}
161 
163  {
164  modelSet_ = nhss.modelSet_;
165  alphabet_ = nhss.alphabet_;
167  rate_ = nhss.rate_;
169  tree_ = nhss.tree_;
170  ownModelSet_ = nhss.ownModelSet_;
171  leaves_ = nhss.leaves_;
172  seqNames_ = nhss.seqNames_;
173  nbNodes_ = nhss.nbNodes_;
174  nbClasses_ = nhss.nbClasses_;
175  nbStates_ = nhss.nbStates_;
177  return *this;
178  }
179 
180 #ifndef NO_VIRTUAL_COV
182 #else
183  Clonable*
184 #endif
185  clone() const { return new NonHomogeneousSequenceSimulator(*this); }
186 
187  private:
193  void init();
194 
195  public:
196 
202  Site* simulateSite() const;
203 
204  Site* simulateSite(size_t ancestralStateIndex) const;
205 
206  Site* simulateSite(size_t ancestralStateIndex, double rate) const;
207 
208  Site* simulateSite(double rate) const;
209 
210  std::vector<std::string> getSequencesNames() const { return seqNames_; }
219 
220  RASiteSimulationResult* dSimulateSite(size_t ancestralStateIndex) const;
221 
222  RASiteSimulationResult* dSimulateSite(size_t ancestralStateIndex, double rate) const;
223 
224  RASiteSimulationResult* dSimulateSite(double rate) const;
232  SiteContainer* simulate(size_t numberOfSites) const;
240  const Alphabet* getAlphabet() const { return alphabet_; }
248  virtual Site* simulateSite(size_t ancestralStateIndex, size_t rateClass) const;
249  virtual RASiteSimulationResult* dSimulateSite(size_t ancestralStateIndex, size_t rateClass) const;
258 
264  const DiscreteDistribution* getRateDistribution() const { return rate_; }
265 
271  const Tree* getTree() const { return templateTree_; }
272 
280  void enableContinuousRates(bool yn) { continuousRates_ = yn; }
281 
282  protected:
283 
290  size_t evolve(const SNode* node, size_t initialStateIndex, size_t rateClass) const;
291 
298  size_t evolve(const SNode* node, size_t initialStateIndex, double rate) const;
299 
305  void multipleEvolve(
306  const SNode* node,
307  const std::vector<size_t>& initialStateIndices,
308  const std::vector<size_t>& rateClasses,
309  std::vector<size_t>& finalStates) const;
310  SiteContainer* multipleEvolve(
311  const std::vector<size_t>& initialStates,
312  const std::vector<size_t>& rateClasses) const;
313 
314  void dEvolve(size_t initialState, double rate, RASiteSimulationResult& rassr) const;
315 
325  void evolveInternal(SNode* node, size_t rateClass) const;
329  void evolveInternal(SNode* node, double rate) const;
333  void multipleEvolveInternal(SNode* node, const std::vector<size_t>& rateClasses) const;
334 
338  void dEvolveInternal(SNode * node, double rate, RASiteSimulationResult & rassr) const;
341 };
342 
343 } //end of namespace bpp.
344 
345 #endif //_NONHOMOGENEOUSSEQUENCESIMULATOR_H_
346 
Substitution models manager for non-homogeneous / non-reversible models of evolution.
Interface for all substitution models.
NonHomogeneousSequenceSimulator & operator=(const NonHomogeneousSequenceSimulator &nhss)
NonHomogeneousSequenceSimulator(const SubstitutionModelSet *modelSet, const DiscreteDistribution *rate, const Tree *tree)
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::vector< size_t > states
const SubstitutionModelSet * getSubstitutionModelSet() const
Get the substitution model associated to this instance.
The phylogenetic tree class.
const SubstitutionModel * model
This interface adds the dSimulate method to the SiteSimulator interface.
NodeTemplate< SimData > SNode
Interface for phylogenetic tree objects.
Definition: Tree.h:148
SiteContainer * simulate(size_t numberOfSites) const
The SequenceSimulator interface. SequenceSimulator classes can simulate whole datasets.
SimData & operator=(const SimData &sd)
void multipleEvolveInternal(SNode *node, const std::vector< size_t > &rateClasses) const
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...
const DiscreteDistribution * getRateDistribution() const
Get the rate distribution associated to this instance.
void evolveInternal(SNode *node, size_t rateClass) const
NonHomogeneousSequenceSimulator(const NonHomogeneousSequenceSimulator &nhss)
RASiteSimulationResult * dSimulateSite() const
Get a detailed simulation result for one site.
SimData(const SimData &sd)
const Tree * getTree() const
Get the tree 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 dEvolveInternal(SNode *node, double rate, RASiteSimulationResult &rassr) const
void dEvolve(size_t initialState, double rate, RASiteSimulationResult &rassr) const
void enableContinuousRates(bool yn)
Enable the use of continuous rates instead of discrete rates.
Data structure to store the result of a DetailedSiteSimulator.
NonHomogeneousSequenceSimulator * clone() const
Site and sequences simulation under non-homogeneous models.
The NodeTemplate class.
Definition: NodeTemplate.h:73
std::vector< std::string > getSequencesNames() const