A library for working with phylogenetic and population genetic data.
v0.27.0
taxonomy/taxonomy.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TAXONOMY_TAXONOMY_H_
2 #define GENESIS_TAXONOMY_TAXONOMY_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2018 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 <string>
35 #include <list>
36 
37 namespace genesis {
38 namespace taxonomy {
39 
40 // =================================================================================================
41 // Forward Declarations
42 // =================================================================================================
43 
44 class Taxon;
45 class Taxonomy;
46 void swap( Taxonomy& lhs, Taxonomy& rhs );
47 
48 // =================================================================================================
49 // Taxonomy
50 // =================================================================================================
51 
96 class Taxonomy
97 {
98 public:
99 
100  // -------------------------------------------------------------------------
101  // Typedefs and Enums
102  // -------------------------------------------------------------------------
103 
104  typedef std::list<Taxon>::iterator iterator;
105  typedef std::list<Taxon>::const_iterator const_iterator;
106 
107  // -------------------------------------------------------------------------
108  // Constructors and Rule of Five
109  // -------------------------------------------------------------------------
110 
111  Taxonomy() = default;
112  virtual ~Taxonomy() = default;
113 
120  Taxonomy( Taxonomy const& );
121 
128  Taxonomy( Taxonomy&& );
129 
136  Taxonomy& operator= ( Taxonomy const& );
137 
145 
146  friend void swap( Taxonomy& lhs, Taxonomy& rhs );
147 
148  // -------------------------------------------------------------------------
149  // Accessors
150  // -------------------------------------------------------------------------
151 
157  size_t size() const;
158 
162  bool has_child ( std::string name ) const;
163 
167  Taxon const& get_child ( std::string name ) const;
168 
172  Taxon& get_child ( std::string name );
173 
177  Taxon const& operator [] ( std::string name ) const;
178 
182  Taxon& operator [] ( std::string name );
183 
189  Taxon const& at ( size_t index ) const;
190 
196  Taxon& at ( size_t index );
197 
203  Taxon const& operator [] ( size_t index ) const;
204 
210  Taxon& operator [] ( size_t index );
211 
212  size_t index_of( std::string const& name ) const;
213 
214  // -------------------------------------------------------------------------
215  // Modifiers
216  // -------------------------------------------------------------------------
217 
226  Taxon& add_child( Taxon const& child, bool merge_duplicates = true );
227 
236  Taxon& add_child( std::string const& name, bool merge_duplicates = true );
237 
244  void remove_child( std::string const& name );
245 
252  void remove_at( size_t index );
253 
257  void clear_children();
258 
262  template <class Compare>
263  void sort( Compare comp )
264  {
265  children_.sort( comp );
266  }
267 
268  // -------------------------------------------------------------------------
269  // Iterators
270  // -------------------------------------------------------------------------
271 
275  iterator begin();
276 
280  iterator end();
281 
285  const_iterator begin() const;
286 
290  const_iterator end() const;
291 
295  const_iterator cbegin() const;
296 
300  const_iterator cend() const;
301 
302  // -------------------------------------------------------------------------
303  // Internal Implementation Details
304  // -------------------------------------------------------------------------
305 
306 protected:
307 
321  virtual Taxon& add_child_( Taxon const& child, bool merge_duplicates );
322 
335  void reset_parent_pointers_( Taxon* parent );
336 
337  // -------------------------------------------------------------------------
338  // Data Members
339  // -------------------------------------------------------------------------
340 
341 private:
342 
343  std::list<Taxon> children_;
344 };
345 
346 } // namespace taxonomy
347 } // namespace genesis
348 
349 #endif // include guard
genesis::taxonomy::Taxonomy::remove_at
void remove_at(size_t index)
Remove a child Taxon at a certain index.
Definition: taxonomy.cpp:213
genesis::taxonomy::Taxonomy::remove_child
void remove_child(std::string const &name)
Remove a child Taxon with a certain name.
Definition: taxonomy.cpp:192
genesis::taxonomy::Taxonomy::const_iterator
std::list< Taxon >::const_iterator const_iterator
Definition: taxonomy/taxonomy.hpp:105
genesis::taxonomy::Taxonomy::operator=
Taxonomy & operator=(Taxonomy const &)
Copy assigment operator.
Definition: taxonomy.cpp:57
genesis::taxonomy::Taxon
Store a Taxon, i.e., an element in a Taxonomy, with its name, rank, ID and sub-taxa.
Definition: taxon.hpp:76
genesis::taxonomy::Taxonomy::reset_parent_pointers_
void reset_parent_pointers_(Taxon *parent)
Internal helper function that resets the parent pointer of all stored Taxa.
Definition: taxonomy.cpp:293
genesis::taxonomy::Taxonomy::index_of
size_t index_of(std::string const &name) const
Definition: taxonomy.cpp:164
genesis::taxonomy::Taxonomy::has_child
bool has_child(std::string name) const
Return whether an immediate child Taxon with the given name exists.
Definition: taxonomy.cpp:89
genesis::taxonomy::Taxonomy::add_child
Taxon & add_child(Taxon const &child, bool merge_duplicates=true)
Add a child Taxon as a copy of a given Taxon and return it.
Definition: taxonomy.cpp:182
genesis::taxonomy::Taxonomy::~Taxonomy
virtual ~Taxonomy()=default
genesis::taxonomy::Taxonomy::sort
void sort(Compare comp)
Sort the taxonomy according to some compare criterion.
Definition: taxonomy/taxonomy.hpp:263
genesis::taxonomy::Taxonomy::operator[]
Taxon const & operator[](std::string name) const
Return the child Taxon with a given name if it exists, or throw otherwise.
Definition: taxonomy.cpp:120
genesis::taxonomy::swap
void swap(Taxon &lhs, Taxon &rhs)
Definition: taxon.cpp:111
genesis::taxonomy::Taxonomy::end
iterator end()
Return an iterator to the end of the child taxa.
Definition: taxonomy.cpp:238
genesis::taxonomy::Taxonomy::begin
iterator begin()
Return an iterator to the beginning of the child taxa.
Definition: taxonomy.cpp:233
genesis::taxonomy::Taxonomy::cbegin
const_iterator cbegin() const
Return a const iterator to the beginning of the child taxa.
Definition: taxonomy.cpp:253
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::taxonomy::Taxonomy
Store a Taxonomy, i.e., a nested hierarchy of Taxa.
Definition: taxonomy/taxonomy.hpp:96
genesis::placement::merge_duplicates
void merge_duplicates(Sample &smp)
Look for Pqueries with the same name and merge them.
Definition: placement/function/functions.cpp:569
genesis::taxonomy::Taxonomy::add_child_
virtual Taxon & add_child_(Taxon const &child, bool merge_duplicates)
Virtual implementation function for adding a child Taxon.
Definition: taxonomy.cpp:267
genesis::taxonomy::Taxonomy::size
size_t size() const
Return the number of immediate child Taxa.
Definition: taxonomy.cpp:84
genesis::taxonomy::Taxonomy::Taxonomy
Taxonomy()=default
genesis::taxonomy::Taxonomy::get_child
Taxon const & get_child(std::string name) const
Return the child Taxon with a given name if it exists, or throw otherwise.
Definition: taxonomy.cpp:100
genesis::taxonomy::Taxonomy::cend
const_iterator cend() const
Return a const iterator to the end of the child taxa.
Definition: taxonomy.cpp:258
genesis::taxonomy::Taxonomy::iterator
std::list< Taxon >::iterator iterator
Definition: taxonomy/taxonomy.hpp:104
genesis::taxonomy::Taxonomy::clear_children
void clear_children()
Remove all children.
Definition: taxonomy.cpp:224
genesis::taxonomy::Taxonomy::at
Taxon const & at(size_t index) const
Return the child Taxon at the given index.
Definition: taxonomy.cpp:130
genesis::taxonomy::Taxonomy::swap
friend void swap(Taxonomy &lhs, Taxonomy &rhs)
Swapperator for Taxonomy.
Definition: taxonomy.cpp:74