A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
taxonomy/functions/operators.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TAXONOMY_FUNCTIONS_OPERATORS_H_
2 #define GENESIS_TAXONOMY_FUNCTIONS_OPERATORS_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 
37 
38 #include <stdexcept>
39 
40 namespace genesis {
41 namespace taxonomy {
42 
43 // =================================================================================================
44 // Data Types
45 // =================================================================================================
46 
53 template< class TaxonDataType >
54 bool taxonomy_data_is( Taxonomy const& taxonomy )
55 {
56  // Slow, because we traverse the whole thing even if the first one is already false...
57  // Works for now.
58  bool correct = true;
59  preorder_for_each( taxonomy, [&] ( Taxon const& taxon ) {
60  if( ! taxon.has_data() ) {
61  correct = false;
62  }
63  auto const& ref = *taxon.data_ptr();
64  if( typeid( ref ) != typeid( TaxonDataType ) ) {
65  correct = false;
66  }
67  });
68  return correct;
69 }
70 
77 template< class TaxonDataType >
78 bool taxonomy_data_is_derived_from( Taxonomy const& taxonomy )
79 {
80  // Slow, because we traverse the whole thing even if the first one is already false...
81  // Works for now.
82  bool correct = true;
83  preorder_for_each( taxonomy, [&] ( Taxon const& taxon ) {
84  if( dynamic_cast< TaxonDataType const* >( taxon.data_ptr() ) == nullptr ) {
85  return false;
86  }
87  });
88  return correct;
89 }
90 
98 template< class TaxonDataType >
99 void reset_taxonomy_data( Taxonomy& taxonomy, bool allow_overwrite = true )
100 {
101  preorder_for_each( taxonomy, [&] ( Taxon& taxon ) {
102  if( taxon.has_data() && ! allow_overwrite ) {
103  throw std::runtime_error(
104  "Trying to overwrite Taxonomy data that already has a value."
105  );
106  }
107  taxon.reset_data(
108  TaxonDataType::create()
109  );
110  });
111 }
112 
113 } // namespace taxonomy
114 } // namespace genesis
115 
116 #endif // include guard
bool has_data() const
Return true if the Taxon has a data object assigned to it.
Definition: taxon.cpp:187
Store a Taxonomy, i.e., a nested hierarchy of Taxa.
void preorder_for_each(Taxonomy &tax, std::function< void(Taxon &)> fn, bool include_inner_taxa=true)
Apply a function to all taxa of the Taxonomy, traversing it in preorder.
bool taxonomy_data_is_derived_from(Taxonomy const &taxonomy)
Check whether the data of a Taxonomy are derived from the specified data type.
void reset_taxonomy_data(Taxonomy &taxonomy, bool allow_overwrite=true)
(Re-)set all Taxon data of a Taxonomy to a specified data type.
bool taxonomy_data_is(Taxonomy const &taxonomy)
Check whether the data of a Taxonomy are exactly of the specified data type.
Store a Taxon, i.e., an element in a Taxonomy, with its name, rank, ID and sub-taxa.
Definition: taxon.hpp:76
Taxon & reset_data(std::unique_ptr< BaseTaxonData > data)
Reset the data pointer of this Taxon.
Definition: taxon.cpp:202
BaseTaxonData * data_ptr()
Return a pointer to the data.
Definition: taxon.cpp:192