bpp-phyl3  3.0.0
ForwardLikelihoodTree.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_PHYL_LIKELIHOOD_DATAFLOW_FORWARDLIKELIHOODTREE_H
6 #define BPP_PHYL_LIKELIHOOD_DATAFLOW_FORWARDLIKELIHOODTREE_H
7 
10 
12 #include "Definitions.h"
13 
14 namespace bpp
15 {
16 // using RowLik = Eigen::Matrix<double, 1, Eigen::Dynamic>;
17 // using MatrixLik = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>;
18 
19 
21 {
22  return Dimension<TransitionFunction>(nbState);
23 }
24 
25 inline MatrixDimension conditionalLikelihoodDimension (Eigen::Index nbState, Eigen::Index nbSite)
26 {
27  return {nbState, nbSite};
28 }
29 
39 
49 
60 
63 
74 
88 // Lower Conditional Likelihood under nodes
91 
92 // Lower Likelihood at top of edges
95 
96 using DAGindexes = std::vector<uint>;
97 using Speciesindex = uint;
98 
99 
100 class ForwardLikelihoodTree : public AssociationDAGlobalGraphObserver<ConditionalLikelihoodForward, ForwardLikelihoodBelow>
101 {
103 
104 private:
106  std::shared_ptr<ProcessTree> processTree_;
109  Eigen::Index nbState_;
110  Eigen::Index nbSites_;
111 
112  /* Map of the indexes of nodes between species tree and
113  * likelihood tree */
114 
115  std::map<Speciesindex, DAGindexes> mapNodesIndexes_; // For nodes that bring
116  // information (ie not the empty ones)
117 
118  std::map<Speciesindex, DAGindexes> mapEdgesIndexes_; // For edges that bring
119  // information (ie not the empty ones)
120 
121 public:
123  std::shared_ptr<ProcessTree> tree,
124  const StateMapInterface& statemap) :
125  DAClass(),
126  context_(c), processTree_(tree), likelihoodMatrixDim_(), statemap_(statemap), nbState_(Eigen::Index(statemap.getNumberOfModelStates())), nbSites_(0)
127  {}
128 
130  {
131  nbSites_ = Eigen::Index(sites.getNumberOfSites ());
134  createNode(bidonRoot);
135  /* Not sure it is necessary:
136 
137  setNodeIndex(bidonRoot,processTree_->getNodeIndex(processTree_->getRoot()));
138  */
139 
140  rootAt(bidonRoot); // for construction, temporary top node for new edges
141  auto n = makeForwardLikelihoodAtNode (processTree_->getRoot(), sites);
142  rootAt(n);
143  deleteNode(bidonRoot);
144  }
145 
146 private:
152  std::shared_ptr<ProcessEdge> edge,
153  const AlignmentDataInterface& sites);
154 
155  /*
156  * @brief Compute ConditionalLikelihood after reading node on
157  * the forward process (ie just above node).
158  */
160  std::shared_ptr<ProcessNode> node,
161  const AlignmentDataInterface& sites);
162 
167  const std::string& sequenceName,
168  const AlignmentDataInterface& sites);
169 
175 
176 protected:
178  {
179  return context_;
180  }
181 
182 public:
183  /*
184  * @brief Links with processTree edges & nodes
185  *
186  */
187  std::shared_ptr<const ProcessTree> getProcessTree() const
188  {
189  return processTree_;
190  }
191 
192  /*
193  * @brief Get the nodes indexes of the DAG that correspond to
194  * the species Index (of the Process tree).
195  */
196  const DAGindexes& getDAGNodesIndexes(const Speciesindex speciesIndex) const
197  {
198  return mapNodesIndexes_.at(speciesIndex);
199  }
200 
201  /*
202  * @brief Get the edges indexes of the DAG that correspond to
203  * the species Index (of the Process tree).
204  */
205  const DAGindexes& getDAGEdgesIndexes(const Speciesindex speciesIndex) const
206  {
207  return mapEdgesIndexes_.at(speciesIndex);
208  }
209 
210  /*
211  * @brief get the forward likehoodarray at a given node in the DAG,
212  * which number may not be the number in the tree.
213  *
214  * @param nodeId : index of the node in the likelihood DAG.
215  */
217  {
218  return getNode(nodeId);
219  }
220 
222  {
223  return getRoot();
224  }
225 
227  friend class ProbabilityDAG;
228 };
229 
238 using ProbaRef = std::shared_ptr<Value<double>>;
239 
241 
243 
245 
247  public DAProb
248 {
249 private:
251 
252 public:
253  ProbabilityDAG(std::shared_ptr<ForwardLikelihoodTree> tree);
254 
255 public:
257  {
258  return getNode(nodeId)->targetValue();
259  }
260 
262  {
263  return getEdge(edgeId)->targetValue();
264  }
265 
266 private:
271  ProbaRef makeProbaAtEdge_(PhyloTree::EdgeIndex edgeIndex, std::shared_ptr<ForwardLikelihoodTree> tree);
272 
273  ProbaRef makeProbaAtNode_(PhyloTree::EdgeIndex edgeIndex, std::shared_ptr<ForwardLikelihoodTree> tree);
274 };
275 } // end of namespace bpp.
276 #endif // BPP_PHYL_LIKELIHOOD_DATAFLOW_FORWARDLIKELIHOODTREE_H
void rootAt(const std::shared_ptr< N > root)
AssociationGraphObserver< N, E >::EdgeIndex EdgeIndex
AssociationGraphObserver< N, E >::NodeIndex NodeIndex
virtual std::shared_ptr< N > getNode(NodeIndex nodeIndex) const=0
virtual void deleteNode(std::shared_ptr< N > nodeObject)=0
virtual std::shared_ptr< N > getRoot() const=0
virtual std::shared_ptr< E > getEdge(EdgeIndex edgeIndex) const=0
virtual void createNode(std::shared_ptr< N > newNodeObject)=0
static std::shared_ptr< Self > create(Context &c, const Dimension< T > &dim)
Build a new ConstantZero node of the given dimension.
Context for dataflow node construction.
Definition: DataFlow.h:527
void initialize(const AlignmentDataInterface &sites)
std::shared_ptr< ProcessTree > processTree_
ForwardLikelihoodTree(Context &c, std::shared_ptr< ProcessTree > tree, const StateMapInterface &statemap)
ConditionalLikelihoodForwardRef makeInitialConditionalLikelihood(const std::string &sequenceName, const AlignmentDataInterface &sites)
Compute ConditionalLikelihood for leaf.
const StateMapInterface & statemap_
std::map< Speciesindex, DAGindexes > mapNodesIndexes_
const ValueRef< MatrixLik > getForwardLikelihoodArray(uint nodeId) const
const ValueRef< MatrixLik > getForwardLikelihoodArrayAtRoot() const
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).
std::shared_ptr< const ProcessTree > getProcessTree() const
std::map< Speciesindex, DAGindexes > mapEdgesIndexes_
const DAGindexes & getDAGEdgesIndexes(const Speciesindex speciesIndex) const
void setSpeciesMapIndexes_()
Map the species indexes and the likelihood DAG indexes.
ConditionalLikelihoodForwardRef makeForwardLikelihoodAtNode(std::shared_ptr< ProcessNode > node, const AlignmentDataInterface &sites)
const DAGindexes & getDAGNodesIndexes(const Speciesindex speciesIndex) const
double getProbaAtNode(PhyloTree::NodeIndex nodeId)
ProbaRef makeProbaAtNode_(PhyloTree::EdgeIndex edgeIndex, std::shared_ptr< ForwardLikelihoodTree > tree)
double getProbaAtEdge(PhyloTree::EdgeIndex edgeId)
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.
Map the states of a given alphabet which have a model state.
Definition: StateMap.h:25
virtual size_t getNumberOfSites() const=0
Defines the basic types of data flow nodes.
std::shared_ptr< Value< T > > ValueRef
Shared pointer alias for Value<T>.
Definition: DataFlow.h:84
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
Dimension< TransitionFunction > transitionFunctionDimension(Eigen::Index nbState)
MatrixDimension conditionalLikelihoodDimension(Eigen::Index nbState, Eigen::Index nbSite)
std::shared_ptr< Value< double > > ProbaRef
ValueRef< MatrixLik > ForwardLikelihoodBelowRef
uint Speciesindex
Basic matrix dimension type.