A toolkit for working with phylogenetic data.
v0.18.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tree/default/newick_writer.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_DEFAULT_NEWICK_WRITER_H_
2 #define GENESIS_TREE_DEFAULT_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 
39 
40 namespace genesis {
41 namespace tree {
42 
43 // =================================================================================================
44 // Default Tree Newick Writer Plugin
45 // =================================================================================================
46 
51 {
52 public:
53 
54  // -------------------------------------------------------------------------
55  // Typedefs and Enums
56  // -------------------------------------------------------------------------
57 
59 
60  // -------------------------------------------------------------------------
61  // Constructor and Rule of Five
62  // -------------------------------------------------------------------------
63 
65  virtual ~DefaultTreeNewickWriterPlugin() = default;
66 
69 
72 
73  // -------------------------------------------------------------------------
74  // Properties
75  // -------------------------------------------------------------------------
76 
80  std::string const& default_leaf_name() const
81  {
82  return default_leaf_name_;
83  }
84 
88  self_type& default_leaf_name( std::string const& value )
89  {
90  default_leaf_name_ = value;
91  return *this;
92  }
93 
97  std::string const& default_inner_name() const
98  {
99  return default_inner_name_;
100  }
101 
105  self_type& default_inner_name( std::string const& value )
106  {
107  default_inner_name_ = value;
108  return *this;
109  }
110 
114  std::string const& default_root_name() const
115  {
116  return default_root_name_;
117  }
118 
122  self_type& default_root_name( std::string const& value )
123  {
124  default_root_name_ = value;
125  return *this;
126  }
127 
132  self_type& set_default_names( std::string const& value )
133  {
134  default_leaf_name_ = value;
135  default_inner_name_ = value;
136  default_root_name_ = value;
137  return *this;
138  }
139 
145  bool use_default_names() const
146  {
147  return use_default_names_;
148  }
149 
173  {
174  use_default_names_ = value;
175  return *this;
176  }
177 
183  bool replace_name_spaces() const
184  {
185  return replace_name_spaces_;
186  }
187 
198  {
199  replace_name_spaces_ = value;
200  return *this;
201  }
202 
208  self_type& enable_names( bool value )
209  {
210  enable_names_ = value;
211  return *this;
212  }
213 
217  bool enable_names() const
218  {
219  return enable_names_;
220  }
221 
226  {
227  enable_branch_lengths_ = value;
228  return *this;
229  }
230 
235  {
236  return enable_branch_lengths_;
237  }
238 
244  {
245  return branch_length_precision_;
246  }
247 
255  {
256  branch_length_precision_ = value;
257  return *this;
258  }
259 
260  // -------------------------------------------------------------------------
261  // Plugin Functions
262  // -------------------------------------------------------------------------
263 
264  void node_to_element( TreeNode const& node, NewickBrokerElement& element ) const
265  {
266  if (enable_names_) {
267  std::string name = node.data<DefaultNodeData>().name;
268 
269  // Handle spaces/underscores.
270  if( replace_name_spaces_ ) {
271  name = utils::replace_all(name, " ", "_");
272  }
273 
274  // Filter out default names if needed.
275  if( use_default_names_ && (
276  ( node.is_leaf() && name == default_leaf_name_ ) ||
277  ( node.is_inner() && name == default_inner_name_ ) ||
278  ( node.is_root() && name == default_root_name_ )
279  )) {
280  name = "";
281  }
282 
283  element.name = name;
284  }
285  }
286 
287  void edge_to_element( TreeEdge const& edge, NewickBrokerElement& element ) const
288  {
289  if (enable_branch_lengths_) {
290  auto const& edge_data = edge.data<DefaultEdgeData>();
291  auto bl = utils::to_string_rounded( edge_data.branch_length, branch_length_precision_ );
292  element.values.insert (element.values.begin(), bl );
293  }
294  }
295 
296  void register_with( NewickWriter& writer ) const
297  {
298  // Add node functions.
299  writer.node_to_element_plugins.push_back(
300  [&]( TreeNode const& node, NewickBrokerElement& element ) {
301  node_to_element( node, element );
302  }
303  );
304 
305  // Add edge functions.
306  writer.edge_to_element_plugins.push_back(
307  [&]( TreeEdge const& edge, NewickBrokerElement& element ) {
308  edge_to_element( edge, element );
309  }
310  );
311  }
312 
313  // -------------------------------------------------------------------------
314  // Data Members
315  // -------------------------------------------------------------------------
316 
317 private:
318 
319  int branch_length_precision_ = 6;
320 
321  std::string default_leaf_name_ = "Leaf_Node";
322  std::string default_inner_name_ = "Inner_Node";
323  std::string default_root_name_ = "Root_Node";
324 
325  bool use_default_names_ = false;
326  bool replace_name_spaces_ = true;
327 
328  bool enable_names_ = true;
329  bool enable_branch_lengths_ = true;
330 
331 };
332 
333 // =================================================================================================
334 // Default Tree Newick Writer
335 // =================================================================================================
336 
352  : public NewickWriter
354 {
355 public:
356 
357  // -------------------------------------------------------------------------
358  // Constructor and Rule of Five
359  // -------------------------------------------------------------------------
360 
362  {
363  register_with( *this );
364  }
365 };
366 
367 } // namespace tree
368 } // namespace genesis
369 
370 #endif // include guard
std::vector< std::string > values
Numerical values associated with the node, i.e. branch lengths.
Definition: element.hpp:104
self_type & branch_length_precision(int value)
Set the maximum precision (in number of digits) used for printing the branch_length floating point nu...
std::string to_string_rounded(double const value, int const precision)
Return a string representation of the input value, using the provided precision value (determining it...
Definition: string.cpp:406
self_type & enable_names(bool value)
Set whether to write node names at all.
Provide a set of plugin functions for NewickWriter to write a DefaultTree.
bool is_root() const
True iff the node is the root of its Tree.
Definition: node.cpp:209
Default class containing the commonly needed data for tree nodes.
Write a Tree to Newick format.
self_type & replace_name_spaces(bool value)
Set whether to replace all spaces (' ') in names with underscores ('_').
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...
std::string replace_all(std::string const &text, std::string const &search, std::string const &replace)
Return a copy of a string, where all occurrences of a search string are replaced by a replace string...
Definition: string.cpp:166
Write default Newick trees, i.e., trees with names and branch lengths.
self_type & set_default_names(std::string const &value)
Shorthand to set the default names for leaf, inner and root node at once, to one value.
Provides some valuable additions to STD.
self_type & use_default_names(bool value)
Set whether to replace default named nodes with an empty string.
bool replace_name_spaces() const
Return whether currently this plugin replaces spaces with underscores.
Default class containing the commonly needed data for tree edges.
std::string const & default_root_name() const
Get the named used to filter out the root node name.
self_type & default_inner_name(std::string const &value)
Set the named used to filter out an inner node name.
bool enable_branch_lengths() const
Get whether currently any branch lengths are written.
self_type & default_leaf_name(std::string const &value)
Set the named used to filter out a leaf node name.
int branch_length_precision() const
Get the currently set maximum precision (in number of digits) used for printing the branch_length flo...
std::string const & default_leaf_name() const
Get the named used to filter out a leaf node name.
bool is_leaf() const
True iff the node is a leaf/tip.
Definition: node.cpp:193
Provides some commonly used string utility functions.
std::string const & default_inner_name() const
Get the named used to filter out an inner node name.
void node_to_element(TreeNode const &node, NewickBrokerElement &element) const
self_type & enable_branch_lengths(bool value)
Set whether to write branch lengths.
bool use_default_names() const
Return whether currently default names are activated in this plugin.
bool enable_names() const
Get whether currently any node names are written at all.
self_type & default_root_name(std::string const &value)
Set the named used to filter out the root node name.
DefaultTreeNewickWriterPlugin & operator=(DefaultTreeNewickWriterPlugin const &)=default
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...
NodeDataType & data()
Definition: node.hpp:108
void edge_to_element(TreeEdge const &edge, NewickBrokerElement &element) const
std::string name
Name of the node.
Definition: element.hpp:96
EdgeDataType & data()
Definition: edge.hpp:118
Store the information for one element of a Newick tree.
Definition: element.hpp:60
bool is_inner() const
True iff the node is an inner node.
Definition: node.cpp:201