A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2018 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 
34 #include <iosfwd>
35 #include <functional>
36 #include <string>
37 #include <vector>
38 
39 namespace genesis {
40 namespace tree {
41 
42 // =================================================================================================
43 // Forward declarations
44 // =================================================================================================
45 
46 class Tree;
47 class TreeNode;
48 class TreeEdge;
49 class TreeSet;
50 
51 class NewickBroker;
52 struct NewickBrokerElement;
53 
54 // =================================================================================================
55 // Newick Writer
56 // =================================================================================================
57 
102 {
103 public:
104 
105  // -------------------------------------------------------------------------
106  // Typedefs and Enums
107  // -------------------------------------------------------------------------
108 
116  using prepare_writing_function = std::function< void(
117  Tree const& tree, NewickBroker& broker
118  ) >;
119 
126  using finish_writing_function = std::function< void(
127  Tree const& tree, NewickBroker& broker
128  ) >;
129 
136  using node_to_element_function = std::function< void(
137  TreeNode const& node, NewickBrokerElement& element
138  ) >;
139 
146  using edge_to_element_function = std::function< void(
147  TreeEdge const& edge, NewickBrokerElement& element
148  ) >;
149 
150  // -------------------------------------------------------------------------
151  // Constructor and Rule of Five
152  // -------------------------------------------------------------------------
153 
154  NewickWriter() = default;
155  virtual ~NewickWriter() = default;
156 
157  NewickWriter(NewickWriter const&) = default;
158  NewickWriter(NewickWriter&&) = default;
159 
160  NewickWriter& operator= (NewickWriter const&) = default;
161  NewickWriter& operator= (NewickWriter&&) = default;
162 
163  // -------------------------------------------------------------------------
164  // Writing
165  // -------------------------------------------------------------------------
166 
170  void to_stream( Tree const& tree, std::ostream& os ) const;
171 
180  void to_file( Tree const& tree, std::string const& filename) const;
181 
185  void to_string( Tree const& tree, std::string& ts ) const;
186 
190  std::string to_string( Tree const& tree ) const;
191 
192  // -------------------------------------------------------------------------
193  // Plugin Functions
194  // -------------------------------------------------------------------------
195 
199  std::vector<prepare_writing_function> prepare_writing_plugins;
200 
204  std::vector<finish_writing_function> finish_writing_plugins;
205 
210  std::vector<node_to_element_function> node_to_element_plugins;
211 
216  std::vector<edge_to_element_function> edge_to_element_plugins;
217 
218  // -------------------------------------------------------------------------
219  // Settings
220  // -------------------------------------------------------------------------
221 
255  {
256  quotation_marks_ = value;
257  return *this;
258  }
259 
265  char quotation_marks() const
266  {
267  return quotation_marks_;
268  }
269 
279  {
280  force_quot_marks_ = value;
281  return *this;
282  }
283 
290  {
291  return force_quot_marks_;
292  }
293 
300  NewickWriter& write_names( bool value )
301  {
302  write_names_ = value;
303  return *this;
304  }
305 
311  bool write_names() const
312  {
313  return write_names_;
314  }
315 
322  NewickWriter& write_values( bool value )
323  {
324  write_values_ = value;
325  return *this;
326  }
327 
333  bool write_values() const
334  {
335  return write_values_;
336  }
337 
345  {
346  write_comments_ = value;
347  return *this;
348  }
349 
355  bool write_comments() const
356  {
357  return write_comments_;
358  }
359 
367  NewickWriter& write_tags( bool value )
368  {
369  write_tags_ = value;
370  return *this;
371  }
372 
378  bool write_tags() const
379  {
380  return write_tags_;
381  }
382 
383  // -------------------------------------------------------------------------
384  // Intermediate Functions
385  // -------------------------------------------------------------------------
386 
390  NewickBroker tree_to_broker( Tree const& tree ) const;
391 
395  void broker_to_stream( NewickBroker const& broker, std::ostream& os ) const;
396 
400  void broker_to_file( NewickBroker const& broker, std::string const& filename) const;
401 
405  void broker_to_string( NewickBroker const& broker, std::string& ts ) const;
406 
410  std::string broker_to_string( NewickBroker const& broker ) const;
411 
412  // -------------------------------------------------------------------------
413  // Internal Functions
414  // -------------------------------------------------------------------------
415 
416 private:
417 
421  std::string element_to_string_( NewickBrokerElement const& bn ) const;
422 
426  std::string to_string_rec_( NewickBroker const& broker, size_t pos ) const;
427 
428  // -------------------------------------------------------------------------
429  // Member Data
430  // -------------------------------------------------------------------------
431 
432  bool force_quot_marks_ = false;
433  char quotation_marks_ = '\"';
434 
435  bool write_names_ = true;
436  bool write_values_ = true;
437  bool write_comments_ = true;
438  bool write_tags_ = true;
439 
440 };
441 
442 } // namespace tree
443 } // namespace genesis
444 
445 #endif // include guard
std::vector< finish_writing_function > finish_writing_plugins
Collect all functions to be called after finishing the actual tree writing.
void broker_to_string(NewickBroker const &broker, std::string &ts) const
Gives a Newick string representation of the tree.
bool write_values() const
Get whether Newick values (e.g., branch lengths) are written.
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 ...
Write a Tree to Newick format.
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...
bool write_names() const
Get whether Newick node names are written.
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...
NewickWriter & write_tags(bool value)
Set whether to write Newick tags (e.g., for jplace files).
void broker_to_file(NewickBroker const &broker, std::string const &filename) const
Writes a NewickBroker to a file in Newick format.
std::function< void(TreeEdge const &edge, NewickBrokerElement &element) > edge_to_element_function
Function type that translates from a TreeEdge to a NewickBrokerElement.
NewickBroker tree_to_broker(Tree const &tree) const
Transform the information of the tree into a NewickBroker object.
virtual ~NewickWriter()=default
bool write_tags() const
Get whether Newick tags (e.g., for jplace files) are written.
void to_file(Tree const &tree, std::string const &filename) const
Writes the tree to a file in Newick format.
void broker_to_stream(NewickBroker const &broker, std::ostream &os) const
Write a NewickBroker to a stream, in Newick format.
Stores a Newick tree in an intermediate format that can be further processed into a Tree...
Definition: broker.hpp:106
char quotation_marks() const
Get the currently set type of quotation marks used for node names.
NewickWriter & write_names(bool value)
Set whether to write Newick node names.
std::function< void(TreeNode const &node, NewickBrokerElement &element) > node_to_element_function
Function type that translates from a TreeNode to a NewickBrokerElement.
NewickWriter & write_comments(bool value)
Set whether to write Newick comments (e.g., some forms of bootstrap values).
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:95
NewickWriter & force_quotation_marks(bool value)
If set to true, all names are wrapped in quotation marks, regardless of whether the name contains any...
std::vector< prepare_writing_function > prepare_writing_plugins
Collect all functions to be called before starting the actual tree writing.
NewickWriter & operator=(NewickWriter const &)=default
NewickWriter & quotation_marks(char value)
Set the type of quotation marks used for node names that contain special characters.
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...
bool force_quotation_marks() const
Get whether all names are wrapped in quotation marks.
void to_stream(Tree const &tree, std::ostream &os) const
Write a Tree to a stream, in Newick format.
NewickWriter & write_values(bool value)
Set whether to write Newick values (e.g., branch lengths).
Store the information for one element of a Newick tree.
Definition: element.hpp:60
bool write_comments() const
Get whether Newick comments (e.g., some forms of bootstrap values) are written.
void to_string(Tree const &tree, std::string &ts) const
Gives a Newick string representation of the tree.