A library for working with phylogenetic and population genetic data.
v0.32.0
keyed_newick_reader.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_ATTRIBUTE_TREE_KEYED_NEWICK_READER_H_
2 #define GENESIS_TREE_ATTRIBUTE_TREE_KEYED_NEWICK_READER_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2020 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 
38 
39 #include <functional>
40 #include <map>
41 #include <string>
42 #include <vector>
43 
44 namespace genesis {
45 namespace tree {
46 
47 // =================================================================================================
48 // Keyed Attribute Tree Newick Reader Plugin
49 // =================================================================================================
50 
82 {
83 public:
84 
85  // -------------------------------------------------------------------------
86  // Typedefs and Enums
87  // -------------------------------------------------------------------------
88 
90 
94  enum class Target
95  {
100  kNode,
101 
106  kEdge
107  };
108 
109  // -------------------------------------------------------------------------
110  // Constructor and Rule of Five
111  // -------------------------------------------------------------------------
112 
114  virtual ~KeyedAttributeTreeNewickReaderPlugin() = default;
115 
118 
121 
122  // -------------------------------------------------------------------------
123  // Settings
124  // -------------------------------------------------------------------------
125 
153  std::string const& key,
154  Target target
155  );
156 
189  std::string const& source_key,
190  Target target,
191  std::string const& target_key
192  );
193 
211  std::string const& source_key,
212  Target target,
213  std::string const& target_key,
214  std::string const& default_value
215  );
216 
240 
272 
285 
292  std::string const& prefix,
293  std::string const& separator,
294  std::string const& assigner = "="
295  ) {
296  prefix_ = prefix;
297  separator_ = separator;
298  assigner_ = assigner;
299  return *this;
300  }
301 
307  self_type& prefix( std::string const& value )
308  {
309  prefix_ = value;
310  return *this;
311  }
312 
316  std::string prefix() const
317  {
318  return prefix_;
319  }
320 
326  self_type& separator( std::string const& value )
327  {
328  separator_ = value;
329  return *this;
330  }
331 
335  std::string separator() const
336  {
337  return separator_;
338  }
339 
345  self_type& assigner( std::string const& value )
346  {
347  assigner_ = value;
348  return *this;
349  }
350 
354  std::string assigner() const
355  {
356  return assigner_;
357  }
358 
364  self_type& trim( bool value )
365  {
366  trim_ = value;
367  return *this;
368  }
369 
373  bool trim() const
374  {
375  return trim_;
376  }
377 
381  void clear();
382 
383  // -------------------------------------------------------------------------
384  // Plugin Functions
385  // -------------------------------------------------------------------------
386 
387  void element_to_node( NewickBrokerElement const& element, TreeNode& node ) const;
388 
389  void element_to_edge( NewickBrokerElement const& element, TreeEdge& edge ) const;
390 
391  void register_with( NewickReader& reader ) const;
392 
393  // -------------------------------------------------------------------------
394  // Internal Functions
395  // -------------------------------------------------------------------------
396 
397 private:
398 
399  using KeyValuePair = std::pair<std::string, std::string>;
400  using PairList = std::vector<KeyValuePair>;
401 
409  bool has_attributes_for_target_( Target target ) const;
410 
414  PairList get_data_( NewickBrokerElement const& element ) const;
415 
416  void process_keyed_attributes_(
417  PairList const& data,
418  AttributeTreeMap& attributes,
419  Target target
420  ) const;
421 
422  void process_catch_all_attributes_(
423  PairList const& data,
424  AttributeTreeMap& attributes,
425  Target target
426  ) const;
427 
428  // -------------------------------------------------------------------------
429  // Data Members
430  // -------------------------------------------------------------------------
431 
432 private:
433 
434  struct AttributeDescriptor
435  {
436  std::string source_key;
437  Target target;
438  std::string target_key;
439  std::string default_value;
440  bool use_default;
441  };
442 
443  std::string prefix_ = "&";
444  std::string separator_ = ",";
445  std::string assigner_ = "=";
446 
447  bool trim_ = true;
448 
449  std::vector<AttributeDescriptor> keyed_attributes_;
450  std::vector<AttributeDescriptor> catch_all_attributes_;
451 
452  static std::vector<AttributeDescriptor> nhx_attributes_;
453 
454 };
455 
456 // =================================================================================================
457 // Attribute Tree Newick Reader
458 // =================================================================================================
459 
470  : public NewickReader
473 {
474 public:
475 
476  // -------------------------------------------------------------------------
477  // Constructor and Rule of Five
478  // -------------------------------------------------------------------------
479 
481  {
482  // We first register the default reader, then the placement reader, because the latter
483  // overwrites the data creation functions.
486  }
487 };
488 
489 } // namespace tree
490 } // namespace genesis
491 
492 #endif // include guard
tree.hpp
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::trim
self_type & trim(bool value)
Set whether to trim keys and values before storing them in the Tree.
Definition: keyed_newick_reader.hpp:364
newick_reader.hpp
genesis::tree::AttributeTreeMap
std::map< std::string, std::string > AttributeTreeMap
Alias for the map type used by an AttributeTree.
Definition: tree/attribute_tree/tree.hpp:81
genesis::tree::NewickReader
Definition: tree/formats/newick/reader.hpp:67
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::clear
void clear()
Reset all settings to the default and delete all attribute settings.
Definition: keyed_newick_reader.cpp:119
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::prefix
std::string prefix() const
Get the currently set prefix to look for in Newick comments.
Definition: keyed_newick_reader.hpp:316
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::element_to_edge
void element_to_edge(NewickBrokerElement const &element, TreeEdge &edge) const
Definition: keyed_newick_reader.cpp:150
genesis::tree::CommonTreeNewickReaderPlugin
Provide a set of plugin functions for NewickReader to read a CommonTree.
Definition: tree/common_tree/newick_reader.hpp:50
element.hpp
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::add_nhx_attributes
self_type & add_nhx_attributes()
Add typical attributes of the NHX format, and set the appropriate delimiters.
Definition: keyed_newick_reader.cpp:110
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::operator=
self_type & operator=(KeyedAttributeTreeNewickReaderPlugin const &)=default
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::assigner
self_type & assigner(std::string const &value)
Set the assign symbol between a key and its value.
Definition: keyed_newick_reader.hpp:345
genesis::tree::CommonTreeNewickReaderPlugin::register_with
void register_with(NewickReader &reader) const
Definition: tree/common_tree/newick_reader.hpp:254
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::~KeyedAttributeTreeNewickReaderPlugin
virtual ~KeyedAttributeTreeNewickReaderPlugin()=default
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::prefix
self_type & prefix(std::string const &value)
Set the prefix to look for in Newick comments.
Definition: keyed_newick_reader.hpp:307
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::assigner
std::string assigner() const
Get the currently set assign symbol between a key and its value.
Definition: keyed_newick_reader.hpp:354
genesis::tree::KeyedAttributeTreeNewickReader
Read default Newick trees, i.e., trees with names and branch lengths.
Definition: keyed_newick_reader.hpp:469
genesis::tree::KeyedAttributeTreeNewickReaderPlugin
Provide a set of plugin functions for NewickReader to read key-value-pair data attributes into an Att...
Definition: keyed_newick_reader.hpp:81
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::element_to_node
void element_to_node(NewickBrokerElement const &element, TreeNode &node) const
Definition: keyed_newick_reader.cpp:134
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::Target::kNode
@ kNode
Store data at the attributes map of an AttributeTreeNode.
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::add_attribute
self_type & add_attribute(std::string const &key, Target target)
Store values of a key at a target (i.e., Node or Edge).
Definition: keyed_newick_reader.cpp:68
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::Target
Target
Select where to store the data, i.e., at Nodes or Edges of the Tree.
Definition: keyed_newick_reader.hpp:94
genesis::tree::TreeEdge
Definition: edge.hpp:60
genesis::tree::TreeNode
Definition: tree/tree/node.hpp:58
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::KeyedAttributeTreeNewickReaderPlugin::KeyedAttributeTreeNewickReaderPlugin
KeyedAttributeTreeNewickReaderPlugin()=default
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::separator
std::string separator() const
Get the currently set separator between key-value-pairs.
Definition: keyed_newick_reader.hpp:335
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::separator
self_type & separator(std::string const &value)
Set the separator between key-value-pairs.
Definition: keyed_newick_reader.hpp:326
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::Target::kEdge
@ kEdge
Store data at the attributes map of an AttributeTreeEdge.
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::add_catch_all
self_type & add_catch_all(Target target=Target::kNode)
Store all key-value-pairs of the Newick data in an AttributeTree.
Definition: keyed_newick_reader.cpp:95
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::trim
bool trim() const
Get whether to trim keys and values before storing them in the Tree.
Definition: keyed_newick_reader.hpp:373
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::set_delimiters
self_type & set_delimiters(std::string const &prefix, std::string const &separator, std::string const &assigner="=")
Set the delimiters for key-value-pairs.
Definition: keyed_newick_reader.hpp:291
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::register_with
void register_with(NewickReader &reader) const
Definition: keyed_newick_reader.cpp:166
genesis::tree::KeyedAttributeTreeNewickReader::KeyedAttributeTreeNewickReader
KeyedAttributeTreeNewickReader()
Definition: keyed_newick_reader.hpp:480
reader.hpp
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::self_type
KeyedAttributeTreeNewickReaderPlugin self_type
Definition: keyed_newick_reader.hpp:89
genesis::tree::NewickBrokerElement
Store the information for one element of a Newick tree.
Definition: element.hpp:60
genesis::tree::KeyedAttributeTreeNewickReaderPlugin::set_nhx_delimiters
self_type & set_nhx_delimiters()
Set the delimiters to the format used by NHX.
Definition: keyed_newick_reader.cpp:102