A toolkit for working with phylogenetic data.
v0.18.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tree/function/operators.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_FUNCTION_OPERATORS_H_
2 #define GENESIS_TREE_FUNCTION_OPERATORS_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2017 Lucas Czech
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 
34 #include "genesis/tree/tree.hpp"
35 
36 #include <functional>
37 #include <iosfwd>
38 #include <memory>
39 #include <typeinfo>
40 
41 namespace genesis {
42 namespace tree {
43 
44 // =================================================================================================
45 // Forward Declarations
46 // =================================================================================================
47 
48 class BaseNodeData;
49 class BaseEdgeData;
50 
51 // =================================================================================================
52 // Data Type Checks
53 // =================================================================================================
54 
62 template< class NodeDataType, class EdgeDataType >
63 bool tree_data_is( Tree const& tree )
64 {
65  // Check node data types.
66  for( auto const& node : tree.nodes() ) {
67  if( ! node->has_data() ) {
68  return false;
69  }
70  auto const& ref = *node->data_ptr();
71  if( typeid( ref ) != typeid( NodeDataType )) {
72  return false;
73  }
74  }
75 
76  // Check edge data types.
77  for( auto const& edge : tree.edges() ) {
78  if( ! edge->has_data() ) {
79  return false;
80  }
81  auto const& ref = *edge->data_ptr();
82  if( typeid( ref ) != typeid( EdgeDataType )) {
83  return false;
84  }
85  }
86 
87  return true;
88 }
89 
97 template< class NodeDataType, class EdgeDataType >
98 bool tree_data_is_derived_from( Tree const& tree )
99 {
100  // Check node data types.
101  for( auto const& node : tree.nodes() ) {
102  if( dynamic_cast< NodeDataType const* >( node->data_ptr() ) == nullptr ) {
103  return false;
104  }
105  }
106 
107  // Check edge data types.
108  for( auto const& edge : tree.edges() ) {
109  if( dynamic_cast< EdgeDataType const* >( edge->data_ptr() ) == nullptr ) {
110  return false;
111  }
112  }
113 
114  return true;
115 }
116 
117 // =================================================================================================
118 // Conversion
119 // =================================================================================================
120 
121 Tree convert(
122  Tree const& source,
123  std::function< std::unique_ptr<BaseNodeData>( BaseNodeData const& node_data )> node_data_converter,
124  std::function< std::unique_ptr<BaseEdgeData>( BaseEdgeData const& edge_data )> edge_data_converter
125 );
126 
127 // =================================================================================================
128 // Equality
129 // =================================================================================================
130 
131 bool equal(
132  Tree const& lhs,
133  Tree const& rhs,
134  std::function<bool ( TreeNode const&, TreeNode const&) > node_comparator,
135  std::function<bool ( TreeEdge const&, TreeEdge const&) > edge_comparator
136 );
137 
138 // bool equal( Tree const& lhs, Tree const& rhs);
139 
140 bool identical_topology( Tree const& lhs, Tree const& rhs);
141 
142 bool belongs_to( Tree const& tree, TreeNode const& node );
143 bool belongs_to( TreeNode const& node, Tree const& tree );
144 bool belongs_to( Tree const& tree, TreeEdge const& edge );
145 bool belongs_to( TreeEdge const& edge, Tree const& tree );
146 bool belongs_to( Tree const& tree, TreeLink const& link );
147 bool belongs_to( TreeLink const& link, Tree const& tree );
148 
149 TreeEdge* edge_between( TreeNode& lhs, TreeNode& rhs );
150 TreeEdge const* edge_between( TreeNode const& lhs, TreeNode const& rhs );
151 
152 // =================================================================================================
153 // Output
154 // =================================================================================================
155 
156 std::ostream& operator << ( std::ostream& out, Tree const& tree );
157 
158 // =================================================================================================
159 // Validate
160 // =================================================================================================
161 
162 bool validate_topology( Tree const& tree );
163 
164 } // namespace tree
165 } // namespace genesis
166 
167 #endif // include guard
Tree convert(Tree const &source, std::function< std::unique_ptr< BaseNodeData >(BaseNodeData const &node_data)> node_data_converter, std::function< std::unique_ptr< BaseEdgeData >(BaseEdgeData const &edge_data)> edge_data_converter)
Create a tree with the same topology as the source tree, while converting its data.
bool tree_data_is(Tree const &tree)
Check whether the data of the nodes and edges of the Tree are exactly of the specified data types...
bool tree_data_is_derived_from(Tree const &tree)
Check whether the data of the nodes and edges of the Tree are derived from the specified data types...
utils::Range< IteratorNodes > nodes()
Definition: tree/tree.cpp:484
TreeEdge * edge_between(TreeNode &lhs, TreeNode &rhs)
Return the TreeEdge between two TreeNode&s, if they are neighbours, or nullptr otherwise.
utils::Range< IteratorEdges > edges()
Definition: tree/tree.cpp:518
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:95
bool belongs_to(Tree const &tree, TreeNode const &node)
Return whether the TreeNode belongs to the Tree, i.e., whether it is owned by the Tree...
bool validate_topology(Tree const &tree)
Validate that all internal pointers of the Tree elements (TreeLinks, TreeNodes, TreeEdges) to each ot...
Header of Tree class.
bool identical_topology(Tree const &lhs, Tree const &rhs)
Returns true iff both trees have an identical topology.
std::ostream & operator<<(std::ostream &out, Tree const &tree)
bool equal(Tree const &lhs, Tree const &rhs, std::function< bool(TreeNode const &, TreeNode const &) > node_comparator, std::function< bool(TreeEdge const &, TreeEdge const &) > edge_comparator)
Compares two trees for equality given binary comparator functionals for their nodes and edges...