A library for working with phylogenetic and population genetic data.
v0.32.0
window.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_POPULATION_WINDOW_WINDOW_H_
2 #define GENESIS_POPULATION_WINDOW_WINDOW_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2024 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 <lczech@carnegiescience.edu>
23  Department of Plant Biology, Carnegie Institution For Science
24  260 Panama Street, Stanford, CA 94305, USA
25 */
26 
34 #include <cassert>
35 #include <deque>
36 #include <stdexcept>
37 #include <string>
38 #include <vector>
39 
42 
43 namespace genesis {
44 namespace population {
45 
46 // =================================================================================================
47 // Auxiliary Structures
48 // =================================================================================================
49 
58 {};
59 
60 // =================================================================================================
61 // Genomic Window
62 // =================================================================================================
63 
104 template<class D, class A = EmptyAccumulator>
105 class Window final : public BaseWindow<D>
106 {
107 public:
108 
109  // -------------------------------------------------------------------------
110  // Typedefs and Enums
111  // -------------------------------------------------------------------------
112 
113  using Data = D;
114  using Accumulator = A;
115 
125  struct Entry
126  {
130  Entry( size_t index, size_t position, Data const& data )
131  : index(index)
132  , position(position)
133  , data(data)
134  {}
135 
140  Entry( size_t index, size_t position, Data&& data )
141  : index(index)
142  , position(position)
143  , data(std::move( data ))
144  {}
145 
151  operator Data&()
152  {
153  return data;
154  }
155 
161  operator Data const&() const
162  {
163  return data;
164  }
165 
173  size_t index;
174 
182  size_t position;
183 
188  };
189 
191  using container = std::deque<Entry>;
192 
193  using value_type = Entry;
195  using const_reference = value_type const&;
196 
197  using iterator = typename container::iterator;
198  using const_iterator = typename container::const_iterator;
199  using reverse_iterator = typename container::reverse_iterator;
200  using const_reverse_iterator = typename container::const_reverse_iterator;
201 
202  using difference_type = typename container::difference_type;
203  using size_type = typename container::size_type;
204 
205  // -------------------------------------------------------------------------
206  // Constructors and Rule of Five
207  // -------------------------------------------------------------------------
208 
209  Window() = default;
210  virtual ~Window() override = default;
211 
212  Window( Window const& ) = default;
213  Window( Window&& ) = default;
214 
215  Window& operator= ( Window const& ) = default;
216  Window& operator= ( Window&& ) = default;
217 
218  // -------------------------------------------------------------------------
219  // General Properties
220  // -------------------------------------------------------------------------
221 
227  size_t entry_count() const
228  {
229  return entries_.size();
230  }
231 
237  size_t size() const
238  {
239  return entries_.size();
240  }
241 
246  bool empty() const
247  {
248  return entries_.empty();
249  }
250 
254  double saturation() const
255  {
256  double const es = static_cast<double>( entries_.size() );
257  double const wd = static_cast<double>( this->width() );
258  double const frac = es / wd;
259 
260  assert( this->width() > 0 );
261  assert( frac >= 0.0 );
262  assert( frac <= 1.0 );
263  return frac;
264  }
265 
277  size_t span() const
278  {
279  if( entries_.empty() ) {
280  return 0;
281  }
282  return entries_.back().position - entries_.front().position + 1;
283  }
284 
285  // -------------------------------------------------------------------------
286  // Data Accessors
287  // -------------------------------------------------------------------------
288 
294  reference operator[]( size_t index )
295  {
296  assert( index < entries_.size() );
297  return entries_[ index ];
298  }
299 
305  const_reference operator[]( size_t index ) const
306  {
307  assert( index < entries_.size() );
308  return entries_[ index ];
309  }
310 
314  reference at( size_t index )
315  {
316  return entries_.at( index );
317  }
318 
322  const_reference at( size_t index ) const
323  {
324  return entries_.at( index );
325  }
326 
331  {
332  return entries_.begin();
333  }
334 
339  {
340  return entries_.begin();
341  }
342 
347  {
348  return entries_.end();
349  }
350 
355  {
356  return entries_.end();
357  }
358 
362  container const& entries() const
363  {
364  return entries_;
365  }
366 
371  {
372  return entries_;
373  }
374 
379  {
380  return accumulator_;
381  }
382 
383 
387  Accumulator const& accumulator() const
388  {
389  return accumulator_;
390  }
391 
392  // -------------------------------------------------------------------------
393  // Modifiers and Helpers
394  // -------------------------------------------------------------------------
395 
403  void validate() const
404  {
405  if( this->first_position() == 0 ) {
406  throw std::runtime_error( "Invalid Window with first_position() == 0." );
407  }
408  if( this->last_position() < this->first_position() ) {
409  throw std::runtime_error( "Invalid Window with last_position() < first_position()." );
410  }
411  for( auto const& entry : entries_ ) {
412  if( entry.position < this->first_position() || entry.position > this->last_position() ) {
413  throw std::runtime_error(
414  "Invalid Window::Entry in chromosome " + this->chromosome() + " at position " +
415  std::to_string( entry.position ) +
416  ", which is not between the window boundaries [" +
417  std::to_string( this->first_position() ) + "," +
418  std::to_string( this->last_position() ) + "]."
419  );
420  }
421  }
422  }
423 
424  // -------------------------------------------------------------------------
425  // Virtual Members
426  // -------------------------------------------------------------------------
427 
428 private:
429 
430  virtual void clear_() override
431  {
432  accumulator_ = Accumulator{};
433  entries_ = container{};
434  }
435 
436  // -------------------------------------------------------------------------
437  // Data Members
438  // -------------------------------------------------------------------------
439 
440 private:
441 
442  Accumulator accumulator_;
443  container entries_;
444 
445 };
446 
447 } // namespace population
448 } // namespace genesis
449 
450 #endif // include guard
genesis::population::Window::accumulator
Accumulator & accumulator()
Get the Accumulator data that can be used for speeding up certain window computations.
Definition: window.hpp:378
genesis::population::Window::operator[]
reference operator[](size_t index)
Return a reference to the element at specified location index.
Definition: window.hpp:294
genesis::population::Window::Window
Window()=default
genesis::population::Window::at
const_reference at(size_t index) const
Return a reference to the element at specified location pos, with bounds checking.
Definition: window.hpp:322
base_window.hpp
genesis::population::Window
Window over the chromosomes of a genome.
Definition: window.hpp:105
genesis::population::Window::Entry::position
size_t position
Genomic position (1-based) of the entry along a chromosome.
Definition: window.hpp:182
genesis::population::Window::validate
void validate() const
Validate the window data.
Definition: window.hpp:403
genesis::population::Window::Entry::index
size_t index
Index of the entry, that is, how many other entries have there been in total in the underlying data f...
Definition: window.hpp:173
genesis::population::Window::entry_count
size_t entry_count() const
Get the number of D/Data Entries that are stored in the Window.
Definition: window.hpp:227
genesis::population::Window::size
size_t size() const
Get the number of D/Data Entries that are stored in the Window.
Definition: window.hpp:237
genesis::population::Window::~Window
virtual ~Window() override=default
genesis::population::Window::accumulator
Accumulator const & accumulator() const
Get the Accumulator data that can be used for speeding up certain window computations.
Definition: window.hpp:387
genesis::population::to_string
std::string to_string(GenomeLocus const &locus)
Definition: function/genome_locus.hpp:52
genome_region.hpp
genesis::population::Window::Entry::Entry
Entry(size_t index, size_t position, Data const &data)
Contructor that takes data by reference.
Definition: window.hpp:130
genesis::population::Window::Entry::data
Data data
Data stored in the Window for this entry.
Definition: window.hpp:187
genesis::population::Window::end
iterator end()
Iterator to the end of the Data Entries.
Definition: window.hpp:346
genesis::population::Window< DataType >::const_reverse_iterator
typename container::const_reverse_iterator const_reverse_iterator
Definition: window.hpp:200
genesis::population::BaseWindow< DataType >::Data
DataType Data
Definition: base_window.hpp:68
genesis::population::Window< DataType >::reference
value_type & reference
Definition: window.hpp:194
genesis::population::BaseWindow::width
size_t width() const
Get the width of the Window.
Definition: base_window.hpp:173
genesis::population::Window< DataType >::value_type
Entry value_type
Definition: window.hpp:193
genesis::population::BaseWindow::chromosome
std::string const & chromosome() const
Get the chromosome name that this Window belongs to.
Definition: base_window.hpp:90
genesis::population::Window::empty
bool empty() const
Return whether the Window is empty, that is, if it does not contain any Entries.
Definition: window.hpp:246
genesis::population::Window::entries
container const & entries() const
Immediate container access to the Data Entries.
Definition: window.hpp:362
genesis::population::Window::span
size_t span() const
Get the distance that is spanned by the first and the last variant (entry) in the Window,...
Definition: window.hpp:277
genesis::population::Window::operator[]
const_reference operator[](size_t index) const
Return a reference to the element at specified location index.
Definition: window.hpp:305
genesis::population::EmptyAccumulator
Empty helper data struct to serve as a dummy for Window.
Definition: window.hpp:57
genesis::population::Window< DataType >::size_type
typename container::size_type size_type
Definition: window.hpp:203
genesis::population::Window< DataType >::container
std::deque< Entry > container
Definition: window.hpp:191
genesis::population::Window::Accumulator
A Accumulator
Definition: window.hpp:114
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::population::Window::Entry
Data that is stored per entry that was enqueued in a window.
Definition: window.hpp:125
genesis::population::Window::Entry::Entry
Entry(size_t index, size_t position, Data &&data)
Contructor that takes data by r-value reference (i.e., moved data); preferred if possible to use,...
Definition: window.hpp:140
genesis::population::Window::at
reference at(size_t index)
Return a reference to the element at specified location pos, with bounds checking.
Definition: window.hpp:314
genesis::population::Window< DataType >::difference_type
typename container::difference_type difference_type
Definition: window.hpp:202
genesis::population::Window::entries
container & entries()
Immediate container access to the Data Entries.
Definition: window.hpp:370
genesis::population::Window::begin
const_iterator begin() const
Const iterator to the begin of the Data Entries.
Definition: window.hpp:338
genesis::population::Window::end
const_iterator end() const
Const iterator to the end of the Data Entries.
Definition: window.hpp:354
genesis::population::Window::operator=
Window & operator=(Window const &)=default
genesis::population::Window< DataType >::const_iterator
typename container::const_iterator const_iterator
Definition: window.hpp:198
genesis::population::BaseWindow::first_position
size_t first_position() const
Get the first position in the chromosome of the Window, that is, where the Window starts.
Definition: base_window.hpp:114
genesis::population::Window::begin
iterator begin()
Iterator to the begin of the Data Entries.
Definition: window.hpp:330
genesis::population::BaseWindow::last_position
size_t last_position() const
Get the last position in the chromosome of the Window, that is, where the Window ends.
Definition: base_window.hpp:134
genesis::population::BaseWindow
Base class for Window and WindowView, to share common functionality.
Definition: base_window.hpp:60
genesis::population::Window< DataType >::reverse_iterator
typename container::reverse_iterator reverse_iterator
Definition: window.hpp:199
genesis::population::Window::saturation
double saturation() const
Get the fraction of entries to window width.
Definition: window.hpp:254
genesis::population::Window< DataType >::iterator
typename container::iterator iterator
Definition: window.hpp:197
genesis::population::Window< DataType >::const_reference
value_type const & const_reference
Definition: window.hpp:195