7 #ifndef JADE_REROOTED_TREE_HPP__
8 #define JADE_REROOTED_TREE_HPP__
10 #include "jade.tree_path.hpp"
18 template <
typename TValue>
52 : _rk (_validate_tree(node))
53 , _node_ptr (node.find_name(
"0")->reroot())
56 const auto & tree = *_node_ptr;
62 for (
size_t i = 1; i <= _rk; i++)
64 std::ostringstream name_stream;
67 const auto name = name_stream.str();
68 const auto node_ptr = tree.find_name(name.c_str());
69 _vector.emplace_back(*node_ptr);
89 return _vector[node1] & _vector[node2];
100 assert(index < _vector.size());
101 return _vector[index];
131 static size_t _validate_tree(
const node_type & tree)
136 const auto leaf_nodes = tree.find_leafs();
137 const auto k = leaf_nodes.size();
144 <<
"invalid Newick tree: there must be at "
150 std::set<std::string> names;
151 for (
const auto leaf : leaf_nodes)
153 if (!leaf->has_name())
154 throw error() <<
"invalid Newick tree: at least one "
155 <<
"leaf node has no name";
157 const auto & name = leaf->get_name();
158 if (names.find(name) != names.end())
159 throw error() <<
"invalid Newick tree; duplicate leaf "
160 <<
"node name '" << name <<
"'";
162 names.insert(leaf->get_name());
169 for (
size_t index = 0; index < k; index++)
171 std::ostringstream name_stream;
172 name_stream << index;
174 const auto name = name_stream.str();
175 if (names.find(name) == names.end())
176 throw error() <<
"invalid Newick tree: missing leaf "
177 <<
"named '" << name <<
"'";
192 #endif // JADE_REROOTED_TREE_HPP__