ʻOhana
Population structure, admixture history, and selection using learning methods.
Public Types | Public Member Functions | Static Public Member Functions
jade::basic_newick_node< TValue > Class Template Reference

A template class representing a node from a Newick tree. More...

#include <jade.newick.hpp>

+ Collaboration diagram for jade::basic_newick_node< TValue >:

Public Types

typedef TValue value_type
 The value type. More...
 
typedef const basic_newick_nodeconst_pointer_type
 The constant Newick node pointer type. More...
 
typedef basic_newick_nodepointer_type
 The Newick node pointer type. More...
 
typedef std::vector< pointer_typechildren_type
 The children vector type. More...
 
typedef basic_scanner< char > scanner_type
 The scanner type. More...
 

Public Member Functions

 ~basic_newick_node ()
 Reclaims resources used by this instance and its children. More...
 
 basic_newick_node ()
 Initializes a new instance of the class. Initially, the instance has no name, no length, no parent, and no children. More...
 
 basic_newick_node (std::istream &in)
 Initializes a new instance of the class from the specified input stream. The input stream must end with a semicolon. If an error occurs, the method throws an exception. More...
 
 basic_newick_node (const std::string &in)
 Initializes a new instance of the class from the specified input string. The input string must end with a semicolon. If an error occurs, the method throws an exception. More...
 
void erase_length ()
 Erases the length. More...
 
template<typename TPredicate >
std::set< const_pointer_typefind_all (const TPredicate predicate) const
 
template<typename TPredicate >
std::set< pointer_typefind_all (const TPredicate predicate)
 
std::set< const_pointer_typefind_descendents () const
 
std::set< pointer_typefind_descendents ()
 
template<typename TPredicate >
const_pointer_type find_first (const TPredicate predicate) const
 
template<typename TPredicate >
pointer_type find_first (const TPredicate predicate)
 
const_pointer_type find_id (const int id) const
 
pointer_type find_id (const int id)
 
std::set< const_pointer_typefind_leafs () const
 
std::set< pointer_typefind_leafs ()
 
const_pointer_type find_name (char const *const name) const
 
pointer_type find_name (char const *const name)
 
const_pointer_type find_root () const
 
pointer_type find_root ()
 
template<typename TAction >
void for_each (const TAction action) const
 Executes the specified action for all descendent nodes, including this instance, in depth-first order. More...
 
template<typename TAction >
void for_each (const TAction action)
 Executes the specified action for all descendent nodes, including this instance, in depth-first order. More...
 
const children_typeget_children () const
 
int get_id () const
 
value_type get_length () const
 
const std::string & get_name () const
 
const_pointer_type get_parent () const
 
pointer_type get_parent ()
 
bool has_length () const
 
bool has_name () const
 
bool is_leaf () const
 
bool is_root () const
 
pointer_type reroot () const
 Creates a new tree based on the tree associated with this node; in the tree returned, a copy of this node is the root. More...
 
void set_length (const value_type length)
 Assigns the length. More...
 
void set_name (char const *const name)
 Assigns the name. More...
 
std::string str () const
 Encodes this instance and returns the output as a string. A semicolon is written as the last character of the file. More...
 
void write (std::ostream &out) const
 Writes this instance into the specified output stream. The output stream receives a semicolon after the tree data. More...
 
void write (char const *const path) const
 Writes this instance into the specified file. A semicolon is written as the last character of the file. More...
 
void write (const std::string &path) const
 Writes this instance into the specified file. A semicolon is written as the last character of the file. More...
 

Static Public Member Functions

static pointer_type from_file (char const *const path)
 
static basic_newick_nodefrom_file (const std::string &path)
 

Detailed Description

template<typename TValue>
class jade::basic_newick_node< TValue >

A template class representing a node from a Newick tree.

Definition at line 18 of file jade.newick.hpp.

Member Typedef Documentation

◆ children_type

template<typename TValue >
typedef std::vector<pointer_type> jade::basic_newick_node< TValue >::children_type

The children vector type.

Definition at line 31 of file jade.newick.hpp.

◆ const_pointer_type

template<typename TValue >
typedef const basic_newick_node* jade::basic_newick_node< TValue >::const_pointer_type

The constant Newick node pointer type.

Definition at line 25 of file jade.newick.hpp.

◆ pointer_type

template<typename TValue >
typedef basic_newick_node* jade::basic_newick_node< TValue >::pointer_type

The Newick node pointer type.

Definition at line 28 of file jade.newick.hpp.

◆ scanner_type

template<typename TValue >
typedef basic_scanner<char> jade::basic_newick_node< TValue >::scanner_type

