A toolkit for working with phylogenetic data.
v0.18.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-2017 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 <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 <functional>
35 #include <string>
36 #include <vector>
37 
38 namespace genesis {
39 namespace tree {
40 
41 // =================================================================================================
42 // Forward declarations
43 // =================================================================================================
44 
45 class Tree;
46 class TreeNode;
47 class TreeEdge;
48 class TreeSet;
49 
50 class NewickBroker;
51 struct NewickBrokerElement;
52 
53 // =================================================================================================
54 // Newick Writer
55 // =================================================================================================
56 
100 {
101 public:
102 
103  // -------------------------------------------------------------------------
104  // Typedefs and Enums
105  // -------------------------------------------------------------------------
106 
114  using prepare_writing_function = std::function< void(
115  Tree const& tree, NewickBroker& broker
116  ) >;
117 
124  using finish_writing_function = std::function< void(
125  Tree const& tree, NewickBroker& broker
126  ) >;
127 
134  using node_to_element_function = std::function< void(
135  TreeNode const& node, NewickBrokerElement& element
136  ) >;
137 
144  using edge_to_element_function = std::function< void(
145  TreeEdge const& edge, NewickBrokerElement& element
146  ) >;
147 
148  // -------------------------------------------------------------------------
149  // Constructor and Rule of Five
150  // -------------------------------------------------------------------------
151 
152  NewickWriter() = default;
153  virtual ~NewickWriter() = default;
154 
155  NewickWriter(NewickWriter const&) = default;
156  NewickWriter(NewickWriter&&) = default;
157 
158  NewickWriter& operator= (NewickWriter const&) = default;
159  NewickWriter& operator= (NewickWriter&&) = default;
160 
161  // -------------------------------------------------------------------------
162  // Writing
163  // -------------------------------------------------------------------------
164 
173  void to_file( Tree const& tree, std::string const& filename) const;
174 
178  void to_string( Tree const& tree, std::string& ts ) const;
179 
183  std::string to_string( Tree const& tree ) const;
184 
185  // -------------------------------------------------------------------------
186  // Plugin Functions
187  // -------------------------------------------------------------------------
188 
192  std::vector<prepare_writing_function> prepare_writing_plugins;
193 
197  std::vector<finish_writing_function> finish_writing_plugins;
198 
203  std::vector<node_to_element_function> node_to_element_plugins;
204 
209  std::vector<edge_to_element_function> edge_to_element_plugins;
210 
211  // -------------------------------------------------------------------------
212  // Settings
213  // -------------------------------------------------------------------------
214 
248  {
249  quotation_marks_ = value;
250  return *this;
251  }
252 
258  char quotation_marks() const
259  {
260  return quotation_marks_;
261  }
262 
269  NewickWriter& write_names( bool value )
270  {
271  write_names_ = value;
272  return *this;
273  }
274 
280  bool write_names() const
281  {
282  return write_names_;
283  }
284 
291  NewickWriter& write_values( bool value )
292  {
293  write_values_ = value;
294  return *this;
295  }
296 
302  bool write_values() const
303  {
304  return write_values_;
305  }
306 
314  {
315  write_comments_ = value;
316  return *this;
317  }
318 
324  bool write_comments() const
325  {
326  return write_comments_;
327  }
328 
336  NewickWriter& write_tags( bool value )
337  {
338  write_tags_ = value;
339  return *this;
340  }
341 
347  bool write_tags() const
348  {
349  return write_tags_;
350  }
351 
352  // -------------------------------------------------------------------------
353  // Internal Member Functions
354  // -------------------------------------------------------------------------
355 
356 private:
357 
361  void tree_to_broker_ (const Tree& tree, NewickBroker& broker) const;
362 
366  std::string element_to_string_( NewickBrokerElement const& bn ) const;
367 
371  std::string to_string_rec_( NewickBroker const& broker, size_t pos ) const;
372 
373  // -------------------------------------------------------------------------
374  // Member Data
375  // -------------------------------------------------------------------------
376 
377  char quotation_marks_ = '\"';
378 
379  bool write_names_ = true;
380  bool write_values_ = true;
381  bool write_comments_ = true;
382  bool write_tags_ = true;
383 
384 };
385 
386 } // namespace tree
387 } // namespace genesis
388 
389 #endif // include guard
std::vector< finish_writing_function > finish_writing_plugins
Collect all functions to be called after finishing the actual tree writing.
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).
std::function< void(TreeEdge const &edge, NewickBrokerElement &element) > edge_to_element_function
Function type that translates from a TreeEdge to a NewickBrokerElement.
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.
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
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...
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.