A library for working with phylogenetic and population genetic data.
v0.32.0
nested.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2018 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 
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() != "" ) || ( print_ids_ && t.id() != "" )) {
90  out << " (";
91  }
92  if( print_ranks_ && t.rank() != "" ) {
93  out << t.rank();
94  }
95  if( print_ids_ && t.id() != "" ) {
96  if( print_ranks_ && t.rank() != "" ) {
97  out << ", ";
98  }
99  out << t.id();
100  }
101  if(( print_ranks_ && t.rank() != "" ) || ( print_ids_ && t.id() != "" )) {
102  out << ")";
103  }
104  out << "\n";
105  ++lines;
106 
107  // Recurse.
108  finished &= print_to_ostream( out, t, depth + 1, lines );
109  }
110 
111  if( depth == 0 && ! finished ) {
112  out << "...\n";
113  }
114  return finished;
115 }
116 
117 // =================================================================================================
118 // Properties
119 // =================================================================================================
120 
122 {
123  line_limit_ = value;
124  return *this;
125 }
126 
128 {
129  return line_limit_;
130 }
131 
133 {
134  depth_limit_ = value;
135  return *this;
136 }
137 
139 {
140  return depth_limit_;
141 }
142 
144 {
145  print_ranks_ = value;
146  return *this;
147 }
148 
150 {
151  return print_ranks_;
152 }
153 
155 {
156  print_ids_ = value;
157  return *this;
158 }
159 
161 {
162  return print_ids_;
163 }
164 
165 } // namespace taxonomy
166 } // namespace genesis
genesis::taxonomy::PrinterNested::print_ranks
bool print_ranks() const
Definition: nested.cpp:149
genesis::taxonomy::PrinterNested::print
void print(std::ostream &out, Taxonomy const &tax) const
Definition: nested.cpp:45
taxonomy.hpp
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
genesis::taxonomy::Taxonomy
Store a Taxonomy, i.e., a nested hierarchy of Taxa.
Definition: taxonomy/taxonomy.hpp:96
taxon.hpp
genesis::taxonomy::PrinterNested::depth_limit
int depth_limit() const
Definition: nested.cpp:138
genesis::taxonomy::PrinterNested::print_ids
bool print_ids() const
Definition: nested.cpp:160
genesis::taxonomy::PrinterNested
Simple printer class for Taxonomy.
Definition: nested.hpp:53
genesis::taxonomy::PrinterNested::line_limit
int line_limit() const
Definition: nested.cpp:127
genesis::taxonomy::PrinterNested::operator()
std::string operator()(Taxonomy const &tax) const
Definition: nested.cpp:61
nested.hpp