A library for working with phylogenetic and population genetic data.
v0.27.0
tree/formats/newick/writer.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_FORMATS_NEWICK_WRITER_H_
2 #define GENESIS_TREE_FORMATS_NEWICK_WRITER_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2020 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
35 
36 #include <iosfwd>
37 #include <functional>
38 #include <string>
39 #include <vector>
40 
41 namespace genesis {
42 namespace tree {
43 
44 // =================================================================================================
45 // Forward Declarations
46 // =================================================================================================
47 
48 class Tree;
49 class TreeNode;
50 class TreeEdge;
51 class TreeSet;
52 
53 class NewickBroker;
54 struct NewickBrokerElement;
55 
56 // =================================================================================================
57 // Newick Writer
58 // =================================================================================================
59 
103 {
104 public:
105 
106  // -------------------------------------------------------------------------
107  // Typedefs and Enums
108  // -------------------------------------------------------------------------
109 
117  using prepare_writing_function = std::function< void(
118  Tree const& tree, NewickBroker& broker
119  ) >;
120 
127  using finish_writing_function = std::function< void(
128  Tree const& tree, NewickBroker& broker
129  ) >;
130 
137  using node_to_element_function = std::function< void(
138  TreeNode const& node, NewickBrokerElement& element
139  ) >;
140 
147  using edge_to_element_function = std::function< void(
148  TreeEdge const& edge, NewickBrokerElement& element
149  ) >;
150 
151  // -------------------------------------------------------------------------
152  // Constructor and Rule of Five
153  // -------------------------------------------------------------------------
154 
155  NewickWriter() = default;
156  virtual ~NewickWriter() = default;
157 
158  NewickWriter(NewickWriter const&) = default;
159  NewickWriter(NewickWriter&&) = default;
160 
161  NewickWriter& operator= (NewickWriter const&) = default;
162  NewickWriter& operator= (NewickWriter&&) = default;
163 
164  // -------------------------------------------------------------------------
165  // Writing
166  // -------------------------------------------------------------------------
167 
174  void write( Tree const& tree, std::shared_ptr<utils::BaseOutputTarget> target ) const;
175 
187  void write( TreeSet const& tree_set, std::shared_ptr<utils::BaseOutputTarget> target, bool with_names = false ) const;
188 
192  std::string to_string( Tree const& tree ) const;
193 
194  // -------------------------------------------------------------------------
195  // Plugin Functions
196  // -------------------------------------------------------------------------
197 
201  std::vector<prepare_writing_function> prepare_writing_plugins;
202 
206  std::vector<finish_writing_function> finish_writing_plugins;
207 
212  std::vector<node_to_element_function> node_to_element_plugins;
213 
218  std::vector<edge_to_element_function> edge_to_element_plugins;
219 
220  // -------------------------------------------------------------------------
221  // Settings
222  // -------------------------------------------------------------------------
223 
257  {
258  quotation_mark_ = value;
259  return *this;
260  }
261 
267  char quotation_mark() const
268  {
269  return quotation_mark_;
270  }
271 
281  {
282  force_quot_marks_ = value;
283  return *this;
284  }
285 
292  {
293  return force_quot_marks_;
294  }
295 
307  NewickWriter& line_length( size_t value )
308  {
309  line_length_ = value;
310  return *this;
311  }
312 
320  size_t line_length() const
321  {
322  return line_length_;
323  }
324 
334  {
335  trailing_new_line_ = value;
336  return *this;
337  }
338 
345  bool trailing_new_line() const
346  {
347  return trailing_new_line_;
348  }
349 
356  NewickWriter& write_names( bool value )
357  {
358  write_names_ = value;
359  return *this;
360  }
361 
367  bool write_names() const
368  {
369  return write_names_;
370  }
371 
378  NewickWriter& write_values( bool value )
379  {
380  write_values_ = value;
381  return *this;
382  }
383 
389  bool write_values() const
390  {
391  return write_values_;
392  }
393 
401  {
402  write_comments_ = value;
403  return *this;
404  }
405 
411  bool write_comments() const
412  {
413  return write_comments_;
414  }
415 
423  NewickWriter& write_tags( bool value )
424  {
425  write_tags_ = value;
426  return *this;
427  }
428 
434  bool write_tags() const
435  {
436  return write_tags_;
437  }
438 
439  // -------------------------------------------------------------------------
440  // Intermediate Functions
441  // -------------------------------------------------------------------------
442 
446  NewickBroker tree_to_broker( Tree const& tree ) const;
447 
451  void write( NewickBroker const& broker, std::shared_ptr<utils::BaseOutputTarget> target ) const;
452 
453  // -------------------------------------------------------------------------
454  // Internal Functions
455  // -------------------------------------------------------------------------
456 
457 private:
458 
464  size_t write_( NewickBrokerElement const& bn, std::ostream& os ) const;
465 
469  // std::string to_string_rec_( NewickBroker const& broker, size_t pos ) const;
470 
471  // -------------------------------------------------------------------------
472  // Member Data
473  // -------------------------------------------------------------------------
474 
475  bool force_quot_marks_ = false;
476  char quotation_mark_ = '\"';
477  size_t line_length_ = 0;
478  bool trailing_new_line_ = true;
479 
480  bool write_names_ = true;
481  bool write_values_ = true;
482  bool write_comments_ = true;
483  bool write_tags_ = true;
484 
485 };
486 
487 } // namespace tree
488 } // namespace genesis
489 
490 #endif // include guard
genesis::tree::NewickWriter::quotation_mark
NewickWriter & quotation_mark(char value)
Set the type of quotation marks used for node names that contain special characters.
Definition: tree/formats/newick/writer.hpp:256
genesis::tree::NewickWriter::tree_to_broker
NewickBroker tree_to_broker(Tree const &tree) const
Transform the information of the tree into a NewickBroker object.
Definition: tree/formats/newick/writer.cpp:101
genesis::tree::NewickWriter::write_names
NewickWriter & write_names(bool value)
Set whether to write Newick node names.
Definition: tree/formats/newick/writer.hpp:356
genesis::tree::NewickWriter::node_to_element_function
std::function< void(TreeNode const &node, NewickBrokerElement &element) > node_to_element_function
Function type that translates from a TreeNode to a NewickBrokerElement.
Definition: tree/formats/newick/writer.hpp:139
genesis::tree::NewickWriter::trailing_new_line
NewickWriter & trailing_new_line(bool value)
Set whether to write a new line char \n at the end of each tree.
Definition: tree/formats/newick/writer.hpp:333
genesis::tree::NewickWriter::force_quotation_marks
NewickWriter & force_quotation_marks(bool value)
If set to true, all names are wrapped in quotation marks, regardless of whether the name contains any...
Definition: tree/formats/newick/writer.hpp:280
genesis::tree::NewickWriter::operator=
NewickWriter & operator=(NewickWriter const &)=default
genesis::tree::NewickWriter::finish_writing_plugins
std::vector< finish_writing_function > finish_writing_plugins
Collect all functions to be called after finishing the actual tree writing.
Definition: tree/formats/newick/writer.hpp:206
genesis::tree::NewickWriter::line_length
NewickWriter & line_length(size_t value)
Set the approximate maximal line length to use when writing Newick trees.
Definition: tree/formats/newick/writer.hpp:307
genesis::tree::NewickWriter
Write a Tree to Newick format.
Definition: tree/formats/newick/writer.hpp:102
genesis::tree::NewickWriter::quotation_mark
char quotation_mark() const
Get the currently set type of quotation marks used for node names.
Definition: tree/formats/newick/writer.hpp:267
genesis::tree::NewickWriter::write_tags
NewickWriter & write_tags(bool value)
Set whether to write Newick tags (e.g., for jplace files).
Definition: tree/formats/newick/writer.hpp:423
genesis::tree::NewickWriter::prepare_writing_plugins
std::vector< prepare_writing_function > prepare_writing_plugins
Collect all functions to be called before starting the actual tree writing.
Definition: tree/formats/newick/writer.hpp:201
genesis::placement::tree_set
tree::TreeSet tree_set(SampleSet const &sample_set)
Return a TreeSet containing all the trees of the SampleSet.
Definition: sample_set.cpp:156
genesis::tree::NewickWriter::write_comments
bool write_comments() const
Get whether Newick comments (e.g., some forms of bootstrap values) are written.
Definition: tree/formats/newick/writer.hpp:411
genesis::tree::NewickWriter::write
void write(Tree const &tree, std::shared_ptr< utils::BaseOutputTarget > target) const
Write a Tree to an output target, using the Newick format.
Definition: tree/formats/newick/writer.cpp:59
genesis::tree::TreeSet
Definition: tree_set.hpp:48
genesis::tree::NewickWriter::NewickWriter
NewickWriter()=default
genesis::tree::Tree
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:97
genesis::tree::NewickWriter::edge_to_element_plugins
std::vector< edge_to_element_function > edge_to_element_plugins
Collect all functions to be called for each TreeEdge in order to translate it to a Newick representat...
Definition: tree/formats/newick/writer.hpp:218
genesis::tree::NewickWriter::to_string
std::string to_string(Tree const &tree) const
Shorthand to write a Tree to Newick format and return it is a string.
Definition: tree/formats/newick/writer.cpp:90
genesis::tree::NewickWriter::write_tags
bool write_tags() const
Get whether Newick tags (e.g., for jplace files) are written.
Definition: tree/formats/newick/writer.hpp:434
genesis::tree::NewickWriter::write_values
bool write_values() const
Get whether Newick values (e.g., branch lengths) are written.
Definition: tree/formats/newick/writer.hpp:389
genesis::tree::TreeEdge
Definition: edge.hpp:60
genesis::tree::NewickWriter::edge_to_element_function
std::function< void(TreeEdge const &edge, NewickBrokerElement &element) > edge_to_element_function
Function type that translates from a TreeEdge to a NewickBrokerElement.
Definition: tree/formats/newick/writer.hpp:149
genesis::tree::TreeNode
Definition: tree/tree/node.hpp:58
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::tree::NewickBroker
Stores a Newick tree in an intermediate format that can be further processed into a Tree.
Definition: broker.hpp:106
genesis::tree::NewickWriter::write_values
NewickWriter & write_values(bool value)
Set whether to write Newick values (e.g., branch lengths).
Definition: tree/formats/newick/writer.hpp:378
genesis::tree::NewickWriter::line_length
size_t line_length() const
Get the currently set approximate maximal line length.
Definition: tree/formats/newick/writer.hpp:320
genesis::tree::NewickWriter::force_quotation_marks
bool force_quotation_marks() const
Get whether all names are wrapped in quotation marks.
Definition: tree/formats/newick/writer.hpp:291
genesis::tree::NewickWriter::write_names
bool write_names() const
Get whether Newick node names are written.
Definition: tree/formats/newick/writer.hpp:367
output_target.hpp
genesis::tree::NewickWriter::~NewickWriter
virtual ~NewickWriter()=default
genesis::tree::NewickBrokerElement
Store the information for one element of a Newick tree.
Definition: element.hpp:60
genesis::tree::NewickWriter::prepare_writing_function
std::function< void(Tree const &tree, NewickBroker &broker) > prepare_writing_function
Function type that allows to do some preparatory work with the Tree and NewickBroker before the actua...
Definition: tree/formats/newick/writer.hpp:119
genesis::tree::NewickWriter::finish_writing_function
std::function< void(Tree const &tree, NewickBroker &broker) > finish_writing_function
Function type that allows to do some finalizing work with the Tree and NewickBroker after the actual ...
Definition: tree/formats/newick/writer.hpp:129
genesis::tree::NewickWriter::write_comments
NewickWriter & write_comments(bool value)
Set whether to write Newick comments (e.g., some forms of bootstrap values).
Definition: tree/formats/newick/writer.hpp:400
genesis::tree::NewickWriter::trailing_new_line
bool trailing_new_line() const
Get the current setting for whether a new line char \n is written to the end of each tree.
Definition: tree/formats/newick/writer.hpp:345
genesis::tree::NewickWriter::node_to_element_plugins
std::vector< node_to_element_function > node_to_element_plugins
Collect all functions to be called for each TreeNode in order to translate it to a Newick representat...
Definition: tree/formats/newick/writer.hpp:212