A toolkit for working with phylogenetic data.
v0.18.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
taxopath_generator.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2017 Lucas Czech
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
32 
37 
38 #include <algorithm>
39 #include <assert.h>
40 #include <stdexcept>
41 
42 namespace genesis {
43 namespace taxonomy {
44 
45 // =================================================================================================
46 // Generating
47 // =================================================================================================
48 
54 std::string TaxopathGenerator::to_string( Taxopath const& taxopath ) const
55 {
56  std::string res;
57 
58  if( trim_nested_duplicates_ ) {
59  // We are going to delete elements, so first make a copy.
60  auto cpy = taxopath.elements();
61 
62  // Delete element if the one before it is the same.
63  // Skip the first and the last one.
64  for( size_t i = cpy.size() - 1; i >= 1; --i ) {
65  if( cpy[i] == cpy[i-1] ) {
66  cpy[i] = "";
67  }
68  }
69  res = utils::join( cpy, delimiter_ );
70 
71  } else {
72  // Without trimming, the result is easy to create.
73  res = utils::join( taxopath.elements(), delimiter_ );
74  }
75 
76  if( append_delimiter_ ) {
77  return res + delimiter_;
78  } else {
79  return res;
80  }
81 }
82 
89 std::string TaxopathGenerator::operator() ( Taxopath const& taxopath ) const
90 {
91  return to_string( taxopath );
92 }
93 
99 std::string TaxopathGenerator::to_string( Taxon const& taxon ) const
100 {
101  // This implementation is probably not the fastest, but it is simple and kind of elegant.
102  // Start with an empty vector that will store the super-taxa of the given taxon.
103  std::vector<std::string> taxa;
104 
105  // Add taxa in reverse order: the deepest taxon will be stored first.
106  // This is fast with a vector.
107  Taxon const* r = &taxon;
108  while( r != nullptr ) {
109  taxa.push_back( r->name() );
110  r = r->parent();
111  }
112 
113  // If wanted, set all taxa to an empty string for which the super-taxon has the same name.
114  // As we stored them in reverse order, we can simply go from start to one-but-the-end and check
115  // for equality.
116  if( trim_nested_duplicates_ ) {
117  for( size_t i = 0; i < taxa.size() - 1; ++i ) {
118  if( taxa[i] == taxa[i+1] ) {
119  taxa[i] = "";
120  }
121  }
122  }
123 
124  // Now reverse and return the joined result.
125  std::reverse( taxa.begin(), taxa.end() );
126  auto res = utils::join( taxa, delimiter_ );
127 
128  if( append_delimiter_ ) {
129  return res + delimiter_;
130  } else {
131  return res;
132  }
133 }
134 
141 std::string TaxopathGenerator::operator() ( Taxon const& taxon ) const
142 {
143  return to_string( taxon );
144 }
145 
146 // =================================================================================================
147 // Properties
148 // =================================================================================================
149 
157 {
158  delimiter_ = value;
159  return *this;
160 }
161 
167 std::string TaxopathGenerator::delimiter() const
168 {
169  return delimiter_;
170 }
171 
187 {
188  trim_nested_duplicates_ = value;
189  return *this;
190 }
191 
198 {
199  return trim_nested_duplicates_;
200 }
201 
212 {
213  append_delimiter_ = value;
214  return *this;
215 }
216 
223 {
224  return append_delimiter_;
225 }
226 
227 } // namespace taxonomy
228 } // namespace genesis
Helper class to generate a taxonomic path string from a Taxopath object or a Taxon.
std::string const & name() const
Return the name of this taxon.
Definition: taxon.cpp:169
std::vector< std::string > const & elements() const
Return the elements of the Taxopath as a vector of strings.
Definition: taxopath.cpp:134
bool append_delimiter() const
Return whether currently a delimiter is appended to the taxonomic path string.
bool trim_nested_duplicates() const
Return the currently set value whether to trim nested duplicates of taxa names.
std::string join(T const &v, std::string const &delimiter)
Return a string where the elements of a container v are joined using the string delimiter in between ...
Definition: string.hpp:318
std::string delimiter() const
Return the currelty set value used to join the taxonomic path string elements.
std::string operator()(Taxopath const &taxopath) const
Shortcut function alias for to_string( Taxopath ).
Store a Taxon, i.e., an element in a Taxonomy, with its name, rank and sub-taxa.
Definition: taxon.hpp:76
Provides some commonly used string utility functions.
Helper class to store a taxonomic path.
Definition: taxopath.hpp:81
std::string to_string(Taxopath const &taxopath) const
Return a string representation of a Taxopath.
Taxon const * parent() const
Return a pointer to the parent of this taxon, or a nullptr if this is the top level taxon...
Definition: taxon.cpp:209