A library for working with phylogenetic and population genetic data.
v0.27.0
tree/common_tree/newick_writer.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_COMMON_TREE_NEWICK_WRITER_H_
2 #define GENESIS_TREE_COMMON_TREE_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 // Common 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 
65  CommonTreeNewickWriterPlugin() = default;
66  virtual ~CommonTreeNewickWriterPlugin() = 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<CommonNodeData>().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<CommonEdgeData>();
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 // Common 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
genesis::tree::CommonTreeNewickWriterPlugin::~CommonTreeNewickWriterPlugin
virtual ~CommonTreeNewickWriterPlugin()=default
genesis::tree::CommonTreeNewickWriterPlugin::default_leaf_name
self_type & default_leaf_name(std::string const &value)
Set the named used to filter out a leaf node name.
Definition: tree/common_tree/newick_writer.hpp:89
genesis::tree::CommonTreeNewickWriterPlugin::enable_branch_lengths
self_type & enable_branch_lengths(bool value)
Set whether to write branch lengths.
Definition: tree/common_tree/newick_writer.hpp:226
genesis::tree::NewickBrokerElement::name
std::string name
Name of the node.
Definition: element.hpp:114
genesis::utils::replace_all
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:530
genesis::tree::NewickWriter
Write a Tree to Newick format.
Definition: tree/formats/newick/writer.hpp:102
genesis::tree::CommonTreeNewickWriterPlugin::enable_branch_lengths
bool enable_branch_lengths() const
Get whether currently any branch lengths are written.
Definition: tree/common_tree/newick_writer.hpp:235
genesis::tree::is_inner
bool is_inner(TreeLink const &link)
Return true iff the node of the given link is an inner node.
Definition: tree/function/functions.cpp:74
genesis::utils::to_string_rounded
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:786
element.hpp
genesis::tree::CommonTreeNewickWriterPlugin::default_root_name
std::string const & default_root_name() const
Get the named used to filter out the root node name.
Definition: tree/common_tree/newick_writer.hpp:115
functions.hpp
genesis::tree::CommonTreeNewickWriterPlugin::branch_length_precision
int branch_length_precision() const
Get the currently set maximum precision (in number of digits) used for printing the branch_length flo...
Definition: tree/common_tree/newick_writer.hpp:244
genesis::tree::CommonTreeNewickWriter
Write default Newick trees, i.e., trees with names and branch lengths.
Definition: tree/common_tree/newick_writer.hpp:352
genesis::tree::CommonTreeNewickWriterPlugin::node_to_element
void node_to_element(TreeNode const &node, NewickBrokerElement &element) const
Definition: tree/common_tree/newick_writer.hpp:265
genesis::tree::CommonTreeNewickWriterPlugin::replace_name_spaces
bool replace_name_spaces() const
Return whether currently this plugin replaces spaces with underscores.
Definition: tree/common_tree/newick_writer.hpp:184
genesis::tree::CommonTreeNewickWriterPlugin::set_default_names
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.
Definition: tree/common_tree/newick_writer.hpp:133
std.hpp
Provides some valuable additions to STD.
genesis::tree::NewickBrokerElement::values
std::vector< std::string > values
Numerical values associated with the node, i.e. branch lengths.
Definition: element.hpp:122
genesis::tree::CommonTreeNewickWriter::CommonTreeNewickWriter
CommonTreeNewickWriter()
Definition: tree/common_tree/newick_writer.hpp:362
string.hpp
Provides some commonly used string utility functions.
genesis::tree::is_root
bool is_root(TreeLink const &link)
Return whether the link belongs to the root node of its Tree.
Definition: tree/function/functions.cpp:89
genesis::tree::CommonTreeNewickWriterPlugin::default_leaf_name
std::string const & default_leaf_name() const
Get the named used to filter out a leaf node name.
Definition: tree/common_tree/newick_writer.hpp:81
genesis::tree::CommonTreeNewickWriterPlugin::use_default_names
self_type & use_default_names(bool value)
Set whether to replace default named nodes with an empty string.
Definition: tree/common_tree/newick_writer.hpp:173
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:218
genesis::tree::CommonTreeNewickWriterPlugin::branch_length_precision
self_type & branch_length_precision(int value)
Set the maximum precision (in number of digits) used for printing the branch_length floating point nu...
Definition: tree/common_tree/newick_writer.hpp:255
genesis::tree::TreeEdge
Definition: edge.hpp:60
genesis::tree::TreeNode
Definition: tree/tree/node.hpp:58
genesis::tree::CommonTreeNewickWriterPlugin::edge_to_element
void edge_to_element(TreeEdge const &edge, NewickBrokerElement &element) const
Definition: tree/common_tree/newick_writer.hpp:288
writer.hpp
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::CommonTreeNewickWriterPlugin::default_inner_name
self_type & default_inner_name(std::string const &value)
Set the named used to filter out an inner node name.
Definition: tree/common_tree/newick_writer.hpp:106
genesis::tree::CommonTreeNewickWriterPlugin::replace_name_spaces
self_type & replace_name_spaces(bool value)
Set whether to replace all spaces (' ') in names with underscores ('_').
Definition: tree/common_tree/newick_writer.hpp:198
genesis::tree::CommonTreeNewickWriterPlugin::use_default_names
bool use_default_names() const
Return whether currently default names are activated in this plugin.
Definition: tree/common_tree/newick_writer.hpp:146
tree.hpp
genesis::tree::CommonTreeNewickWriterPlugin::enable_names
self_type & enable_names(bool value)
Set whether to write node names at all.
Definition: tree/common_tree/newick_writer.hpp:209
genesis::tree::CommonTreeNewickWriterPlugin
Provide a set of plugin functions for NewickWriter to write a CommonTree.
Definition: tree/common_tree/newick_writer.hpp:51
genesis::tree::CommonEdgeData
Common class containing the commonly needed data for tree edges.
Definition: tree/common_tree/tree.hpp:144
genesis::tree::CommonTreeNewickWriterPlugin::register_with
void register_with(NewickWriter &writer) const
Definition: tree/common_tree/newick_writer.hpp:297
genesis::tree::CommonNodeData
Common class containing the commonly needed data for tree nodes.
Definition: tree/common_tree/tree.hpp:79
genesis::tree::CommonTreeNewickWriterPlugin::default_inner_name
std::string const & default_inner_name() const
Get the named used to filter out an inner node name.
Definition: tree/common_tree/newick_writer.hpp:98
genesis::tree::CommonTreeNewickWriterPlugin::enable_names
bool enable_names() const
Get whether currently any node names are written at all.
Definition: tree/common_tree/newick_writer.hpp:218
genesis::tree::TreeNode::data
NodeDataType & data()
Definition: tree/tree/node.hpp:203
genesis::tree::TreeEdge::data
EdgeDataType & data()
Definition: edge.hpp:217
genesis::tree::is_leaf
bool is_leaf(TreeLink const &link)
Return true iff the node of the given link is a leaf node.
Definition: tree/function/functions.cpp:59
genesis::tree::NewickBrokerElement
Store the information for one element of a Newick tree.
Definition: element.hpp:60
genesis::tree::CommonTreeNewickWriterPlugin::default_root_name
self_type & default_root_name(std::string const &value)
Set the named used to filter out the root node name.
Definition: tree/common_tree/newick_writer.hpp:123
genesis::tree::CommonTreeNewickWriterPlugin::operator=
CommonTreeNewickWriterPlugin & operator=(CommonTreeNewickWriterPlugin const &)=default
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:212
genesis::tree::CommonTreeNewickWriterPlugin::CommonTreeNewickWriterPlugin
CommonTreeNewickWriterPlugin()=default