A library for working with phylogenetic and population genetic data.
v0.32.0
variant_filter_positional.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_POPULATION_FILTER_VARIANT_FILTER_POSITIONAL_H_
2 #define GENESIS_POPULATION_FILTER_VARIANT_FILTER_POSITIONAL_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 
40 
41 #include <functional>
42 #include <iosfwd>
43 #include <memory>
44 #include <stdexcept>
45 #include <string>
46 #include <utility>
47 #include <vector>
48 
49 namespace genesis {
50 namespace population {
51 
52 // =================================================================================================
53 // Filter by region, excluding
54 // =================================================================================================
55 
66 inline std::function<bool(Variant const&)> make_variant_filter_by_region_excluding(
67  GenomeRegion const& region,
68  bool complement = false
69 ) {
70  return [region, complement]( Variant const& variant ){
71  return complement ^ is_covered( region, variant );
72  };
73 }
74 
85 inline std::function<bool(Variant const&)> make_variant_filter_by_region_excluding(
86  std::shared_ptr<GenomeRegionList> regions,
87  bool complement = false
88 ) {
89  if( ! regions ) {
90  throw std::invalid_argument(
91  "Can only used make_variant_filter_by_region_excluding() "
92  "with a valid shared pointer to a GenomeRegionList."
93  );
94  }
95  return [regions, complement]( Variant const& variant ){
96  return complement ^ is_covered( *regions, variant );
97  };
98 }
99 
103 inline std::function<bool(Variant const&)> make_variant_filter_by_region_excluding(
104  std::shared_ptr<GenomeLocusSet> loci,
105  bool complement = false
106 ) {
107  if( ! loci ) {
108  throw std::invalid_argument(
109  "Can only used make_variant_filter_by_region_excluding() "
110  "with a valid shared pointer to a GenomeLocusSet."
111  );
112  }
113  return [loci, complement]( Variant const& variant ){
114  return complement ^ is_covered( *loci, variant );
115  };
116 }
117 
118 // =================================================================================================
119 // Filter by region, tagging
120 // =================================================================================================
121 
138 inline std::function<void(Variant&)> make_variant_filter_by_region_tagging(
139  GenomeRegion const& region,
140  VariantFilterTag tag,
141  bool complement = false
142 ) {
144  throw std::invalid_argument(
145  "Can only use VariantFilterTag::kMaskedPosition or VariantFilterTag::kMaskedRegion "
146  "as tags for make_variant_filter_by_region_tagging()."
147  );
148  }
149  return [region, tag, complement]( Variant& variant )
150  {
151  if( ! variant.status.passing() ) {
152  return;
153  }
154  auto const keep = complement ^ is_covered( region, variant );
155  if( ! keep ) {
156  variant.status.set( tag );
157  }
158  };
159 }
160 
177 inline std::function<void(Variant&)> make_variant_filter_by_region_tagging(
178  std::shared_ptr<GenomeRegionList> regions,
179  VariantFilterTag tag,
180  bool complement = false
181 ) {
183  throw std::invalid_argument(
184  "Can only use VariantFilterTag::kMaskedPosition or VariantFilterTag::kMaskedRegion "
185  "as tags for make_variant_filter_by_region_tagging()."
186  );
187  }
188  if( ! regions ) {
189  throw std::invalid_argument(
190  "Can only used make_variant_filter_by_region_tagging() "
191  "with a valid shared pointer to a GenomeRegionList."
192  );
193  }
194  return [regions, tag, complement]( Variant& variant )
195  {
196  if( ! variant.status.passing() ) {
197  return;
198  }
199  auto const keep = complement ^ is_covered( *regions, variant );
200  if( ! keep ) {
201  variant.status.set( tag );
202  }
203  };
204 }
205 
209 inline std::function<void(Variant&)> make_variant_filter_by_region_tagging(
210  std::shared_ptr<GenomeLocusSet> loci,
211  VariantFilterTag tag,
212  bool complement = false
213 ) {
215  throw std::invalid_argument(
216  "Can only use VariantFilterTag::kMaskedPosition or VariantFilterTag::kMaskedRegion "
217  "as tags for make_variant_filter_by_region_tagging()."
218  );
219  }
220  if( ! loci ) {
221  throw std::invalid_argument(
222  "Can only used make_variant_filter_by_region_tagging() "
223  "with a valid shared pointer to a GenomeLocusSet."
224  );
225  }
226  return [loci, tag, complement]( Variant& variant )
227  {
228  if( ! variant.status.passing() ) {
229  return;
230  }
231  auto const keep = complement ^ is_covered( *loci, variant );
232  if( ! keep ) {
233  variant.status.set( tag );
234  }
235  };
236 }
237 
238 } // namespace population
239 } // namespace genesis
240 
241 #endif // include guard
genome_region.hpp
genesis::population::VariantFilterTag::kMaskedPosition
@ kMaskedPosition
Position has been masked out from processing.
genesis::population::make_variant_filter_by_region_excluding
std::function< bool(Variant const &)> make_variant_filter_by_region_excluding(GenomeRegion const &region, bool complement=false)
Filter function to be used with VariantInputStream to filter by a genome region, by excluding non-cov...
Definition: variant_filter_positional.hpp:66
genesis::population::is_covered
bool is_covered(GenomeRegion const &region, std::string const &chromosome, size_t position)
Test whether the chromosome/position is within a given genomic region.
Definition: genome_region.cpp:207
genome_region.hpp
sample_counts.hpp
genesis::population::VariantFilterTag::kMaskedRegion
@ kMaskedRegion
Position is part of a masked region.
genesis::population::Variant
A single variant at a position in a chromosome, along with SampleCounts for a set of samples.
Definition: variant.hpp:65
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
genome_locus_set.hpp
variant_filter.hpp
genesis::population::VariantFilterTag
VariantFilterTag
List of filters that we apply to a Variant, to indicate whether the Variant passed or not.
Definition: variant_filter.hpp:71
genesis::population::GenomeRegion
A region (between two positions) on a chromosome.
Definition: genome_region.hpp:70
genesis::population::make_variant_filter_by_region_tagging
std::function< void(Variant &)> make_variant_filter_by_region_tagging(GenomeRegion const &region, VariantFilterTag tag, bool complement=false)
Filter function to be used with VariantInputStream to filter by a genome region, by tagging non-cover...
Definition: variant_filter_positional.hpp:138
variant.hpp