bpp-phyl3  3.0.0
DetailedSiteSimulator.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_DETAILEDSITESIMULATOR_H
6 #define BPP_PHYL_SIMULATION_DETAILEDSITESIMULATOR_H
7 
8 
9 #include "../Likelihood/ParametrizablePhyloTree.h"
10 #include "MutationProcess.h"
11 #include "SiteSimulator.h"
12 
13 // From the STL:
14 #include <map>
15 #include <vector>
16 
17 namespace bpp
18 {
25 {
26 private:
27  mutable std::map<uint, size_t> indexes_;
28  size_t currentIndex_;
29  std::vector<MutationPath> paths_;
30  std::vector<size_t> ancestralStates_;
31  std::shared_ptr<const ParametrizablePhyloTree> tree_;
32  std::vector<unsigned int> leavesId_;
33  std::shared_ptr<const StateMapInterface> statemap_;
34 
35 public:
37  std::shared_ptr<const ParametrizablePhyloTree> tree,
38  std::shared_ptr<const StateMapInterface> statemap,
39  size_t ancestralState) :
40  indexes_ (),
41  currentIndex_ (0),
42  paths_ (),
44  tree_ (tree),
45  leavesId_ (tree->getNodeIndexes(tree->getLeavesUnderNode(tree->getRoot()))),
46  statemap_ (statemap)
47  {
48  indexes_[tree->getRootIndex()] = 0;
49  // Warning, watch out the indices there!
50  ancestralStates_.push_back(ancestralState);
51  }
52 
54  indexes_ (ssr.indexes_),
56  paths_ (ssr.paths_),
58  tree_ (ssr.tree_),
59  leavesId_ (ssr.leavesId_),
60  statemap_ (ssr.statemap_)
61  {}
62 
64  {
65  indexes_ = ssr.indexes_;
67  paths_ = ssr.paths_;
69  tree_ = ssr.tree_;
70  leavesId_ = ssr.leavesId_;
71  statemap_ = ssr.statemap_;
72  return *this;
73  }
74 
75  virtual ~SiteSimulationResult() {}
76 
77 public:
81  std::shared_ptr<const Alphabet> getAlphabet() const { return statemap_->getAlphabet(); }
82 
83  const Alphabet& alphabet() const { return statemap_->alphabet(); }
84 
85  virtual void addNode(unsigned int nodeId, MutationPath path)
86  {
87  indexes_[nodeId] = currentIndex_;
88  currentIndex_++;
89  paths_.push_back(path);
90  ancestralStates_.push_back(path.getFinalState());
91  }
92 
93  virtual size_t getAncestralState(size_t i) const { return ancestralStates_[i]; }
94 
95  virtual size_t getAncestralState(unsigned int nodeId) const { return ancestralStates_[1 + indexes_[nodeId]]; }
96 
97  virtual const MutationPath& getMutationPath(size_t i) const { return paths_[i]; }
98 
99  virtual const MutationPath& getMutationPath(unsigned int nodeId) const { return paths_[indexes_[nodeId]]; }
100 
101  virtual size_t getSubstitutionCount(size_t i) const { return paths_[i].getNumberOfEvents(); }
102 
103  virtual void getSubstitutionCount(
104  size_t i,
106  std::vector<double>& counts) const
107  {
108  paths_[i].getEventCounts(counts, reg);
109  }
110 
111  virtual size_t getSubstitutionCount(unsigned int nodeId) const { return paths_[indexes_[nodeId]].getNumberOfEvents(); }
112 
113  virtual void getSubstitutionCount(
114  unsigned int nodeId,
116  std::vector<double>& counts) const
117  {
118  paths_[indexes_[nodeId]].getEventCounts(counts, reg);
119  }
120 
122  {
123  size_t n = paths_.size();
124  VVdouble counts(n);
125  for (size_t i = 0; i < n; ++i)
126  {
127  counts[i].resize(reg.getNumberOfSubstitutionTypes());
128  paths_[i].getEventCounts(counts[i], reg);
129  }
130  return counts;
131  }
132 
136  virtual std::vector<size_t> getFinalStates() const
137  {
138  size_t n = leavesId_.size();
139  std::vector<size_t> states(n);
140  for (size_t i = 0; i < n; i++)
141  {
142  states[i] = ancestralStates_[1 + indexes_[leavesId_[i]]];
143  }
144  return states;
145  }
146 
150  virtual std::unique_ptr<SiteInterface> getSite(const TransitionModelInterface& model) const
151  {
152  std::vector<size_t> mstates = getFinalStates();
153  std::vector<int> astates(mstates.size());
154  for (size_t i = 0; i < mstates.size(); ++i)
155  {
156  astates[i] = statemap_->getAlphabetStateAsInt(mstates[i]);
157  }
158  auto alphabet = statemap_->getAlphabet();
159  return std::make_unique<Site>(astates, alphabet);
160  }
161 
165  virtual std::vector<std::string> getLeaveNames() const
166  {
167  size_t n = leavesId_.size();
168  std::vector<std::string> names(n);
169  for (size_t i = 0; i < n; i++)
170  {
171  names[i] = tree_->getNode(leavesId_[i])->getName();
172  }
173  return names;
174  }
175 };
176 
177 // ---------------------------------------------------------------------------
178 
186  public SiteSimulationResult
187 {
188 protected:
189  double rate_;
190 
191 public:
193  std::shared_ptr<const ParametrizablePhyloTree> tree,
194  std::shared_ptr<const StateMapInterface> stateMap,
195  size_t ancestralStateIndex,
196  double rate) :
197  SiteSimulationResult(tree, stateMap, ancestralStateIndex),
198  rate_(rate) {}
199 
201 
202 public:
206  virtual double getRate() const { return rate_; }
207 };
208 
209 // ---------------------------------------------------------------------------
210 
217  public virtual SiteSimulatorInterface
218 {
219 public:
222 
223  DetailedSiteSimulatorInterface* clone() const override = 0;
224 
225 public:
232  virtual std::unique_ptr<SiteSimulationResult> dSimulateSite() const = 0;
233  virtual std::unique_ptr<SiteSimulationResult> dSimulateSite(size_t rateClass) const = 0;
234  virtual std::unique_ptr<SiteSimulationResult> dSimulateSite(double rate) const = 0;
235  virtual std::unique_ptr<SiteSimulationResult> dSimulateSite(size_t ancestralStateIndex, double rate) const = 0;
236 };
237 } // end of namespace bpp.
238 #endif // BPP_PHYL_SIMULATION_DETAILEDSITESIMULATOR_H
This interface adds the dSimulate method to the SiteSimulator interface.
virtual std::unique_ptr< SiteSimulationResult > dSimulateSite(size_t rateClass) const =0
virtual std::unique_ptr< SiteSimulationResult > dSimulateSite() const =0
Get a detailed simulation result for one site.
virtual std::unique_ptr< SiteSimulationResult > dSimulateSite(size_t ancestralStateIndex, double rate) const =0
DetailedSiteSimulatorInterface * clone() const override=0
virtual std::unique_ptr< SiteSimulationResult > dSimulateSite(double rate) const =0
This class is used by MutationProcess to store detailed results of simulations.
size_t getFinalState() const
Retrieve the final state of this path.
Data structure to store the result of a DetailedSiteSimulator.
RASiteSimulationResult(std::shared_ptr< const ParametrizablePhyloTree > tree, std::shared_ptr< const StateMapInterface > stateMap, size_t ancestralStateIndex, double rate)
virtual double getRate() const
Data structure to store the result of a DetailedSiteSimulator.
virtual void addNode(unsigned int nodeId, MutationPath path)
std::shared_ptr< const StateMapInterface > statemap_
std::vector< size_t > ancestralStates_
SiteSimulationResult(std::shared_ptr< const ParametrizablePhyloTree > tree, std::shared_ptr< const StateMapInterface > statemap, size_t ancestralState)
virtual const MutationPath & getMutationPath(unsigned int nodeId) const
virtual size_t getSubstitutionCount(unsigned int nodeId) const
virtual std::vector< std::string > getLeaveNames() const
virtual size_t getAncestralState(unsigned int nodeId) const
virtual size_t getSubstitutionCount(size_t i) const
SiteSimulationResult & operator=(const SiteSimulationResult &ssr)
const Alphabet & alphabet() const
virtual void getSubstitutionCount(size_t i, const SubstitutionRegisterInterface &reg, std::vector< double > &counts) const
std::map< uint, size_t > indexes_
SiteSimulationResult(const SiteSimulationResult &ssr)
virtual std::vector< size_t > getFinalStates() const
virtual size_t getAncestralState(size_t i) const
std::shared_ptr< const ParametrizablePhyloTree > tree_
virtual void getSubstitutionCount(unsigned int nodeId, const SubstitutionRegisterInterface &reg, std::vector< double > &counts) const
virtual VVdouble getSubstitutionVector(const SubstitutionRegisterInterface &reg) const
std::vector< unsigned int > leavesId_
virtual const MutationPath & getMutationPath(size_t i) const
std::vector< MutationPath > paths_
std::shared_ptr< const Alphabet > getAlphabet() const
virtual std::unique_ptr< SiteInterface > getSite(const TransitionModelInterface &model) const
The SiteSimulator interface. SiteSimulator classes can simulate single sites.
Definition: SiteSimulator.h:24
The SubstitutionRegister interface.
virtual size_t getNumberOfSubstitutionTypes() const =0
Interface for all transition models.
Defines the basic types of data flow nodes.
std::vector< Vdouble > VVdouble