A toolkit for working with phylogenetic data.
v0.18.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
trees.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_FORMATS_NEXUS_TREES_H_
2 #define GENESIS_UTILS_FORMATS_NEXUS_TREES_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 
37 
38 #include <algorithm>
39 #include <ostream>
40 #include <stdexcept>
41 #include <string>
42 #include <vector>
43 
44 namespace genesis {
45 namespace utils {
46 
47 // =================================================================================================
48 // Forward Declarations
49 // =================================================================================================
50 
51 // class NexusTrees;
52 // bool operator == ( NexusTrees const& lhs, NexusTrees const& rhs );
53 // bool operator != ( NexusTrees const& lhs, NexusTrees const& rhs );
54 
55 // =================================================================================================
56 // Nexus Block Trees
57 // =================================================================================================
58 
62 class NexusTrees : public NexusBlock
63 {
64 
65  // -----------------------------------------------------
66  // Member Types
67  // -----------------------------------------------------
68 
69 public:
70 
71  struct Entry
72  {
73  std::string name;
74  std::vector<std::string> properties;
75  std::string tree;
76  };
77 
78  using container = std::vector<Entry>;
79  using const_iterator = container::const_iterator;
80 
81  // -----------------------------------------------------
82  // Constructor and Rule of Five
83  // -----------------------------------------------------
84 
85 public:
86 
87  NexusTrees() = default;
88  ~NexusTrees() = default;
89 
90  NexusTrees( NexusTrees const& ) = default;
91  NexusTrees( NexusTrees&& ) = default;
92 
93  NexusTrees& operator= ( NexusTrees const& ) = default;
94  NexusTrees& operator= ( NexusTrees&& ) = default;
95 
96  void swap( NexusTrees& other )
97  {
98  using std::swap;
99  swap( entries_, other.entries_ );
100  }
101 
102  // -----------------------------------------------------
103  // Operators
104  // -----------------------------------------------------
105 
106 public:
107 
108  // friend bool operator == ( NexusTrees const& lhs, NexusTrees const& rhs )
109  // {
110  // return lhs.entries_ == rhs.entries_;
111  // }
112  //
113  // friend bool operator != ( NexusTrees const& lhs, NexusTrees const& rhs )
114  // {
115  // return !(lhs == rhs);
116  // }
117 
118  // -----------------------------------------------------
119  // Accessors
120  // -----------------------------------------------------
121 
122 public:
123 
124  bool empty() const
125  {
126  return entries_.empty();
127  }
128 
129  size_t size() const
130  {
131  return entries_.size();
132  }
133 
134  bool has_tree( std::string name ) const
135  {
136  return entries_.end() != std::find_if(
137  entries_.begin(),
138  entries_.end(),
139  [&name] (Entry const& entry) {
140  return entry.name == name;
141  }
142  );
143 
144  // for( auto& entry : entries_ ) {
145  // if( entry.name == name ) {
146  // return true;
147  // }
148  // }
149  // return false;
150  }
151 
152  // -----------------------------------------------------
153  // Iterators
154  // -----------------------------------------------------
155 
156 public:
157 
159  {
160  return entries_.cbegin();
161  }
162 
164  {
165  return entries_.cend();
166  }
167 
168  // -----------------------------------------------------
169  // Modifiers
170  // -----------------------------------------------------
171 
172 public:
173 
174  void add_tree( std::string name, std::string tree )
175  {
176  if( has_tree(name) ) {
177  throw std::invalid_argument(
178  "Tree with name '" + name + "' already exists in this tree block."
179  );
180  }
181 
182  // auto e = Entry();
183  // e.name = name;
184  // e.tree = tree;
185  // entries_.push_back(e);
186 
187  entries_.push_back({ name, {}, tree });
188  // entries_.emplace_back(name, {}, tree);
189  }
190 
191  void erase_tree( std::string name )
192  {
194  entries_,
195  [&name] (Entry const& entry) {
196  return entry.name == name;
197  }
198  );
199  }
200 
201  void clear()
202  {
203  entries_.clear();
204  }
205 
206  // -----------------------------------------------------
207  // Virtual Functions
208  // -----------------------------------------------------
209 
210 public:
211 
212  std::string block_name() const override
213  {
214  return "TREES";
215  }
216 
217  void to_stream( std::ostream& os ) const override
218  {
219  for( auto& entry : entries_ ) {
220  os << " TREE " << entry.name << " = ";
221  for( auto& prop : entry.properties ) {
222  os << "[" << prop << "] ";
223  }
224  os << entry.tree << "\n";
225  }
226  }
227 
228  // -----------------------------------------------------
229  // Data Members
230  // -----------------------------------------------------
231 
232 private:
233 
234  container entries_;
235 
236 };
237 
238 // =================================================================================================
239 // Operators
240 // =================================================================================================
241 
242 inline void swap( NexusTrees& lhs, NexusTrees& rhs )
243 {
244  lhs.swap(rhs);
245 }
246 
247 } // namespace utils
248 } // namespace genesis
249 
250 #endif // include guard
void add_tree(std::string name, std::string tree)
Definition: trees.hpp:174
container::const_iterator const_iterator
Definition: trees.hpp:79
Provides some valuable algorithms that are not part of the C++ 11 STL.
std::string tree
Definition: trees.hpp:75
void erase_if(Container &c, UnaryPredicate p)
Erases all elements from the container that satisfy a given predicate. An element is erased...
Definition: algorithm.hpp:70
Definition: trees.hpp:71
NexusTrees & operator=(NexusTrees const &)=default
std::string name
Definition: trees.hpp:73
void swap(NexusTrees &other)
Definition: trees.hpp:96
bool empty() const
Definition: trees.hpp:124
void swap(NexusTrees &lhs, NexusTrees &rhs)
Definition: trees.hpp:242
bool has_tree(std::string name) const
Definition: trees.hpp:134
void swap(NexusTaxa &lhs, NexusTaxa &rhs)
Definition: taxa.hpp:207
void erase_tree(std::string name)
Definition: trees.hpp:191
size_t size() const
Definition: trees.hpp:129
std::vector< Entry > container
Definition: trees.hpp:78
std::vector< std::string > properties
Definition: trees.hpp:74
std::string block_name() const override
Definition: trees.hpp:212
void to_stream(std::ostream &os) const override
Definition: trees.hpp:217
const_iterator end() const
Definition: trees.hpp:163
const_iterator begin() const
Definition: trees.hpp:158