13 shared_ptr<const SubstitutionProcessInterface> process) :
14 BaseTree(process->getModelScenario() ? 0 :
15 (process->getParametrizablePhyloTree() ? process->getParametrizablePhyloTree()->getGraph() : 0)),
18 shared_ptr<const ParametrizablePhyloTree> ptree = process->getParametrizablePhyloTree();
20 throw Exception(
"ProcessComputationTree::ProcessComputationTree: missing tree.");
23 auto scenario = process->getModelScenario();
27 auto itN = ptree->allNodesIterator();
28 for (itN->start(); !itN->end(); itN->next())
30 auto index = ptree->getNodeIndex(*(*itN));
31 auto nnode = make_shared<ProcessComputationNode>(*(*(*itN)), index);
37 auto itE = ptree->allEdgesIterator();
38 for (itE->start(); !itE->end(); itE->next())
40 auto index = ptree->getEdgeIndex(*(*itE));
41 auto model = process->getModelForNode(index);
42 size_t nmodel = process->getModelNumberForNode(index);
43 auto nedge = make_shared<ProcessComputationEdge>(model, nmodel, index);
52 map<shared_ptr<MixedTransitionModelInterface>, uint> mMrca;
54 auto vMod = scenario->getModels();
55 map<shared_ptr<MixedTransitionModelInterface>, vector<shared_ptr<PhyloNode>>> mnodes;
57 auto vNodes = ptree->getAllNodes();
59 auto root = ptree->getRoot();
62 for (
const auto& node:vNodes)
67 const auto medge =
process_->getModelForNode(ptree->getNodeIndex(node));
68 shared_ptr<MixedTransitionModelInterface> mok(0);
69 for (
const auto& mod:vMod)
71 if (mod.get() == medge.get())
80 if (mnodes.find(mok) == mnodes.end())
81 mnodes[mok] = vector<shared_ptr<PhyloNode>>();
82 mnodes[mok].push_back(node);
87 for (
const auto& mnode:mnodes)
89 auto nrca = ptree->MRCA(mnode.second);
90 mMrca[mnode.first] = ptree->getNodeIndex(nrca);
95 auto nroot = make_shared<ProcessComputationNode>(*root, ptree->getRootIndex());
105 shared_ptr<ProcessComputationNode> father,
107 map<shared_ptr<MixedTransitionModelInterface>,
110 auto spInd = father->getSpeciesIndex();
114 auto ptree =
process_->getParametrizablePhyloTree();
116 shared_ptr<MixedTransitionModelInterface> mrca(0);
119 for (
const auto& mod:mMrca)
121 if (mod.second == spInd)
124 mMrca.erase(mod.first);
136 auto vbrInd = ptree->getBranches(spInd);
138 for (
const auto& brInd:vbrInd)
140 auto sonInd = ptree->getSon(brInd);
141 auto son = ptree->getNode(sonInd);
143 auto modSon =
process_->getModelForNode(sonInd);
144 size_t nmodel =
process_->getModelNumberForNode(sonInd);
146 auto sonnode = make_shared<ProcessComputationNode>(*son, sonInd);
151 auto mixMod = dynamic_pointer_cast<const MixedTransitionModelInterface>(modSon);
155 auto nedge = make_shared<ProcessComputationEdge>(modSon, nmodel, sonInd);
156 link(father, sonnode, nedge);
164 map<Vuint, vector<shared_ptr<ModelPath>>> vMP;
167 for (
size_t i = 0; i < nbpath; i++)
169 auto smp = make_shared<ModelPath>(*scenario.
getModelPath(i));
170 if (!smp->hasModel(mixMod))
172 if (vMP.find(
Vuint()) == vMP.end())
173 vMP[v0] = vector<shared_ptr<ModelPath>>(1, smp);
175 vMP[v0].push_back(smp);
179 const Vuint np(smp->getPathNode(mixMod));
180 if (vMP.find(np) == vMP.end())
181 vMP[np] = vector<shared_ptr<ModelPath>>(1, smp);
183 vMP[np].push_back(smp);
190 auto vedge = make_shared<ProcessComputationEdge>(
nullptr, 0, sonInd,
true);
191 link(father, sonnode, vedge);
198 auto ssonnode = make_shared<ProcessComputationNode>(*son, sonInd);
202 auto ssedge = make_shared<ProcessComputationEdge>(mixMod, nmodel, sonInd,
true, vmp.first);
203 link(sonnode, ssonnode, ssedge);
207 auto s3onnode = make_shared<ProcessComputationNode>(*son, sonInd);
210 auto s3edge = make_shared<ProcessComputationEdge>(mixMod, nmodel, sonInd,
false, vmp.first);
211 link(ssonnode, s3onnode, s3edge);
214 if (vmp.second.size() > 1)
226 auto beg = vMP.begin();
227 auto nedge = make_shared<ProcessComputationEdge>(mixMod, nmodel, sonInd,
false, beg->first);
228 link(father, sonnode, nedge);
231 if (beg->second.size() == 1)
244 map<Vuint, vector<shared_ptr<ModelPath>>> vMP;
247 for (
size_t i = 0; i < nbpath; i++)
249 auto smp = make_shared<ModelPath>(*scenario.
getModelPath(i));
250 if (!smp->hasModel(mrca))
252 if (vMP.find(
Vuint()) == vMP.end())
253 vMP[v0] = vector<shared_ptr<ModelPath>>(1, smp);
255 vMP[v0].push_back(smp);
259 const Vuint np(smp->getPathNode(mrca));
260 if (vMP.find(np) == vMP.end())
261 vMP[np] = vector<shared_ptr<ModelPath>>(1, smp);
263 vMP[np].push_back(smp);
269 auto modNbs =
process_->getModelNumbers();
270 for (
auto nb: modNbs)
280 throw Exception(
"ProcessComputationTree::ProcessComputationTree : unknown model for process : " + mrca->getName());
283 auto node = ptree->getNode(spInd);
287 auto sonnode = make_shared<ProcessComputationNode>(*node, spInd);
291 auto sedge = make_shared<ProcessComputationEdge>(mrca, nmodel, spInd,
true, vmp.first);
292 link(father, sonnode, sedge);
295 if (vmp.second.size() > 1)
310 auto nodeIndex = father->getSpeciesIndex();
312 auto ptree =
process_->getParametrizablePhyloTree();
316 auto vbrInd = ptree->getBranches(nodeIndex);
318 for (
const auto brInd:vbrInd)
321 auto sonInd = ptree->getSon(brInd);
322 auto son = ptree->getNode(sonInd);
324 auto sonnode = make_shared<ProcessComputationNode>(*son, sonInd);
330 auto modSon =
process_->getModelForNode(sonInd);
331 auto nmodel =
process_->getModelNumberForNode(sonInd);
333 shared_ptr<ProcessComputationEdge> nedge;
334 auto mixmod = dynamic_pointer_cast<const MixedTransitionModelInterface>(modSon);
336 if (mixmod && path.
hasModel(mixmod))
337 nedge = make_shared<ProcessComputationEdge>(modSon, nmodel, sonInd,
false, (
const Vuint&)path.
getPathNode(mixmod));
339 nedge = make_shared<ProcessComputationEdge>(modSon, nmodel, sonInd);
341 link(father, sonnode, nedge);
EdgeIndex addEdgeIndex(const Eref edgeObject)
NodeIndex addNodeIndex(const Nref nodeObject)
virtual void link(std::shared_ptr< N > nodeObjectA, std::shared_ptr< N > nodeObjectB, std::shared_ptr< E > edgeObject=00)=0
virtual NodeIndex setNodeIndex(const std::shared_ptr< N > nodeObject, NodeIndex index)=0
virtual void associateNode(std::shared_ptr< N > nodeObject, NodeGraphid node)=0
virtual EdgeIndex setEdgeIndex(const std::shared_ptr< E > edgeObject, EdgeIndex index)=0
virtual void associateEdge(std::shared_ptr< E > edgeObject, EdgeGraphid edge)=0
virtual void createNode(std::shared_ptr< N > newNodeObject)=0
void rootAt(const std::shared_ptr< N > root)
Organization of submodels in mixed substitution models in a path. See class ModelScenario for a thoro...
const PathNode & getPathNode(std::shared_ptr< MixedTransitionModelInterface > mMod) const
gets the pathnode associated with a model
bool hasModel(std::shared_ptr< MixedTransitionModelInterface > mMod) const
checks if there is a pathnode associated with a model
Organization of submodels in mixed substitution models as paths.
std::shared_ptr< ModelPath > getModelPath(size_t i)
size_t getNumberOfModelPaths() const
static const NodeEvent speciationEvent
static const NodeEvent mixtureEvent
void buildFollowingScenario_(std::shared_ptr< ProcessComputationNode > father, const ModelScenario &scenario, std::map< std::shared_ptr< MixedTransitionModelInterface >, uint > &mMrca)
void buildFollowingPath_(std::shared_ptr< ProcessComputationNode > father, const ModelPath &path)
Build rest of the tree under given father (event on father will be set in this method)
std::shared_ptr< const SubstitutionProcessInterface > process_
ProcessComputationTree(std::shared_ptr< const SubstitutionProcessInterface > process)
construction of a ProcessComputationTree from a SubstitutionProcess
Defines the basic types of data flow nodes.
std::vector< unsigned int > Vuint