A library for working with phylogenetic and population genetic data.
v0.32.0
base_window.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_POPULATION_WINDOW_BASE_WINDOW_H_
2 #define GENESIS_POPULATION_WINDOW_BASE_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 <stdexcept>
35 #include <string>
36 #include <unordered_map>
37 
39 
40 namespace genesis {
41 namespace population {
42 
43 // =================================================================================================
44 // Genomic Window
45 // =================================================================================================
46 
59 template<class D>
61 {
62 public:
63 
64  // -------------------------------------------------------------------------
65  // Typedefs and Enums
66  // -------------------------------------------------------------------------
67 
68  using Data = D;
69 
70  // -------------------------------------------------------------------------
71  // Constructors and Rule of Five
72  // -------------------------------------------------------------------------
73 
74  BaseWindow() = default;
75  virtual ~BaseWindow() = default;
76 
77  BaseWindow( BaseWindow const& ) = default;
78  BaseWindow( BaseWindow&& ) = default;
79 
80  BaseWindow& operator= ( BaseWindow const& ) = default;
81  BaseWindow& operator= ( BaseWindow&& ) = default;
82 
83  // -------------------------------------------------------------------------
84  // Chromosome and Positions
85  // -------------------------------------------------------------------------
86 
90  std::string const& chromosome() const
91  {
92  return chromosome_;
93  }
94 
98  void chromosome( std::string const& value )
99  {
100  chromosome_ = value;
101  }
102 
114  size_t first_position() const
115  {
116  return first_position_;
117  }
118 
124  void first_position( size_t value )
125  {
126  first_position_ = value;
127  }
128 
134  size_t last_position() const
135  {
136  return last_position_;
137  }
138 
144  void last_position( size_t value )
145  {
146  last_position_ = value;
147  }
148 
156  {
157  return { chromosome_, first_position_, last_position_ };
158  }
159 
173  size_t width() const
174  {
175  // We need to do the check here, when this is used.
176  if( first_position_ > last_position_ ) {
177  throw std::runtime_error(
178  "Invalidly set first and last position in the Window, with " +
179  std::to_string( first_position_ ) + " > " + std::to_string( last_position_ )
180  );
181  }
182  return last_position_ - first_position_ + 1;
183  }
184 
185  // -------------------------------------------------------------------------
186  // Whole Genome
187  // -------------------------------------------------------------------------
188 
192  bool is_whole_genome() const
193  {
194  return is_whole_genome_;
195  }
196 
197 
201  void is_whole_genome( bool value )
202  {
203  is_whole_genome_ = value;
204  }
205 
214  std::unordered_map<std::string, size_t> const& chromosomes() const
215  {
216  return chromosomes_;
217  }
218 
222  std::unordered_map<std::string, size_t>& chromosomes()
223  {
224  return chromosomes_;
225  }
226 
227  // -------------------------------------------------------------------------
228  // Modifiers and Helpers
229  // -------------------------------------------------------------------------
230 
234  void clear()
235  {
236  chromosome_ = "";
237  first_position_ = 0;
238  last_position_ = 0;
239  is_whole_genome_ = false;
240  chromosomes_.clear();
241  clear_();
242  }
243 
244  // -------------------------------------------------------------------------
245  // Virtual Members
246  // -------------------------------------------------------------------------
247 
248 protected:
249 
253  virtual void clear_()
254  {
255  // Do nothing. Override by derived classes to clear their data.
256  }
257 
258  // -------------------------------------------------------------------------
259  // Data Members
260  // -------------------------------------------------------------------------
261 
262 private:
263 
264  // Normal case of window within chromosome
265  std::string chromosome_;
266  size_t first_position_ = 0;
267  size_t last_position_ = 0;
268 
269  // Special case of window over whole genome
270  bool is_whole_genome_ = false;
271  std::unordered_map<std::string, size_t> chromosomes_;
272 
273 };
274 
275 } // namespace population
276 } // namespace genesis
277 
278 #endif // include guard
genesis::population::BaseWindow::~BaseWindow
virtual ~BaseWindow()=default
genesis::population::BaseWindow::is_whole_genome
void is_whole_genome(bool value)
Set whether this instance is intended to be used for a whole genome stream.
Definition: base_window.hpp:201
genesis::population::BaseWindow::chromosome
void chromosome(std::string const &value)
Set the chromosome name that this Window belongs to.
Definition: base_window.hpp:98
genesis::population::BaseWindow::chromosomes
std::unordered_map< std::string, size_t > const & chromosomes() const
Get the list of all chromosomes along the genome, with their length.
Definition: base_window.hpp:214
genesis::population::to_string
std::string to_string(GenomeLocus const &locus)
Definition: function/genome_locus.hpp:52
genome_region.hpp
genesis::population::BaseWindow< DataType >::Data
DataType Data
Definition: base_window.hpp:68
genesis::population::BaseWindow::first_position
void first_position(size_t value)
Set the first position in the chromosome of the Window, that is, where the Window starts.
Definition: base_window.hpp:124
genesis::population::BaseWindow::width
size_t width() const
Get the width of the Window.
Definition: base_window.hpp:173
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::BaseWindow::is_whole_genome
bool is_whole_genome() const
Return if this instance is intended to be used for a whole genome stream.
Definition: base_window.hpp:192
genesis::population::BaseWindow::chromosomes
std::unordered_map< std::string, size_t > & chromosomes()
Get the list of all chromosomes along the genome, with their length.
Definition: base_window.hpp:222
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::BaseWindow::last_position
void last_position(size_t value)
Set the last position in the chromosome of the Window, that is, where the Window ends.
Definition: base_window.hpp:144
genesis::population::BaseWindow::clear
void clear()
Clear all data from the Window.
Definition: base_window.hpp:234
genesis::population::BaseWindow::operator=
BaseWindow & operator=(BaseWindow const &)=default
genesis::population::BaseWindow::clear_
virtual void clear_()
Virtual clear function for derived classes to clear their data.
Definition: base_window.hpp:253
genesis::population::GenomeRegion
A region (between two positions) on a chromosome.
Definition: genome_region.hpp:70
genesis::population::BaseWindow::genome_region
GenomeRegion genome_region() const
Return the genome region that this Windows is defined over.
Definition: base_window.hpp:155
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::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::BaseWindow::BaseWindow
BaseWindow()=default