A toolkit for working with phylogenetic data.
v0.20.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-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 
40 
41 namespace genesis {
42 namespace tree {
43 
44 // =================================================================================================
45 // Default Tree Newick Writer Plugin
46 // =================================================================================================
47 
52 {
53 public:
54 
55  // -------------------------------------------------------------------------
56  // Typedefs and Enums
57  // -------------------------------------------------------------------------
58 
60 
61  // -------------------------------------------------------------------------
62  // Constructor and Rule of Five
63  // -------------------------------------------------------------------------
64 
66  virtual ~DefaultTreeNewickWriterPlugin() = default;
67 
70 
73 
74  // -------------------------------------------------------------------------
75  // Properties
76  // -------------------------------------------------------------------------
77 
81  std::string const& default_leaf_name() const
82  {
83  return default_leaf_name_;
84  }
85 
89  self_type& default_leaf_name( std::string const& value )
90  {
91  default_leaf_name_ = value;
92  return *this;
93  }
94 
98  std::string const& default_inner_name() const
99  {
100  return default_inner_name_;
101  }
102 
106  self_type& default_inner_name( std::string const& value )
107  {
108  default_inner_name_ = value;
109  return *this;
110  }
111 
115  std::string const& default_root_name() const
116  {
117  return default_root_name_;
118  }
119 
123  self_type& default_root_name( std::string const& value )
124  {
125  default_root_name_ = value;
126  return *this;
127  }
128 
133  self_type& set_default_names( std::string const& value )
134  {
135  default_leaf_name_ = value;
136  default_inner_name_ = value;
137  default_root_name_ = value;
138  return *this;
139  }
140 
146  bool use_default_names() const
147  {
148  return use_default_names_;
149  }
150 
174  {
175  use_default_names_ = value;
176  return *this;
177  }
178 
184  bool replace_name_spaces() const
185  {
186  return replace_name_spaces_;
187  }
188 
199  {
200  replace_name_spaces_ = value;
201  return *this;
202  }
203 
209  self_type& enable_names( bool value )
210  {
211  enable_names_ = value;
212  return *this;
213  }
214 
218  bool enable_names() const
219  {
220  return enable_names_;
221  }
222 
227  {
228  enable_branch_lengths_ = value;
229  return *this;
230  }
231 
236  {
237  return enable_branch_lengths_;
238  }
239 
245  {
246  return branch_length_precision_;
247  }
248 
256  {
257  branch_length_precision_ = value;
258  return *this;
259  }
260 
261  // -------------------------------------------------------------------------
262  // Plugin Functions
263  // -------------------------------------------------------------------------
264 
265  void node_to_element( TreeNode const& node, NewickBrokerElement& element ) const
266  {
267  if (enable_names_) {
268  std::string name = node.data<DefaultNodeData>().name;
269 
270  // Handle spaces/underscores.
271  if( replace_name_spaces_ ) {
272  name = utils::replace_all(name, " ", "_");
273  }
274 
275  // Filter out default names if needed.
276  if( use_default_names_ && (
277  ( is_leaf( node ) && name == default_leaf_name_ ) ||
278  ( is_inner( node ) && name == default_inner_name_ ) ||
279  ( is_root( node ) && name == default_root_name_ )
280  )) {
281  name = "";
282  }
283 
284  element.name = name;
285  }
286  }
287 
288  void edge_to_element( TreeEdge const& edge, NewickBrokerElement& element ) const
289  {
290  if (enable_branch_lengths_) {
291  auto const& edge_data = edge.data<DefaultEdgeData>();
292  auto bl = utils::to_string_rounded( edge_data.branch_length, branch_length_precision_ );
293  element.values.insert (element.values.begin(), bl );
294  }
295  }
296 
297  void register_with( NewickWriter& writer ) const
298  {
299  // Add node functions.
300  writer.node_to_element_plugins.push_back(
301  [&]( TreeNode const& node, NewickBrokerElement& element ) {
302  node_to_element( node, element );
303  }
304  );
305 
306  // Add edge functions.
307  writer.edge_to_element_plugins.push_back(
308  [&]( TreeEdge const& edge, NewickBrokerElement& element ) {
309  edge_to_element( edge, element );
310  }
311  );
312  }
313 
314  // -------------------------------------------------------------------------
315  // Data Members
316  // -------------------------------------------------------------------------
317 
318 private:
319 
320  int branch_length_precision_ = 6;
321 
322  std::string default_leaf_name_ = "Leaf_Node";
323  std::string default_inner_name_ = "Inner_Node";
324  std::string default_root_name_ = "Root_Node";
325 
326  bool use_default_names_ = false;
327  bool replace_name_spaces_ = true;
328 
329  bool enable_names_ = true;
330  bool enable_branch_lengths_ = true;
331 
332 };
333 
334 // =================================================================================================
335 // Default Tree Newick Writer
336 // =================================================================================================
337 
353  : public NewickWriter
355 {
356 public:
357 
358  // -------------------------------------------------------------------------
359  // Constructor and Rule of Five
360  // -------------------------------------------------------------------------
361 
363  {
364  register_with( *this );
365  }
366 };
367 
368 } // namespace tree
369 } // namespace genesis
370 
371 #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:446
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.
Default class containing the commonly needed data for tree nodes.
Write a Tree to Newick format.
bool is_root(TreeNode const &node)
Return whether the node is the root of its Tree.
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:280
bool is_leaf(TreeLink const &link)
Return true iff the node of the given link is a leaf node.
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 is_inner(TreeLink const &link)
Return true iff the node of the given link is an inner node.
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.
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:152
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:183
Store the information for one element of a Newick tree.
Definition: element.hpp:60