A toolkit for working with phylogenetic data.
v0.22.1
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-2018 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
IteratorLinks begin_links()
Definition: tree/tree.hpp:364
ContainerType< TreeEdge > EdgeContainerType
Alias for the container type that is used to store TreeEdges.
Definition: tree/tree.hpp:125
utils::Range< ConstIteratorLinks > links() const
Definition: tree/tree.hpp:389
IteratorLinks end_links()
Definition: tree/tree.hpp:374
utils::Range< ConstIteratorNodes > nodes() const
Definition: tree/tree.hpp:423
Iterator class that exposes elements in a container of pointers.
utils::Range< IteratorLinks > links()
Definition: tree/tree.hpp:384
TreeEdge const & edge_at(size_t index) const
Return the TreeEdge at a certain index.
Definition: tree/tree.hpp:247
friend bool validate_topology(Tree const &tree)
Validate the correctness of all Tree pointers etc.
bool empty() const
Return whether the Tree is empty (i.e., has no nodes, edges and links).
Definition: tree/tree.hpp:194
TreeLink const & link_at(size_t index) const
Return the TreeLink at a certain index.
Definition: tree/tree.hpp:211
size_t edge_count() const
Return the number of TreeEdges of the Tree.
Definition: tree/tree.hpp:272
ConstIteratorLinks end_links() const
Definition: tree/tree.hpp:379
ConstIteratorNodes end_nodes() const
Definition: tree/tree.hpp:413
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
ContainerType< TreeNode > NodeContainerType
Alias for the container type that is used to store TreeNodes.
Definition: tree/tree.hpp:120
utils::Range< ConstIteratorEdges > edges() const
Definition: tree/tree.hpp:457
Tree clone_topology() const
Return a Tree with the same topology, but without any data.
Definition: tree/tree.cpp:109
utils::Range< IteratorNodes > nodes()
Definition: tree/tree.hpp:418
Tree & operator=(Tree const &other)
Assignment operator.
Definition: tree/tree.cpp:96
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:97
size_t link_count() const
Return the number of TreeLinks of the Tree.
Definition: tree/tree.hpp:256
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
ConstIteratorEdges begin_edges() const
Definition: tree/tree.hpp:437
utils::Range< IteratorEdges > edges()
Definition: tree/tree.hpp:452
TreeNode & node_at(size_t index)
Return the TreeNode at a certain index.
Definition: tree/tree.hpp:220
ConstIteratorNodes begin_nodes() const
Definition: tree/tree.hpp:403
void swap(Tree &other)
Swap.
Definition: tree/tree.cpp:161
TreeLink & root_link()
Return the TreeLink at the current root of the Tree.
Definition: tree/tree.hpp:284
LinkContainerType & expose_link_container()
Get the container that stores all TreeLinks of the Tree.
Definition: tree/tree.cpp:192
TreeEdge & edge_at(size_t index)
Return the TreeEdge at a certain index.
Definition: tree/tree.hpp:238
ConstIteratorEdges end_edges() const
Definition: tree/tree.hpp:447
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
TreeLink const & root_link() const
Return the TreeLink at the current root of the Tree.
Definition: tree/tree.hpp:292
size_t node_count() const
Return the number of TreeNodes of the Tree.
Definition: tree/tree.hpp:264
bool validate_topology(Tree const &tree)
Validate that all internal pointers of the Tree elements (TreeLinks, TreeNodes, TreeEdges) to each ot...
TreeLink & link_at(size_t index)
Return the TreeLink at a certain index.
Definition: tree/tree.hpp:202
void clear()
Deletes all data of the tree, including all links, nodes and edges.
Definition: tree/tree.cpp:172
ContainerType< TreeLink > LinkContainerType
Alias for the container type that is used to store TreeLinks.
Definition: tree/tree.hpp:115
IteratorEdges begin_edges()
Definition: tree/tree.hpp:432
TreeNode & root_node()
Return the TreeNode at the current root of the Tree.
Definition: tree/tree.hpp:300
IteratorEdges end_edges()
Definition: tree/tree.hpp:442
IteratorNodes end_nodes()
Definition: tree/tree.hpp:408
ConstIteratorLinks begin_links() const
Definition: tree/tree.hpp:369
Simple wrapper for typical begin() and end() iterators, to be used in range-based for loops...
Definition: range.hpp:46
TreeNode const & root_node() const
Return the TreeNode at the current root of the Tree.
Definition: tree/tree.hpp:308
TreeNode const & node_at(size_t index) const
Return the TreeNode at a certain index.
Definition: tree/tree.hpp:229
EdgeContainerType & expose_edge_container()
Get the container that stores all TreeEdges of the Tree.
Definition: tree/tree.cpp:202
IteratorNodes begin_nodes()
Definition: tree/tree.hpp:398
NodeContainerType & expose_node_container()
Get the container that stores all TreeNodes of the Tree.
Definition: tree/tree.cpp:197