A library for working with phylogenetic and population genetic data.
v0.27.0
indexed_newick_reader.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_ATTRIBUTE_TREE_INDEXED_NEWICK_READER_H_
2 #define GENESIS_TREE_ATTRIBUTE_TREE_INDEXED_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 // Indexed Attribute Tree Newick Reader Plugin
49 // =================================================================================================
50 
96 {
97 public:
98 
99  // -------------------------------------------------------------------------
100  // Typedefs and Enums
101  // -------------------------------------------------------------------------
102 
104 
110  enum class Source
111  {
115  kValue,
116 
120  kComment,
121 
125  kTag
126  };
127 
131  enum class Target
132  {
137  kNode,
138 
143  kEdge
144  };
145 
146  // -------------------------------------------------------------------------
147  // Constructor and Rule of Five
148  // -------------------------------------------------------------------------
149 
151  virtual ~IndexedAttributeTreeNewickReaderPlugin() = default;
152 
155 
158 
159  // -------------------------------------------------------------------------
160  // Settings
161  // -------------------------------------------------------------------------
162 
207  Source source,
208  size_t index,
209  Target target,
210  std::string const& target_key
211  );
212 
241  Source source,
242  size_t index,
243  Target target,
244  std::string const& target_key,
245  std::string const& default_value
246  );
247 
283  Source source,
284  Target target,
285  std::string const& target_key_prefix
286  );
287 
300 
304  void clear();
305 
306  // -------------------------------------------------------------------------
307  // Plugin Functions
308  // -------------------------------------------------------------------------
309 
310  void element_to_node( NewickBrokerElement const& element, TreeNode& node ) const;
311 
312  void element_to_edge( NewickBrokerElement const& element, TreeEdge& edge ) const;
313 
314  void register_with( NewickReader& reader ) const;
315 
316  // -------------------------------------------------------------------------
317  // Internal Functions
318  // -------------------------------------------------------------------------
319 
320 private:
321 
322  std::vector<std::string> const& get_attribute_source_(
323  NewickBrokerElement const& element,
324  Source source
325  ) const;
326 
327  void process_indexed_attributes_(
328  NewickBrokerElement const& element,
329  AttributeTreeMap& attributes,
330  Target target
331  ) const;
332 
333  void process_catch_all_attributes_(
334  NewickBrokerElement const& element,
335  AttributeTreeMap& attributes,
336  Target target
337  ) const;
338 
339  // -------------------------------------------------------------------------
340  // Data Members
341  // -------------------------------------------------------------------------
342 
343 private:
344 
345  struct AttributeDescriptor
346  {
347  Source source;
348  size_t index;
349  Target target;
350  std::string target_key;
351  std::string default_value;
352  bool use_default;
353  };
354 
355  std::vector<AttributeDescriptor> indexed_attributes_;
356  std::vector<AttributeDescriptor> catch_all_attributes_;
357 
358 };
359 
360 // =================================================================================================
361 // Attribute Tree Newick Reader
362 // =================================================================================================
363 
374  : public NewickReader
377 {
378 public:
379 
380  // -------------------------------------------------------------------------
381  // Constructor and Rule of Five
382  // -------------------------------------------------------------------------
383 
385  {
386  // We first register the default reader, then the placement reader, because the latter
387  // overwrites the data creation functions.
390  }
391 };
392 
393 } // namespace tree
394 } // namespace genesis
395 
396 #endif // include guard
genesis::tree::IndexedAttributeTreeNewickReaderPlugin::self_type
IndexedAttributeTreeNewickReaderPlugin self_type
Definition: indexed_newick_reader.hpp:103
genesis::tree::IndexedAttributeTreeNewickReaderPlugin::element_to_edge
void element_to_edge(NewickBrokerElement const &element, TreeEdge &edge) const
Definition: indexed_newick_reader.cpp:101
tree.hpp
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::IndexedAttributeTreeNewickReaderPlugin::IndexedAttributeTreeNewickReaderPlugin
IndexedAttributeTreeNewickReaderPlugin()=default
genesis::tree::IndexedAttributeTreeNewickReader
Read Newick trees with ordered attributes for the Nodes and Edges.
Definition: indexed_newick_reader.hpp:373
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::IndexedAttributeTreeNewickReaderPlugin::Source
Source
Select which kind of Newick data to take, i.e., either comments, values, or tags.
Definition: indexed_newick_reader.hpp:110
genesis::tree::IndexedAttributeTreeNewickReaderPlugin
Provide a set of plugin functions for NewickReader to read ordered attributes of the Nodes and Edges ...
Definition: indexed_newick_reader.hpp:95
genesis::tree::CommonTreeNewickReaderPlugin::register_with
void register_with(NewickReader &reader) const
Definition: tree/common_tree/newick_reader.hpp:254
genesis::tree::IndexedAttributeTreeNewickReaderPlugin::~IndexedAttributeTreeNewickReaderPlugin
virtual ~IndexedAttributeTreeNewickReaderPlugin()=default
genesis::tree::IndexedAttributeTreeNewickReaderPlugin::Target
Target
Select where to store the data, i.e., at Nodes or Edges of the Tree.
Definition: indexed_newick_reader.hpp:131
genesis::tree::IndexedAttributeTreeNewickReaderPlugin::Source::kValue
@ kValue
Take data from Newick values, i.e., :3.14.
genesis::tree::IndexedAttributeTreeNewickReaderPlugin::Source::kComment
@ kComment
Take data from Newick comments, i.e., [something].
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::IndexedAttributeTreeNewickReaderPlugin::Target::kEdge
@ kEdge
Store data at the attributes map of an AttributeTreeEdge.
genesis::tree::IndexedAttributeTreeNewickReader::IndexedAttributeTreeNewickReader
IndexedAttributeTreeNewickReader()
Definition: indexed_newick_reader.hpp:384
genesis::tree::IndexedAttributeTreeNewickReaderPlugin::clear
void clear()
Reset all settings to the default, i.e., delete all attribute settings.
Definition: indexed_newick_reader.cpp:82
genesis::tree::IndexedAttributeTreeNewickReaderPlugin::Source::kTag
@ kTag
Take data from Newick tags, i.e., {42}.
reader.hpp
genesis::tree::IndexedAttributeTreeNewickReaderPlugin::add_attribute
self_type & add_attribute(Source source, size_t index, Target target, std::string const &target_key)
Store the Newick data at a given index in an AttributeTreeMap.
Definition: indexed_newick_reader.cpp:43
genesis::tree::IndexedAttributeTreeNewickReaderPlugin::operator=
self_type & operator=(IndexedAttributeTreeNewickReaderPlugin const &)=default
genesis::tree::NewickBrokerElement
Store the information for one element of a Newick tree.
Definition: element.hpp:60
genesis::tree::IndexedAttributeTreeNewickReaderPlugin::register_with
void register_with(NewickReader &reader) const
Definition: indexed_newick_reader.cpp:110
genesis::tree::IndexedAttributeTreeNewickReaderPlugin::Target::kNode
@ kNode
Store data at the attributes map of an AttributeTreeNode.
genesis::tree::IndexedAttributeTreeNewickReaderPlugin::element_to_node
void element_to_node(NewickBrokerElement const &element, TreeNode &node) const
Definition: indexed_newick_reader.cpp:92
genesis::tree::IndexedAttributeTreeNewickReaderPlugin::add_catch_all
self_type & add_catch_all(Source source, Target target, std::string const &target_key_prefix)
Store all Newick data of a given source in an AttributeTreeMap.
Definition: indexed_newick_reader.cpp:64