A library for working with phylogenetic and population genetic data.
v0.32.0
detailed.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2019 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"
34 
35 // TODO used for conversion - ensure typesafety!
37 
39 
42 
43 #include <cassert>
44 #include <sstream>
45 #include <vector>
46 
47 namespace genesis {
48 namespace tree {
49 
50 // =================================================================================================
51 // Print Detailed
52 // =================================================================================================
53 
55  std::ostream& out,
56  Tree const& tree
57 ) const {
58  std::vector<size_t> depth = node_path_length_vector(tree);
59  std::vector<size_t> done;
60 
61  // Prepare text color styles.
62  auto node_color = utils::Style( "red", true );
63  auto link_color = utils::Style( "blue" );
64  auto next_color = utils::Style( "green" );
65  auto edge_color = utils::Style( "cyan" );
66 
67  if( ! use_color_ ) {
68  node_color.enabled( false );
69  link_color.enabled( false );
70  next_color.enabled( false );
71  edge_color.enabled( false );
72  }
73 
74  // prepare link so that we point to the root link. this will ensure that the order of nodes
75  // displayed by this funtion is the one expected by the user. usually, we would go into
76  // the first branch immediately, but then there would be no way of first nicely displaying
77  // the information about the root node. so we need to do it a bit more complex than the
78  // usual iteration...
79  auto l = &tree.root_link();
80  while( &l->next() != &tree.root_link() ) {
81  l = &l->next();
82  }
83 
84  // do an euler tour traversal over all links. (we cannot use the iterator here, as
85  // we need each link on its own, and not each node as the iterator gives)
86  do {
87  auto& n = l->node();
88  std::string indent = std::string(4 * depth[n.index()], ' ');
89  if( ! utils::contains( done, n.index() )) {
90  out << indent
91  // TODO typesafety!
92  << node_color( "Node " + std::to_string(n.index()) + ": \"" + n.data<CommonNodeData>().name + "\"" )
93  << "\n";
94  }
95  done.push_back(n.index());
96 
97  // dont display the next link when we are at the first iteration.
98  if( &l->next() == &tree.root_link() ) {
99  l = &l->next();
100  } else {
101  out << indent;
102  out << " " << link_color( "Link " + std::to_string( l->index() ));
103  l = &l->next();
104  out << " " << next_color(">") << " "
105  << link_color( "Link " + std::to_string( l->index() )) << "\n";
106  }
107 
108  out << indent;
109  out << " -- " << link_color( "Link " + std::to_string( l->index() ));
110  out << " -- " << edge_color( "Edge " + std::to_string( l->edge().index() ));
111  l = &l->outer();
112  out << " --> " << link_color( "Link " + std::to_string( l->index() )) << "\n";
113  } while( &l->next() != &tree.root_link() );
114 
115  // output the last next link back to the root, because we skipped this in the loop
116  // (the one that was skipped in the beginning).
117  out << " " << link_color( "Link " + std::to_string( l->index() ));
118  l = &l->next();
119  out << " " << next_color(">") << " "
120  << link_color( "Link " + std::to_string( l->index() )) << "\n";
121 }
122 
124  Tree const& tree
125 ) const {
126  std::ostringstream res;
127  print( res, tree );
128  return res.str();
129 }
130 
131 } // namespace tree
132 } // namespace genesis
algorithm.hpp
Provides some valuable algorithms that are not part of the C++ 11 STL.
genesis::utils::indent
std::string indent(std::string const &text, std::string const &indentation)
Indent each line of text with indentation and return the result.
Definition: string.cpp:719
tree.hpp
Header of Tree class.
genesis::population::to_string
std::string to_string(GenomeLocus const &locus)
Definition: function/genome_locus.hpp:52
genesis::tree::PrinterDetailed::print
void print(std::ostream &out, Tree const &tree) const
Definition: detailed.cpp:54
genesis::tree::Tree
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:97
genesis::tree::Tree::root_link
TreeLink & root_link()
Return the TreeLink at the current root of the Tree.
Definition: tree/tree.hpp:284
genesis::tree::CommonNodeData::name
std::string name
Name of the node.
Definition: tree/common_tree/tree.hpp:127
genesis::utils::Style
Simple text style class for colorized and bold output to a terminal.
Definition: style.hpp:81
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
tree.hpp
detailed.hpp
genesis::tree::CommonNodeData
Common class containing the commonly needed data for tree nodes.
Definition: tree/common_tree/tree.hpp:79
genesis::utils::contains
bool contains(const C &v, const T &x)
Returns whether a container object has a certain element.
Definition: algorithm.hpp:74
distances.hpp
Header of Tree distance methods.
genesis::tree::node_path_length_vector
std::vector< size_t > node_path_length_vector(Tree const &tree, TreeNode const &node)
Return a vector containing the depth of all nodes with respect to the given start node.
Definition: tree/function/distances.cpp:98
style.hpp