bpp-core3  3.0.0
AssociationTreeGraphImplObserver.h
Go to the documentation of this file.
1 //
2 // File: AssociationTreeGraphImplObserver.h
3 // Authors:
4 // Thomas Bigot
5 // Last modified: vendredi 4 novembre 2016, à 10h 21
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
10 
11  This software is a computer program whose purpose is to provide utilitary
12  classes. This file belongs to the Bio++ Project.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 #ifndef BPP_GRAPH_ASSOCIATIONTREEGRAPHIMPLOBSERVER_H
42 #define BPP_GRAPH_ASSOCIATIONTREEGRAPHIMPLOBSERVER_H
43 
44 #include <iostream>
45 #include <map>
46 #include <memory>
47 #include <ostream>
48 #include <vector>
49 
52 #include "TreeGraphImpl.h"
53 
54 namespace bpp
55 {
56 template<class N, class E, class TreeGraphImpl>
58  public AssociationTreeGraphObserver<N, E>,
59  public AssociationGraphImplObserver<N, E, TreeGraphImpl>
60 {
61 public:
64 
65  typedef typename Graph::NodeId NodeGraphid;
66 
67  typedef typename Graph::EdgeId EdgeGraphid;
68 
69 public:
74  AssociationTreeGraphImplObserver(bool rooted = false) :
76  {}
77 
83  AssociationTreeGraphImplObserver(std::shared_ptr<TreeGraphImpl> subjectTreeGraph) :
84  AssociationGraphImplObserver<N, E, TreeGraphImpl>(subjectTreeGraph)
85  {}
86 
93  AssociationGraphImplObserver<N, E, TreeGraphImpl>(treeGraphObserver)
94  {}
95 
101  template<class N2, class E2>
104  {}
105 
106 
112  {
114  return *this;
115  }
116 
117 
122  {}
123 
124 
129 
130 
135  bool isValid() const
136  {
137  return this->getGraph()->isValid();
138  }
139 
145  std::shared_ptr<E> getEdgeToFather(const std::shared_ptr<N> nodeObject) const
146  {
147  return this->getEdgeFromGraphid(this->getGraph()->getEdgeToFather(this->getNodeGraphid(nodeObject)));
148  }
149 
150  std::shared_ptr<E> getEdgeToFather(const NodeIndex index) const
151  {
152  return this->getEdgeFromGraphid(this->getGraph()->getEdgeToFather(this->getNodeGraphid(this->getNode(index))));
153  }
154 
155 
160  void rootAt(const std::shared_ptr<N> root)
161  {
162  this->getGraph()->rootAt(this->getNodeGraphid(root));
163  }
164 
165  /*
166  * @brief check if rooted, ie directed
167  *
168  */
169  bool isRooted() const
170  {
171  return this->getGraph()->isRooted();
172  }
173 
179  std::shared_ptr<N> getFatherOfNode(const std::shared_ptr<N> nodeObject) const
180  {
181  return this->getNodeFromGraphid(this->getGraph()->getFatherOfNode(this->getNodeGraphid(nodeObject)));
182  }
183 
187  bool hasFather(const std::shared_ptr<N> nodeObject) const
188  {
189  return this->getGraph()->hasFather(this->getNodeGraphid(nodeObject));
190  }
191 
192  bool hasFather(const NodeIndex index) const
193  {
194  return this->getGraph()->hasFather(this->getNodeGraphid(this->getNode(index)));
195  }
196 
202  std::vector<std::shared_ptr<N> > getSons(const std::shared_ptr<N> node) const
203  {
204  return this->getNodesFromGraphid(this->getGraph()->getSons(this->getNodeGraphid(node)));
205  }
206 
207  std::vector<NodeIndex> getSons(const NodeIndex node) const
208  {
209  return this->getNodeIndexes(this->getNodesFromGraphid(this->getGraph()->getSons(this->getNodeGraphid(this->getNode(node)))));
210  }
211 
217  std::vector<std::shared_ptr<E> > getBranches(const std::shared_ptr<N> node) const
218  {
219  return this->getEdgesFromGraphid(this->getGraph()->getBranches(this->getNodeGraphid(node)));
220  }
221 
222  std::vector<EdgeIndex> getBranches(const NodeIndex node) const
223  {
224  return this->getEdgeIndexes(this->getEdgesFromGraphid(this->getGraph()->getBranches(this->getNodeGraphid(this->getNode(node)))));
225  }
226 
232  std::shared_ptr<N> getSon(const std::shared_ptr<E> edge) const
233  {
234  return this->getNodeFromGraphid(this->getGraph()->getBottom(this->getEdgeGraphid(edge)));
235  }
236 
237  NodeIndex getSon(const EdgeIndex edge) const
238  {
239  return this->getNodeIndex(this->getNodeFromGraphid(this->getGraph()->getBottom(this->getEdgeGraphid(this->getEdge(edge)))));
240  }
241 
247  std::shared_ptr<N> getFatherOfEdge(const std::shared_ptr<E> edge) const
248  {
249  return this->getNodeFromGraphid(this->getGraph()->getTop(this->getEdgeGraphid(edge)));
250  }
251 
253  {
254  return this->getNodeIndex(this->getNodeFromGraphid(this->getGraph()->getTop(this->getEdgeGraphid(this->getEdge(edge)))));
255  }
256 
257 
263  size_t getNumberOfSons(const std::shared_ptr<N> node) const
264  {
265  return this->getGraph()->getNumberOfSons(this->getNodeGraphid(node));
266  }
267 
274  std::vector<std::shared_ptr<N> > getLeavesUnderNode(std::shared_ptr<N> node) const
275  {
276  return this->getNodesFromGraphid(this->getGraph()->getLeavesUnderNode(this->getNodeGraphid(node)));
277  }
278 
279  std::vector<NodeIndex> getLeavesUnderNode(NodeIndex node) const
280  {
281  return this->getNodeIndexes(getLeavesUnderNode(this->getNode(node)));
282  }
283 
288  std::vector<std::shared_ptr<N> > removeSons(const std::shared_ptr<N> node)
289  {
290  return this->getNodesFromGraphid(this->getGraph()->removeSons(this->getNodeGraphid(node)));
291  }
292 
296  void removeSon(const std::shared_ptr<N> node, const std::shared_ptr<N> son)
297  {
298  this->getGraph()->removeSon(this->getNodeGraphid(node), this->getNodeGraphid(son));
299  }
300 
308  void setFather(const std::shared_ptr<N> nodeObject, const std::shared_ptr<N> fatherNodeObject, const std::shared_ptr<E> edgeObject = 0)
309  {
310  if (edgeObject)
311  this->getGraph()->setFather(this->getNodeGraphid(nodeObject), this->getNodeGraphid(fatherNodeObject), this->getEdgeGraphid(edgeObject));
312  else
313  this->getGraph()->setFather(this->getNodeGraphid(nodeObject), this->getNodeGraphid(fatherNodeObject));
314  }
315 
316 
324  void addSon(const std::shared_ptr<N> nodeObject, const std::shared_ptr<N> sonNodeObject, const std::shared_ptr<E> edgeObject = 0)
325  {
326  if (edgeObject)
327  this->getGraph()->addSon(this->getNodeGraphid(nodeObject), this->getNodeGraphid(sonNodeObject), this->getEdgeGraphid(edgeObject));
328  else
329  this->getGraph()->addSon(this->getNodeGraphid(nodeObject), this->getNodeGraphid(sonNodeObject));
330  }
331 
337  /*
338  * @brief builds iterator on the sons of a Node
339  *
340  */
341  std::unique_ptr<typename AssociationTreeGraphObserver<N, E>::NodeIterator> sonsIterator(std::shared_ptr<N> node)
342  {
343  return this->outgoingNeighborNodesIterator(node);
344  }
345 
346  std::unique_ptr<typename AssociationTreeGraphObserver<N, E>::NodeIterator> sonsIterator(std::shared_ptr<N> node) const
347  {
348  return this->outgoingNeighborNodesIterator(node);
349  }
350 
351  /*
352  * @brief builds iterator on the branches to sons of a Node
353  *
354  */
355  std::unique_ptr<typename AssociationTreeGraphObserver<N, E>::EdgeIterator> branchesIterator(std::shared_ptr<N> node)
356  {
357  return this->outgoingEdgesIterator(node);
358  }
359 
360  std::unique_ptr<typename AssociationTreeGraphObserver<N, E>::EdgeIterator> branchesIterator(std::shared_ptr<N> node) const
361  {
362  return this->outgoingEdgesIterator(node);
363  }
364 
374  std::vector<std::shared_ptr<N> > getNodePathBetweenTwoNodes(const std::shared_ptr<N> nodeA, const std::shared_ptr<N> nodeB, bool includeAncestor = true) const
375  {
376  return this->getNodesFromGraphid(this->getGraph()->getNodePathBetweenTwoNodes(this->getNodeGraphid(nodeA), this->getNodeGraphid(nodeB), includeAncestor));
377  }
378 
379  std::vector<std::shared_ptr<E> > getEdgePathBetweenTwoNodes(const std::shared_ptr<N> nodeA, const std::shared_ptr<N> nodeB) const
380  {
381  return this->getEdgesFromGraphid(this->getGraph()->getEdgePathBetweenTwoNodes(this->getNodeGraphid(nodeA), this->getNodeGraphid(nodeB)));
382  }
383 
384  std::vector<NodeIndex> getNodePathBetweenTwoNodes(const NodeIndex nodeA, const NodeIndex nodeB, bool includeAncestor = true) const
385  {
386  return getNodeIndexes(getNodePathBetweenTwoNodes(this->getNode(nodeA), this->getNode(nodeB), includeAncestor));
387  }
388 
389  std::vector<EdgeIndex> getEdgePathBetweenTwoNodes(const NodeIndex nodeA, const NodeIndex nodeB, bool includeAncestor = true) const
390  {
391  return this->getEdgeIndexes(getEdgePathBetweenTwoNodes(this->getNode(nodeA), this->getNode(nodeB), includeAncestor));
392  }
393 
394  std::vector<std::shared_ptr<N> > getSubtreeNodes(const std::shared_ptr<N> localRoot) const
395  {
396  return this->getNodesFromGraphid(this->getGraph()->getSubtreeNodes(this->getNodeGraphid(localRoot)));
397  }
398 
399  std::vector<std::shared_ptr<E> > getSubtreeEdges(const std::shared_ptr<N> localRoot) const
400  {
402  }
403 
404  std::vector<NodeIndex> getSubtreeNodes(const NodeIndex localRoot) const
405  {
406  return getNodeIndexes(getSubtreeNodes(this->getNode(localRoot)));
407  }
408 
409  std::vector<EdgeIndex> getSubtreeEdges(const NodeIndex localRoot) const
410  {
411  return this->getEdgeIndexes(getSubtreeEdges(this->getNode(localRoot)));
412  }
413 
419  std::shared_ptr<N> MRCA(const std::vector<std::shared_ptr<N> > vNodeObject) const
420  {
421  std::vector<Graph::NodeId> vNid(vNodeObject.size());
422 
423  std::transform(vNodeObject.begin(), vNodeObject.end(), vNid.begin(), [this](const std::shared_ptr<N>& nodeObject){return this->getNodeGraphid(nodeObject);});
424 
425  return this->getNodeFromGraphid(this->getGraph()->MRCA(vNid));
426  }
427 };
428 
429 /********************/
430 
431 template<class N, class E>
433 }
434 #endif // BPP_GRAPH_ASSOCIATIONTREEGRAPHIMPLOBSERVER_H
const std::shared_ptr< TreeGraphImpl > getGraph() const
std::vector< EdgeIndex > getEdgeIndexes(std::vector< Eref > edges) const
std::unique_ptr< typename AssociationGraphObserver< N, E >::NodeIterator > outgoingNeighborNodesIterator(Nref node)
std::vector< Eref > getEdgesFromGraphid(std::vector< EdgeGraphid > edges) const
std::vector< NodeIndex > getNodeIndexes(std::vector< Nref > nodes) const
std::vector< Nref > getNodesFromGraphid(std::vector< NodeGraphid > nodes) const
std::unique_ptr< typename AssociationGraphObserver< N, E >::EdgeIterator > outgoingEdgesIterator(Nref node)
AssociationGraphImplObserver< N, E, GraphImpl > & operator=(bpp::AssociationGraphImplObserver< N, E, GraphImpl > const &graphObserver)
std::vector< EdgeIndex > getSubtreeEdges(const NodeIndex localRoot) 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::shared_ptr< E > getEdgeToFather(const NodeIndex index) const
std::vector< std::shared_ptr< N > > getSubtreeNodes(const std::shared_ptr< N > localRoot) const
void setFather(const std::shared_ptr< N > nodeObject, const std::shared_ptr< N > fatherNodeObject, const std::shared_ptr< E > edgeObject=0)
std::vector< NodeIndex > getSons(const NodeIndex node) const
NodeIndex getSon(const EdgeIndex edge) const
std::vector< std::shared_ptr< N > > getSons(const std::shared_ptr< N > node) const
bool hasFather(const std::shared_ptr< N > nodeObject) const
std::shared_ptr< E > getEdgeToFather(const std::shared_ptr< N > nodeObject) const
void rootAt(const std::shared_ptr< N > root)
Sets the root and make the tree directed from root to leaves.
std::unique_ptr< typename AssociationTreeGraphObserver< N, E >::NodeIterator > sonsIterator(std::shared_ptr< N > node)
AssociationTreeGraphImplObserver(std::shared_ptr< TreeGraphImpl > subjectTreeGraph)
std::vector< std::shared_ptr< N > > getLeavesUnderNode(std::shared_ptr< N > node) const
size_t getNumberOfSons(const std::shared_ptr< N > node) const
AssociationTreeGraphImplObserver(bpp::AssociationTreeGraphImplObserver< N, E, TreeGraphImpl > const &treeGraphObserver)
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.
std::vector< NodeIndex > getLeavesUnderNode(NodeIndex node) const
std::shared_ptr< N > getFatherOfNode(const std::shared_ptr< N > nodeObject) const
AssociationTreeGraphImplObserver(bpp::AssociationTreeGraphImplObserver< N2, E2, TreeGraphImpl > const &treeGraphObserver)
std::vector< std::shared_ptr< N > > removeSons(const std::shared_ptr< N > node)
AssociationGraphObserver< N, E >::NodeIndex NodeIndex
std::vector< NodeIndex > getNodePathBetweenTwoNodes(const NodeIndex nodeA, const NodeIndex nodeB, bool includeAncestor=true) const
AssociationTreeGraphImplObserver< N, E, TreeGraphImpl > * clone() const
std::shared_ptr< N > getFatherOfEdge(const std::shared_ptr< E > edge) const
std::unique_ptr< typename AssociationTreeGraphObserver< N, E >::EdgeIterator > branchesIterator(std::shared_ptr< N > node) const
std::shared_ptr< N > getSon(const std::shared_ptr< E > edge) const
std::vector< NodeIndex > getSubtreeNodes(const NodeIndex localRoot) const
std::vector< std::shared_ptr< E > > getBranches(const std::shared_ptr< N > node) const
void removeSon(const std::shared_ptr< N > node, const std::shared_ptr< N > son)
AssociationTreeGraphImplObserver< N, E, TreeGraphImpl > & operator=(bpp::AssociationTreeGraphImplObserver< N, E, TreeGraphImpl > const &treeGraphObserver)
std::vector< std::shared_ptr< E > > getSubtreeEdges(const std::shared_ptr< N > localRoot) const
std::shared_ptr< N > MRCA(const std::vector< std::shared_ptr< N > > vNodeObject) const
std::vector< std::shared_ptr< E > > getEdgePathBetweenTwoNodes(const std::shared_ptr< N > nodeA, const std::shared_ptr< N > nodeB) const
std::unique_ptr< typename AssociationTreeGraphObserver< N, E >::EdgeIterator > branchesIterator(std::shared_ptr< N > node)
AssociationGraphObserver< N, E >::EdgeIndex EdgeIndex
std::vector< EdgeIndex > getBranches(const NodeIndex node) const
std::unique_ptr< typename AssociationTreeGraphObserver< N, E >::NodeIterator > sonsIterator(std::shared_ptr< N > node) const
std::vector< EdgeIndex > getEdgePathBetweenTwoNodes(const NodeIndex nodeA, const NodeIndex nodeB, bool includeAncestor=true) const
Defines a Tree Graph Associator. It is a template which follows (subscribed to) a Graph.
unsigned int NodeId
Definition: Graph.h:66
unsigned int EdgeId
Definition: Graph.h:67