SeqAn3 3.4.0-rc.3
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
search_configurator.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
24
25namespace seqan3::detail
26{
27
31class search_configurator
32{
33private:
40 template <typename search_configuration_t, typename index_t, typename query_index_t>
41 struct select_search_result
42 {
43 private:
45 using index_cursor_type = typename index_t::cursor_type;
47 using index_size_type = typename index_t::size_type;
49 using traits_type = search_traits<search_configuration_t>;
50
58 using reference_begin_position_t =
60
61 public:
63 using type = search_result<query_id_t, index_cursor_t, reference_id_t, reference_begin_position_t>;
64 };
65
72 template <typename configuration_t, typename index_t, typename... policies_t>
73 struct select_search_algorithm
74 {
76 using type = lazy_conditional_t<template_specialisation_of<typename index_t::cursor_type, bi_fm_index_cursor>,
77 lazy<search_scheme_algorithm, configuration_t, index_t, policies_t...>,
78 lazy<unidirectional_search_algorithm, configuration_t, index_t, policies_t...>>;
79 };
80
81public:
92 template <typename configuration_t>
93 static auto add_default_hit_configuration(configuration_t const & cfg)
94 {
95 if constexpr (!detail::search_traits<configuration_t>::has_hit_configuration)
96 return cfg | search_cfg::hit_all{};
97 else
98 return cfg;
99 }
100
110 template <typename configuration_t>
111 static auto add_default_output_configuration(configuration_t const & cfg)
112 {
113 if constexpr (!seqan3::detail::search_traits<configuration_t>::has_output_configuration)
114 return cfg | search_cfg::output_query_id{} | search_cfg::output_reference_id{}
115 | search_cfg::output_reference_begin_position{};
116 else
117 return cfg;
118 }
119
132 template <typename configuration_t>
133 static auto add_defaults(configuration_t const & cfg)
134 {
135 static_assert(detail::is_type_specialisation_of_v<configuration_t, configuration>,
136 "cfg must be a specialisation of seqan3::configuration.");
137
138 auto cfg1 = add_default_hit_configuration(cfg);
139 auto cfg2 = add_default_output_configuration(cfg1);
140
141 return cfg2;
142 }
143
159 template <typename query_t, typename configuration_t, typename index_t>
160 static auto configure_algorithm(configuration_t const & cfg, index_t const & index)
161 {
162 using query_index_t = std::tuple_element_t<0, query_t>;
163 using search_result_t = typename select_search_result<configuration_t, index_t, query_index_t>::type;
164 using callback_t = std::function<void(search_result_t)>;
165 using type_erased_algorithm_t = std::function<void(query_t, callback_t)>;
166
167 auto complete_config = cfg | search_cfg::detail::result_type<search_result_t>{};
168 return std::pair{configure_hit_strategy<type_erased_algorithm_t>(complete_config, index), complete_config};
169 }
170
171 template <typename algorithm_t, typename configuration_t, typename index_t>
172 static algorithm_t configure_hit_strategy(configuration_t const &, index_t const &);
173
188 template <typename algorithm_t, typename configuration_t, typename index_t>
189 static algorithm_t select_and_return_algorithm(configuration_t const & config, index_t const & index)
190 {
191 using selected_algorithm_t =
192 typename select_search_algorithm<configuration_t,
193 index_t,
194 policy_max_error,
195 policy_search_result_builder<configuration_t>>::type;
196
197 return selected_algorithm_t{config, index};
198 }
199};
200
221template <typename algorithm_t, typename configuration_t, typename index_t>
222algorithm_t search_configurator::configure_hit_strategy(configuration_t const & cfg, index_t const & index)
223{
224 // Delegate to the next config with the modified configuration.
225 auto next_config_step = [&](auto new_cfg) -> algorithm_t
226 {
227 return select_and_return_algorithm<algorithm_t>(new_cfg, index);
228 };
229
230 // Check if dynamic config present, otherwise continue.
231 if constexpr (configuration_t::template exists<search_cfg::hit>())
232 {
233 auto hit_variant = get<search_cfg::hit>(cfg).hit_variant;
234
235 if (std::holds_alternative<empty_type>(hit_variant))
236 throw std::invalid_argument{"The dynamic hit strategy was not initialised! "
237 "Please refer to the configuration documentation of the search algorithm for "
238 "more details."};
239
240 // Remove dynamic config first.
241 auto cfg_without_hit = cfg.template remove<search_cfg::hit>();
242
243 // Apply the correct static configuration element.
245 {
246 return next_config_step(cfg_without_hit | search_cfg::hit_all_best{});
247 }
249 {
250 return next_config_step(cfg_without_hit | search_cfg::hit_single_best{});
251 }
253 {
254 return next_config_step(cfg_without_hit | std::get<search_cfg::hit_strata>(hit_variant));
255 }
256 else
257 return next_config_step(cfg_without_hit | search_cfg::hit_all{});
258 }
259 else // Already statically configured.
260 {
261 static_assert(detail::search_traits<configuration_t>::has_hit_configuration,
262 "The hit strategy for the search algorithm was not configured. "
263 "Please refer to the configuration documentation of the search algorithm for more details.");
264
265 return next_config_step(cfg);
266 }
267}
268
269} // namespace seqan3::detail
Provides the configuration to define the hit strategies "hit_strata", "hit_all", "hit_all_best",...
Provides lazy template instantiation traits.
Provides the configuration for maximum number of errors for all error types.
Provides a type that combines multiple invocables.
Provides the seqan3::detail::policy_max_error.
Provides the seqan3::detail::policy_search_result_builder.
Provides seqan3::search_cfg::detail::result_type.
Provides the configuration for the content of the search result.
Provides seqan3::search_result.
Provides the algorithm to search in an index using search schemes.
Provides type traits for working with templates.
T tuple_size_v
Provides an approximate string matching algorithm based on simple backtracking. This should only be u...
Hide me