The scanner type.

Definition at line 34 of file jade.newick.hpp.

◆ value_type

template<typename TValue >
typedef TValue jade::basic_newick_node< TValue >::value_type

The value type.

Definition at line 22 of file jade.newick.hpp.

Constructor & Destructor Documentation

◆ ~basic_newick_node()

template<typename TValue >
jade::basic_newick_node< TValue >::~basic_newick_node ( )
inline

Reclaims resources used by this instance and its children.

Definition at line 39 of file jade.newick.hpp.

40  {
41  _destruct();
42  }

◆ basic_newick_node() [1/3]

template<typename TValue >
jade::basic_newick_node< TValue >::basic_newick_node ( )
inline

Initializes a new instance of the class. Initially, the instance has no name, no length, no parent, and no children.

Definition at line 48 of file jade.newick.hpp.

49  : _children ()
50  , _has_length (false)
51  , _id (0)
52  , _length (0)
53  , _name ()
54  , _parent ()
55  {
56  }
+ Here is the caller graph for this function:

◆ basic_newick_node() [2/3]

template<typename TValue >
jade::basic_newick_node< TValue >::basic_newick_node ( std::istream &  in)
inlineexplicit

Initializes a new instance of the class from the specified input stream. The input stream must end with a semicolon. If an error occurs, the method throws an exception.

Exceptions
jade::errorThrown if there is an error parsing the tree.
Parameters
inThe input stream to read.

Definition at line 65 of file jade.newick.hpp.

68  {
69  scanner_type s (in);
70  _read(s);
71  }

◆ basic_newick_node() [3/3]

template<typename TValue >
jade::basic_newick_node< TValue >::basic_newick_node ( const std::string &  in)
inlineexplicit

Initializes a new instance of the class from the specified input string. The input string must end with a semicolon. If an error occurs, the method throws an exception.

Exceptions
jade::errorThrown if there is an error parsing the tree.
Parameters
inThe input string to read.

Definition at line 80 of file jade.newick.hpp.

83  {
84  scanner_type s (in);
85  _read(s);
86  }

Member Function Documentation

◆ erase_length()

template<typename TValue >
void jade::basic_newick_node< TValue >::erase_length ( )
inline

Erases the length.

Definition at line 91 of file jade.newick.hpp.

92  {
93  _length = 0;
94  _has_length = false;
95  }
+ Here is the caller graph for this function:

◆ find_all() [1/2]

template<typename TValue >
template<typename TPredicate >
std::set<pointer_type> jade::basic_newick_node< TValue >::find_all ( const TPredicate  predicate)
inline
Returns
A colleciton of all descendents that match the specified predicate. The method searches in depth-first order and examines this node as well.
Parameters
predicateThe predicate to match.

Definition at line 126 of file jade.newick.hpp.

128  {
129  std::set<pointer_type> container;
130 
131  this->for_each([predicate, &container](
132  pointer_type node)
133  -> void
134  {
135  if (predicate(node))
136  container.insert(node);
137  });
138 
139  return container;
140  }
+ Here is the call graph for this function:

◆ find_all() [2/2]

template<typename TValue >
template<typename TPredicate >
std::set<const_pointer_type> jade::basic_newick_node< TValue >::find_all ( const TPredicate  predicate) const
inline
Returns
A colleciton of all descendents that match the specified predicate. The method searches in depth-first order and examines this node as well.
Parameters
predicateThe predicate to match.

Definition at line 103 of file jade.newick.hpp.

