A library for working with phylogenetic and population genetic data.
v0.32.0
fst_pool_calculator.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_POPULATION_FUNCTION_FST_POOL_CALCULATOR_H_
2 #define GENESIS_POPULATION_FUNCTION_FST_POOL_CALCULATOR_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 <lucas.czech@sund.ku.dk>
23  University of Copenhagen, Globe Institute, Section for GeoGenetics
24  Oster Voldgade 5-7, 1350 Copenhagen K, Denmark
25 */
26 
37 
38 #include <algorithm>
39 #include <cassert>
40 #include <vector>
41 
42 namespace genesis {
43 namespace population {
44 
45 // =================================================================================================
46 // Fst Pool Calculator
47 // =================================================================================================
48 
66 {
67 public:
68 
69  // -------------------------------------------------------------------------
70  // Constructors and Rule of Five
71  // -------------------------------------------------------------------------
72 
73  BaseFstPoolCalculator() = default;
74  virtual ~BaseFstPoolCalculator() = default;
75 
76  BaseFstPoolCalculator( BaseFstPoolCalculator const& ) = default;
78 
81 
82  // -------------------------------------------------------------------------
83  // Calculator Functions
84  // -------------------------------------------------------------------------
85 
86  void reset()
87  {
88  reset_();
89  filter_stats_.clear();
90  }
91 
92  void process( SampleCounts const& p1, SampleCounts const& p2 )
93  {
94  // For now, we accept two populations as input here.
95  // If we ever implement an FST equation that also works for multiple populations,
96  // see https://stackoverflow.com/a/9377363/4184258 for a way to implement this.
97  // Or simply make this function accept the Variant instead, and store the needed sample
98  // indices here, instead of the list of sample pairs stored in the FstPoolProcessor.
99 
100  // We only want to process samples that are both passing.
101  // Otherwise, if both fail, we want to record that, but it does not really matter which
102  // of the two reasons for failing we record. However, as we generally have our filter status
103  // tags numerically sorted in the order in which they are applied, we here use the first
104  // tag (smaller number) of the two samples that failed, as that is usually a better and
105  // more general idicator of what failed. Probably does not really matter much anyway,
106  // but why not. Lastly, if only one of the samples failed, we just record that one.
107  // This can be done as the max of the two values, as one of them will be zero.
108  if( p1.status.passing() && p2.status.passing() ) {
109  process_( p1, p2 );
110  ++filter_stats_[SampleCountsFilterTag::kPassed];
111  } else if( ! p1.status.passing() && ! p2.status.passing() ) {
112  ++filter_stats_[ std::min( p1.status.get(), p2.status.get() )];
113  } else {
114  ++filter_stats_[ std::max( p1.status.get(), p2.status.get() )];
115  }
116  }
117 
118  double get_result() const
119  {
120  return get_result_();
121  }
122 
130  {
131  return filter_stats_;
132  }
133 
134  // -------------------------------------------------------------------------
135  // Abstract Members
136  // -------------------------------------------------------------------------
137 
138 protected:
139 
140  // Non-virtual interface.
141  virtual void reset_() = 0;
142  virtual void process_( SampleCounts const& p1, SampleCounts const& p2 ) = 0;
143  virtual double get_result_() const = 0;
144 
145  // -------------------------------------------------------------------------
146  // Private Members
147  // -------------------------------------------------------------------------
148 
149 private:
150 
151  // Count how many sample pairs were processed here,
152  // and how many of them passed or failed the filters.
153  SampleCountsFilterStats filter_stats_;
154 
155 };
156 
157 } // namespace population
158 } // namespace genesis
159 
160 #endif // include guard
genesis::population::BaseFstPoolCalculator::process
void process(SampleCounts const &p1, SampleCounts const &p2)
Definition: fst_pool_calculator.hpp:92
genesis::population::BaseFstPoolCalculator::reset_
virtual void reset_()=0
genesis::population::FilterStatus::get
IntType get() const
Definition: filter_status.hpp:91
genesis::population::BaseFstPoolCalculator::BaseFstPoolCalculator
BaseFstPoolCalculator()=default
genesis::population::SampleCounts
One set of nucleotide sample counts, for example for a given sample that represents a pool of sequenc...
Definition: sample_counts.hpp:56
genesis::population::BaseFstPoolCalculator::get_filter_stats
SampleCountsFilterStats get_filter_stats() const
Get the sum of filter statistics of all sample pairs processed here.
Definition: fst_pool_calculator.hpp:129
genesis::population::BaseFstPoolCalculator::operator=
BaseFstPoolCalculator & operator=(BaseFstPoolCalculator const &)=default
genesis::population::BaseFstPoolCalculator::get_result
double get_result() const
Definition: fst_pool_calculator.hpp:118
genesis::population::BaseFstPoolCalculator::process_
virtual void process_(SampleCounts const &p1, SampleCounts const &p2)=0
genesis::population::SampleCounts::status
FilterStatus status
Status to indicate whether any applied filters failed to pass.
Definition: sample_counts.hpp:96
genesis::population::BaseFstPoolCalculator::reset
void reset()
Definition: fst_pool_calculator.hpp:86
sample_counts_filter.hpp
sample_counts.hpp
genesis::population::SampleCountsFilterTag::kPassed
@ kPassed
Sample has passed all filters.
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::FilterStatus::passing
bool passing() const
Definition: filter_status.hpp:75
genesis::population::BaseFstPoolCalculator
Base class to compute FST between two pooled samples, given two instances of SampleCounts.
Definition: fst_pool_calculator.hpp:65
genesis::population::BaseFstPoolCalculator::~BaseFstPoolCalculator
virtual ~BaseFstPoolCalculator()=default
genesis::population::FilterStats< SampleCountsFilterTag >
variant.hpp
genesis::population::FilterStats::clear
void clear()
Definition: filter_stats.hpp:175
genesis::population::BaseFstPoolCalculator::get_result_
virtual double get_result_() const =0