18 const string& sequenceName,
24 Eigen::MatrixXd initCondLik ((
int)nbState_, (
int)nbSites);
25 for (
size_t site = 0; site < nbSites; ++site)
27 for (
auto state = 0; state < nbState_; ++state)
29 initCondLik (Eigen::Index (state), Eigen::Index (site)) =
30 sites (site, sequenceIndex, statemap_.getAlphabetStateAsInt(
size_t(state)));
38 shared_ptr<ProcessEdge> processEdge,
41 const auto brlen = processEdge->getBrLen();
42 const auto model = processEdge->getModel();
43 const auto nMod = processEdge->getNMod();
44 const auto brprob = processEdge->getProba();
46 auto childConditionalLikelihood = makeForwardLikelihoodAtNode (processTree_->getSon(processEdge), sites);
50 auto zero = context_.getZero();
54 if (dynamic_pointer_cast<const TransitionModelInterface>(model->targetValue()))
56 auto transitionMatrix = ConfiguredParametrizable::createMatrix<ConfiguredModel, TransitionMatrixFromModel, Eigen::MatrixXd>(context_, {model, brlen,
zero, nMod},
transitionMatrixDimension (
size_t(nbState_)));
58 processEdge->setTransitionMatrix(transitionMatrix);
61 context_, {transitionMatrix, childConditionalLikelihood}, likelihoodMatrixDim_);
72 forwardEdge = ForwardProportion::create(
73 context_, {brprob, childConditionalLikelihood}, likelihoodMatrixDim_);
77 forwardEdge = childConditionalLikelihood;
80 if (!hasEdgeIndex(forwardEdge))
85 link(getRoot(), childConditionalLikelihood, forwardEdge);
87 setEdgeIndex(forwardEdge, processTree_->getEdgeIndex(processEdge));
89 auto spIndex = processEdge->getSpeciesIndex();
93 if (mapEdgesIndexes_.find(spIndex) == mapEdgesIndexes_.end())
95 mapEdgesIndexes_[spIndex].push_back(getEdgeIndex(forwardEdge));
100 std::cerr <<
"E " << getEdgeIndex(forwardEdge) <<
" : forwardEdge " << forwardEdge << endl;
101 std::cerr <<
" -> N " << processTree_->getNodeIndex(processTree_->getSon(processEdge)) << std::endl;
109 const auto childBranches = processTree_->getBranches (processNode);
111 auto spIndex = processNode->getSpeciesIndex();
115 if (childBranches.empty ())
117 forwardNode = makeInitialConditionalLikelihood (processNode->getName (), sites);
119 if (!hasNodeIndex(forwardNode))
121 createNode(forwardNode);
122 setNodeIndex(forwardNode, processTree_->getNodeIndex(processNode));
123 if (mapNodesIndexes_.find(spIndex) == mapNodesIndexes_.end())
125 mapNodesIndexes_[spIndex].push_back(getNodeIndex(forwardNode));
132 std::vector<ForwardLikelihoodBelowRef> depE(childBranches.size());
135 for (
size_t i = 0; i < childBranches.size (); ++i)
137 depE[i] = makeForwardLikelihoodAtEdge (childBranches[i], sites);
141 if (processNode->isSpeciation())
142 forwardNode = SpeciationForward::create(context_, std::move(deps),
143 likelihoodMatrixDim_);
144 else if (processNode->isMixture())
146 likelihoodMatrixDim_);
148 throw Exception(
"ForwardLikelihoodTree::makeConditionalLikelihoodAtNode : event not recognized for node " +
TextTools::toString(processNode->getSpeciesIndex()));
151 if (!hasNodeIndex(forwardNode))
153 createNode(forwardNode);
154 setNodeIndex(forwardNode, processTree_->getNodeIndex(processNode));
159 bool fathmixture(
false);
161 if (processTree_->hasFather(processNode))
163 auto fatherNode = processTree_->getFatherOfNode (processNode);
164 fathmixture = fatherNode->isMixture();
169 if (mapNodesIndexes_.find(spIndex) == mapNodesIndexes_.end())
171 mapNodesIndexes_[spIndex].push_back(getNodeIndex(forwardNode));
174 for (
size_t i = 0; i < depE.size (); ++i)
176 auto fs = getNodes(depE[i]);
178 unlink(fs.first, fs.second);
179 link(forwardNode, fs.second, depE[i]);
185 std::cerr <<
"N " << getNodeIndex(forwardNode) <<
" : forwardNode " << forwardNode << endl;
186 for (
size_t i = 0; i < childBranches.size (); ++i)
188 std::cerr <<
" -> E " << processTree_->getEdgeIndex(childBranches[i]) << std::endl;
202 DAProb(forwardTree->getGraph()), context_(forwardTree->getContext())
206 associateNode(rootProb, forwardTree->getNodeGraphid(forwardTree->getRoot()));
209 auto allIndex = forwardTree->getAllNodesIndexes();
210 std::vector<const Node_DF*> vN;
212 for (
auto id: allIndex)
215 if (forwardTree->getOutgoingEdges(
id).size() == 0)
230 auto fatherIndex = forwardTree->getFatherOfEdge(edgeIndex);
231 auto edgeForward = forwardTree->getEdge(edgeIndex);
234 auto probaNode =
hasNode(fatherIndex)
238 const auto processEdge = forwardTree->getProcessTree()->getEdge(edgeIndex);
240 const auto brprob = processEdge->getProba();
256 associateEdge(probaEdge, forwardTree->getEdgeGraphid(edgeForward));
267 const auto edgesIndexes = forwardTree->getIncomingEdges(nodeIndex);
273 auto forwardNode = forwardTree->getNode(nodeIndex);
275 for (
const auto& edgeIndex:edgesIndexes)
277 auto backEdge =
hasEdge(edgeIndex)
281 if (edgesIndexes.size() == 1)
287 deps.push_back(backEdge);
296 associateNode(probaNode, forwardTree->getNodeGraphid(forwardNode));
AssociationGraphObserver< N, E >::EdgeIndex EdgeIndex
AssociationGraphObserver< N, E >::NodeIndex NodeIndex
virtual std::shared_ptr< N > getNode(NodeIndex nodeIndex) const=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 bool hasNode(NodeIndex nodeIndex) const=0
virtual std::shared_ptr< E > getEdge(EdgeIndex edgeIndex) const=0
virtual bool hasEdge(EdgeIndex edgeIndex) const=0
virtual void associateEdge(std::shared_ptr< E > edgeObject, EdgeGraphid edge)=0
static ValueRef< R > create(Context &c, NodeRefVec &&deps, const Dimension< R > &dim)
Build a new CWiseAdd node with the given output dimensions.
static ValueRef< R > create(Context &c, NodeRefVec &&deps, const Dimension< R > &dim)
Build a new CWiseApply node.
static std::shared_ptr< Self > create(Context &c, const Dimension< T > &dim)
Build a new ConstantOne node of the given dimension.
ConditionalLikelihoodForwardRef makeInitialConditionalLikelihood(const std::string &sequenceName, const AlignmentDataInterface &sites)
Compute ConditionalLikelihood for leaf.
ForwardLikelihoodBelowRef makeForwardLikelihoodAtEdge(std::shared_ptr< ProcessEdge > edge, const AlignmentDataInterface &sites)
Compute ConditionalLikelihood after reading edge on the forward process (ie at top of the edge).
ConditionalLikelihoodForwardRef makeForwardLikelihoodAtNode(std::shared_ptr< ProcessNode > node, const AlignmentDataInterface &sites)
static ValueRef< R > create(Context &c, NodeRefVec &&deps, const Dimension< R > &dim, size_t id=0)
Build a new Convert node with the given output dimensions.
static ValueRef< R > create(Context &c, NodeRefVec &&deps, const Dimension< R > &dim)
Build a new MatrixProduct node with the given output dimensions.
ProbaRef makeProbaAtNode_(PhyloTree::EdgeIndex edgeIndex, std::shared_ptr< ForwardLikelihoodTree > tree)
ProbabilityDAG(std::shared_ptr< ForwardLikelihoodTree > tree)
ProbaRef makeProbaAtEdge_(PhyloTree::EdgeIndex edgeIndex, std::shared_ptr< ForwardLikelihoodTree > tree)
computation of the probabilities with the same approach as for BackwardLikelihoodTree.
static ValueRef< T > create(Context &c, T &&value, const std::string &name)
virtual size_t getNumberOfSites() const=0
virtual size_t getSequencePosition(const std::string &sequenceKey) const =0
static std::shared_ptr< Self > create(Context &c, NodeRefVec &&deps, const Dimension< T > &dim)
std::string toString(T t)
T zero(const Dimension< T > &)
Defines the basic types of data flow nodes.
std::vector< uint > DAGindexes
Helper: create a map with mutable dataflow nodes for each branch of the tree. The map is indexed by b...
ValueRef< MatrixLik > ConditionalLikelihoodForwardRef
std::vector< NodeRef > NodeRefVec
Alias for a dependency vector (of NodeRef).
Dimension< TransitionFunction > transitionFunctionDimension(Eigen::Index nbState)
std::shared_ptr< Value< double > > ProbaRef
ValueRef< MatrixLik > ForwardLikelihoodBelowRef
MatrixDimension transitionMatrixDimension(std::size_t nbState)
Specialisation of Dimension<T> for floating point types.