6 #include "../Model/SubstitutionModelSetTools.h"
7 #include "../../Tree/PhyloTreeTools.h"
23 std::shared_ptr<const SubstitutionModelSet> modelSet,
24 std::shared_ptr<const DiscreteDistributionInterface> rate,
25 std::shared_ptr<const Tree> tree) :
27 alphabet_(modelSet_->getAlphabet()),
28 supportedStates_(modelSet_->getAlphabetStates()),
33 leaves_(tree_.getLeaves()),
36 nbClasses_(rate_->getNumberOfCategories()),
37 nbStates_(modelSet_->getNumberOfStates()),
38 continuousRates_(false),
39 outputInternalSequences_(false)
41 if (!modelSet->isFullySetUpFor(*
tree))
42 throw Exception(
"NonHomogeneousSequenceSimulator(constructor). Model set is not fully specified.");
49 std::shared_ptr<const TransitionModelInterface> model,
50 std::shared_ptr<const DiscreteDistributionInterface> rate,
51 std::shared_ptr<const Tree> tree) :
53 alphabet_(model->getAlphabet()),
54 supportedStates_(model->getAlphabetStates()),
59 leaves_(tree_.getLeaves()),
62 nbClasses_(rate_->getNumberOfCategories()),
63 nbStates_(model->getNumberOfStates()),
64 continuousRates_(false),
65 outputInternalSequences_(false)
67 auto fSet = make_shared<FixedFrequencySet>(model->getStateMap(), model->getFrequencies());
68 fSet->setNamespace(
"anc.");
77 vector<SNode*> nodes =
tree_.getNodes();
82 for (
size_t i = 0; i <
seqNames_.size(); i++)
84 if (nodes[i]->isLeaf())
97 for (
size_t i = 0; i <
seqNames_.size(); i++)
106 for (
size_t i = 0; i < nodes.size(); i++)
108 SNode* node = nodes[i];
115 VVdouble* cumpxy_node_c_ = &(*cumpxy_node_)[c];
120 Vdouble* cumpxy_node_c_x_ = &(*cumpxy_node_c_)[x];
122 (*cumpxy_node_c_x_)[0] = P(x, 0);
125 (*cumpxy_node_c_x_)[y] = (*cumpxy_node_c_x_)[y - 1] + P(x, y);
137 size_t initialStateIndex = 0;
140 vector<double> freqs =
modelSet_->getRootFrequencies();
146 initialStateIndex = i;
160 double rate =
rate_->randC();
167 size_t rateClass = RandomTools::giveIntRandomNumberBetweenZeroAndEntry<size_t>(
rate_->getNumberOfCategories());
179 root->
getInfos().state = ancestralStateIndex;
194 vector<SNode*> nodes =
tree_.getNodes();
195 for (
size_t i = 0; i < n; i++)
199 site[i] = nodes[i - 1]->getInfos().model->getAlphabetStateAsInt(nodes[i]->getInfos().state);
203 site[i] = nodes[i]->getInfos().model->getAlphabetStateAsInt(nodes[i]->getInfos().state);
209 for (
size_t i = 0; i <
leaves_.size(); ++i)
211 site[i] =
leaves_[i]->getInfos().model->getAlphabetStateAsInt(
leaves_[i]->getInfos().state);
214 return make_unique<Site>(site,
alphabet_);
223 root->
getInfos().state = ancestralStateIndex;
238 vector<SNode*> nodes =
tree_.getNodes();
239 for (
size_t i = 0; i < n; i++)
243 site[i] = nodes[i - 1]->getInfos().model->getAlphabetStateAsInt(nodes[i]->getInfos().state);
247 site[i] = nodes[i]->getInfos().model->getAlphabetStateAsInt(nodes[i]->getInfos().state);
253 for (
size_t i = 0; i <
leaves_.size(); i++)
255 site[i] =
leaves_[i]->getInfos().model->getAlphabetStateAsInt(
leaves_[i]->getInfos().state);
258 return make_unique<Site>(site,
alphabet_);
266 size_t ancestralStateIndex = 0;
269 vector<double> freqs =
modelSet_->getRootFrequencies();
275 ancestralStateIndex = i;
287 vector<size_t> ancestralStateIndices(numberOfSites, 0);
288 for (
size_t j = 0; j < numberOfSites; j++)
292 vector<double> freqs =
modelSet_->getRootFrequencies();
298 ancestralStateIndices[j] = i;
306 sites->setSequenceNames(
seqNames_,
true);
307 for (
size_t j = 0; j < numberOfSites; j++)
310 site->setCoordinate(
static_cast<int>(j));
311 sites->addSite(site);
319 vector<size_t> rateClasses(numberOfSites);
320 size_t nCat =
rate_->getNumberOfCategories();
321 for (
size_t j = 0; j < numberOfSites; j++)
323 rateClasses[j] = RandomTools::giveIntRandomNumberBetweenZeroAndEntry<size_t>(nCat);
335 size_t ancestralStateIndex = 0;
338 vector<double> freqs =
modelSet_->getRootFrequencies();
344 ancestralStateIndex = i;
359 double rate =
rate_->randC();
364 size_t rateClass = RandomTools::giveIntRandomNumberBetweenZeroAndEntry<size_t>(
rate_->getNumberOfCategories());
375 auto hssr = make_unique<RASiteSimulationResult>(
phyloTree_,
modelSet_->getStateMap(), ancestralStateIndex, rate);
376 dEvolve(ancestralStateIndex, rate, *hssr);
377 return std::move(hssr);
392 size_t ancestralStateIndex = 0;
395 vector<double> freqs =
modelSet_->getRootFrequencies();
401 ancestralStateIndex = i;
412 const Vdouble* cumpxy_node_c_x_ = &node->
getInfos().cumpxy[rateClass][initialStateIndex];
416 if (rand < (*cumpxy_node_c_x_)[y])
429 auto model = node->
getInfos().model;
432 cumpxy += model->Pij_t(initialStateIndex, y, l);
444 const std::vector<size_t>& initialStateIndices,
445 const vector<size_t>& rateClasses,
446 std::vector<size_t>& finalStateIndices)
const
449 for (
size_t i = 0; i < initialStateIndices.size(); i++)
451 const Vdouble* cumpxy_node_c_x_ = &(*cumpxy_node_)[rateClasses[i]][initialStateIndices[i]];
455 if (rand < (*cumpxy_node_c_x_)[y])
457 finalStateIndices[i] = y;
470 cerr <<
"DEBUG: NonHomogeneousSequenceSimulator::evolveInternal. Forbidden call of method on root node." << endl;
486 cerr <<
"DEBUG: NonHomogeneousSequenceSimulator::evolveInternal. Forbidden call of method on root node." << endl;
502 cerr <<
"DEBUG: NonHomogeneousSequenceSimulator::multipleEvolveInternal. Forbidden call of method on root node." << endl;
505 const vector<size_t>* initialStates = &node->
getFather()->getInfos().states;
506 size_t n = initialStates->size();
518 const std::vector<size_t>& initialStateIndices,
519 const vector<size_t>& rateClasses)
const
523 root->
getInfos().states = initialStateIndices;
529 auto sites = make_unique<AlignedSequenceContainer>(
alphabet_);
530 size_t nbSites = initialStateIndices.size();
531 shared_ptr<const TransitionModelInterface> model =
nullptr;
534 vector<SNode*> nodes =
tree_.getNodes();
536 for (
size_t i = 0; i < n; i++)
538 vector<int> content(nbSites);
539 vector<size_t>& states = nodes[i]->getInfos().states;
542 model = nodes[i - 1]->getInfos().model;
546 model = nodes[i]->getInfos().model;
548 for (
size_t j = 0; j < nbSites; j++)
550 content[j] = model->getAlphabetStateAsInt(states[j]);
552 if (nodes[i]->isLeaf())
554 auto seq = make_unique<Sequence>(nodes[i]->getName(), content,
alphabet_);
555 sites->addSequence(nodes[i]->getName(), seq);
567 for (
size_t i = 0; i < n; i++)
569 vector<int> content(nbSites);
570 vector<size_t>& states =
leaves_[i]->getInfos().states;
571 model =
leaves_[i]->getInfos().model;
572 for (
size_t j = 0; j < nbSites; j++)
574 content[j] = model->getAlphabetStateAsInt(states[j]);
576 auto seq = make_unique<Sequence>(
leaves_[i]->getName(), content,
alphabet_);
577 sites->addSequence(
leaves_[i]->getName(), seq);
580 return std::move(sites);
589 root->
getInfos().state = initialState;
602 cerr <<
"DEBUG: NonHomogeneousSequenceSimulator::evolveInternal. Forbidden call of method on root node." << endl;
606 if (!dynamic_pointer_cast<const SubstitutionModelInterface>(tm))
607 throw Exception(
"NonHomogeneousSequenceSimulator::dEvolveInternal : detailed simulation not possible for non-markovian model");
615 rassr.
addNode(
static_cast<unsigned int>(node->
getId()), mp);
631 vector<SNode*> nodes =
tree_.getNodes();
633 for (
size_t i = 0; i <
seqNames_.size(); i++)
635 if (nodes[i]->isLeaf())
648 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...
This class is used by MutationProcess to store detailed results of simulations.
size_t getFinalState() const
Retrieve the final state of this path.
const NodeTemplate< NodeInfos > * getSon(size_t i) const
const NodeTemplate< NodeInfos > * getFather() const
Get the father of this node is there is one.
virtual const NodeInfos & getInfos() const
virtual int getId() const
Get the node's id.
virtual bool hasFather() const
Tell if this node has a father node.
virtual double getDistanceToFather() const
Get the distance to the father node is there is one, otherwise throw a NodeException.
virtual size_t getNumberOfSons() const
std::shared_ptr< const Alphabet > alphabet_
bool outputInternalSequences_
std::vector< std::string > seqNames_
std::shared_ptr< const SubstitutionModelSet > modelSet_
std::unique_ptr< SiteContainerInterface > simulate(size_t numberOfSites) const override
std::shared_ptr< const DiscreteDistributionInterface > rate_
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
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::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
std::shared_ptr< const Tree > templateTree_
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)
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.
void init()
Init all probabilities.
std::unique_ptr< SiteSimulationResult > dSimulateSite() const override
Get a detailed simulation result for one site.
std::shared_ptr< const ParametrizablePhyloTree > phyloTree_
void evolveInternal(SNode *node, size_t rateClass) const
TreeTemplate< SNode > tree_
PhyloTree with Parametrizable Phylo Branches. They SHARE their branch length parameters.
Data structure to store the result of a DetailedSiteSimulator.
Generally used mutation process model.
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