A toolkit for working with phylogenetic data.
v0.24.0
taxa.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_FORMATS_NEXUS_TAXA_H_
2 #define GENESIS_UTILS_FORMATS_NEXUS_TAXA_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 
36 
37 #include <algorithm>
38 #include <ostream>
39 #include <stdexcept>
40 #include <string>
41 #include <vector>
42 
43 namespace genesis {
44 namespace utils {
45 
46 // =================================================================================================
47 // Forward Declarations
48 // =================================================================================================
49 
50 class NexusTaxa;
51 bool operator == ( NexusTaxa const& lhs, NexusTaxa const& rhs );
52 bool operator != ( NexusTaxa const& lhs, NexusTaxa const& rhs );
53 
54 // =================================================================================================
55 // Nexus Block Taxa
56 // =================================================================================================
57 
61 class NexusTaxa : public NexusBlock
62 {
63 
64  // -----------------------------------------------------
65  // Member Types
66  // -----------------------------------------------------
67 
68 public:
69 
70  using container = std::vector<std::string>;
71  using const_iterator = container::const_iterator;
72 
73  // -----------------------------------------------------
74  // Constructor and Rule of Five
75  // -----------------------------------------------------
76 
77 public:
78 
79  NexusTaxa() = default;
80  virtual ~NexusTaxa() override = default;
81 
82  NexusTaxa( NexusTaxa const& ) = default;
83  NexusTaxa( NexusTaxa&& ) = default;
84 
85  NexusTaxa& operator= ( NexusTaxa const& ) = default;
86  NexusTaxa& operator= ( NexusTaxa&& ) = default;
87 
88  void swap( NexusTaxa& other )
89  {
90  using std::swap;
91  swap( taxa_, other.taxa_ );
92  }
93 
94  // -----------------------------------------------------
95  // Operators
96  // -----------------------------------------------------
97 
98 public:
99 
100  friend bool operator == ( NexusTaxa const& lhs, NexusTaxa const& rhs )
101  {
102  return lhs.taxa_ == rhs.taxa_;
103  }
104 
105  friend bool operator != ( NexusTaxa const& lhs, NexusTaxa const& rhs )
106  {
107  return !(lhs == rhs);
108  }
109 
110  // -----------------------------------------------------
111  // Accessors
112  // -----------------------------------------------------
113 
114 public:
115 
116  bool empty() const
117  {
118  return taxa_.empty();
119  }
120 
121  size_t size() const
122  {
123  return taxa_.size();
124  }
125 
126  bool has_taxon( std::string const& name ) const
127  {
128  return utils::contains( taxa_, name );
129  }
130 
131  // -----------------------------------------------------
132  // Iterators
133  // -----------------------------------------------------
134 
135 public:
136 
138  {
139  return taxa_.cbegin();
140  }
141 
143  {
144  return taxa_.cend();
145  }
146 
147  // -----------------------------------------------------
148  // Modifiers
149  // -----------------------------------------------------
150 
151 public:
152 
153  void add_taxon( std::string const& name )
154  {
155  insert_sorted( taxa_, name );
156  }
157 
158  void add_taxa( std::vector<std::string> const& taxa )
159  {
160  // for( auto const& taxon : taxa ) {
161  // add_taxon( taxon );
162  // }
163 
164  // Faster: Don't move on every insertion, and only do the sorting once.
165  taxa_.insert( taxa_.end(), taxa.begin(), taxa.end() );
166  std::sort( taxa_.begin(), taxa_.end() );
167  }
168 
169  void clear()
170  {
171  taxa_.clear();
172  }
173 
174  // -----------------------------------------------------
175  // Virtual Functions
176  // -----------------------------------------------------
177 
178 public:
179 
180  std::string block_name() const override
181  {
182  return "TAXA";
183  }
184 
185  void to_stream( std::ostream& os ) const override
186  {
187  os << " dimensions ntax=" << taxa_.size() << ";\n";
188  os << " taxlabels\n";
189  for( auto& taxon : taxa_ ) {
190  os << " " << taxon << "\n";
191  }
192  os << " ;\n";
193  }
194 
195  // -----------------------------------------------------
196  // Data Members
197  // -----------------------------------------------------
198 
199 private:
200 
201  container taxa_;
202 
203 };
204 
205 // =================================================================================================
206 // Operators
207 // =================================================================================================
208 
209 inline void swap( NexusTaxa& lhs, NexusTaxa& rhs )
210 {
211  lhs.swap(rhs);
212 }
213 
214 } // namespace utils
215 } // namespace genesis
216 
217 #endif // include guard
Provides some valuable algorithms that are not part of the C++ 11 STL.
friend bool operator!=(NexusTaxa const &lhs, NexusTaxa const &rhs)
Definition: taxa.hpp:105
bool empty() const
Definition: taxa.hpp:116
virtual ~NexusTaxa() override=default
const_iterator end() const
Definition: taxa.hpp:142
const_iterator begin() const
Definition: taxa.hpp:137
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
void add_taxon(std::string const &name)
Definition: taxa.hpp:153
bool contains(const C &v, const T &x)
Returns whether a container object has a certain element.
Definition: algorithm.hpp:74
void swap(NexusTaxa &lhs, NexusTaxa &rhs)
Definition: taxa.hpp:209
NexusTaxa & operator=(NexusTaxa const &)=default
std::string block_name() const override
Definition: taxa.hpp:180
size_t size() const
Definition: taxa.hpp:121
bool operator==(JsonDocument::const_reference lhs, JsonDocument::const_reference rhs)
void add_taxa(std::vector< std::string > const &taxa)
Definition: taxa.hpp:158
container::const_iterator const_iterator
Definition: taxa.hpp:71
void swap(NexusTaxa &other)
Definition: taxa.hpp:88
bool has_taxon(std::string const &name) const
Definition: taxa.hpp:126
std::vector< std::string > container
Definition: taxa.hpp:70
void to_stream(std::ostream &os) const override
Definition: taxa.hpp:185
friend bool operator==(NexusTaxa const &lhs, NexusTaxa const &rhs)
Definition: taxa.hpp:100
bool operator!=(JsonDocument const &lhs, JsonDocument const &rhs)
std::vector< T >::iterator insert_sorted(std::vector< T > &vec, T const &item)
Insert into a vector vec, sorted by the value of the item. The vector must already be sorted...
Definition: algorithm.hpp:152