A library for working with phylogenetic and population genetic data.
v0.32.0
filter_stats.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_POPULATION_FILTER_FILTER_STATS_H_
2 #define GENESIS_POPULATION_FILTER_FILTER_STATS_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 
35 
36 #include <array>
37 #include <cstdint>
38 #include <stdexcept>
39 #include <type_traits>
40 
41 namespace genesis {
42 namespace population {
43 
44 // =================================================================================================
45 // Filter Stats
46 // =================================================================================================
47 
60 template<typename FilterTag>
62 {
63  // -------------------------------------------------------------------------
64  // Typedefs and Enums
65  // -------------------------------------------------------------------------
66 
67  using FilterTagArray = std::array<size_t, static_cast<size_t>( FilterTag::kEnd )>;
68 
69  // -------------------------------------------------------------------------
70  // Constructor
71  // -------------------------------------------------------------------------
72 
73  // Value-initialize the array, so that it is all zero.
75  : data()
76  {}
77 
78  // -------------------------------------------------------------------------
79  // Operators and Iteration
80  // -------------------------------------------------------------------------
81 
82  size_t& operator[] ( FilterTag tag )
83  {
84  auto const i = static_cast<typename std::underlying_type<FilterTag>::type>(tag);
85  if( i >= data.size() ) {
86  throw std::invalid_argument( "Invalid filter tag value " + std::to_string(i) );
87  }
88  return data[ i ];
89  }
90 
91  size_t operator[] ( FilterTag tag ) const
92  {
93  auto const i = static_cast<typename std::underlying_type<FilterTag>::type>(tag);
94  if( i >= data.size() ) {
95  throw std::invalid_argument( "Invalid filter tag value " + std::to_string(i) );
96  }
97  return data[ i ];
98  }
99 
100  size_t& operator[] ( size_t index )
101  {
102  if( index >= data.size() ) {
103  throw std::invalid_argument( "Invalid filter tag value " + std::to_string(index) );
104  }
105  return data[ index ];
106  }
107 
108  size_t operator[] ( size_t index ) const
109  {
110  if( index >= data.size() ) {
111  throw std::invalid_argument( "Invalid filter tag value " + std::to_string(index) );
112  }
113  return data[ index ];
114  }
115 
116  typename FilterTagArray::iterator begin()
117  {
118  return data.begin();
119  }
120 
121  typename FilterTagArray::const_iterator begin() const
122  {
123  return data.begin();
124  }
125 
126  typename FilterTagArray::iterator end()
127  {
128  return data.end();
129  }
130 
131  typename FilterTagArray::const_iterator end() const
132  {
133  return data.end();
134  }
135 
136  size_t size() const
137  {
138  return data.size();
139  }
140 
141  // -------------------------------------------------------------------------
142  // Member Functions
143  // -------------------------------------------------------------------------
144 
145  size_t sum() const
146  {
147  size_t s = 0;
148  for( auto v : data ) {
149  s += v;
150  }
151  return s;
152  }
153 
154  size_t sum_failing() const
155  {
156  // Check that our special 0 case is there and has the right value.
157  static_assert(
158  static_cast<typename std::underlying_type<FilterTag>::type>(FilterTag::kPassed) == 0,
159  "Filter Tag enum value kPassed != 0"
160  );
161 
162  // Sum up all but the first (passing) value.
163  size_t s = 0;
164  for( size_t i = 1; i < data.size(); ++i ) {
165  s += data[i];
166  }
167  return s;
168  }
169 
170  size_t sum_non_passing() const
171  {
172  return sum_failing();
173  }
174 
175  void clear()
176  {
177  data = {};
178  }
179 
180  // -------------------------------------------------------------------------
181  // Member Data
182  // -------------------------------------------------------------------------
183 
185 };
186 
187 } // namespace population
188 } // namespace genesis
189 
190 #endif // include guard
genesis::population::FilterStats::sum
size_t sum() const
Definition: filter_stats.hpp:145
genesis::population::FilterStats::begin
FilterTagArray::iterator begin()
Definition: filter_stats.hpp:116
genesis::population::FilterStats::sum_non_passing
size_t sum_non_passing() const
Definition: filter_stats.hpp:170
genesis::population::FilterStats::data
FilterTagArray data
Definition: filter_stats.hpp:184
genesis::population::to_string
std::string to_string(GenomeLocus const &locus)
Definition: function/genome_locus.hpp:52
genesis::population::FilterStats::sum_failing
size_t sum_failing() const
Definition: filter_stats.hpp:154
filter_status.hpp
genesis::population::FilterStats::FilterStats
FilterStats()
Definition: filter_stats.hpp:74
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::FilterStats::end
FilterTagArray::iterator end()
Definition: filter_stats.hpp:126
genesis::population::FilterStats::begin
FilterTagArray::const_iterator begin() const
Definition: filter_stats.hpp:121
genesis::population::FilterStats::end
FilterTagArray::const_iterator end() const
Definition: filter_stats.hpp:131
genesis::population::FilterStats
Counts of how many entries with a particular Filter Tag occured in some data.
Definition: filter_stats.hpp:61
genesis::population::FilterStats::operator[]
size_t & operator[](FilterTag tag)
Definition: filter_stats.hpp:82
genesis::population::FilterStats::clear
void clear()
Definition: filter_stats.hpp:175
genesis::population::FilterStats::size
size_t size() const
Definition: filter_stats.hpp:136
genesis::population::FilterStats< SampleCountsFilterTag >::FilterTagArray
std::array< size_t, static_cast< size_t >(SampleCountsFilterTag ::kEnd)> FilterTagArray
Definition: filter_stats.hpp:67