A library for working with phylogenetic and population genetic data.
v0.27.0
tree/common_tree/newick_reader.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_COMMON_TREE_NEWICK_READER_H_
2 #define GENESIS_TREE_COMMON_TREE_NEWICK_READER_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 
39 
40 namespace genesis {
41 namespace tree {
42 
43 // =================================================================================================
44 // Common Tree Newick Reader Plugin
45 // =================================================================================================
46 
51 {
52 public:
53 
54  // -------------------------------------------------------------------------
55  // Typedefs and Enums
56  // -------------------------------------------------------------------------
57 
59 
60  // -------------------------------------------------------------------------
61  // Constructor and Rule of Five
62  // -------------------------------------------------------------------------
63 
64  CommonTreeNewickReaderPlugin() = default;
65  virtual ~CommonTreeNewickReaderPlugin() = default;
66 
69 
72 
73  // -------------------------------------------------------------------------
74  // Settings
75  // -------------------------------------------------------------------------
76 
80  double default_branch_length() const
81  {
82  return default_branch_length_;
83  }
84 
91  {
92  default_branch_length_ = value;
93  return *this;
94  }
95 
99  std::string const& default_leaf_name() const
100  {
101  return default_leaf_name_;
102  }
103 
107  self_type& default_leaf_name( std::string const& value )
108  {
109  default_leaf_name_ = value;
110  return *this;
111  }
112 
116  std::string const& default_inner_name() const
117  {
118  return default_inner_name_;
119  }
120 
124  self_type& default_inner_name( std::string const& value )
125  {
126  default_inner_name_ = value;
127  return *this;
128  }
129 
133  std::string const& default_root_name() const
134  {
135  return default_root_name_;
136  }
137 
141  self_type& default_root_name( std::string const& value )
142  {
143  default_root_name_ = value;
144  return *this;
145  }
146 
151  self_type& set_default_names( std::string const& value )
152  {
153  default_leaf_name_ = value;
154  default_inner_name_ = value;
155  default_root_name_ = value;
156  return *this;
157  }
158 
164  bool use_default_names() const
165  {
166  return use_default_names_;
167  }
168 
186  {
187  use_default_names_ = value;
188  return *this;
189  }
190 
197  {
198  return replace_name_underscores_;
199  }
200 
210  {
211  replace_name_underscores_ = value;
212  return *this;
213  }
214 
215  // -------------------------------------------------------------------------
216  // Plugin Functions
217  // -------------------------------------------------------------------------
218 
219  void element_to_node( NewickBrokerElement const& element, TreeNode& node ) const
220  {
221  std::string name = element.name;
222 
223  // Insert default names if needed.
224  if( name.empty() && use_default_names_ ) {
225  if( element.is_leaf() ) {
226  name = default_leaf_name_;
227  } else if( element.is_root() ) {
228  name = default_root_name_;
229  } else {
230  name = default_inner_name_;
231  }
232  }
233 
234  // Handle underscores/spaces.
235  if( replace_name_underscores_ ) {
236  name = utils::replace_all(name, "_", " ");
237  }
238 
239  node.data<CommonNodeData>().name = name;
240  }
241 
242  void element_to_edge( NewickBrokerElement const& element, TreeEdge& edge ) const
243  {
244  // We assume that the branch length is always the first (or only) value.
245  // If there is an interpretation where this is not the case, it is best to introduce
246  // an array index for this as a paramter of this class.
247  if( element.values.size() > 0 ) {
248  edge.data<CommonEdgeData>().branch_length = std::stod( element.values[0] );
249  } else {
250  edge.data<CommonEdgeData>().branch_length = default_branch_length_;
251  }
252  }
253 
254  void register_with( NewickReader& reader ) const
255  {
256  // Set node data creation function.
257  reader.create_node_data_plugin = []( TreeNode& node ){
258  node.reset_data( CommonNodeData::create() );
259  };
260 
261  // Set edge data creation function.
262  reader.create_edge_data_plugin = []( TreeEdge& edge ){
263  edge.reset_data( CommonEdgeData::create() );
264  };
265 
266  // Add node manipulation functions.
267  reader.element_to_node_plugins.push_back(
268  [&]( NewickBrokerElement const& element, TreeNode& node ) {
269  element_to_node( element, node );
270  }
271  );
272 
273  // Add edge manipulation functions.
274  reader.element_to_edge_plugins.push_back(
275  [&]( NewickBrokerElement const& element, TreeEdge& edge ) {
276  element_to_edge( element, edge );
277  }
278  );
279 
280  // Alternative version using bind.
281  // reader.element_to_edge_plugins.push_back(
282  // std::bind(
283  // &CommonTreeNewickReaderPlugin::element_to_edge,
284  // this,
285  // std::placeholders::_1,
286  // std::placeholders::_2
287  // )
288  // );
289  }
290 
291  // -------------------------------------------------------------------------
292  // Data Members
293  // -------------------------------------------------------------------------
294 
295 private:
296 
297  double default_branch_length_ = 1.0;
298 
299  std::string default_leaf_name_ = "Leaf_Node";
300  std::string default_inner_name_ = "Inner_Node";
301  std::string default_root_name_ = "Root_Node";
302 
303  bool use_default_names_ = false;
304  bool replace_name_underscores_ = false;
305 
306 };
307 
308 // =================================================================================================
309 // Common Tree Newick Reader
310 // =================================================================================================
311 
327  : public NewickReader
329 {
330 public:
331 
332  // -------------------------------------------------------------------------
333  // Constructor and Rule of Five
334  // -------------------------------------------------------------------------
335 
337  {
338  // This is mindfuck. We derive from two classes - the function register_with() calls
339  // the plugin function of CommonTreeNewickReaderPlugin, and uses our own inherited
340  // NewickReader instance as argument. Thus, it registeres its own plugin part with its own
341  // reader part.
342  register_with( *this );
343  }
344 };
345 
346 } // namespace tree
347 } // namespace genesis
348 
349 #endif // include guard
genesis::tree::CommonTreeNewickReaderPlugin::replace_name_underscores
self_type & replace_name_underscores(bool value)
Set whether to replace all underscores ('_') in names with spaces (' ').
Definition: tree/common_tree/newick_reader.hpp:209
genesis::tree::NewickReader::create_node_data_plugin
create_node_data_function create_node_data_plugin
Definition: tree/formats/newick/reader.hpp:313
genesis::tree::CommonTreeNewickReaderPlugin::use_default_names
self_type & use_default_names(bool value)
Set whether to replace unnamed nodes with a default name.
Definition: tree/common_tree/newick_reader.hpp:185
genesis::tree::CommonTreeNewickReaderPlugin::element_to_node
void element_to_node(NewickBrokerElement const &element, TreeNode &node) const
Definition: tree/common_tree/newick_reader.hpp:219
genesis::tree::CommonTreeNewickReaderPlugin::default_branch_length
double default_branch_length() const
Get the default branch length used when there is none given for an edge.
Definition: tree/common_tree/newick_reader.hpp:80
genesis::tree::NewickBrokerElement::name
std::string name
Name of the node.
Definition: element.hpp:114
genesis::tree::NewickBrokerElement::is_root
bool is_root() const
Return whether this is the root node of the tree.
Definition: element.hpp:160
genesis::tree::NewickReader
Definition: tree/formats/newick/reader.hpp:67
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::CommonTreeNewickReaderPlugin::default_root_name
self_type & default_root_name(std::string const &value)
Set the default named used when there is none given for the root node.
Definition: tree/common_tree/newick_reader.hpp:141
genesis::tree::CommonTreeNewickReaderPlugin
Provide a set of plugin functions for NewickReader to read a CommonTree.
Definition: tree/common_tree/newick_reader.hpp:50
genesis::tree::CommonEdgeData::create
static std::unique_ptr< CommonEdgeData > create()
Definition: tree/common_tree/tree.hpp:168
genesis::tree::NewickReader::create_edge_data_plugin
create_edge_data_function create_edge_data_plugin
Definition: tree/formats/newick/reader.hpp:314
genesis::tree::CommonTreeNewickReaderPlugin::default_inner_name
std::string const & default_inner_name() const
Get the default named used when there is none given for an inner node.
Definition: tree/common_tree/newick_reader.hpp:116
element.hpp
genesis::tree::CommonTreeNewickReader::CommonTreeNewickReader
CommonTreeNewickReader()
Definition: tree/common_tree/newick_reader.hpp:336
genesis::tree::CommonTreeNewickReaderPlugin::default_inner_name
self_type & default_inner_name(std::string const &value)
Set the default named used when there is none given for an inner node.
Definition: tree/common_tree/newick_reader.hpp:124
genesis::tree::CommonTreeNewickReaderPlugin::register_with
void register_with(NewickReader &reader) const
Definition: tree/common_tree/newick_reader.hpp:254
std.hpp
Provides some valuable additions to STD.
genesis::tree::CommonTreeNewickReaderPlugin::replace_name_underscores
bool replace_name_underscores() const
Return whether currently this plugin replaces underscores with spaces.
Definition: tree/common_tree/newick_reader.hpp:196
genesis::tree::CommonTreeNewickReader
Read default Newick trees, i.e., trees with names and branch lengths.
Definition: tree/common_tree/newick_reader.hpp:326
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::CommonTreeNewickReaderPlugin::~CommonTreeNewickReaderPlugin
virtual ~CommonTreeNewickReaderPlugin()=default
string.hpp
Provides some commonly used string utility functions.
genesis::tree::NewickReader::element_to_edge_plugins
std::vector< element_to_edge_function > element_to_edge_plugins
Definition: tree/formats/newick/reader.hpp:317
genesis::tree::CommonTreeNewickReaderPlugin::CommonTreeNewickReaderPlugin
CommonTreeNewickReaderPlugin()=default
genesis::tree::TreeEdge
Definition: edge.hpp:60
genesis::tree::TreeNode
Definition: tree/tree/node.hpp:58
genesis::tree::CommonTreeNewickReaderPlugin::use_default_names
bool use_default_names() const
Return whether currently default names are activated in this plugin.
Definition: tree/common_tree/newick_reader.hpp:164
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
tree.hpp
genesis::tree::CommonTreeNewickReaderPlugin::default_leaf_name
std::string const & default_leaf_name() const
Get the default named used when there is none given for a leaf node.
Definition: tree/common_tree/newick_reader.hpp:99
genesis::tree::NewickBrokerElement::is_leaf
bool is_leaf() const
Return whether this is a leaf node.
Definition: element.hpp:168
genesis::tree::CommonTreeNewickReaderPlugin::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_reader.hpp:151
reader.hpp
genesis::tree::NewickReader::element_to_node_plugins
std::vector< element_to_node_function > element_to_node_plugins
Definition: tree/formats/newick/reader.hpp:316
genesis::tree::CommonTreeNewickReaderPlugin::element_to_edge
void element_to_edge(NewickBrokerElement const &element, TreeEdge &edge) const
Definition: tree/common_tree/newick_reader.hpp:242
genesis::tree::CommonTreeNewickReaderPlugin::default_root_name
std::string const & default_root_name() const
Get the default named used when there is none given for the root node.
Definition: tree/common_tree/newick_reader.hpp:133
genesis::tree::CommonEdgeData
Common class containing the commonly needed data for tree edges.
Definition: tree/common_tree/tree.hpp:144
genesis::tree::CommonNodeData
Common class containing the commonly needed data for tree nodes.
Definition: tree/common_tree/tree.hpp:79
genesis::tree::CommonTreeNewickReaderPlugin::default_leaf_name
self_type & default_leaf_name(std::string const &value)
Set the default named used when there is none given for a leaf node.
Definition: tree/common_tree/newick_reader.hpp:107
genesis::tree::CommonNodeData::create
static std::unique_ptr< CommonNodeData > create()
Definition: tree/common_tree/tree.hpp:103
genesis::tree::TreeNode::data
NodeDataType & data()
Definition: tree/tree/node.hpp:203
genesis::tree::TreeEdge::data
EdgeDataType & data()
Definition: edge.hpp:217
genesis::tree::NewickBrokerElement
Store the information for one element of a Newick tree.
Definition: element.hpp:60
genesis::tree::CommonTreeNewickReaderPlugin::operator=
CommonTreeNewickReaderPlugin & operator=(CommonTreeNewickReaderPlugin const &)=default
genesis::tree::CommonTreeNewickReaderPlugin::default_branch_length
self_type & default_branch_length(double value)
Set the default branch length used when there is none given for an edge.
Definition: tree/common_tree/newick_reader.hpp:90