A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
simple.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 
37 
38 #include <algorithm>
39 #include <cassert>
40 #include <sstream>
41 
42 namespace genesis {
43 namespace sequence {
44 
45 // =================================================================================================
46 // Print
47 // =================================================================================================
48 
50  std::ostream& out,
51  Sequence const& seq
52 ) const {
53  print_sequence_( out, seq );
54 }
55 
57  std::ostream& out,
58  SequenceSet const& set
59 ) const {
60  // How many lines to print.
61  size_t sequence_limit = ( sequence_limit_ > 0
62  ? std::min( sequence_limit_, set.size() )
63  : set.size()
64  );
65 
66  // Get longest label length.
67  size_t label_len = 0;
68  if( label_mode_ == LabelMode::kSameLine ) {
69  for( size_t i = 0; i < sequence_limit; ++i ) {
70  label_len = std::max( label_len, set[i].label().size() );
71  }
72  }
73 
74  // Print sequences.
75  for( size_t i = 0; i < sequence_limit; ++i ) {
76  print_sequence_( out, set[i], label_len );
77  }
78 
79  // Append ellipsis if needed.
80  if( set.size() > sequence_limit ) {
81  out << "...\n";
82  }
83 }
84 
86  Sequence const& seq
87 ) const {
88  std::ostringstream res;
89  print( res, seq );
90  return res.str();
91 }
92 
94  SequenceSet const& set
95 ) const {
96  std::ostringstream res;
97  print( res, set );
98  return res.str();
99 }
100 
102  Sequence const& seq
103 ) const {
104  return print(seq);
105 }
106 
108  SequenceSet const& set
109 ) const {
110  return print(set);
111 }
112 
113 // =================================================================================================
114 // Properties
115 // =================================================================================================
116 
118 {
119  sequence_limit_ = value;
120  return *this;
121 }
122 
124 {
125  return sequence_limit_;
126 }
127 
129 {
130  line_length_ = value;
131  return *this;
132 }
133 
135 {
136  return line_length_;
137 }
138 
140 {
141  length_limit_ = value;
142  return *this;
143 }
144 
146 {
147  return length_limit_;
148 }
149 
150 PrinterSimple& PrinterSimple::color_map( std::map<char, std::string> const& value )
151 {
152  color_map_ = value;
153  return *this;
154 }
155 
156 std::map<char, std::string> const& PrinterSimple::color_map() const
157 {
158  return color_map_;
159 }
160 
162 {
163  color_mode_ = value;
164  return *this;
165 }
166 
168 {
169  return color_mode_;
170 }
171 
173 {
174  label_mode_ = value;
175  return *this;
176 }
177 
179 {
180  return label_mode_;
181 }
182 
183 // =================================================================================================
184 // Internal Functions
185 // =================================================================================================
186 
187 void PrinterSimple::print_character_(
188  std::ostream& out,
189  char site
190 ) const {
191  // Print plain if we use that mode or if there is no color in the map for this char.
192  if( color_mode_ == ColorMode::kNone || color_map_.count( site ) == 0 ) {
193  out << site;
194  } else {
195  auto color = color_map_.at( site );
196 
197  // Choose style.
198  if( color_mode_ == ColorMode::kForeground ) {
199  out << utils::Style( color)( std::string( 1, site ) );
200 
201  } else if( color_mode_ == ColorMode::kBackground ) {
202  out << utils::Style( "black", color )( std::string( 1, site ) );
203 
204  } else {
205 
206  // Invalid color mode. There are only the ones used above.
207  // In debug mode, this terminates; in release, it simply uses no color.
208  out << site;
209  assert( false );
210  }
211  }
212 }
213 
214 void PrinterSimple::print_sites_(
215  std::ostream& out,
216  Sequence const& seq
217 ) const {
218  // Get the max number of sites to be printed.
219  auto length_limit = ( length_limit_ > 0
220  ? std::min( length_limit_, seq.length() )
221  : seq.length()
222  );
223 
224  // Print all chars of the sequence.
225  for( size_t l = 0; l < length_limit; ++l ) {
226  // Print new line if the length is > 0 and we are at a multiple of the length.
227  if( l > 0 && line_length_ > 0 && l % line_length_ == 0 ) {
228  out << "\n";
229  }
230 
231  print_character_( out, seq[l] );
232  }
233 
234  // Append ellipsis if needed.
235  out << ( seq.length() > length_limit ? " ...\n" : "\n" );
236 }
237 
238 void PrinterSimple::print_sequence_(
239  std::ostream& out,
240  Sequence const& seq,
241  size_t label_len
242 ) const {
243  // Print label if needed.
244  if( label_mode_ == LabelMode::kSeparateLine ) {
245  out << seq.label() << "\n";
246 
247  } else if( label_mode_ == LabelMode::kSameLine ) {
248  out << seq.label() << ": ";
249  if( label_len > 0 && seq.label().size() > label_len ) {
250  out << std::string( label_len - seq.label().size(), ' ' );
251  }
252  }
253 
254  print_sites_( out, seq );
255 }
256 
257 } // namespace sequence
258 } // namespace genesis
std::string operator()(Sequence const &seq) const
Return a string representing the print of a single Sequence.
Definition: simple.cpp:101
void print(std::ostream &out, Sequence const &seq) const
Print a single Sequence to a stream.
Definition: simple.cpp:49
size_t line_length() const
Get the currently set line length, i.e., when to wrap.
Definition: simple.cpp:134
Simple text style class for colorized and bold output to a terminal.
Definition: style.hpp:81
std::map< char, std::string > const & color_map() const
Get the currently set list of colors for each Sequence character.
Definition: simple.cpp:156
LabelMode
Modes for how to print Sequence labels.
Definition: simple.hpp:98
ColorMode color_mode() const
Get the currently set color mode.
Definition: simple.cpp:167
The label is printed on a line preceeding the Sequence sites.
The label is printed on the line where the Sequence sites start, separated from them by ": "...
size_t length_limit() const
Get the currently set length limit.
Definition: simple.cpp:145
No color, even if a color_map() is provided.
Provides some commonly used string utility functions.
Store a set of Sequences.
Color the text foreground of the characters, leave the background at default.
ColorMode
Modes for how the Sequence sites are colored.
Definition: simple.hpp:78
Simple printer class for Sequences and SequenceSets.
Definition: simple.hpp:61
LabelMode label_mode() const
Get the currently set LabelMode.
Definition: simple.cpp:178
Color the text background of the characters, set the foreground to black.
size_t sequence_limit() const
Get the currently set limit for how many Sequences to print.
Definition: simple.cpp:123