A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
function/tree_set.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2018 Lucas Czech and HITS gGmbH
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
32 
33 #include "genesis/tree/tree.hpp"
35 
39 
40 #include <stdexcept>
41 #include <vector>
42 
43 namespace genesis {
44 namespace tree {
45 
46 // =================================================================================================
47 // Tree Set Functions
48 // =================================================================================================
49 
50 Tree* find_tree ( TreeSet& tset, std::string const& name)
51 {
52  for (auto& tree : tset) {
53  if( tree.name == name ) {
54  return &tree.tree;
55  }
56  }
57  return nullptr;
58 }
59 
60 Tree const* find_tree ( TreeSet const& tset, std::string const& name)
61 {
62  for (auto& tree : tset) {
63  if( tree.name == name ) {
64  return &tree.tree;
65  }
66  }
67  return nullptr;
68 }
69 
70 // =================================================================================================
71 // Comparators
72 // =================================================================================================
73 
74 bool all_equal(
75  TreeSet const& tset,
76  std::function<bool( TreeNode const&, TreeNode const& )> node_comparator,
77  std::function<bool( TreeEdge const&, TreeEdge const& )> edge_comparator
78 ) {
79  // If all pairs of two adjacent trees are equal, all of them are.
80  // Thus, we do not need a complete pairwise comparision.
81  // TODO the namespace thing is weird, but currently neccesary because of an ambiguous call...
82  for (size_t i = 1; i < tset.size(); i++) {
83  if( ! tree::equal( tset[i-1].tree, tset[i].tree, node_comparator, edge_comparator )) {
84  return false;
85  }
86  }
87  return true;
88 }
89 
90 // bool all_equal( TreeSet const& tset )
91 // {
92 // // If all pairs of two adjacent trees are equal, all of them are.
93 // // Thus, we do not need a complete pairwise comparision.
94 // for (size_t i = 1; i < tset.size(); i++) {
95 // if( ! equal( tset[i-1].tree, tset[i].tree )) {
96 // return false;
97 // }
98 // }
99 // return true;
100 // }
101 
102 bool all_identical_topology( TreeSet const& tset )
103 {
104  // If all pairs of two adjacent trees have same the topology, all of them have.
105  // Thus, we do not need a complete pairwise comparision.
106  for (size_t i = 1; i < tset.size(); i++) {
107  if( ! identical_topology( tset[i-1].tree, tset[i].tree )) {
108  return false;
109  }
110  }
111  return true;
112 }
113 
114 } // namespace tree
115 } // namespace genesis
Tree operator functions.
size_t size() const
Return the size of the TreeSet, i.e., the number of stored Trees.
Definition: tree_set.cpp:139
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:95
Header of Tree class.
Tree * find_tree(TreeSet &tset, std::string const &name)
Get the first Tree in a TreeSet that is stored with a given name, or nullptr if not found...
bool identical_topology(Tree const &lhs, Tree const &rhs)
Returns true iff both trees have an identical topology.
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...
bool all_identical_topology(TreeSet const &tset)
Compare whether all Trees in a TreeSet are equal using their default comparision operators for nodes ...
bool all_equal(TreeSet const &tset, std::function< bool(TreeNode const &, TreeNode const &)> node_comparator, std::function< bool(TreeEdge const &, TreeEdge const &)> edge_comparator)
Compare whether all Trees in a TreeSet are equal using a given comparator functional.