SeqAn3
max_error.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 {
54 struct search_config_max_error
55 {
57  std::tuple<uint8_t, uint8_t, uint8_t, uint8_t> value;
58 };
59 
63 struct search_config_max_error_adaptor : public configuration_fn_base<search_config_max_error_adaptor>
64 {
65 private:
71  template <typename error_type_t, typename out_t, typename ... error_types_t>
72  constexpr void get_type_out(out_t & out, std::tuple<error_types_t...> const & in) const
73  {
74  constexpr auto count = meta::count<meta::list<error_types_t...>, error_type_t>::value;
75  static_assert(count <= 1, "The same error type has been passed multiple times to max_error.");
76  if constexpr (count > 0)
77  std::get<error_type_t>(out) = std::get<error_type_t>(in);
78  }
79 
80 public:
87  template <typename configuration_t, typename ... error_types_t>
89  requires is_algorithm_configuration_v<remove_cvref_t<configuration_t>>
91  constexpr auto invoke(configuration_t && cfg, error_types_t && ...error_types) const
92  {
93  static_assert(is_valid_search_configuration_v<search_cfg::id::max_error, remove_cvref_t<configuration_t>>,
94  SEQAN3_INVALID_CONFIG(search_cfg::id::max_error));
95 
96  std::tuple in{error_types...};
97  std::tuple<search_cfg::total<uint8_t>, search_cfg::substitution<uint8_t>,
98  search_cfg::insertion<uint8_t>, search_cfg::deletion<uint8_t>> out{0, 0, 0, 0};
99 
100  get_type_out<search_cfg::total<uint8_t>>(out, in);
101  get_type_out<search_cfg::substitution<uint8_t>>(out, in);
102  get_type_out<search_cfg::insertion<uint8_t>>(out, in);
103  get_type_out<search_cfg::deletion<uint8_t>>(out, in);
104 
105  using error_types_list_t = meta::list<remove_cvref_t<error_types_t>...>;
106  constexpr bool total_set = meta::in<error_types_list_t, search_cfg::total<uint8_t>>::value;
107  constexpr bool other_error_types_set = meta::count<error_types_list_t, search_cfg::total<uint8_t>>::value !=
108  meta::size<error_types_list_t>::value;
109 
110  // no specific error types specified
111  if constexpr (!other_error_types_set)
112  {
113  // only total is set: set all to total
114  if constexpr (total_set)
115  {
116  uint8_t const total_v{std::get<0>(out)};
117  std::get<1>(out) = search_cfg::substitution<uint8_t>{total_v};
118  std::get<2>(out) = search_cfg::insertion<uint8_t>{total_v};
119  std::get<3>(out) = search_cfg::deletion<uint8_t>{total_v};
120  }
121  }
122  // at least one specific error type specified
123  else if constexpr (!total_set)
124  {
125  uint8_t const substitution_error = static_cast<uint8_t>(std::get<1>(out));
126  uint8_t const insertion_error = static_cast<uint8_t>(std::get<2>(out));
127  uint8_t const deletion_error = static_cast<uint8_t>(std::get<3>(out));
128  // total not set. set it to sum of all error types
129  std::get<0>(out) = search_cfg::total<uint8_t>{std::min<uint8_t>(255, substitution_error
130  + insertion_error
131  + deletion_error)};
132  }
133 
134  search_config_max_error tmp{static_cast<std::tuple<uint8_t, uint8_t, uint8_t, uint8_t>>(out)};
135  return std::forward<configuration_t>(cfg).push_front(std::move(tmp));
136  }
137 };
138 
141 template <>
142 struct on_search_config<search_cfg::id::max_error>
143 {
145  template <config_element_concept t>
146  using invoke = typename std::is_same<t, search_config_max_error>::type;
147 };
148 
151 template <>
152 struct search_config_type_to_id<search_config_max_error>
153 {
155  static constexpr search_cfg::id value = search_cfg::id::max_error;
156 };
157 } // namespace seqan3::detail
158 
159 namespace seqan3::search_cfg
160 {
168 inline constexpr detail::search_config_max_error_adaptor max_error;
169 
170 } // 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.
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.
constexpr detail::search_config_max_error_adaptor max_error
A configuration element for the maximum number of errors across all error types (mismatches, insertions, deletions). This is an upper bound of errors independent from error numbers of specific error types.
Definition: max_error.hpp:168
Identifier for max_errors configuration.