A library for working with phylogenetic and population genetic data.
v0.32.0
tree/tree.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_TREE_H_
2 #define GENESIS_TREE_TREE_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2020 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
44 
47 
48 #include <cassert>
49 #include <memory>
50 #include <vector>
51 
52 namespace genesis {
53 namespace tree {
54 
55 // =================================================================================================
56 // Forward Declarations
57 // =================================================================================================
58 
59 class Tree;
60 bool validate_topology( Tree const& tree );
61 
62 // =================================================================================================
63 // Tree
64 // =================================================================================================
65 
97 class Tree
98 {
99 public:
100 
101  // -------------------------------------------------------------------------
102  // Typedefs
103  // -------------------------------------------------------------------------
104 
109  template< class T >
110  using ContainerType = std::vector< std::unique_ptr< T >>;
111 
116 
121 
126 
129 
132 
135 
136  // -------------------------------------------------------------------------
137  // Construction and Rule of Five
138  // -------------------------------------------------------------------------
139 
140  Tree() = default;
141  ~Tree() = default;
142 
157  Tree( Tree const& other );
158  Tree( Tree&& ) = default;
159 
165  Tree& operator= ( Tree const& other );
166  Tree& operator= ( Tree&& ) = default;
167 
173  Tree clone_topology() const;
174 
178  void swap( Tree& other );
179 
185  void clear();
186 
187  // -------------------------------------------------------------------------
188  // Accessors
189  // -------------------------------------------------------------------------
190 
194  bool empty() const
195  {
196  return links_.empty() && nodes_.empty() && edges_.empty();
197  }
198 
202  TreeLink& link_at(size_t index)
203  {
204  assert( index < links_.size() );
205  return *links_[ index ].get();
206  }
207 
211  TreeLink const& link_at(size_t index) const
212  {
213  assert( index < links_.size() );
214  return *links_[ index ].get();
215  }
216 
220  TreeNode& node_at(size_t index)
221  {
222  assert( index < nodes_.size() );
223  return *nodes_[ index ].get();
224  }
225 
229  TreeNode const& node_at(size_t index) const
230  {
231  assert( index < nodes_.size() );
232  return *nodes_[ index ].get();
233  }
234 
238  TreeEdge& edge_at(size_t index)
239  {
240  assert( index < edges_.size() );
241  return *edges_[ index ].get();
242  }
243 
247  TreeEdge const& edge_at(size_t index) const
248  {
249  assert( index < edges_.size() );
250  return *edges_[ index ].get();
251  }
252 
256  size_t link_count() const
257  {
258  return links_.size();
259  }
260 
264  size_t node_count() const
265  {
266  return nodes_.size();
267  }
268 
272  size_t edge_count() const
273  {
274  return edges_.size();
275  }
276 
277  // -------------------------------------------------------------------------
278  // Root
279  // -------------------------------------------------------------------------
280 
285  {
286  return *root_link_;
287  }
288 
292  TreeLink const& root_link() const
293  {
294  return *root_link_;
295  }
296 
301  {
302  return root_link_->node();
303  }
304 
308  TreeNode const& root_node() const
309  {
310  return root_link_->node();
311  }
312 
313  // -------------------------------------------------------------------------
314  // Data Accessors
315  // -------------------------------------------------------------------------
316 
328 
337 
346 
355 
356  // -------------------------------------------------------------------------
357  // Iterators
358  // -------------------------------------------------------------------------
359 
360  // -----------------------------------------------------
361  // Links
362  // -----------------------------------------------------
363 
365  {
366  return links_.begin();
367  }
368 
370  {
371  return links_.cbegin();
372  }
373 
375  {
376  return links_.end();
377  }
378 
380  {
381  return links_.cend();
382  }
383 
385  {
386  return { links_ };
387  }
388 
390  {
391  return { links_ };
392  }
393 
394  // -----------------------------------------------------
395  // Nodes
396  // -----------------------------------------------------
397 
399  {
400  return nodes_.begin();
401  }
402 
404  {
405  return nodes_.cbegin();
406  }
407 
409  {
410  return nodes_.end();
411  }
412 
414  {
415  return nodes_.cend();
416  }
417 
419  {
420  return { nodes_ };
421  }
422 
424  {
425  return { nodes_ };
426  }
427 
428  // -----------------------------------------------------
429  // Edges
430  // -----------------------------------------------------
431 
433  {
434  return edges_.begin();
435  }
436 
438  {
439  return edges_.cbegin();
440  }
441 
443  {
444  return edges_.end();
445  }
446 
448  {
449  return edges_.cend();
450  }
451 
453  {
454  return { edges_ };
455  }
456 
458  {
459  return { edges_ };
460  }
461 
462  // -------------------------------------------------------------------------
463  // Debug and Dump
464  // -------------------------------------------------------------------------
465 
471  friend bool validate_topology( Tree const& tree );
472 
473  // -------------------------------------------------------------------------
474  // Data Members
475  // -------------------------------------------------------------------------
476 
477 private:
478 
479  TreeLink* root_link_ = nullptr;
480 
481  LinkContainerType links_;
482  NodeContainerType nodes_;
483  EdgeContainerType edges_;
484 
485 };
486 
487 } // namespace tree
488 } // namespace genesis
489 
490 #endif // include guard
genesis::tree::Tree::Tree
Tree()=default
genesis::tree::Tree::end_links
IteratorLinks end_links()
Definition: tree/tree.hpp:374
genesis::tree::Tree::node_count
size_t node_count() const
Return the number of TreeNodes of the Tree.
Definition: tree/tree.hpp:264
edge_data.hpp
genesis::tree::Tree::ContainerType
std::vector< std::unique_ptr< T > > ContainerType
Alias for the container type that is used to store TreeLinks, TreeNodes and TreeEdges.
Definition: tree/tree.hpp:110
genesis::tree::Tree::begin_links
IteratorLinks begin_links()
Definition: tree/tree.hpp:364
node.hpp
genesis::tree::Tree::link_count
size_t link_count() const
Return the number of TreeLinks of the Tree.
Definition: tree/tree.hpp:256
genesis::tree::Tree::end_links
ConstIteratorLinks end_links() const
Definition: tree/tree.hpp:379
node_data.hpp
genesis::tree::Tree::expose_link_container
LinkContainerType & expose_link_container()
Get the container that stores all TreeLinks of the Tree.
Definition: tree/tree.cpp:192
genesis::tree::Tree::end_edges
IteratorEdges end_edges()
Definition: tree/tree.hpp:442
genesis::tree::Tree::expose_node_container
NodeContainerType & expose_node_container()
Get the container that stores all TreeNodes of the Tree.
Definition: tree/tree.cpp:197
genesis::tree::Tree::node_at
TreeNode & node_at(size_t index)
Return the TreeNode at a certain index.
Definition: tree/tree.hpp:220
genesis::tree::Tree::edge_at
TreeEdge & edge_at(size_t index)
Return the TreeEdge at a certain index.
Definition: tree/tree.hpp:238
genesis::utils::DereferenceIterator
Iterator class that exposes elements in a container of pointers.
Definition: deref_iterator.hpp:60
genesis::tree::Tree::edges
utils::Range< ConstIteratorEdges > edges() const
Definition: tree/tree.hpp:457
genesis::tree::Tree::root_node
TreeNode & root_node()
Return the TreeNode at the current root of the Tree.
Definition: tree/tree.hpp:300
genesis::tree::Tree::node_at
TreeNode const & node_at(size_t index) const
Return the TreeNode at a certain index.
Definition: tree/tree.hpp:229
genesis::tree::Tree::end_nodes
ConstIteratorNodes end_nodes() const
Definition: tree/tree.hpp:413
genesis::tree::Tree::links
utils::Range< ConstIteratorLinks > links() const
Definition: tree/tree.hpp:389
genesis::tree::Tree::begin_edges
IteratorEdges begin_edges()
Definition: tree/tree.hpp:432
genesis::tree::Tree
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:97
genesis::tree::Tree::reset_root_link
Tree & reset_root_link(TreeLink *root_link)
Reset the link that is considered to be the root of the Tree.
Definition: tree/tree.cpp:184
genesis::tree::Tree::begin_edges
ConstIteratorEdges begin_edges() const
Definition: tree/tree.hpp:437
genesis::tree::Tree::links
utils::Range< IteratorLinks > links()
Definition: tree/tree.hpp:384
genesis::tree::Tree::root_node
TreeNode const & root_node() const
Return the TreeNode at the current root of the Tree.
Definition: tree/tree.hpp:308
range.hpp
genesis::tree::Tree::begin_nodes
IteratorNodes begin_nodes()
Definition: tree/tree.hpp:398
genesis::tree::Tree::validate_topology
friend bool validate_topology(Tree const &tree)
Validate the correctness of all Tree pointers etc.
Definition: tree/function/operators.cpp:332
edge.hpp
genesis::tree::Tree::empty
bool empty() const
Return whether the Tree is empty (i.e., has no nodes, edges and links).
Definition: tree/tree.hpp:194
genesis::tree::Tree::root_link
TreeLink & root_link()
Return the TreeLink at the current root of the Tree.
Definition: tree/tree.hpp:284
genesis::tree::Tree::clear
void clear()
Deletes all data of the tree, including all links, nodes and edges.
Definition: tree/tree.cpp:172
genesis::tree::Tree::LinkContainerType
ContainerType< TreeLink > LinkContainerType
Alias for the container type that is used to store TreeLinks.
Definition: tree/tree.hpp:115
genesis::tree::Tree::swap
void swap(Tree &other)
Swap.
Definition: tree/tree.cpp:161
genesis::tree::Tree::expose_edge_container
EdgeContainerType & expose_edge_container()
Get the container that stores all TreeEdges of the Tree.
Definition: tree/tree.cpp:202
genesis::tree::TreeEdge
Definition: edge.hpp:60
genesis::tree::TreeNode
Definition: tree/tree/node.hpp:58
genesis::tree::Tree::link_at
TreeLink const & link_at(size_t index) const
Return the TreeLink at a certain index.
Definition: tree/tree.hpp:211
genesis::utils::Range
Simple wrapper for typical begin() and end() iterators, to be used in range-based for loops.
Definition: range.hpp:46
genesis::tree::Tree::nodes
utils::Range< ConstIteratorNodes > nodes() const
Definition: tree/tree.hpp:423
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::tree::Tree::nodes
utils::Range< IteratorNodes > nodes()
Definition: tree/tree.hpp:418
genesis::tree::Tree::operator=
Tree & operator=(Tree const &other)
Assignment operator.
Definition: tree/tree.cpp:96
genesis::tree::Tree::edges
utils::Range< IteratorEdges > edges()
Definition: tree/tree.hpp:452
genesis::tree::Tree::clone_topology
Tree clone_topology() const
Return a Tree with the same topology, but without any data.
Definition: tree/tree.cpp:109
genesis::tree::Tree::EdgeContainerType
ContainerType< TreeEdge > EdgeContainerType
Alias for the container type that is used to store TreeEdges.
Definition: tree/tree.hpp:125
genesis::tree::Tree::NodeContainerType
ContainerType< TreeNode > NodeContainerType
Alias for the container type that is used to store TreeNodes.
Definition: tree/tree.hpp:120
genesis::tree::Tree::link_at
TreeLink & link_at(size_t index)
Return the TreeLink at a certain index.
Definition: tree/tree.hpp:202
genesis::tree::Tree::edge_at
TreeEdge const & edge_at(size_t index) const
Return the TreeEdge at a certain index.
Definition: tree/tree.hpp:247
genesis::tree::Tree::begin_nodes
ConstIteratorNodes begin_nodes() const
Definition: tree/tree.hpp:403
genesis::tree::Tree::~Tree
~Tree()=default
genesis::tree::Tree::end_nodes
IteratorNodes end_nodes()
Definition: tree/tree.hpp:408
genesis::tree::Tree::begin_links
ConstIteratorLinks begin_links() const
Definition: tree/tree.hpp:369
genesis::tree::validate_topology
bool validate_topology(Tree const &tree)
Validate that all internal pointers of the Tree elements (TreeLinks, TreeNodes, TreeEdges) to each ot...
Definition: tree/function/operators.cpp:332
deref_iterator.hpp
genesis::tree::Tree::edge_count
size_t edge_count() const
Return the number of TreeEdges of the Tree.
Definition: tree/tree.hpp:272
genesis::tree::Tree::end_edges
ConstIteratorEdges end_edges() const
Definition: tree/tree.hpp:447
genesis::tree::Tree::root_link
TreeLink const & root_link() const
Return the TreeLink at the current root of the Tree.
Definition: tree/tree.hpp:292