A toolkit for working with phylogenetic data.
v0.24.0
element.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_FORMATS_NEWICK_ELEMENT_H_
2 #define GENESIS_TREE_FORMATS_NEWICK_ELEMENT_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2019 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 
34 #include <stdexcept>
35 #include <string>
36 #include <vector>
37 
38 namespace genesis {
39 namespace tree {
40 
41 // =================================================================================================
42 // Forward Declarations
43 // =================================================================================================
44 
45 class NewickBroker;
46 
47 // =================================================================================================
48 // NewickBrokerElement
49 // =================================================================================================
50 
61 {
62  friend NewickBroker;
63 
64  // -------------------------------------------------------------------------
65  // Constructor and Rule of Five
66  // -------------------------------------------------------------------------
67 
68 public:
69 
74  : depth( -1 )
75  , rank_( -1 )
76  {}
77 
81  explicit NewickBrokerElement( long depth )
82  : depth( depth )
83  , rank_( -1 )
84  {}
85 
89  NewickBrokerElement( std::string const& name, long depth )
90  : name( name )
91  , depth( depth )
92  , rank_( -1 )
93  {}
94 
95  ~NewickBrokerElement() = default;
96 
97  NewickBrokerElement(NewickBrokerElement const&) = default;
99 
102 
103  // -------------------------------------------------------------------------
104  // Public Data Members
105  // -------------------------------------------------------------------------
106 
114  std::string name;
115 
122  std::vector<std::string> values;
123 
127  std::vector<std::string> tags;
128 
132  std::vector<std::string> comments;
133 
137  long depth;
138 
139  // -------------------------------------------------------------------------
140  // Additional Members
141  // -------------------------------------------------------------------------
142 
149  long rank() const
150  {
151  if (rank_ < 0) {
152  throw std::logic_error("NewickBroker::assign_ranks() was not called before.");
153  }
154  return rank_;
155  }
156 
160  bool is_root() const
161  {
162  return depth == 0;
163  }
164 
168  bool is_leaf() const
169  {
170  if (rank_ < 0) {
171  throw std::logic_error("NewickBroker::is_leaf() was not called before.");
172  }
173  return rank_ == 0;
174  }
175 
179  bool is_inner() const
180  {
181  if (rank_ < 0) {
182  throw std::logic_error("NewickBroker::is_leaf() was not called before.");
183  }
184  return rank_ != 0;
185  }
186 
187 private:
188 
192  mutable long rank_;
193 };
194 
195 } // namespace tree
196 } // namespace genesis
197 
198 #endif // include guard
NewickBrokerElement(std::string const &name, long depth)
Constructor, initializes the item values and sets the name and the depth.
Definition: element.hpp:89
std::vector< std::string > values
Numerical values associated with the node, i.e. branch lengths.
Definition: element.hpp:122
bool is_root() const
Return whether this is the root node of the tree.
Definition: element.hpp:160
NewickBrokerElement()
Constructor, initializes the item values.
Definition: element.hpp:73
NewickBrokerElement(long depth)
Constructor, initializes the item values and sets the depth.
Definition: element.hpp:81
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
std::vector< std::string > comments
Arbitrary strings that can be attached to a node, e.g. in Newick format via "[]". ...
Definition: element.hpp:132
std::vector< std::string > tags
Arbitrary strings that can be attached to a node, e.g. in Newick format via "{}". ...
Definition: element.hpp:127
bool is_inner() const
Return whether this is an inner node, i.e., not a leaf node.
Definition: element.hpp:179
long rank() const
Returns the rank (number of immediate children) of this node.
Definition: element.hpp:149
std::string name
Name of the node.
Definition: element.hpp:114
Store the information for one element of a Newick tree.
Definition: element.hpp:60
bool is_leaf() const
Return whether this is a leaf node.
Definition: element.hpp:168
NewickBrokerElement & operator=(NewickBrokerElement const &)=default
long depth
Depth of the node in the tree, i.e. its distance from the root.
Definition: element.hpp:137