A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
nested.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2017 Lucas Czech
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 
35 
36 #include <sstream>
37 
38 namespace genesis {
39 namespace taxonomy {
40 
41 // =================================================================================================
42 // Print
43 // =================================================================================================
44 
46  std::ostream& out,
47  Taxonomy const& tax
48 ) const {
49  size_t lines = 0;
50  print_to_ostream( out, tax, 0, lines );
51 }
52 
54  Taxonomy const& tax
55 ) const {
56  std::ostringstream res;
57  print( res, tax );
58  return res.str();
59 }
60 
62  Taxonomy const& tax
63 ) const {
64  return print( tax );
65 }
66 
67 bool PrinterNested::print_to_ostream(
68  std::ostream& out,
69  Taxonomy const& tax,
70  size_t depth,
71  size_t& lines
72 ) const {
73  // Check depth limit. The cast is always legel, because we first check.
74  if( depth_limit_ > -1 && depth > static_cast< size_t >( depth_limit_ )) {
75  return true;
76  }
77 
78  // Indent string.
79  auto in = std::string( depth, '\t' );
80 
81  bool finished = true;
82  for( auto const& t : tax ) {
83  if( line_limit_ > -1 && lines >= static_cast< size_t >( line_limit_ ) ) {
84  return false;
85  }
86 
87  // Print and count.
88  out << in << t.name();
89  if( print_ranks_ && t.rank() != "" ) {
90  out << " (" << t.rank() << ")";
91  }
92  out << "\n";
93  ++lines;
94 
95  // Recurse.
96  finished &= print_to_ostream( out, t, depth + 1, lines );
97  }
98 
99  if( depth == 0 && ! finished ) {
100  out << "...\n";
101  }
102  return finished;
103 }
104 
105 // =================================================================================================
106 // Properties
107 // =================================================================================================
108 
110 {
111  line_limit_ = value;
112  return *this;
113 }
114 
116 {
117  return line_limit_;
118 }
119 
121 {
122  depth_limit_ = value;
123  return *this;
124 }
125 
127 {
128  return depth_limit_;
129 }
130 
132 {
133  print_ranks_ = value;
134  return *this;
135 }
136 
138 {
139  return print_ranks_;
140 }
141 
142 } // namespace taxonomy
143 } // namespace genesis
void print(std::ostream &out, Taxonomy const &tax) const
Definition: nested.cpp:45
std::string operator()(Taxonomy const &tax) const
Definition: nested.cpp:61
Store a Taxonomy, i.e., a nested hierarchy of Taxa.
Simple printer class for Taxonomy.
Definition: nested.hpp:53