SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
search.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
12#include <algorithm>
13#include <ranges>
14
27
28namespace seqan3::detail
29{
34{
39 template <typename query_t>
40 static void validate_query_type()
41 {
42 using pure_query_t = std::remove_cvref_t<query_t>;
43 if constexpr (range_dimension_v<pure_query_t> == 1u)
44 {
45 static_assert(std::ranges::random_access_range<pure_query_t>,
46 "The query sequence must model random_access_range.");
47 static_assert(std::ranges::sized_range<pure_query_t>, "The query sequence must model sized_range.");
48 }
49 else
50 {
51 static_assert(std::ranges::forward_range<pure_query_t>, "The query collection must model forward_range.");
52 static_assert(std::ranges::sized_range<pure_query_t>, "The query collection must model sized_range.");
53 static_assert(std::ranges::random_access_range<std::ranges::range_value_t<pure_query_t>>,
54 "Elements of the query collection must model random_access_range.");
55 static_assert(std::ranges::sized_range<std::ranges::range_value_t<pure_query_t>>,
56 "Elements of the query collection must model sized_range.");
57 }
58 }
59};
60} // namespace seqan3::detail
61
62namespace seqan3
63{
99template <typename index_t,
100 std::ranges::forward_range queries_t,
101 typename configuration_t = decltype(search_cfg::default_configuration)>
102 requires std::ranges::forward_range<std::ranges::range_reference_t<queries_t>>
103 && std::same_as<range_innermost_value_t<queries_t>, typename index_t::alphabet_type>
104inline auto
105search(queries_t && queries, index_t const & index, configuration_t const & cfg = search_cfg::default_configuration)
106{
107 auto updated_cfg = detail::search_configurator::add_defaults(cfg);
108
109 detail::search_configuration_validator::validate_query_type<queries_t>();
110
111 size_t queries_size = std::ranges::distance(queries);
112 auto indexed_queries = views::zip(std::views::iota(size_t{0}, queries_size), std::forward<queries_t>(queries));
113
114 using indexed_queries_t = decltype(indexed_queries);
115
116 using query_t = std::ranges::range_reference_t<indexed_queries_t>;
117 auto [algorithm, complete_config] = detail::search_configurator::configure_algorithm<query_t>(updated_cfg, index);
118
119 using complete_configuration_t = decltype(complete_config);
121 using algorithm_result_t = typename traits_t::search_result_type;
125
126 // Select the execution handler for the search configuration.
127 auto select_execution_handler = [parallel = complete_config.get_or(search_cfg::parallel{})]()
128 {
129 if constexpr (std::same_as<execution_handler_t, detail::execution_handler_parallel>)
130 {
131 auto thread_count = parallel.thread_count;
132 if (!thread_count)
133 throw std::runtime_error{"You must configure the number of threads in seqan3::search_cfg::parallel."};
134
135 return execution_handler_t{*thread_count};
136 }
137 else
138 {
139 return execution_handler_t{};
140 }
141 };
142
143 // Finally, choose between two way execution returning an algorithm range or calling a user callback on every hit.
144 if constexpr (traits_t::has_user_callback)
145 {
146 select_execution_handler().bulk_execute(algorithm,
147 indexed_queries,
148 get<search_cfg::on_result>(complete_config).callback);
149 }
150 else
151 {
152 using executor_t = detail::algorithm_executor_blocking<indexed_queries_t,
153 decltype(algorithm),
154 algorithm_result_t,
155 execution_handler_t>;
156
157 return algorithm_result_generator_range{executor_t{std::move(indexed_queries),
158 std::move(algorithm),
159 algorithm_result_t{},
160 select_execution_handler()}};
161 }
162}
163
165// Convert query sequence if it does not match the alphabet type of the index.
167template <typename index_t,
168 std::ranges::forward_range queries_t,
169 typename configuration_t = decltype(search_cfg::default_configuration)>
170 requires std::ranges::forward_range<std::ranges::range_reference_t<queries_t>>
171 && (!std::same_as<range_innermost_value_t<queries_t>, typename index_t::alphabet_type>)
172inline auto search(queries_t && queries,
173 index_t const & index,
174 configuration_t const & cfg = search_cfg::default_configuration)
175{
176 static_assert(std::convertible_to<range_innermost_value_t<queries_t>, typename index_t::alphabet_type>,
177 "The alphabet of the text collection must be convertible to the alphabet of the index.");
178
179 if constexpr (range_dimension_v<queries_t> == 2u)
180 return search(queries | views::deep{views::convert<typename index_t::alphabet_type>}, index, cfg);
181 else
182 return search(queries | views::convert<typename index_t::alphabet_type>, index, cfg);
183}
184
185// Overload for a single query (not a collection of queries)
187template <typename index_t,
188 std::ranges::forward_range query_t,
189 typename configuration_t = decltype(search_cfg::default_configuration)>
190inline auto
191search(query_t && query, index_t const & index, configuration_t const & cfg = search_cfg::default_configuration)
192{
193 return search(std::views::single(std::forward<query_t>(query)), index, cfg);
194}
195
197template <typename index_t, typename configuration_t = decltype(search_cfg::default_configuration)>
198inline auto search(char const * const queries,
199 index_t const & index,
200 configuration_t const & cfg = search_cfg::default_configuration)
201{
202 return search(std::string_view{queries}, index, cfg);
203}
204
206template <typename index_t, typename configuration_t = decltype(search_cfg::default_configuration)>
208 index_t const & index,
209 configuration_t const & cfg = search_cfg::default_configuration)
210{
212 query.reserve(std::ranges::size(queries));
213 std::ranges::for_each(queries,
214 [&query](char const * const q)
215 {
216 query.push_back(std::string_view{q});
217 });
218 return search(std::move(query) | seqan3::detail::all, index, cfg);
219}
221
222} // namespace seqan3
Provides seqan3::detail::algorithm_executor_blocking.
Provides seqan3::detail::algorithm_result_generator_range.
Provides seqan3::detail::all.
An input range over the algorithm results generated by the underlying algorithm executor.
Definition algorithm_result_generator_range.hpp:45
A blocking algorithm executor for algorithms.
Definition algorithm_executor_blocking.hpp:60
Handles the parallel execution of algorithms.
Definition execution_handler_parallel.hpp:52
A global configuration type used to enable parallel execution of algorithms.
Definition configuration_element_parallel_mode.hpp:29
static auto add_defaults(configuration_t const &cfg)
Adds default configurations if they were not set by the user.
Definition search_configurator.hpp:133
A wrapper type around an existing view adaptor that enables "deep view" behaviour for that view.
Definition deep.hpp:101
Provides seqan3::configuration and utility functions.
Provides seqan3::views::deep.
Provides the default configuration for the seqan3::search() interface.
T for_each(T... args)
seqan::stl::views::all all
Returns a view that includes all elements of the range argument.
Definition all_view.hpp:35
constexpr configuration default_configuration
The default configuration: Compute all exact matches.
Definition default_configuration.hpp:26
auto search(queries_t &&queries, index_t const &index, configuration_t const &cfg=search_cfg::default_configuration)
Search a query or a range of queries in an index.
Definition search.hpp:105
seqan::stl::views::zip zip
A view adaptor that takes several views and returns tuple-like values from every i-th element of each...
Definition zip.hpp:24
The internal SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
Provides seqan3::search_cfg::on_result.
Provides seqan3::search_cfg::parallel configuration.
T push_back(T... args)
T reserve(T... args)
Provides seqan3::detail::search_configurator.
Provides seqan3::detail::search_traits.
Handles the sequential execution of algorithms.
Definition execution_handler_sequential.hpp:31
Class used to validate the search configuration.
Definition search.hpp:34
static void validate_query_type()
Validates the query type to model std::ranges::random_access_range and std::ranges::sized_range.
Definition search.hpp:40
A collection of traits extracted from the search configuration.
Definition search_traits.hpp:31
Provides seqan3::views::convert.
Provides seqan3::views::zip.
Hide me