bpp-core3  3.0.0
AssociationTreeGraphImplObserver.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_GRAPH_ASSOCIATIONTREEGRAPHIMPLOBSERVER_H
6 #define BPP_GRAPH_ASSOCIATIONTREEGRAPHIMPLOBSERVER_H
7 
8 #include <iostream>
9 #include <map>
10 #include <memory>
11 #include <ostream>
12 #include <vector>
13 
16 #include "TreeGraphImpl.h"
17 
18 namespace bpp
19 {
20 template<class N, class E, class TreeGraphImpl>
22  public AssociationTreeGraphObserver<N, E>,
23  public AssociationGraphImplObserver<N, E, TreeGraphImpl>
24 {
25 public:
28 
29  typedef typename Graph::NodeId NodeGraphid;
30 
31  typedef typename Graph::EdgeId EdgeGraphid;
32 
33 public:
38  AssociationTreeGraphImplObserver(bool rooted = false) :
40  {}
41 
47  AssociationTreeGraphImplObserver(std::shared_ptr<TreeGraphImpl> subjectTreeGraph) :
48  AssociationGraphImplObserver<N, E, TreeGraphImpl>(subjectTreeGraph)
49  {}
50 
57  AssociationGraphImplObserver<N, E, TreeGraphImpl>(treeGraphObserver)
58  {}
59 
65  template<class N2, class E2>
68  {}
69 
70 
76  {
78  return *this;
79  }
80 
81 
86  {}
87 
88 
93 
94 
99  bool isValid() const
100  {
101  return this->getGraph()->isValid();
102  }
103 
109  std::shared_ptr<E> getEdgeToFather(const std::shared_ptr<N> nodeObject) const
110  {
111  return this->getEdgeFromGraphid(this->getGraph()->getEdgeToFather(this->getNodeGraphid(nodeObject)));
112  }
113 
114  std::shared_ptr<E> getEdgeToFather(const NodeIndex index) const
115  {
116  return this->getEdgeFromGraphid(this->getGraph()->getEdgeToFather(this->getNodeGraphid(this->getNode(index))));
117  }
118 
119 
124  void rootAt(const std::shared_ptr<N> root)
125  {
126  this->getGraph()->rootAt(this->getNodeGraphid(root));
127  }
128 
129  /*
130  * @brief check if rooted, ie directed
131  *
132  */
133  bool isRooted() const
134  {
135  return this->getGraph()->isRooted();
136  }
137 
143  std::shared_ptr<N> getFatherOfNode(const std::shared_ptr<N> nodeObject) const
144  {
145  return this->getNodeFromGraphid(this->getGraph()->getFatherOfNode(this->getNodeGraphid(nodeObject)));
146  }
147 
151  bool hasFather(const std::shared_ptr<N> nodeObject) const
152  {
153  return this->getGraph()->hasFather(this->getNodeGraphid(nodeObject));
154  }
155 
156  bool hasFather(const NodeIndex index) const
157  {
158  return this->getGraph()->hasFather(this->getNodeGraphid(this->getNode(index)));
159  }
160 
166  std::vector<std::shared_ptr<N>> getSons(const std::shared_ptr<N> node) const
167  {
168  return this->getNodesFromGraphid(this->getGraph()->getSons(this->getNodeGraphid(node)));
169  }
170 
171  std::vector<NodeIndex> getSons(const NodeIndex node) const
172  {
173  return this->getNodeIndexes(this->getNodesFromGraphid(this->getGraph()->getSons(this->getNodeGraphid(this->getNode(node)))));
174  }
175 
181  std::vector<std::shared_ptr<E>> getBranches(const std::shared_ptr<N> node) const
182  {
183  return this->getEdgesFromGraphid(this->getGraph()->getBranches(this->getNodeGraphid(node)));
184  }
185 
186  std::vector<EdgeIndex> getBranches(const NodeIndex node) const
187  {
188  return this->getEdgeIndexes(this->getEdgesFromGraphid(this->getGraph()->getBranches(this->getNodeGraphid(this->getNode(node)))));
189  }
190 
196  std::shared_ptr<N> getSon(const std::shared_ptr<E> edge) const
197  {
198  return this->getNodeFromGraphid(this->getGraph()->getBottom(this->getEdgeGraphid(edge)));
199  }
200 
201  NodeIndex getSon(const EdgeIndex edge) const
202  {
203  return this->getNodeIndex(this->getNodeFromGraphid(this->getGraph()->getBottom(this->getEdgeGraphid(this->getEdge(edge)))));
204  }
205 
211  std::shared_ptr<N> getFatherOfEdge(const std::shared_ptr<E> edge) const
212  {
213  return this->getNodeFromGraphid(this->getGraph()->getTop(this->getEdgeGraphid(edge)));
214  }
215 
216  NodeIndex getFatherOfEdge(const EdgeIndex edge) const
217  {
218  return this->getNodeIndex(this->getNodeFromGraphid(this->getGraph()->getTop(this->getEdgeGraphid(this->getEdge(edge)))));
219  }
220 
221 
227  size_t getNumberOfSons(const std::shared_ptr<N> node) const
228  {
229  return this->getGraph()->getNumberOfSons(this->getNodeGraphid(node));
230  }
231 
238  std::vector<std::shared_ptr<N>> getLeavesUnderNode(std::shared_ptr<N> node) const
239  {
240  return this->getNodesFromGraphid(this->getGraph()->getLeavesUnderNode(this->getNodeGraphid(node)));
241  }
242 
243  std::vector<NodeIndex> getLeavesUnderNode(NodeIndex node) const
244  {
245  return this->getNodeIndexes(getLeavesUnderNode(this->getNode(node)));
246  }
247 
252  std::vector<std::shared_ptr<N>> removeSons(const std::shared_ptr<N> node)
253  {
254  return this->getNodesFromGraphid(this->getGraph()->removeSons(this->getNodeGraphid(node)));
255  }
256 
260  void removeSon(const std::shared_ptr<N> node, const std::shared_ptr<N> son)
261  {
262  this->getGraph()->removeSon(this->getNodeGraphid(node), this->getNodeGraphid(son));
263  }
264 
272  void setFather(const std::shared_ptr<N> nodeObject, const std::shared_ptr<N> fatherNodeObject, const std::shared_ptr<E> edgeObject = 0)
273  {
274  if (edgeObject)
275  this->getGraph()->setFather(this->getNodeGraphid(nodeObject), this->getNodeGraphid(fatherNodeObject), this->getEdgeGraphid(edgeObject));
276  else
277  this->getGraph()->setFather(this->getNodeGraphid(nodeObject), this->getNodeGraphid(fatherNodeObject));
278  }
279 
280 
288  void addSon(const std::shared_ptr<N> nodeObject, const std::shared_ptr<N> sonNodeObject, const std::shared_ptr<E> edgeObject = 0)
289  {
290  if (edgeObject)
291  this->getGraph()->addSon(this->getNodeGraphid(nodeObject), this->getNodeGraphid(sonNodeObject), this->getEdgeGraphid(edgeObject));
292  else
293  this->getGraph()->addSon(this->getNodeGraphid(nodeObject), this->getNodeGraphid(sonNodeObject));
294  }
295 
301  /*
302  * @brief builds iterator on the sons of a Node
303  *
304  */
305  std::unique_ptr<typename AssociationTreeGraphObserver<N, E>::NodeIterator> sonsIterator(std::shared_ptr<N> node)
306  {
307  return this->outgoingNeighborNodesIterator(node);
308  }
309 
310  std::unique_ptr<typename AssociationTreeGraphObserver<N, E>::NodeIterator> sonsIterator(std::shared_ptr<N> node) const
311  {
312  return this->outgoingNeighborNodesIterator(node);
313  }
314 
315  /*
316  * @brief builds iterator on the branches to sons of a Node
317  *
318  */
319  std::unique_ptr<typename AssociationTreeGraphObserver<N, E>::EdgeIterator> branchesIterator(std::shared_ptr<N> node)
320  {
321  return this->outgoingEdgesIterator(node);
322  }
323 
324  std::unique_ptr<typename AssociationTreeGraphObserver<N, E>::EdgeIterator> branchesIterator(std::shared_ptr<N> node) const
325  {
326  return this->outgoingEdgesIterator(node);
327  }
328 
338  std::vector<std::shared_ptr<N>> getNodePathBetweenTwoNodes(const std::shared_ptr<N> nodeA, const std::shared_ptr<N> nodeB, bool includeAncestor = true) const
339  {
340  return this->getNodesFromGraphid(this->getGraph()->getNodePathBetweenTwoNodes(this->getNodeGraphid(nodeA), this->getNodeGraphid(nodeB), includeAncestor));
341  }
342 
343  std::vector<std::shared_ptr<E>> getEdgePathBetweenTwoNodes(const std::shared_ptr<N> nodeA, const std::shared_ptr<N> nodeB) const
344  {
345  return this->getEdgesFromGraphid(this->getGraph()->getEdgePathBetweenTwoNodes(this->getNodeGraphid(nodeA), this->getNodeGraphid(nodeB)));
346  }
347 
348  std::vector<NodeIndex> getNodePathBetweenTwoNodes(const NodeIndex nodeA, const NodeIndex nodeB, bool includeAncestor = true) const
349  {
350  return getNodeIndexes(getNodePathBetweenTwoNodes(this->getNode(nodeA), this->getNode(nodeB), includeAncestor));
351  }
352 
353  std::vector<EdgeIndex> getEdgePathBetweenTwoNodes(const NodeIndex nodeA, const NodeIndex nodeB, bool includeAncestor = true) const
354  {
355  return this->getEdgeIndexes(getEdgePathBetweenTwoNodes(this->getNode(nodeA), this->getNode(nodeB), includeAncestor));
356  }
357 
358  std::vector<std::shared_ptr<N>> getSubtreeNodes(const std::shared_ptr<N> localRoot) const
359  {
360  return this->getNodesFromGraphid(this->getGraph()->getSubtreeNodes(this->getNodeGraphid(localRoot)));
361  }
362 
363  std::vector<std::shared_ptr<E>> getSubtreeEdges(const std::shared_ptr<N> localRoot) const
364  {
366  }
367 
368  std::vector<NodeIndex> getSubtreeNodes(const NodeIndex localRoot) const
369  {
370  return getNodeIndexes(getSubtreeNodes(this->getNode(localRoot)));
371  }
372 
373  std::vector<EdgeIndex> getSubtreeEdges(const NodeIndex localRoot) const
374  {
375  return this->getEdgeIndexes(getSubtreeEdges(this->getNode(localRoot)));
376  }
377 
383  std::shared_ptr<N> MRCA(const std::vector<std::shared_ptr<N>> vNodeObject) const
384  {
385  std::vector<Graph::NodeId> vNid(vNodeObject.size());
386 
387  std::transform(vNodeObject.begin(), vNodeObject.end(), vNid.begin(), [this](const std::shared_ptr<N>& nodeObject){return this->getNodeGraphid(nodeObject);});
388 
389  return this->getNodeFromGraphid(this->getGraph()->MRCA(vNid));
390  }
391 };
392 
393 /********************/
394 
395 template<class N, class E>
397 }
398 #endif // BPP_GRAPH_ASSOCIATIONTREEGRAPHIMPLOBSERVER_H
std::vector< std::shared_ptr< N > > getLeavesUnderNode(std::shared_ptr< N > node) const
std::vector< EdgeIndex > getSubtreeEdges(const NodeIndex localRoot) const
std::vector< std::shared_ptr< N > > removeSons(const std::shared_ptr< N > node)
AssociationGraphObserver< N, E >::NodeIndex NodeIndex
std::vector< std::shared_ptr< N > > getNodePathBetweenTwoNodes(const std::shared_ptr< N > nodeA, const std::shared_ptr< N > nodeB, bool includeAncestor=true) const
Get a vector of ancestor nodes between to nodes.
AssociationTreeGraphImplObserver(bpp::AssociationTreeGraphImplObserver< N2, E2, TreeGraphImpl > const &treeGraphObserver)
std::vector< EdgeIndex > getEdgeIndexes(std::vector< Eref > edges) const
AssociationTreeGraphImplObserver(bpp::AssociationTreeGraphImplObserver< N, E, TreeGraphImpl > const &treeGraphObserver)
AssociationTreeGraphImplObserver(std::shared_ptr< TreeGraphImpl > subjectTreeGraph)
std::vector< Nref > getNodesFromGraphid(std::vector< NodeGraphid > nodes) const
void addSon(const std::shared_ptr< N > nodeObject, const std::shared_ptr< N > sonNodeObject, const std::shared_ptr< E > edgeObject=0)
NodeIndex getFatherOfEdge(const EdgeIndex edge) const
std::unique_ptr< typename AssociationTreeGraphObserver< N, E >::NodeIterator > sonsIterator(std::shared_ptr< N > node)
AssociationTreeGraphImplObserver< N, E, TreeGraphImpl > * clone() const
Defines a Tree Graph Associator. It is a template which follows (subscribed to) a Graph...
std::vector< NodeIndex > getSons(const NodeIndex node) const
unsigned int NodeId
Definition: Graph.h:30
const std::shared_ptr< TreeGraphImpl > getGraph() const
std::vector< EdgeIndex > getBranches(const NodeIndex node) const
std::vector< Eref > getEdgesFromGraphid(std::vector< EdgeGraphid > edges) const
AssociationGraphImplObserver< N, E, GraphImpl > & operator=(bpp::AssociationGraphImplObserver< N, E, GraphImpl > const &graphObserver)
std::vector< std::shared_ptr< E > > getBranches(const std::shared_ptr< N > node) const
std::vector< std::shared_ptr< E > > getEdgePathBetweenTwoNodes(const std::shared_ptr< N > nodeA, const std::shared_ptr< N > nodeB) const
std::shared_ptr< N > getFatherOfNode(const std::shared_ptr< N > nodeObject) const
void removeSon(const std::shared_ptr< N > node, const std::shared_ptr< N > son)
std::shared_ptr< N > getSon(const std::shared_ptr< E > edge) const
std::vector< NodeIndex > getNodeIndexes(std::vector< Nref > nodes) const
std::unique_ptr< typename AssociationTreeGraphObserver< N, E >::NodeIterator > sonsIterator(std::shared_ptr< N > node) const
std::vector< std::shared_ptr< N > > getSubtreeNodes(const std::shared_ptr< N > localRoot) const
AssociationGraphObserver< N, E >::EdgeIndex EdgeIndex
size_t getNumberOfSons(const std::shared_ptr< N > node) const
std::unique_ptr< typename AssociationTreeGraphObserver< N, E >::EdgeIterator > branchesIterator(std::shared_ptr< N > node) const
std::vector< NodeIndex > getNodePathBetweenTwoNodes(const NodeIndex nodeA, const NodeIndex nodeB, bool includeAncestor=true) const
bool hasFather(const std::shared_ptr< N > nodeObject) const
void setFather(const std::shared_ptr< N > nodeObject, const std::shared_ptr< N > fatherNodeObject, const std::shared_ptr< E > edgeObject=0)
std::unique_ptr< typename AssociationTreeGraphObserver< N, E >::EdgeIterator > branchesIterator(std::shared_ptr< N > node)
std::vector< std::shared_ptr< N > > getSons(const std::shared_ptr< N > node) const
std::vector< NodeIndex > getSubtreeNodes(const NodeIndex localRoot) const
std::vector< NodeIndex > getLeavesUnderNode(NodeIndex node) const
std::unique_ptr< typename AssociationGraphObserver< N, E >::NodeIterator > outgoingNeighborNodesIterator(Nref node)
std::shared_ptr< E > getEdgeToFather(const NodeIndex index) const
void rootAt(const std::shared_ptr< N > root)
Sets the root and make the tree directed from root to leaves.
std::vector< EdgeIndex > getEdgePathBetweenTwoNodes(const NodeIndex nodeA, const NodeIndex nodeB, bool includeAncestor=true) const
std::vector< std::shared_ptr< E > > getSubtreeEdges(const std::shared_ptr< N > localRoot) const
AssociationTreeGraphImplObserver< N, E, TreeGraphImpl > & operator=(bpp::AssociationTreeGraphImplObserver< N, E, TreeGraphImpl > const &treeGraphObserver)
std::shared_ptr< N > getFatherOfEdge(const std::shared_ptr< E > edge) const
std::shared_ptr< E > getEdgeToFather(const std::shared_ptr< N > nodeObject) const
std::shared_ptr< N > MRCA(const std::vector< std::shared_ptr< N >> vNodeObject) const
unsigned int EdgeId
Definition: Graph.h:31
NodeIndex getSon(const EdgeIndex edge) const
std::unique_ptr< typename AssociationGraphObserver< N, E >::EdgeIterator > outgoingEdgesIterator(Nref node)