A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tree/default/newick_reader.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_DEFAULT_NEWICK_READER_H_
2 #define GENESIS_TREE_DEFAULT_NEWICK_READER_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 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 
65  virtual ~DefaultTreeNewickReaderPlugin() = 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<DefaultNodeData>().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<DefaultEdgeData>().branch_length = std::stod( element.values[0] );
249  } else {
250  edge.data<DefaultEdgeData>().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( DefaultNodeData::create() );
259  };
260 
261  // Set edge data creation function.
262  reader.create_edge_data_plugin = []( TreeEdge& edge ){
263  edge.reset_data( DefaultEdgeData::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  // &DefaultTreeNewickReaderPlugin::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 // Default 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 DefaultTreeNewickReaderPlugin, 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
std::vector< std::string > values
Numerical values associated with the node, i.e. branch lengths.
Definition: element.hpp:104
static std::unique_ptr< DefaultEdgeData > create()
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.
Default class containing the commonly needed data for tree nodes.
self_type & default_inner_name(std::string const &value)
Set the default named used when there is none given for an inner node.
bool replace_name_underscores() const
Return whether currently this plugin replaces underscores with spaces.
void element_to_node(NewickBrokerElement const &element, TreeNode &node) const
Read default Newick trees, i.e., trees with names and branch lengths.
self_type & default_leaf_name(std::string const &value)
Set the default named used when there is none given for a leaf node.
DefaultTreeNewickReaderPlugin & operator=(DefaultTreeNewickReaderPlugin const &)=default
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:239
Provides some valuable additions to STD.
Default class containing the commonly needed data for tree edges.
self_type & use_default_names(bool value)
Set whether to replace unnamed nodes with a default name.
double default_branch_length() const
Get the default branch length used when there is none given for an edge.
create_edge_data_function create_edge_data_plugin
std::string const & default_root_name() const
Get the default named used when there is none given for the root node.
Provides some commonly used string utility functions.
static std::unique_ptr< DefaultNodeData > create()
std::string const & default_leaf_name() const
Get the default named used when there is none given for a leaf node.
std::vector< element_to_edge_function > element_to_edge_plugins
Provide a set of plugin functions for NewickReader to read a DefaultTree.
create_node_data_function create_node_data_plugin
void element_to_edge(NewickBrokerElement const &element, TreeEdge &edge) const
NodeDataType & data()
Definition: node.hpp:108
bool is_root() const
Return whether this is the root node of the tree.
Definition: element.hpp:142
std::string const & default_inner_name() const
Get the default named used when there is none given for an inner node.
bool use_default_names() const
Return whether currently default names are activated in this plugin.
std::string name
Name of the node.
Definition: element.hpp:96
EdgeDataType & data()
Definition: edge.hpp:118
bool is_leaf() const
Return whether this is a leaf node.
Definition: element.hpp:150
Store the information for one element of a Newick tree.
Definition: element.hpp:60
self_type & default_branch_length(double value)
Set the default branch length used when there is none given for an edge.
self_type & default_root_name(std::string const &value)
Set the default named used when there is none given for the root node.
self_type & replace_name_underscores(bool value)
Set whether to replace all underscores ('_') in names with spaces (' ').
std::vector< element_to_node_function > element_to_node_plugins