A toolkit for working with phylogenetic data.
v0.18.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-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 
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
void element_to_edge(NewickBrokerElement const &element, TreeEdge &edge) const
self_type & trim(bool value)
Set whether to trim keys and values before storing them in the Tree.
Target
Select where to store the data, i.e., at Nodes or Edges of the Tree.
bool trim() const
Get whether to trim keys and values before storing them in the Tree.
self_type & separator(std::string const &value)
Set the separator between key-value-pairs.
self_type & add_catch_all(Target target=Target::kNode)
Store all key-value-pairs of the Newick data in an AttributeTree.
std::string separator() const
Get the currently set separator between key-value-pairs.
Provide a set of plugin functions for NewickReader to read key-value-pair data attributes into an Att...
self_type & set_nhx_delimiters()
Set the delimiters to the format used by NHX.
self_type & assigner(std::string const &value)
Set the assign symbol between a key and its value.
std::string assigner() const
Get the currently set assign symbol between a key and its value.
Store data at the attributes map of an AttributeTreeNode.
self_type & add_nhx_attributes()
Add typical attributes of the NHX format, and set the appropriate delimiters.
self_type & operator=(KeyedAttributeTreeNewickReaderPlugin const &)=default
self_type & prefix(std::string const &value)
Set the prefix to look for in Newick comments.
self_type & set_delimiters(std::string const &prefix, std::string const &separator, std::string const &assigner="=")
Set the delimiters for key-value-pairs.
Store data at the attributes map of an AttributeTreeEdge.
Provide a set of plugin functions for NewickReader to read a DefaultTree.
void clear()
Reset all settings to the default and delete all attribute settings.
self_type & add_attribute(std::string const &key, Target target)
Store values of a key at a target (i.e., Node or Edge).
std::map< std::string, std::string > AttributeTreeMap
Alias for the map type used by an AttributeTree.
void element_to_node(NewickBrokerElement const &element, TreeNode &node) const
Read default Newick trees, i.e., trees with names and branch lengths.
std::string prefix() const
Get the currently set prefix to look for in Newick comments.
Store the information for one element of a Newick tree.
Definition: element.hpp:60