A library for working with phylogenetic and population genetic data.
v0.32.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-2023 Lucas Czech
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 <lczech@carnegiescience.edu>
23  Department of Plant Biology, Carnegie Institution For Science
24  260 Panama Street, Stanford, CA 94305, USA
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(
188  TreeSet const& tree_set,
189  std::shared_ptr<utils::BaseOutputTarget> target,
190  bool with_names = false
191  ) const;
192 
196  std::string to_string( Tree const& tree ) const;
197 
198  // -------------------------------------------------------------------------
199  // Plugin Functions
200  // -------------------------------------------------------------------------
201 
205  std::vector<prepare_writing_function> prepare_writing_plugins;
206 
210  std::vector<finish_writing_function> finish_writing_plugins;
211 
216  std::vector<node_to_element_function> node_to_element_plugins;
217 
222  std::vector<edge_to_element_function> edge_to_element_plugins;
223 
224  // -------------------------------------------------------------------------
225  // Settings
226  // -------------------------------------------------------------------------
227 
261  {
262  quotation_mark_ = value;
263  return *this;
264  }
265 
271  char quotation_mark() const
272  {
273  return quotation_mark_;
274  }
275 
285  {
286  force_quot_marks_ = value;
287  return *this;
288  }
289 
296  {
297  return force_quot_marks_;
298  }
299 
311  NewickWriter& line_length( size_t value )
312  {
313  line_length_ = value;
314  return *this;
315  }
316 
324  size_t line_length() const
325  {
326  return line_length_;
327  }
328 
338  {
339  trailing_new_line_ = value;
340  return *this;
341  }
342 
349  bool trailing_new_line() const
350  {
351  return trailing_new_line_;
352  }
353 
360  NewickWriter& write_names( bool value )
361  {
362  write_names_ = value;
363  return *this;
364  }
365 
371  bool write_names() const
372  {
373  return write_names_;
374  }
375 
382  NewickWriter& write_values( bool value )
383  {
384  write_values_ = value;
385  return *this;
386  }
387 
393  bool write_values() const
394  {
395  return write_values_;
396  }
397 
405  {
406  write_comments_ = value;
407  return *this;
408  }
409 
415  bool write_comments() const
416  {
417  return write_comments_;
418  }
419 
427  NewickWriter& write_tags( bool value )
428  {
429  write_tags_ = value;
430  return *this;
431  }
432 
438  bool write_tags() const
439  {
440  return write_tags_;
441  }
442 
443  // -------------------------------------------------------------------------
444  // Intermediate Functions
445  // -------------------------------------------------------------------------
446 
450  NewickBroker tree_to_broker( Tree const& tree ) const;
451 
455  void write( NewickBroker const& broker, std::shared_ptr<utils::BaseOutputTarget> target ) const;
456 
457  // -------------------------------------------------------------------------
458  // Internal Functions
459  // -------------------------------------------------------------------------
460 
461 private:
462 
468  size_t write_( NewickBrokerElement const& bn, std::ostream& os ) const;
469 
473  // std::string to_string_rec_( NewickBroker const& broker, size_t pos ) const;
474 
475  // -------------------------------------------------------------------------
476  // Member Data
477  // -------------------------------------------------------------------------
478 
479  bool force_quot_marks_ = false;
480  char quotation_mark_ = '\"';
481  size_t line_length_ = 0;
482  bool trailing_new_line_ = true;
483 
484  bool write_names_ = true;
485  bool write_values_ = true;
486  bool write_comments_ = true;
487  bool write_tags_ = true;
488 
489 };
490 
491 } // namespace tree
492 } // namespace genesis
493 
494 #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:260
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:105
genesis::tree::NewickWriter::write_names
NewickWriter & write_names(bool value)
Set whether to write Newick node names.
Definition: tree/formats/newick/writer.hpp:360
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:337
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:284
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:210
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:311
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:271
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:427
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:205
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:415
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:60
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:222
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:94
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:438
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:393
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:382
genesis::tree::NewickWriter::line_length
size_t line_length() const
Get the currently set approximate maximal line length.
Definition: tree/formats/newick/writer.hpp:324
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:295
genesis::tree::NewickWriter::write_names
bool write_names() const
Get whether Newick node names are written.
Definition: tree/formats/newick/writer.hpp:371
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:404
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:349
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:216