A library for working with phylogenetic and population genetic data.
v0.27.0
population/window/functions.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_POPULATION_WINDOW_FUNCTIONS_H_
2 #define GENESIS_POPULATION_WINDOW_FUNCTIONS_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2022 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 <string>
36 #include <vector>
37 
39 
40 namespace genesis {
41 namespace population {
42 
43 // =================================================================================================
44 // Helper Functions for Window
45 // =================================================================================================
46 
52 enum class WindowAnchorType
53 {
62 };
63 
76 template<class D, class A = EmptyAccumulator>
78  Window<D, A> const& window,
80 ) {
81  auto check_entries = [&](){
82  if( window.entries().empty() ) {
83  throw std::runtime_error(
84  "Cannot use empty Window (with no variants/entries) for variant-based anchor "
85  "positions. Typically these anchor positions are used with WindowType::kVariants."
86  );
87  }
88  };
89 
90  // Calculate the SNP position that we want to output when emitting a window.
91  // Some use integer division, which is intended. We don't want the hassle of floating
92  // point genomic positions, so we have to do these roundings... But given a large window
93  // size, that should probably not matter much.
94  switch( anchor_type ) {
96  return window.first_position();
97  }
99  return window.last_position();
100  }
102  return ( window.first_position() + window.last_position() ) / 2;
103  }
105  check_entries();
106  assert( ! window.entries().empty() );
107  return window.entries().front().position;
108  }
110  check_entries();
111  assert( ! window.entries().empty() );
112  return window.entries().back().position;
113  }
115  check_entries();
116  assert( ! window.entries().empty() );
117  return window.entries()[ window.entries().size() / 2 ].position;
118  }
120  check_entries();
121  assert( ! window.entries().empty() );
122 
123  size_t sum = 0;
124  for( auto const& e : window.entries() ) {
125  sum += e.position;
126  }
127  return sum / window.entries().size();
128  }
130  check_entries();
131  assert( ! window.entries().empty() );
132  return ( window.entries().front().position + window.entries().back().position ) / 2;
133  }
134  default: {
135  throw std::runtime_error( "Invalid WindowAnchorType." );
136  }
137  }
138  assert( false );
139  return 0;
140 }
141 
142 } // namespace population
143 } // namespace genesis
144 
145 #endif // include guard
genesis::population::WindowAnchorType::kVariantMean
@ kVariantMean
genesis::utils::sum
double sum(const Histogram &h)
Definition: utils/math/histogram/stats.cpp:140
genesis::population::WindowAnchorType::kVariantLast
@ kVariantLast
genesis::population::WindowAnchorType::kVariantMedian
@ kVariantMedian
genesis::population::Window
Window over the chromosomes of a genome.
Definition: window.hpp:104
genesis::population::WindowAnchorType::kIntervalBegin
@ kIntervalBegin
genesis::population::WindowAnchorType::kIntervalEnd
@ kIntervalEnd
genesis::population::Window::last_position
size_t last_position() const
Get the last position in the chromosome of the Window, that is, where the Window ends.
Definition: window.hpp:358
genesis::population::Window::entries
container const & entries() const
Immediate container access to the Data Entries.
Definition: window.hpp:461
genesis::population::WindowAnchorType::kIntervalMidpoint
@ kIntervalMidpoint
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
window.hpp
genesis::population::WindowAnchorType::kVariantFirst
@ kVariantFirst
genesis::population::Window::first_position
size_t first_position() const
Get the first position in the chromosome of the Window, that is, where the Window starts.
Definition: window.hpp:338
genesis::population::WindowAnchorType::kVariantMidpoint
@ kVariantMidpoint
genesis::population::WindowAnchorType
WindowAnchorType
Position in the genome that is used for reporting when emitting or using a window.
Definition: population/window/functions.hpp:52
genesis::population::anchor_position
size_t anchor_position(Window< D, A > const &window, WindowAnchorType anchor_type=WindowAnchorType::kIntervalBegin)
Get the position in the chromosome reported according to a specific WindowAnchorType.
Definition: population/window/functions.hpp:77