SeqAn3
The Modern C++ library for sequence analysis.
search.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2019, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2019, Knut Reinert & MPI für molekulare Genetik
4 // This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5 // shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6 // -----------------------------------------------------------------------------------------------------
7 
13 #pragma once
14 
19 #include <seqan3/std/algorithm>
20 #include <seqan3/std/ranges>
21 
22 namespace seqan3
23 {
24 
85 template <fm_index_specialisation index_t, typename queries_t, typename configuration_t = decltype(search_cfg::default_configuration)>
86 inline auto search(queries_t && queries,
87  index_t const & index,
88  configuration_t const & cfg = search_cfg::default_configuration)
89 {
90  if constexpr(dimension_v<queries_t> == 1u)
91  {
92  static_assert(std::ranges::random_access_range<queries_t>, "The query sequence must model random_access_range.");
93  static_assert(std::ranges::sized_range<queries_t>, "The query sequence must model sized_range.");
94  }
95  else
96  {
97  static_assert(std::ranges::forward_range<queries_t>, "The query collection must model forward_range.");
98  static_assert(std::ranges::sized_range<queries_t>, "The query collection must model sized_range.");
99  static_assert(std::ranges::random_access_range<value_type_t<queries_t>>,
100  "Elements of the query collection must model random_access_range.");
101  static_assert(std::ranges::sized_range<value_type_t<queries_t>>,
102  "Elements of the query collection must model sized_range.");
103  }
104 
105  static_assert(detail::is_type_specialisation_of_v<remove_cvref_t<configuration_t>, configuration>,
106  "cfg must be a specialisation of seqan3::configuration.");
107 
108  using cfg_t = remove_cvref_t<configuration_t>;
109 
110  if constexpr (cfg_t::template exists<search_cfg::max_error>())
111  {
112  auto & [total, subs, ins, del] = get<search_cfg::max_error>(cfg).value;
113  if (subs > total)
114  throw std::invalid_argument("The substitution error threshold is higher than the total error threshold.");
115  if (ins > total)
116  throw std::invalid_argument("The insertion error threshold is higher than the total error threshold.");
117  if (del > total)
118  throw std::invalid_argument("The deletion error threshold is higher than the total error threshold.");
119  }
120  else if constexpr (cfg_t::template exists<search_cfg::max_error_rate>())
121  {
122  auto & [total, subs, ins, del] = get<search_cfg::max_error_rate>(cfg).value;
123  if (subs > total)
124  throw std::invalid_argument("The substitution error threshold is higher than the total error threshold.");
125  if (ins > total)
126  throw std::invalid_argument("The insertion error threshold is higher than the total error threshold.");
127  if (del > total)
128  throw std::invalid_argument("The deletion error threshold is higher than the total error threshold.");
129  }
130 
131  if constexpr (cfg_t::template exists<search_cfg::mode>())
132  {
133  if constexpr (cfg_t::template exists<search_cfg::output>())
134  return detail::search_all(index, queries, cfg);
135  else
136  return detail::search_all(index, queries, cfg | search_cfg::output{search_cfg::text_position});
137  }
138  else
139  {
140  configuration const cfg2 = cfg | search_cfg::mode{search_cfg::all};
141  if constexpr (cfg_t::template exists<search_cfg::output>())
142  return detail::search_all(index, queries, cfg2);
143  else
144  return detail::search_all(index, queries, cfg2 | search_cfg::output{search_cfg::text_position});
145  }
146 }
149 template <fm_index_specialisation index_t, typename configuration_t = decltype(search_cfg::default_configuration)>
150 inline auto search(char const * const queries,
151  index_t const & index,
152  configuration_t const & cfg = search_cfg::default_configuration)
153 {
154  return search(std::string_view{queries}, index, cfg);
155 }
156 
158 template <fm_index_specialisation index_t, typename configuration_t = decltype(search_cfg::default_configuration)>
159 inline auto search(std::initializer_list<char const * const> const & queries,
160  index_t const & index,
161  configuration_t const & cfg = search_cfg::default_configuration)
162 {
164  query.reserve(std::ranges::size(queries));
165  std::ranges::for_each(queries, [&query] (char const * const q) { query.push_back(std::string_view{q}); });
166  return search(query, index, cfg);
167 }
169 
171 
172 } // namespace seqan3
seqan3::search
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:86
all.hpp
Meta-header for the FM index module.
std::string_view
std::vector::reserve
T reserve(T... args)
std::vector
configuration.hpp
Provides seqan3::detail::configuration and utility functions.
algorithm
Adaptations of algorithms from the Ranges TS.
std::vector::push_back
T push_back(T... args)
seqan3::configuration
Collection of elements to configure an algorithm.
Definition: configuration.hpp:81
seqan3::value_type_t
typename value_type< t >::type value_type_t
Shortcut for seqan3::value_type (transformation_trait shortcut).
Definition: pre.hpp:48
seqan3::search_cfg::default_configuration
constexpr configuration default_configuration
The default configuration.
Definition: default_configuration.hpp:28
seqan3
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:36
persist.hpp
Provides seqan3::views::persist.
std::invalid_argument
seqan3::search_cfg::all
constexpr detail::search_mode_all all
Configuration element to receive all hits within the error bounds.
Definition: mode.hpp:43
seqan3::pack_traits::size
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:116
ranges
Adaptations of concepts from the Ranges TS.
std::remove_cv_t
seqan3::search_cfg::mode
Configuration element to determine the search mode.
Definition: mode.hpp:86
seqan3::search_cfg::output
Configuration element to determine the output type of hits.
Definition: output.hpp:59
search.hpp
Provides the public interface for search algorithms.
seqan3::search_cfg::text_position
constexpr detail::search_output_text_position text_position
Configuration element to receive all hits within the lowest number of errors.
Definition: output.hpp:42
std::initializer_list