43 #include "../Model/SubstitutionModelSetTools.h" 45 #include <Bpp/App/ApplicationTools.h> 46 #include <Bpp/Numeric/VectorTools.h> 47 #include <Bpp/Numeric/Matrix/MatrixTools.h> 50 #include <Bpp/Seq/Container/VectorSiteContainer.h> 59 const DiscreteDistribution* rate,
60 const Tree* tree)
throw (Exception) :
62 alphabet_(modelSet_->getAlphabet()),
63 supportedStates_(modelSet_->getAlphabetStates()),
68 leaves_(tree_.getLeaves()),
71 nbClasses_(rate_->getNumberOfCategories()),
72 nbStates_(modelSet_->getNumberOfStates()),
73 continuousRates_(
false)
75 if (!modelSet->isFullySetUpFor(*tree))
76 throw Exception(
"NonHomogeneousSequenceSimulator(constructor). Model set is not fully specified.");
84 const DiscreteDistribution* rate,
87 alphabet_(model->getAlphabet()),
88 supportedStates_(model->getAlphabetStates()),
93 leaves_(tree_.getLeaves()),
96 nbClasses_(rate_->getNumberOfCategories()),
97 nbStates_(model->getNumberOfStates()),
98 continuousRates_(false)
101 fSet->setNamespace(
"anc.");
111 for (
size_t i = 0; i <
seqNames_.size(); i++)
116 vector<SNode*> nodes =
tree_.getNodes();
120 for (
size_t i = 0; i < nodes.size(); i++)
122 SNode* node = nodes[i];
125 VVVdouble* cumpxy_node_ = &node->
getInfos().cumpxy;
129 VVdouble* cumpxy_node_c_ = &(*cumpxy_node_)[c];
131 RowMatrix<double> P = node->
getInfos().model->getPij_t(d *
rate_->getCategory(c));
134 Vdouble* cumpxy_node_c_x_ = &(*cumpxy_node_c_)[x];
136 (*cumpxy_node_c_x_)[0] = P(x, 0);
139 (*cumpxy_node_c_x_)[y] = (*cumpxy_node_c_x_)[y - 1] + P(x, y);
151 size_t initialStateIndex = 0;
152 double r = RandomTools::giveRandomNumberBetweenZeroAndEntry(1.);
160 initialStateIndex = i;
174 double rate =
rate_->randC();
181 size_t rateClass = RandomTools::giveIntRandomNumberBetweenZeroAndEntry<size_t>(
rate_->getNumberOfCategories());
193 root->
getInfos().state = ancestralStateIndex;
200 for (
size_t i = 0; i <
leaves_.size(); ++i)
202 site[i] =
leaves_[i]->getInfos().model->getAlphabetStateAsInt(
leaves_[i]->getInfos().state);
213 root->
getInfos().state = ancestralStateIndex;
220 for (
size_t i = 0; i <
leaves_.size(); i++)
222 site[i] =
leaves_[i]->getInfos().model->getAlphabetStateAsInt(
leaves_[i]->getInfos().state);
232 size_t ancestralStateIndex = 0;
233 double r = RandomTools::giveRandomNumberBetweenZeroAndEntry(1.);
241 ancestralStateIndex = i;
253 vector<size_t> ancestralStateIndices(numberOfSites, 0);
254 for (
size_t j = 0; j < numberOfSites; j++)
256 double r = RandomTools::giveRandomNumberBetweenZeroAndEntry(1.);
264 ancestralStateIndices[j] = i;
273 for (
size_t j = 0; j < numberOfSites; j++)
276 site->setPosition(static_cast<int>(j));
277 sites->addSite(*site);
286 vector<size_t> rateClasses(numberOfSites);
287 size_t nCat =
rate_->getNumberOfCategories();
288 for (
size_t j = 0; j < numberOfSites; j++)
290 rateClasses[j] = RandomTools::giveIntRandomNumberBetweenZeroAndEntry<size_t>(nCat);
293 SiteContainer* sites =
multipleEvolve(ancestralStateIndices, rateClasses);
303 size_t ancestralStateIndex = 0;
304 double r = RandomTools::giveRandomNumberBetweenZeroAndEntry(1.);
312 ancestralStateIndex = i;
327 double rate =
rate_->randC();
332 size_t rateClass = RandomTools::giveIntRandomNumberBetweenZeroAndEntry<size_t>(
rate_->getNumberOfCategories());
344 dEvolve(ancestralStateIndex, rate, *hssr);
360 size_t ancestralStateIndex = 0;
361 double r = RandomTools::giveRandomNumberBetweenZeroAndEntry(1.);
369 ancestralStateIndex = i;
380 const Vdouble* cumpxy_node_c_x_ = &node->
getInfos().cumpxy[rateClass][initialStateIndex];
381 double rand = RandomTools::giveRandomNumberBetweenZeroAndEntry(1.);
384 if (rand < (*cumpxy_node_c_x_)[y])
return y;
386 throw Exception(
"HomogeneousSequenceSimulator::evolve. The impossible happened! rand = " + TextTools::toString(rand) +
".");
394 double rand = RandomTools::giveRandomNumberBetweenZeroAndEntry(1.);
399 cumpxy += model->
Pij_t(initialStateIndex, y, l);
400 if (rand < cumpxy)
return y;
402 MatrixTools::print(model->
getPij_t(l));
403 throw Exception(
"HomogeneousSequenceSimulator::evolve. The impossible happened! rand = " + TextTools::toString(rand) +
".");
410 const std::vector<size_t>& initialStateIndices,
411 const vector<size_t>& rateClasses,
412 std::vector<size_t>& finalStateIndices)
const 414 const VVVdouble* cumpxy_node_ = &node->
getInfos().cumpxy;
415 for (
size_t i = 0; i < initialStateIndices.size(); i++)
417 const Vdouble* cumpxy_node_c_x_ = &(*cumpxy_node_)[rateClasses[i]][initialStateIndices[i]];
418 double rand = RandomTools::giveRandomNumberBetweenZeroAndEntry(1.);
421 if (rand < (*cumpxy_node_c_x_)[y])
423 finalStateIndices[i] = y;
436 cerr <<
"DEBUG: NonHomogeneousSequenceSimulator::evolveInternal. Forbidden call of method on root node." << endl;
452 cerr <<
"DEBUG: NonHomogeneousSequenceSimulator::evolveInternal. Forbidden call of method on root node." << endl;
468 cerr <<
"DEBUG: NonHomogeneousSequenceSimulator::multipleEvolveInternal. Forbidden call of method on root node." << endl;
471 const vector<size_t>* initialStates = &node->
getFather()->getInfos().states;
472 size_t n = initialStates->size();
484 const std::vector<size_t>& initialStateIndices,
485 const vector<size_t>& rateClasses)
const 489 root->
getInfos().states = initialStateIndices;
495 AlignedSequenceContainer* sites =
new AlignedSequenceContainer(
alphabet_);
497 size_t nbSites = initialStateIndices.size();
499 for (
size_t i = 0; i < n; i++)
501 vector<int> content(nbSites);
502 vector<size_t>& states =
leaves_[i]->getInfos().states;
503 model =
leaves_[i]->getInfos().model;
504 for (
size_t j = 0; j < nbSites; j++)
508 sites->addSequence(BasicSequence(
leaves_[i]->getName(), content,
alphabet_),
false);
519 root->
getInfos().state = initialState;
532 cerr <<
"DEBUG: NonHomogeneousSequenceSimulator::evolveInternal. Forbidden call of method on root node." << endl;
537 node->
getInfos().state = mp.getFinalState();
const NodeTemplate< NodeInfos > * getFather() const
Get the father of this node is there is one.
Substitution models manager for non-homogeneous / non-reversible models of evolution.
Interface for all substitution models.
const SubstitutionModel * getModelForNode(int nodeId) const
Get the model associated to a particular node id.
const Alphabet * alphabet_
virtual int getAlphabetStateAsInt(size_t index) const =0
FrequenciesSet useful for homogeneous and stationary models.
const NodeTemplate< NodeInfos > * getSon(size_t i) const
virtual const StateMap & getStateMap() const =0
NonHomogeneousSequenceSimulator(const SubstitutionModelSet *modelSet, const DiscreteDistribution *rate, const Tree *tree)
This class is used by MutationProcess to store detailed results of simulations.
void init()
Init all probabilities.
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. ...
virtual void addNode(int nodeId, MutationPath path)
std::vector< double > getRootFrequencies() const
virtual StateMap * clone() const =0
Interface for phylogenetic tree objects.
const SubstitutionModelSet * modelSet_
SiteContainer * simulate(size_t numberOfSites) const
virtual const Vdouble & getFrequencies() const =0
virtual bool hasFather() const
Tell if this node has a father node.
void multipleEvolveInternal(SNode *node, const std::vector< size_t > &rateClasses) const
virtual double getDistanceToFather() const
Get the distance to the father node is there is one, otherwise throw a NodeException.
const Alphabet * getAlphabet() const
virtual const Matrix< double > & getPij_t(double t) const =0
virtual int getId() const
Get the node's id.
virtual double Pij_t(size_t i, size_t j, double t) const =0
TreeTemplate< SNode > tree_
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...
void evolveInternal(SNode *node, size_t rateClass) const
std::vector< std::string > seqNames_
const Tree * templateTree_
RASiteSimulationResult * dSimulateSite() const
Get a detailed simulation result for one site.
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
virtual size_t getNumberOfSons() const
void dEvolve(size_t initialState, double rate, RASiteSimulationResult &rassr) const
SubstitutionModel * clone() const =0
Data structure to store the result of a DetailedSiteSimulator.
virtual const NodeInfos & getInfos() const
Generally used mutation process model.
const DiscreteDistribution * rate_
Site * simulateSite() const