|
ʻOhana
Population structure, admixture history, and selection using learning methods.
|
7 #ifndef JADE_NEWICK_HPP__
8 #define JADE_NEWICK_HPP__
10 #include "jade.scanner.hpp"
17 template <
typename TValue>
81 const std::string & in)
102 template <
typename TPredicate>
104 const TPredicate predicate)
107 std::set<const_pointer_type> container;
109 this->
for_each([predicate, &container](
114 container.insert(node);
125 template <
typename TPredicate>
127 const TPredicate predicate)
129 std::set<pointer_type> container;
131 this->
for_each([predicate, &container](
136 container.insert(node);
169 template <
typename TPredicate>
171 const TPredicate predicate)
174 for (
auto child : _children)
176 const auto first = child->
find_first(predicate);
177 if (
nullptr != first)
181 return predicate(
this) ? this :
nullptr;
189 template <
typename TPredicate>
191 const TPredicate predicate)
193 for (
auto child : _children)
196 if (
nullptr != first)
200 return predicate(
this) ? this :
nullptr;
212 return node->_id == id;
224 return node->_id == id;
256 char const *
const name)
261 return node->_name == name;
269 char const *
const name)
273 return node->_name == name;
297 template <
typename TAction>
299 const TAction action)
302 for (
auto child : _children)
303 child->for_each(action);
312 template <
typename TAction>
314 const TAction action)
316 for (
auto child : _children)
317 child->for_each(action);
328 char const *
const path)
330 std::ifstream in (path);
340 const std::string & path)
408 return !_name.empty();
416 return _children.empty();
424 return _parent ==
nullptr;
438 auto root = _clone();
439 _copy_children(
this, root,
nullptr);
440 _copy_parents(
this, root);
447 root->_length = old_root->_length;
448 root->_has_length = old_root->_has_length;
459 assert(!std::isnan(length));
460 assert(!std::isinf(length));
469 char const *
const name)
471 assert(name !=
nullptr);
481 inline std::string
str()
const
483 std::ostringstream out;
505 char const *
const path)
508 std::ofstream out (path);
518 const std::string & path)
532 node->_has_length = _has_length;
534 node->_length = _length;
540 static void _copy_children(
548 for (
auto source_child : source->_children)
553 if (source_child == excluded)
559 auto target_child = source_child->_clone();
560 target->_children.push_back(target_child);
561 target_child->_parent = target;
566 _copy_children(source_child, target_child,
nullptr);
571 static void _copy_parents(
578 const auto source_parent = source->_parent;
579 if (source_parent ==
nullptr)
585 auto target_child = source_parent->_clone();
586 target->_children.push_back(target_child);
587 target_child->_parent = target;
592 _copy_children(source_parent, target_child, source);
593 _copy_parents(source_parent, target_child);
598 target_child->_length = source->_length;
599 target_child->_has_length = source->_has_length;
603 inline void _destruct()
605 for (
auto child : _children)
624 _read(in, node_count);
634 in.skip_whitespace();
635 if (in.is_end_of_data())
639 <<
"expected end of stream but encountered "
640 <<
" '" << in.read_token() <<
"'";
655 static const char delims[] =
";:(),";
665 if (in.try_char(
'('))
677 child->_parent =
this;
678 _children.push_back(child);
679 child->_read(in, node_count);
681 while (in.try_char(
','));
692 _name = _trim(in.read_token(delims));
697 if (in.try_char(
':'))
705 static std::string _trim(
const std::string & s)
708 size_t end = s.length();
709 while (begin < end && ::isspace(s[begin]))
711 while (end > begin && ::isspace(s[end - 1]))
713 return s.substr(begin, end - begin);
717 void _write(std::ostream & out)
const
719 const auto end = _children.end();
724 auto iter = _children.begin();
731 (*iter)->_write(out);
736 while (++iter != end)
739 (*iter)->_write(out);
758 out <<
':' << _length;
777 template <
typename TValue>
778 inline std::ostream & operator << (
786 #endif // JADE_NEWICK_HPP__
const std::string & get_name() const
basic_newick_node()
Initializes a new instance of the class. Initially, the instance has no name, no length,...
std::set< pointer_type > find_leafs()
std::set< pointer_type > find_descendents()
~basic_newick_node()
Reclaims resources used by this instance and its children.
const_pointer_type find_first(const TPredicate predicate) const
pointer_type reroot() const
Creates a new tree based on the tree associated with this node; in the tree returned,...
static basic_newick_node * from_file(const std::string &path)
void erase_length()
Erases the length.
const basic_newick_node * const_pointer_type
The constant Newick node pointer type.
pointer_type get_parent()
void for_each(const TAction action) const
Executes the specified action for all descendent nodes, including this instance, in depth-first order...
const_pointer_type find_name(char const *const name) const
const_pointer_type find_root() const
void write(std::ostream &out) const
Writes this instance into the specified output stream. The output stream receives a semicolon after t...
void write(const std::string &path) const
Writes this instance into the specified file. A semicolon is written as the last character of the fil...
A template class representing a node from a Newick tree.
void set_length(const value_type length)
Assigns the length.
std::set< const_pointer_type > find_all(const TPredicate predicate) const
basic_scanner< char > scanner_type
The scanner type.
void set_name(char const *const name)
Assigns the name.
std::set< const_pointer_type > find_descendents() const
basic_newick_node * pointer_type
The Newick node pointer type.
const_pointer_type get_parent() const
std::set< const_pointer_type > find_leafs() const
A template class that parses text and throws meaningful error messages.
const_pointer_type find_id(const int id) const
std::set< pointer_type > find_all(const TPredicate predicate)
TValue value_type
The value type.
void write(char const *const path) const
Writes this instance into the specified file. A semicolon is written as the last character of the fil...
static pointer_type from_file(char const *const path)
std::string str() const
Encodes this instance and returns the output as a string. A semicolon is written as the last characte...
pointer_type find_name(char const *const name)
A template for a class representing an exception thrown from this namespace.
const children_type & get_children() const
pointer_type find_id(const int id)
pointer_type find_first(const TPredicate predicate)
std::vector< pointer_type > children_type
The children vector type.
void for_each(const TAction action)
Executes the specified action for all descendent nodes, including this instance, in depth-first order...
basic_newick_node(const std::string &in)
Initializes a new instance of the class from the specified input string. The input string must end wi...
basic_newick_node(std::istream &in)
Initializes a new instance of the class from the specified input stream. The input stream must end wi...
value_type get_length() const