23 std::shared_ptr<const SubstitutionProcessInterface> process) :
25 phyloTree_(process_->getParametrizablePhyloTree()),
30 seqNames_(phyloTree_->getAllLeavesNames()),
33 nbClasses_(process_->getNumberOfClasses()),
34 nbStates_(process_->getNumberOfStates()),
35 continuousRates_(false),
36 outputInternalSites_(false)
52 const auto dRate =
process_->getRateDistribution();
68 for (
auto& edge : edges)
70 const auto model = edge->getModel();
74 const auto transmodel = dynamic_pointer_cast<const TransitionModelInterface>(model);
83 double brlen = dRate->getCategory(c) *
phyloTree_->getEdge(edge->getSpeciesIndex())->getLength();
85 VVdouble* cumpxy_node_c_ = &(*cumpxy_node_)[c];
95 const auto& vSub(edge->subModelNumbers());
98 P = &transmodel->getPij_t(brlen);
102 throw Exception(
"SubstitutionProcessSiteSimulator::init : only 1 submodel can be used.");
104 const auto mmodel = dynamic_pointer_cast<const MixedTransitionModelInterface>(transmodel);
106 const auto model2 = mmodel->getNModel(vSub[0]);
108 P = &model2->getPij_t(brlen);
113 Vdouble* cumpxy_node_c_x_ = &(*cumpxy_node_c_)[x];
115 (*cumpxy_node_c_x_)[0] = (*P)(x, 0);
118 (*cumpxy_node_c_x_)[y] = (*cumpxy_node_c_x_)[y - 1] + (*P)(x, y);
127 for (
auto node:nodes)
129 if (node->isMixture())
134 for (
auto edge : outEdges)
136 auto model = dynamic_pointer_cast<const MixedTransitionModelInterface>(edge->getModel());
140 const auto& vNb(edge->subModelNumbers());
145 x += model->getNProbability(nb);
172 double rate =
process_->getRateDistribution()->randC();
191 root->state_ = initialStateIndex;
197 for (
size_t i = 0; i <
seqNames_.size(); ++i)
202 return make_unique<Site>(site,
alphabet);
213 root->state_ = initialStateIndex;
219 for (
size_t i = 0; i <
seqNames_.size(); ++i)
225 return make_unique<Site>(site,
alphabet);
232 root->state_ = ancestralStateIndex;
238 for (
size_t i = 0; i <
seqNames_.size(); ++i)
244 return make_unique<Site>(site,
alphabet);
253 double rate =
process_->getRateDistribution()->randC();
272 root->state_ = initialStateIndex;
274 auto ssr = make_unique<SiteSimulationResult>(
phyloTree_,
process_->getStateMap(), initialStateIndex);
289 root->state_ = initialStateIndex;
291 auto ssr = make_unique<SiteSimulationResult>(
phyloTree_,
process_->getStateMap(), initialStateIndex);
301 root->state_ = ancestralStateIndex;
303 auto ssr = make_unique<SiteSimulationResult>(
phyloTree_,
process_->getStateMap(), ancestralStateIndex);
314 std::shared_ptr<SimProcessNode> node,
320 if (node->isSpeciation())
324 for (
auto edge : vEdge)
328 if (edge->getModel())
332 auto tm = dynamic_pointer_cast<const SubstitutionModelInterface>(edge->getModel());
335 throw Exception(
"SimpleSubstitutionProcessSiteSimulator::EvolveInternal : detailed simulation not possible for non-markovian model on edge " +
TextTools::toString(son->getSpeciesIndex()) +
" for model " + edge->getModel()->getName());
339 double brlen =
process_->getRateDistribution()->getCategory(rateClass) *
phyloTree_->getEdge(edge->getSpeciesIndex())->getLength();
341 MutationPath mp(tm->getAlphabet(), node->state_, brlen);
354 ssr->
addNode(edge->getSpeciesIndex(), mp);
360 son->state_ = node->state_;
365 else if (node->isMixture())
367 const auto& cumProb = node->cumProb_[rateClass];
370 auto son = node->sons_[y];
371 son->state_ = node->state_;
381 std::shared_ptr<SimProcessNode> node,
387 if (node->isSpeciation())
391 for (
auto edge : vEdge)
395 if (edge->getModel())
397 auto tm = dynamic_pointer_cast<const TransitionModelInterface>(edge->getModel());
399 double brlen = rate *
phyloTree_->getEdge(edge->getSpeciesIndex())->getLength();
404 auto sm = dynamic_pointer_cast<const SubstitutionModelInterface>(edge->getModel());
407 throw Exception(
"SimpleSubstitutionProcessSiteSimulator::EvolveInternal : detailed simulation not possible for non-markovian model on edge " +
TextTools::toString(son->getSpeciesIndex()) +
" for model " + tm->getName());
411 MutationPath mp(tm->getAlphabet(), node->state_, brlen);
421 size_t rateClass =
process_->getRateDistribution()->getCategoryIndex(rate);
428 ssr->
addNode(edge->getSpeciesIndex(), mp);
437 const auto& vSub(edge->subModelNumbers());
439 if (vSub.size() == 0)
440 P = &tm->getPij_t(brlen);
444 throw Exception(
"SubstitutionProcessSiteSimulator::init : only 1 submodel can be used.");
446 const auto mmodel = dynamic_pointer_cast<const MixedTransitionModelInterface>(tm);
447 const auto model = mmodel->getNModel(vSub[0]);
449 P = &model->getPij_t(brlen);
455 rand -= (*P)(node->state_, y);
466 son->state_ = node->state_;
472 else if (node->isMixture())
474 const auto& cumProb = node->cumProb_[0];
477 auto son = node->sons_[y];
478 son->state_ = node->state_;
495 for (
size_t i = 0; i <
seqNames_.size(); i++)
497 auto index =
phyloTree_->getNodeIndex(vCN[i]);
507 for (
size_t i = 0; i <
seqNames_.size(); i++)
MutationPath detailedEvolve(size_t initialState, double time) const
Simulation a character evolution during a specified time according to the given substitution model an...
virtual std::vector< std::shared_ptr< E > > getAllEdges() const=0
virtual std::shared_ptr< N > getRoot() const=0
virtual EdgeIndex getEdgeIndex(const std::shared_ptr< E > edgeObject) const=0
virtual std::vector< std::shared_ptr< N > > getAllNodes() const=0
virtual NodeIndex getNodeIndex(const std::shared_ptr< N > nodeObject) const=0
virtual std::vector< std::shared_ptr< E > > getOutgoingEdges(const std::shared_ptr< N > node) const=0
std::shared_ptr< N > getSon(const std::shared_ptr< E > edge) const
Site simulation under a unique substitution process, given data.
virtual void resize(size_t nRows, size_t nCols)=0
This class is used by MutationProcess to store detailed results of simulations.
size_t getFinalState() const
Retrieve the final state of this path.
Generally used mutation process model.
std::shared_ptr< const ParametrizablePhyloTree > phyloTree_
std::shared_ptr< const SubstitutionProcessInterface > process_
void evolveInternal(std::shared_ptr< SimProcessNode > node, size_t rateClass, SiteSimulationResult *ssr=nullptr) const
SPTree tree_
To store states & transition probabilities of the simulator.
SimpleSubstitutionProcessSiteSimulator(std::shared_ptr< const SubstitutionProcessInterface > process)
std::unique_ptr< Site > simulateSite() const override
bool outputInternalSites_
const Alphabet & alphabet() const override
std::map< size_t, std::shared_ptr< SimProcessNode > > speciesNodes_
Map between species Indexes & used nodes, may change at each simulation.
virtual void init()
Init all probabilities.
std::vector< size_t > seqIndexes_
Vector of indexes of sequenced output species.
void outputInternalSites(bool yn) override
Sets whether we will output the internal sequences or not.
std::vector< std::string > seqNames_
Vector of names of sequenced output species.
std::shared_ptr< const Alphabet > getAlphabet() const override
std::unique_ptr< SiteSimulationResult > dSimulateSite() const override
Get a detailed simulation result for one site.
Vdouble qRates_
cumsum probas of the substitution rates
Data structure to store the result of a DetailedSiteSimulator.
virtual void addNode(unsigned int nodeId, MutationPath path)
std::string toString(T t)
Defines the basic types of data flow nodes.
std::vector< double > Vdouble
std::vector< VVdouble > VVVdouble
std::vector< Vdouble > VVdouble