A toolkit for working with phylogenetic data.
v0.18.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-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 
35 
36 #include <ostream>
37 #include <stdexcept>
38 #include <string>
39 #include <unordered_set>
40 #include <vector>
41 
42 namespace genesis {
43 namespace utils {
44 
45 // =================================================================================================
46 // Forward Declarations
47 // =================================================================================================
48 
49 class NexusTaxa;
50 bool operator == ( NexusTaxa const& lhs, NexusTaxa const& rhs );
51 bool operator != ( NexusTaxa const& lhs, NexusTaxa const& rhs );
52 
53 // =================================================================================================
54 // Nexus Block Taxa
55 // =================================================================================================
56 
60 class NexusTaxa : public NexusBlock
61 {
62 
63  // -----------------------------------------------------
64  // Member Types
65  // -----------------------------------------------------
66 
67 public:
68 
69  using container = std::unordered_set<std::string>;
70  using const_iterator = container::const_iterator;
71 
72  // -----------------------------------------------------
73  // Constructor and Rule of Five
74  // -----------------------------------------------------
75 
76 public:
77 
78  NexusTaxa() = default;
79  ~NexusTaxa() = default;
80 
81  NexusTaxa( NexusTaxa const& ) = default;
82  NexusTaxa( NexusTaxa&& ) = default;
83 
84  NexusTaxa& operator= ( NexusTaxa const& ) = default;
85  NexusTaxa& operator= ( NexusTaxa&& ) = default;
86 
87  void swap( NexusTaxa& other )
88  {
89  using std::swap;
90  swap( taxa_, other.taxa_ );
91  }
92 
93  // -----------------------------------------------------
94  // Operators
95  // -----------------------------------------------------
96 
97 public:
98 
99  friend bool operator == ( NexusTaxa const& lhs, NexusTaxa const& rhs )
100  {
101  return lhs.taxa_ == rhs.taxa_;
102  }
103 
104  friend bool operator != ( NexusTaxa const& lhs, NexusTaxa const& rhs )
105  {
106  return !(lhs == rhs);
107  }
108 
109  // -----------------------------------------------------
110  // Accessors
111  // -----------------------------------------------------
112 
113 public:
114 
115  bool empty() const
116  {
117  return taxa_.empty();
118  }
119 
120  size_t size() const
121  {
122  return taxa_.size();
123  }
124 
125  bool has_taxon( std::string name ) const
126  {
127  return taxa_.count(name);
128  }
129 
130  // -----------------------------------------------------
131  // Iterators
132  // -----------------------------------------------------
133 
134 public:
135 
137  {
138  return taxa_.cbegin();
139  }
140 
142  {
143  return taxa_.cend();
144  }
145 
146  // -----------------------------------------------------
147  // Modifiers
148  // -----------------------------------------------------
149 
150 public:
151 
152  void add_taxon( std::string name )
153  {
154  taxa_.insert( name );
155  }
156 
157  void add_taxa( std::unordered_set<std::string> taxa )
158  {
159  taxa_.insert( taxa.begin(), taxa.end() );
160  }
161 
162  void erase_taxon( std::string name )
163  {
164  taxa_.erase( name );
165  }
166 
167  void clear()
168  {
169  taxa_.clear();
170  }
171 
172  // -----------------------------------------------------
173  // Virtual Functions
174  // -----------------------------------------------------
175 
176 public:
177 
178  std::string block_name() const override
179  {
180  return "TAXA";
181  }
182 
183  void to_stream( std::ostream& os ) const override
184  {
185  os << " dimensions ntax=" << taxa_.size() << ";\n";
186  os << " taxlabels\n";
187  for( auto& taxon : taxa_ ) {
188  os << " " << taxon << "\n";
189  }
190  os << " ;\n";
191  }
192 
193  // -----------------------------------------------------
194  // Data Members
195  // -----------------------------------------------------
196 
197 private:
198 
199  container taxa_;
200 
201 };
202 
203 // =================================================================================================
204 // Operators
205 // =================================================================================================
206 
207 inline void swap( NexusTaxa& lhs, NexusTaxa& rhs )
208 {
209  lhs.swap(rhs);
210 }
211 
212 } // namespace utils
213 } // namespace genesis
214 
215 #endif // include guard
friend bool operator!=(NexusTaxa const &lhs, NexusTaxa const &rhs)
Definition: taxa.hpp:104
size_t size() const
Definition: taxa.hpp:120
bool empty() const
Definition: taxa.hpp:115
bool operator!=(SortedVector< T > const &lhs, SortedVector< T > const &rhs)
bool has_taxon(std::string name) const
Definition: taxa.hpp:125
void swap(NexusTaxa &lhs, NexusTaxa &rhs)
Definition: taxa.hpp:207
NexusTaxa & operator=(NexusTaxa const &)=default
std::string block_name() const override
Definition: taxa.hpp:178
void add_taxon(std::string name)
Definition: taxa.hpp:152
bool operator==(SortedVector< T > const &lhs, SortedVector< T > const &rhs)
container::const_iterator const_iterator
Definition: taxa.hpp:70
void erase_taxon(std::string name)
Definition: taxa.hpp:162
const_iterator end() const
Definition: taxa.hpp:141
void swap(NexusTaxa &other)
Definition: taxa.hpp:87
void to_stream(std::ostream &os) const override
Definition: taxa.hpp:183
void add_taxa(std::unordered_set< std::string > taxa)
Definition: taxa.hpp:157
friend bool operator==(NexusTaxa const &lhs, NexusTaxa const &rhs)
Definition: taxa.hpp:99
std::unordered_set< std::string > container
Definition: taxa.hpp:69
const_iterator begin() const
Definition: taxa.hpp:136