106  {
107  std::set<const_pointer_type> container;
108 
109  this->for_each([predicate, &container](
110  const_pointer_type node)
111  -> void
112  {
113  if (predicate(node))
114  container.insert(node);
115  });
116 
117  return container;
118  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ find_descendents() [1/2]

template<typename TValue >
std::set<pointer_type> jade::basic_newick_node< TValue >::find_descendents ( )
inline
Returns
The collection of descendents of this node.

Definition at line 156 of file jade.newick.hpp.

157  {
158  return find_all([this](pointer_type node) -> bool
159  {
160  return node != this;
161  });
162  }
+ Here is the call graph for this function:

◆ find_descendents() [2/2]

template<typename TValue >
std::set<const_pointer_type> jade::basic_newick_node< TValue >::find_descendents ( ) const
inline
Returns
The collection of descendents of this node.

Definition at line 145 of file jade.newick.hpp.

146  {
147  return find_all([this](const_pointer_type node) -> bool
148  {
149  return node != this;
150  });
151  }
+ Here is the call graph for this function:

◆ find_first() [1/2]

template<typename TValue >
template<typename TPredicate >
pointer_type jade::basic_newick_node< TValue >::find_first ( const TPredicate  predicate)
inline
Returns
The first descendent that matches the specified predicate. The method searches in depth-first order and examines this node as well.
Parameters
predicateThe predicate to match.

Definition at line 190 of file jade.newick.hpp.

192  {
193  for (auto child : _children)
194  {
195  const pointer_type first = child->find_first(predicate);
196  if (nullptr != first)
197  return first;
198  }
199 
200  return predicate(this) ? this : nullptr;
201  }
+ Here is the call graph for this function:

◆ find_first() [2/2]

template<typename TValue >
template<typename TPredicate >
const_pointer_type jade::basic_newick_node< TValue >::find_first ( const TPredicate  predicate) const
inline
Returns
The first descendent that matches the specified predicate. The method searches in depth-first order and examines this node as well.
Parameters
predicateThe predicate to match.

Definition at line 170 of file jade.newick.hpp.

173  {
174  for (auto child : _children)
175  {
176  const auto first = child->find_first(predicate);
177  if (nullptr != first)
178  return first;
179  }
180 
181  return predicate(this) ? this : nullptr;
182  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ find_id() [1/2]

template<typename TValue >
pointer_type jade::basic_newick_node< TValue >::find_id ( const int  id)
inline
Returns
The node with the specified id.
Parameters
idThe id of the node to find.

Definition at line 219 of file jade.newick.hpp.

221  {
222  return find_first([id](pointer_type node) -> bool
223  {
224  return node->_id == id;
225  });
226  }
+ Here is the call graph for this function:

◆ find_id() [2/2]

template<typename TValue >
const_pointer_type jade::basic_newick_node< TValue >::find_id ( const int  id) const
inline
Returns
The node with the specified id.
Parameters
idThe id of the node to find.

Definition at line 206 of file jade.newick.hpp.

209  {
210  return find_first([id](const_pointer_type node) -> bool
211  {
212  return node->_id == id;
213  });
214  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ find_leafs() [1/2]

template<typename TValue >
std::set<pointer_type> jade::basic_newick_node< TValue >::find_leafs ( )
inline
Returns
The set of leaf nodes. If this node is a leaf node, it is returned in the collection.

Definition at line 244 of file jade.newick.hpp.

245  {
246  return find_all([](pointer_type node) -> bool
247  {
248  return node->is_leaf();
249  });
250  }
+ Here is the call graph for this function:

◆ find_leafs() [2/2]

template<typename TValue >
std::set<const_pointer_type> jade::basic_newick_node< TValue >::find_leafs ( ) const
inline
Returns
The set of leaf nodes. If this node is a leaf node, it is returned in the collection.

Definition at line 232 of file jade.newick.hpp.

233  {
234  return find_all([](const_pointer_type node) -> bool
235  {
236  return node->is_leaf();
237  });
238  }
+ Here is the call graph for this function:

◆ find_name() [1/2]

template<typename TValue >
pointer_type jade::basic_newick_node< TValue >::find_name ( char const *const  name)
inline
Returns
The node with the specified name.
Parameters
nameThe name of the node to find.

Definition at line 268 of file jade.newick.hpp.

270  {
271  return find_first([name](pointer_type node) -> bool
272  {
273  return node->_name == name;
274  });
275  }
+ Here is the call graph for this function:

◆ find_name() [2/2]

template<typename TValue >
const_pointer_type jade::basic_newick_node< TValue >::find_name ( char const *const  name) const
inline
Returns
The node with the specified name.
Parameters
nameThe name of the node to find.

Definition at line 255 of file jade.newick.hpp.

258  {
259  return find_first([name](const_pointer_type node) -> bool
260  {
261  return node->_name == name;
262  });
263  }
+ Here is the call graph for this function:

◆ find_root() [1/2]

template<typename TValue >
pointer_type jade::basic_newick_node< TValue >::find_root ( )
inline
Returns
The root of the tree associated with this node.

Definition at line 288 of file jade.newick.hpp.

289  {
290  return is_root() ? this : _parent->find_root();
291  }
+ Here is the call graph for this function:

◆ find_root() [2/2]

template<typename TValue >
const_pointer_type jade::basic_newick_node< TValue >::find_root ( ) const
inline
Returns
The root of the tree associated with this node.

Definition at line 280 of file jade.newick.hpp.

281  {
282  return is_root() ? this : _parent->find_root();
283  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ for_each() [1/2]

template<typename TValue >
template<typename TAction >
void jade::basic_newick_node< TValue >::for_each ( const TAction  action)
inline

Executes the specified action for all descendent nodes, including this instance, in depth-first order.

Parameters
actionThe action to perform.

Definition at line 313 of file jade.newick.hpp.

315  {
316  for (auto child : _children)
317  child->for_each(action);
318 
319  action(this);
320  }

◆ for_each() [2/2]

template<typename TValue >
template<typename TAction >
void jade::basic_newick_node< TValue >::for_each ( const TAction  action) const
inline

Executes the specified action for all descendent nodes, including this instance, in depth-first order.

Parameters
actionThe action to perform.

Definition at line 298 of file jade.newick.hpp.

301  {
302  for (auto child : _children)
303  child->for_each(action);
304 
305  action(this);
306  }
+ Here is the caller graph for this function:

◆ from_file() [1/2]

template<typename TValue >
static pointer_type jade::basic_newick_node< TValue >::from_file ( char const *const  path)
inlinestatic
Returns
A new instance based on the specified file.
Exceptions
jade::errorThrown if there is an error parsing the tree.
Parameters
pathThe path to the file.

Definition at line 327 of file jade.newick.hpp.

329  {
330  std::ifstream in (path);
331  return new basic_newick_node(in);
332  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ from_file() [2/2]

template<typename TValue >
static basic_newick_node* jade::basic_newick_node< TValue >::from_file ( const std::string &  path)
inlinestatic
Returns
A new instance based on the specified file.
Exceptions
jade::errorThrown if there is an error parsing the tree.
Parameters
pathThe path to the file to decode.

Definition at line 339 of file jade.newick.hpp.

341  {
342  return from_file(path.c_str());
343  }
+ Here is the call graph for this function:

◆ get_children()

template<typename TValue >
const children_type& jade::basic_newick_node< TValue >::get_children ( ) const
inline
Returns
The collection of immediate children of this node.

Definition at line 348 of file jade.newick.hpp.

349  {
350  return _children;
351  }

◆ get_id()

template<typename TValue >
int jade::basic_newick_node< TValue >::get_id ( ) const
inline
Returns
The unique ID for this node within the tree. If the tree is rerooted, this id persists into the new tree.

Definition at line 357 of file jade.newick.hpp.

358  {
359  return _id;
360  }
+ Here is the caller graph for this function:

◆ get_length()

template<typename TValue >
value_type jade::basic_newick_node< TValue >::get_length ( ) const
inline
Returns
The length of this node; the length is valid only if the has_length method returns true.

Definition at line 366 of file jade.newick.hpp.

367  {
368  return _length;
369  }

◆ get_name()

template<typename TValue >
const std::string& jade::basic_newick_node< TValue >::get_name ( ) const
inline
Returns
The name of this node.

Definition at line 374 of file jade.newick.hpp.

375  {
376  return _name;
377  }

◆ get_parent() [1/2]

template<typename TValue >
pointer_type jade::basic_newick_node< TValue >::get_parent ( )
inline
Returns
The parent of this node.

Definition at line 390 of file jade.newick.hpp.

391  {
392  return _parent;
393  }

◆ get_parent() [2/2]

template<typename TValue >
const_pointer_type jade::basic_newick_node< TValue >::get_parent ( ) const
inline
Returns
The parent of this node.

Definition at line 382 of file jade.newick.hpp.

383  {
384  return _parent;
385  }

◆ has_length()

template<typename TValue >
bool jade::basic_newick_node< TValue >::has_length ( ) const
inline
Returns
True if the node has a defined length.

Definition at line 398 of file jade.newick.hpp.

399  {
400  return _has_length;
401  }

◆ has_name()

template<typename TValue >
bool jade::basic_newick_node< TValue >::has_name ( ) const
inline
Returns
True if the node has a name.

Definition at line 406 of file jade.newick.hpp.

407  {
408  return !_name.empty();
409  }

◆ is_leaf()

template<typename TValue >
bool jade::basic_newick_node< TValue >::is_leaf ( ) const
inline
Returns
True if the node has no children; i.e. it is a leaf node.

Definition at line 414 of file jade.newick.hpp.

415  {
416  return _children.empty();
417  }
+ Here is the caller graph for this function:

◆ is_root()

template<typename TValue >
bool jade::basic_newick_node< TValue >::is_root ( ) const
inline
Returns
True if the node has no parent; i.e. it is a root node.

Definition at line 422 of file jade.newick.hpp.

423  {
424  return _parent == nullptr;
425  }
+ Here is the caller graph for this function:

◆ reroot()

template<typename TValue >
pointer_type jade::basic_newick_node< TValue >::reroot ( ) const
inline

Creates a new tree based on the tree associated with this node; in the tree returned, a copy of this node is the root.

Returns
A new Newick node, the root of an equivalent tree.

Definition at line 433 of file jade.newick.hpp.

434  {
435  //
436  // Reroot the structure of the tree.
437  //
438  auto root = _clone();
439  _copy_children(this, root, nullptr);
440  _copy_parents(this, root);
441 
442  //
443  // To preserve information, assign the length
444  // to the old root to this new root.
445  //
446  auto old_root = find_root();
447  root->_length = old_root->_length;
448  root->_has_length = old_root->_has_length;
449 
450  return root;
451  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ set_length()

template<typename TValue >
void jade::basic_newick_node< TValue >::set_length ( const value_type  length)
inline

Assigns the length.

Parameters
lengthThe length to assign.

Definition at line 456 of file jade.newick.hpp.

458  {
459  assert(!std::isnan(length));
460  assert(!std::isinf(length));
461  _length = length;
462  _has_length = true;
463  }
+ Here is the caller graph for this function:

◆ set_name()

template<typename TValue >
void jade::basic_newick_node< TValue >::set_name ( char const *const  name)
inline

Assigns the name.

Parameters
nameThe name to assign.

Definition at line 468 of file jade.newick.hpp.

470  {
471  assert(name != nullptr);
472  _name = name;
473  }

◆ str()

template<typename TValue >
std::string jade::basic_newick_node< TValue >::str ( ) const
inline

Encodes this instance and returns the output as a string. A semicolon is written as the last character of the file.

Returns
The encoded string.

Definition at line 481 of file jade.newick.hpp.

482  {
483  std::ostringstream out;
484  write(out);
485  return out.str();
486  }
+ Here is the call graph for this function:

◆ write() [1/3]

template<typename TValue >
void jade::basic_newick_node< TValue >::write ( char const *const  path) const
inline

Writes this instance into the specified file. A semicolon is written as the last character of the file.

Parameters
pathThe path to the output file.

Definition at line 504 of file jade.newick.hpp.

507  {
508  std::ofstream out (path);
509  write(out);
510  out << std::endl;
511  }
+ Here is the call graph for this function:

◆ write() [2/3]

template<typename TValue >
void jade::basic_newick_node< TValue >::write ( const std::string &  path) const
inline

Writes this instance into the specified file. A semicolon is written as the last character of the file.

Parameters
pathThe path to the output file.

Definition at line 517 of file jade.newick.hpp.

520  {
521  write(path.c_str());
522  }
+ Here is the call graph for this function:

◆ write() [3/3]

template<typename TValue >
void jade::basic_newick_node< TValue >::write ( std::ostream &  out) const
inline

Writes this instance into the specified output stream. The output stream receives a semicolon after the tree data.

Parameters
outThe output stream.

Definition at line 492 of file jade.newick.hpp.

495  {
496  _write(out);
497  out << ';';
498  }
+ Here is the caller graph for this function:

The documentation for this class was generated from the following file:
jade::basic_newick_node::basic_newick_node
basic_newick_node()
Initializes a new instance of the class. Initially, the instance has no name, no length,...
Definition: jade.newick.hpp:48
jade::basic_newick_node::find_first
const_pointer_type find_first(const TPredicate predicate) const
Definition: jade.newick.hpp:170
jade::basic_newick_node::const_pointer_type
const basic_newick_node * const_pointer_type
The constant Newick node pointer type.
Definition: jade.newick.hpp:25
jade::basic_newick_node::for_each
void for_each(const TAction action) const
Executes the specified action for all descendent nodes, including this instance, in depth-first order...
Definition: jade.newick.hpp:298
jade::basic_newick_node::find_root
const_pointer_type find_root() const
Definition: jade.newick.hpp:280
jade::basic_newick_node::write
void write(std::ostream &out) const
Writes this instance into the specified output stream. The output stream receives a semicolon after t...
Definition: jade.newick.hpp:492
jade::basic_newick_node::find_all
std::set< const_pointer_type > find_all(const TPredicate predicate) const
Definition: jade.newick.hpp:103
jade::basic_newick_node::scanner_type
basic_scanner< char > scanner_type
The scanner type.
Definition: jade.newick.hpp:34
jade::basic_newick_node::pointer_type
basic_newick_node * pointer_type
The Newick node pointer type.
Definition: jade.newick.hpp:28
jade::basic_newick_node::from_file
static pointer_type from_file(char const *const path)
Definition: jade.newick.hpp:327
jade::basic_newick_node::is_root
bool is_root() const
Definition: jade.newick.hpp:422