bpp-phyl  2.2.0
DetailedSiteSimulator.h
Go to the documentation of this file.
1 //
2 // File: DetailedSiteSimulator.h
3 // Created by: Julien Dutheil
4 // Created on: Tue Mar 14 10:51 2006
5 // from old file DetailedSequenceSimulator.h
6 // Created on: Wed Aug 24 15:20 2005
7 //
8 
9 /*
10 Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
11 
12 This software is a computer program whose purpose is to provide classes
13 for phylogenetic data analysis.
14 
15 This software is governed by the CeCILL license under French law and
16 abiding by the rules of distribution of free software. You can use,
17 modify and/ or redistribute the software under the terms of the CeCILL
18 license as circulated by CEA, CNRS and INRIA at the following URL
19 "http://www.cecill.info".
20 
21 As a counterpart to the access to the source code and rights to copy,
22 modify and redistribute granted by the license, users are provided only
23 with a limited warranty and the software's author, the holder of the
24 economic rights, and the successive licensors have only limited
25 liability.
26 
27 In this respect, the user's attention is drawn to the risks associated
28 with loading, using, modifying and/or developing or reproducing the
29 software by the user in light of its specific status of free software,
30 that may mean that it is complicated to manipulate, and that also
31 therefore means that it is reserved for developers and experienced
32 professionals having in-depth computer knowledge. Users are therefore
33 encouraged to load and test the software's suitability as regards their
34 requirements in conditions enabling the security of their systems and/or
35 data to be ensured and, more generally, to use and operate it in the
36 same conditions as regards security.
37 
38 The fact that you are presently reading this means that you have had
39 knowledge of the CeCILL license and that you accept its terms.
40 */
41 
42 #ifndef _DETAILEDSITESIMULATOR_H_
43 #define _DETAILEDSITESIMULATOR_H_
44 
45 #include "SiteSimulator.h"
46 #include "MutationProcess.h"
47 #include "../TreeTemplate.h"
48 
49 // From the STL:
50 #include <map>
51 #include <vector>
52 
53 namespace bpp
54 {
55 
62 {
63  private:
64  mutable std::map<int, size_t> indexes_;
65  size_t currentIndex_;
66  std::vector<MutationPath> paths_;
67  std::vector<size_t> ancestralStates_;
68  const Tree* tree_;
69  std::vector<int> leavesId_;
70  const Alphabet* alphabet_;
71 
72  public:
73  SiteSimulationResult(const Tree* tree, const Alphabet* alphabet, size_t ancestralState) :
74  indexes_ (),
75  currentIndex_ (0),
76  paths_ (),
78  tree_ (tree),
79  leavesId_ (tree->getLeavesId()),
80  alphabet_ (alphabet)
81  {
82  indexes_[tree->getRootId()] = 0;
83  //Warning, watch out the indices there!
84  ancestralStates_.push_back(ancestralState);
85  }
86 
88  indexes_ (ssr.indexes_),
90  paths_ (ssr.paths_),
92  tree_ (ssr.tree_),
93  leavesId_ (ssr.leavesId_),
94  alphabet_ (ssr.alphabet_)
95  {}
96 
98  {
99  indexes_ = ssr.indexes_;
101  paths_ = ssr.paths_;
103  tree_ = ssr.tree_;
104  leavesId_ = ssr.leavesId_;
105  alphabet_ = ssr.alphabet_;
106  return *this;
107  }
108 
110 
111  public:
115  virtual const Alphabet* getAlphabet() const { return alphabet_; }
116 
117  virtual void addNode(int nodeId, MutationPath path)
118  {
119  indexes_[nodeId] = currentIndex_;
120  currentIndex_++;
121  paths_.push_back(path);
122  ancestralStates_.push_back(path.getFinalState());
123  }
124 
125  virtual size_t getAncestralState(size_t i) const { return ancestralStates_[i]; }
126 
127  virtual size_t getAncestralState(int nodeId) const { return ancestralStates_[1 + indexes_[nodeId]]; }
128 
129  virtual const MutationPath& getMutationPath(size_t i) const { return paths_[i]; }
130 
131  virtual const MutationPath& getMutationPath(int nodeId) const { return paths_[indexes_[nodeId]]; }
132 
133  virtual size_t getSubstitutionCount(size_t i) const { return paths_[i].getNumberOfEvents(); }
134 
135  virtual void getSubstitutionCount(size_t i, const SubstitutionRegister& reg, std::vector<double>& counts) const {
136  paths_[i].getEventCounts(counts, reg);
137  }
138 
139  virtual size_t getSubstitutionCount(int nodeId) const { return paths_[indexes_[nodeId]].getNumberOfEvents(); }
140 
141  virtual void getSubstitutionCount(int nodeId, const SubstitutionRegister& reg, std::vector<double>& counts) const {
142  paths_[indexes_[nodeId]].getEventCounts(counts, reg);
143  }
144 
145  virtual VVdouble getSubstitutionVector(const SubstitutionRegister& reg) const
146  {
147  size_t n = paths_.size();
148  VVdouble counts(n);
149  for (size_t i = 0; i < n; ++i) {
150  counts[i].resize(reg.getNumberOfSubstitutionTypes());
151  paths_[i].getEventCounts(counts[i], reg);
152  }
153  return counts;
154  }
155 
159  virtual std::vector<size_t> getFinalStates() const
160  {
161  size_t n = leavesId_.size();
162  std::vector<size_t> states(n);
163  for (size_t i = 0; i < n; i++)
164  {
165  states[i] = ancestralStates_[1 + indexes_[leavesId_[i]]];
166  }
167  return states;
168  }
169 
173  virtual Site* getSite(const SubstitutionModel& model) const {
174  std::vector<size_t> mstates = getFinalStates();
175  std::vector<int> astates(mstates.size());
176  for (size_t i = 0; i < mstates.size(); ++i) {
177  astates[i] = model.getAlphabetStateAsInt(mstates[i]);
178  }
179  return new Site(astates, alphabet_);
180  }
181 
185  virtual std::vector<std::string> getLeaveNames() const
186  {
187  size_t n = leavesId_.size();
188  std::vector<std::string> names(n);
189  for (size_t i = 0; i < n; i++)
190  {
191  names[i] = tree_->getNodeName(leavesId_[i]);
192  }
193  return names;
194  }
195 
196 };
197 
198 //---------------------------------------------------------------------------
199 
207  public SiteSimulationResult
208 {
209  protected:
210  double rate_;
211 
212  public:
213  RASiteSimulationResult(const Tree* tree, const Alphabet * alphabet, size_t ancestralStateIndex, double rate):
214  SiteSimulationResult(tree, alphabet, ancestralStateIndex),
215  rate_(rate) {}
216 
218 
219  public:
223  virtual double getRate() const { return rate_; }
224 };
225 
226 //---------------------------------------------------------------------------
227 
234  public virtual SiteSimulator
235 {
236  public:
239 
240  public:
247  virtual SiteSimulationResult* dSimulateSite() const = 0;
248  virtual SiteSimulationResult* dSimulateSite(size_t ancestralStateIndex) const = 0;
249  virtual SiteSimulationResult* dSimulateSite(size_t ancestralStateIndex, double rate) const = 0;
250  virtual SiteSimulationResult* dSimulateSite(double rate) const = 0;
251 
252 };
253 
254 } //end of namespace bpp.
255 
256 #endif // _DETAILEDSITESIMULATOR_H_
257 
virtual SiteSimulationResult * dSimulateSite() const =0
Get a detailed simulation result for one site.
Interface for all substitution models.
virtual int getAlphabetStateAsInt(size_t index) const =0
The SiteSimulator interface. SiteSimulator classes can simulate single sites.
Definition: SiteSimulator.h:55
std::vector< MutationPath > paths_
virtual Site * getSite(const SubstitutionModel &model) const
This class is used by MutationProcess to store detailed results of simulations.
virtual void addNode(int nodeId, MutationPath path)
virtual size_t getAncestralState(size_t i) const
The SubstitutionRegister interface.
virtual int getRootId() const =0
virtual const MutationPath & getMutationPath(size_t i) const
This interface adds the dSimulate method to the SiteSimulator interface.
Interface for phylogenetic tree objects.
Definition: Tree.h:148
SiteSimulationResult(const Tree *tree, const Alphabet *alphabet, size_t ancestralState)
virtual size_t getSubstitutionCount(size_t i) const
RASiteSimulationResult(const Tree *tree, const Alphabet *alphabet, size_t ancestralStateIndex, double rate)
virtual void getSubstitutionCount(size_t i, const SubstitutionRegister &reg, std::vector< double > &counts) const
Data structure to store the result of a DetailedSiteSimulator.
virtual void getSubstitutionCount(int nodeId, const SubstitutionRegister &reg, std::vector< double > &counts) const
virtual VVdouble getSubstitutionVector(const SubstitutionRegister &reg) const
virtual std::vector< std::string > getLeaveNames() const
virtual const Alphabet * getAlphabet() const
virtual size_t getNumberOfSubstitutionTypes() const =0
virtual size_t getAncestralState(int nodeId) const
SiteSimulationResult & operator=(const SiteSimulationResult &ssr)
virtual double getRate() const
virtual size_t getSubstitutionCount(int nodeId) const
std::map< int, size_t > indexes_
SiteSimulationResult(const SiteSimulationResult &ssr)
Data structure to store the result of a DetailedSiteSimulator.
std::vector< size_t > ancestralStates_
virtual const MutationPath & getMutationPath(int nodeId) const
size_t getFinalState() const
Retrieve the final state of this path.
virtual std::vector< size_t > getFinalStates() const
virtual std::string getNodeName(int nodeId) const =0