A library for working with phylogenetic and population genetic data.
v0.27.0
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-2020 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 
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  virtual ~NexusTrees() override = 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 const& 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 const& name, std::string const& 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, std::vector<std::string>{}, tree });
188  // entries_.emplace_back(name, {}, tree);
189  }
190 
191  void erase_tree( std::string const& 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
algorithm.hpp
Provides some valuable algorithms that are not part of the C++ 11 STL.
genesis::utils::NexusTrees::Entry
Definition: trees.hpp:71
genesis::utils::NexusTrees::Entry::tree
std::string tree
Definition: trees.hpp:75
genesis::utils::NexusBlock
Definition: block.hpp:47
genesis::utils::NexusTrees::clear
void clear()
Definition: trees.hpp:201
genesis::utils::NexusTrees::end
const_iterator end() const
Definition: trees.hpp:163
genesis::utils::NexusTrees
Definition: trees.hpp:62
genesis::utils::NexusTrees::container
std::vector< Entry > container
Definition: trees.hpp:78
block.hpp
genesis::utils::NexusTrees::NexusTrees
NexusTrees()=default
genesis::utils::NexusTrees::block_name
std::string block_name() const override
Definition: trees.hpp:212
genesis::utils::NexusTrees::operator=
NexusTrees & operator=(NexusTrees const &)=default
genesis::utils::NexusTrees::begin
const_iterator begin() const
Definition: trees.hpp:158
genesis::utils::NexusTrees::has_tree
bool has_tree(std::string const &name) const
Definition: trees.hpp:134
genesis::utils::erase_if
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:107
genesis::utils::NexusTrees::erase_tree
void erase_tree(std::string const &name)
Definition: trees.hpp:191
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::NexusTrees::size
size_t size() const
Definition: trees.hpp:129
genesis::utils::NexusTrees::empty
bool empty() const
Definition: trees.hpp:124
genesis::utils::NexusTrees::~NexusTrees
virtual ~NexusTrees() override=default
genesis::utils::swap
void swap(NexusTrees &lhs, NexusTrees &rhs)
Definition: trees.hpp:242
genesis::utils::NexusTrees::const_iterator
container::const_iterator const_iterator
Definition: trees.hpp:79
genesis::utils::NexusTrees::to_stream
void to_stream(std::ostream &os) const override
Definition: trees.hpp:217
genesis::utils::NexusTrees::Entry::properties
std::vector< std::string > properties
Definition: trees.hpp:74
genesis::utils::swap
void swap(Optional< T > &x, Optional< T > &y)
Definition: optional.hpp:566
genesis::utils::NexusTrees::swap
void swap(NexusTrees &other)
Definition: trees.hpp:96
genesis::utils::NexusTrees::Entry::name
std::string name
Definition: trees.hpp:73
genesis::utils::NexusTrees::add_tree
void add_tree(std::string const &name, std::string const &tree)
Definition: trees.hpp:174