bpp-phyl3  3.0.0
BackwardLikelihoodTree.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
6 #include "Parametrizable.h"
7 
8 using namespace bpp;
9 using namespace std;
10 
12 {
13  if (!forwardTree_)
14  throw Exception("BackwardLikelihoodTree::makeBackwardLikelihoodAtEdge: forwardTree_ is missing.");
15 
16  auto edgeForward = forwardTree_->getEdge(edgeIndex);
17  auto fatherIndex = forwardTree_->getFatherOfEdge(edgeIndex);
18  auto fatherForward = forwardTree_->getNode(fatherIndex);
19 
20  // get/check if node with backward likelihood exists
21  auto backNode = hasNode(fatherIndex)
22  ? getNode(fatherIndex)
23  : makeBackwardLikelihoodAtNode(fatherIndex);
24 
25  auto fatherNode = forwardTree_->getProcessTree()->getNode(fatherIndex);
26 
27  BackwardLikelihoodAboveRef backwardEdge;
28 
29  if (fatherNode->isSpeciation())
30  {
31  // get forward likelihoods of brothers
32  auto edgeIds = forwardTree_->getOutgoingEdges(fatherIndex);
33 
34  NodeRefVec deps;
35  for (auto eId : edgeIds)
36  {
37  if (eId != edgeIndex)
38  deps.push_back(forwardTree_->getEdge(eId));
39  }
40  deps.push_back(backNode);
41 
42  backwardEdge = SpeciationBackward::create (context_, std::move (deps), likelihoodMatrixDim_);
43  }
44  else if (fatherNode->isMixture()) // Transmit array with no modification
45  {
46  backwardEdge = backNode;
47  }
48  else
49  throw Exception("BackwardLikelihoodTree::makeBackwardLikelihoodAtEdge : event not recognized for node " + TextTools::toString(fatherNode->getSpeciesIndex()));
50 
51  // put object in the tree
52  if (!hasEdge(backwardEdge))
53  {
54  associateEdge(backwardEdge, forwardTree_->getEdgeGraphid(edgeForward));
55  setEdgeIndex(backwardEdge, edgeIndex);
56  }
57 
58  return backwardEdge;
59 }
60 
61 
63 {
64  if (!forwardTree_)
65  throw Exception("BackwardLikelihoodTree::makeBackwardLikelihoodAtNode: forwardTree_ is missing.");
66 
67  auto forwardNode = forwardTree_->getNode(nodeIndex);
68  // if root
69  if (nodeIndex == forwardTree_->getRootIndex())
70  return setRootFrequencies(rFreqs_);
71 
72  // else get incoming edges
73  const auto edgesIndexes = forwardTree_->getIncomingEdges(nodeIndex);
74 
75  // get upper dependencies
76  NodeRefVec deps;
77  ConditionalLikelihoodRef backwardNode(0);
78 
79  for (const auto& edgeIndex:edgesIndexes)
80  {
81  auto backEdge = hasEdge(edgeIndex)
82  ? getEdge(edgeIndex)
83  : makeBackwardLikelihoodAtEdge(edgeIndex);
84 
85  auto iedge = forwardTree_->getEdge(edgeIndex);
86 
87  const auto processEdge = forwardTree_->getProcessTree()->getEdge(edgeIndex);
88 
89  const auto brprob = processEdge->getProba();
90 
91  ConditionalLikelihoodRef backLikeEdge;
92 
93  if (processEdge->getBrLen()) // Branch with transition through a model
94  {
95  auto transitionMatrix = processEdge->getTransitionMatrix();
96 
97  // Uses the transposed transition matrix to compute the bottom
98  // of the edge
99 
100  backLikeEdge = BackwardTransition::create (context_, {transitionMatrix, backEdge}, likelihoodMatrixDim_);
101  }
102  else if (brprob)
103  backLikeEdge = BackwardProportion::create(context_, {brprob, backEdge}, likelihoodMatrixDim_);
104  else
105  throw Exception("BackwardLikelihoodTree::makeBackwardLikelihoodAtNode : missing information on edge " + TextTools::toString(processEdge->getSpeciesIndex()));
106 
107  if (edgesIndexes.size() == 1)
108  {
109  backwardNode = backLikeEdge;
110  break;
111  }
112  else
113  deps.push_back(backLikeEdge);
114  }
115 
116  // Then compute sum if several incoming nodes
117  if (deps.size() > 1)
118  backwardNode = MixtureBackward::create(context_, std::move(deps), likelihoodMatrixDim_);
119 
120 
121  if (!hasNode(backwardNode))
122  {
123  associateNode(backwardNode, forwardTree_->getNodeGraphid(forwardNode));
124  setNodeIndex(backwardNode, nodeIndex);
125  }
126 
127  return backwardNode;
128 }
AssociationGraphObserver< N, E >::EdgeIndex EdgeIndex
AssociationGraphObserver< N, E >::NodeIndex NodeIndex
BackwardLikelihoodAboveRef makeBackwardLikelihoodAtEdge(PhyloTree::EdgeIndex index)
ConditionalLikelihoodRef makeBackwardLikelihoodAtNode(PhyloTree::NodeIndex index)
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 MatrixProduct node with the given output dimensions.
std::string toString(T t)
Defines the basic types of data flow nodes.
ValueRef< MatrixLik > ConditionalLikelihoodRef
std::vector< NodeRef > NodeRefVec
Alias for a dependency vector (of NodeRef).
Definition: DataFlow.h:81
ValueRef< MatrixLik > BackwardLikelihoodAboveRef