SeqAn3
max_error_rate.hpp
Go to the documentation of this file.
1 // ============================================================================
2 // SeqAn - The Library for Sequence Analysis
3 // ============================================================================
4 //
5 // Copyright (c) 2006-2018, Knut Reinert & Freie Universitaet Berlin
6 // Copyright (c) 2016-2018, Knut Reinert & MPI Molekulare Genetik
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // * Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // * Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // * Neither the name of Knut Reinert or the FU Berlin nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 // ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
25 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 // DAMAGE.
32 //
33 // ============================================================================
34 
40 #pragma once
41 
47 
48 namespace seqan3::detail
49 {
55 struct search_config_max_error_rate
56 {
58  std::tuple<double, double, double, double> value;
59 };
60 
64 struct search_config_max_error_rate_adaptor :
65  public configuration_fn_base<search_config_max_error_rate_adaptor>
66 {
67 private:
73  template <typename error_type_t, typename out_t, typename ... error_types_t>
74  constexpr void get_type_out(out_t & out, std::tuple<error_types_t...> const & in) const
75  {
76  constexpr auto count = meta::count<meta::list<error_types_t...>, error_type_t>::value;
77  static_assert(count <= 1, "The same error type has been passed multiple times to max_error.");
78  if constexpr (count > 0)
79  std::get<error_type_t>(out) = std::get<error_type_t>(in);
80  }
81 
82 public:
89  template <typename configuration_t, typename ... error_types_t>
91  requires is_algorithm_configuration_v<remove_cvref_t<configuration_t>>
93  constexpr auto invoke(configuration_t && cfg, error_types_t && ...error_types) const
94  {
95  static_assert(is_valid_search_configuration_v<search_cfg::id::max_error_rate, remove_cvref_t<configuration_t>>,
96  SEQAN3_INVALID_CONFIG(search_cfg::id::max_error_rate));
97 
98  std::tuple in{error_types...};
99  std::tuple<search_cfg::total<double>, search_cfg::substitution<double>,
100  search_cfg::insertion<double>, search_cfg::deletion<double>> out{.0, .0, .0, .0};
101 
102  get_type_out<search_cfg::total<double>>(out, in);
103  get_type_out<search_cfg::substitution<double>>(out, in);
104  get_type_out<search_cfg::insertion<double>>(out, in);
105  get_type_out<search_cfg::deletion<double>>(out, in);
106 
107  using error_types_list_t = meta::list<remove_cvref_t<error_types_t>...>;
108  constexpr bool total_set = meta::in<error_types_list_t, search_cfg::total<double>>::value;
109  constexpr bool other_error_types_set = meta::count<error_types_list_t, search_cfg::total<double>>::value !=
110  meta::size<error_types_list_t>::value;
111 
112  double const total_v {std::get<0>(out)};
113  double const substitution_v{std::get<1>(out)};
114  double const insertion_v {std::get<2>(out)};
115  double const deletion_v {std::get<3>(out)};
116 
117  if ((0 > total_v || total_v > 1) || (0 > substitution_v || substitution_v > 1) ||
118  (0 > insertion_v || insertion_v > 1) || (0 > deletion_v || deletion_v > 1))
119  {
120  throw std::invalid_argument("Error rates must be between 0 and 1.");
121  }
122 
123  // no specific error types specified
124  if constexpr (!other_error_types_set)
125  {
126  // only total is set: set all to total
127  if constexpr (total_set)
128  {
129  std::get<1>(out) = search_cfg::substitution<double>{total_v};
130  std::get<2>(out) = search_cfg::insertion<double>{total_v};
131  std::get<3>(out) = search_cfg::deletion<double>{total_v};
132  }
133  }
134  // at least one specific error type specified
135  else if constexpr (!total_set)
136  {
137  // total not set. set it to sum of all error types
138  std::get<0>(out) = search_cfg::total<double>{std::min(1., substitution_v + insertion_v + deletion_v)};
139  }
140 
141  search_config_max_error_rate tmp{static_cast<std::tuple<double, double, double, double>>(out)};
142  return std::forward<configuration_t>(cfg).push_front(std::move(tmp));
143  }
144 };
145 
148 template <>
149 struct on_search_config<search_cfg::id::max_error_rate>
150 {
152  template <config_element_concept t>
153  using invoke = typename std::is_same<t, search_config_max_error_rate>::type;
154 };
155 
158 template <>
159 struct search_config_type_to_id<search_config_max_error_rate>
160 {
162  static constexpr search_cfg::id value = search_cfg::id::max_error_rate;
163 };
164 } // namespace seqan3::detail
165 
166 namespace seqan3::search_cfg
167 {
176 inline constexpr detail::search_config_max_error_rate_adaptor max_error_rate;
177 
178 } // namespace seqan3::search_cfg
Provides the error types for maximum number of errors.
Provides functionality to access get function by enum values.
Provides seqan3::type_list and auxiliary metafunctions.
constexpr detail::search_config_max_error_rate_adaptor max_error_rate
A configuration element for the maximum number of errors in percent of the query length across all er...
Definition: max_error_rate.hpp:176
SeqAn specific customisations in the standard namespace.
Definition: align_result.hpp:221
A special sub namespace for the search configurations.
constexpr auto const & get(detail::configuration< cfg_elements_t... > const &cfg) noexcept
Definition: utility.hpp:364
id
Specifies an id for every configuration element.
Definition: utility.hpp:68
std::remove_cv_t< std::remove_reference_t< t > > remove_cvref_t
Return the input type with const, volatile and references removed [Type metafunction].
Definition: basic.hpp:64
Definition: aligned_sequence_concept.hpp:288
Provides various metafunctions on generic types.
Meta-Header for components of the algorithm submodule.