SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
unidirectional_search_algorithm.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2021, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2021, 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 
14 #pragma once
15 
16 #include <seqan3/std/ranges>
17 #include <type_traits>
18 
24 
25 namespace seqan3::detail
26 {
27 
29 enum class error_type : uint8_t
30 {
31  deletion,
32  insertion,
33  matchmm,
34  none
35 };
36 
45 template <typename configuration_t, typename index_t, typename ...policies_t>
46 class unidirectional_search_algorithm : protected policies_t...
47 {
48 private:
50  using traits_t = search_traits<configuration_t>;
52  using search_result_type = typename traits_t::search_result_type;
53 
54  static_assert(!std::same_as<search_result_type, empty_type>, "The search result type was not configured.");
55 
56 public:
60  unidirectional_search_algorithm() = default;
61  unidirectional_search_algorithm(unidirectional_search_algorithm const &) = default;
62  unidirectional_search_algorithm(unidirectional_search_algorithm &&) = default;
63  unidirectional_search_algorithm & operator=(unidirectional_search_algorithm const &) = default;
64  unidirectional_search_algorithm & operator=(unidirectional_search_algorithm &&) = default;
65  ~unidirectional_search_algorithm() = default;
66 
77  unidirectional_search_algorithm(configuration_t const & cfg, index_t const & index) : policies_t{cfg}...
78  {
79  stratum = cfg.get_or(search_cfg::hit_strata{0}).stratum;
80  index_ptr = &index;
81  }
83 
104  template <typename indexed_query_t, typename callback_t>
106  requires (std::tuple_size_v<indexed_query_t> == 2) &&
107  std::ranges::forward_range<std::tuple_element_t<1, indexed_query_t>> &&
108  std::invocable<callback_t, search_result_type>
110  void operator()(indexed_query_t && indexed_query, callback_t && callback)
111  {
112  auto && [query_idx, query] = indexed_query;
113  auto error_state = this->max_error_counts(query); // see policy_max_error
114 
115  // construct internal delegate for collecting hits for later filtering (if necessary)
117  delegate = [&internal_hits] (auto const & it)
118  {
119  internal_hits.push_back(it);
120  };
121 
122  perform_search_by_hit_strategy(internal_hits, query, error_state);
123 
124  this->make_results(std::move(internal_hits), query_idx, callback); // see policy_search_result_builder
125  }
126 
127 private:
129  index_t const * index_ptr{nullptr};
130 
132  std::function<void(typename index_t::cursor_type const &)> delegate;
133 
135  uint8_t stratum{};
136 
137  // forward declaration
138  template <bool abort_on_hit, typename query_t>
139  bool search_trivial(typename index_t::cursor_type cur,
140  query_t & query,
141  typename index_t::cursor_type::size_type const query_pos,
142  search_param const error_left,
143  error_type const prev_error);
144 
151  template <typename query_t>
152  void perform_search_by_hit_strategy(std::vector<typename index_t::cursor_type> & internal_hits,
153  query_t & query,
154  search_param error_state)
155  {
156  if constexpr (!traits_t::search_all_hits)
157  {
158  auto max_total = error_state.total;
159  error_state.total = 0; // start search with less errors
160 
161  while (internal_hits.empty() && error_state.total <= max_total)
162  {
163  // * If you only want the best hit (traits_t::search_single_best_hit), you stop after finding the
164  // first hit, the hit with the least errors (`abort_on_hit` is true).
165  // * If you are in strata mode (traits_t::search_strata_hits), you do the same as with best hits,
166  // but then do the extra step afterwards (`abort_on_hit` is true).
167  // * If you want all best hits (traits_t::search_all_best_hits), you do not stop after the first
168  // hit but continue the current search algorithm/max_error pattern (`abort_on_hit` is true).
169  constexpr bool abort_on_hit = !traits_t::search_all_best_hits;
170  search_trivial<abort_on_hit>(index_ptr->cursor(), query, 0, error_state, error_type::none);
171  ++error_state.total;
172  }
173 
174  if constexpr (traits_t::search_strata_hits)
175  {
176  if (!internal_hits.empty())
177  {
178  internal_hits.clear();
179  error_state.total += stratum - 1;
180  search_trivial<false>(index_ptr->cursor(), query, 0, error_state, error_type::none);
181  }
182  }
183  }
184  else // traits_t::search_all
185  {
186  // If you want to find all hits, you cannot stop once you found any hit (<false>)
187  // since you have to find all paths in the search tree that satisfy the hit condition.
188  search_trivial<false>(index_ptr->cursor(), query, 0, error_state, error_type::none);
189  }
190  }
191 };
192 
211 template <typename configuration_t, typename index_t, typename ...policies_t>
212 template <bool abort_on_hit, typename query_t>
213 inline bool unidirectional_search_algorithm<configuration_t, index_t, policies_t...>::search_trivial(
214  typename index_t::cursor_type cur,
215  query_t & query,
216  typename index_t::cursor_type::size_type const query_pos,
217  search_param const error_left,
218  error_type const prev_error)
219 {
220  // Exact case (end of query sequence or no errors left)
221  if (query_pos == std::ranges::size(query) || error_left.total == 0)
222  {
223  // If not at end of query sequence, try searching the remaining suffix without any errors.
224  using drop_size_t = std::ranges::range_difference_t<query_t>;
225  if (query_pos == std::ranges::size(query) ||
226  cur.extend_right(std::views::drop(query, static_cast<drop_size_t>(query_pos))))
227  {
228  delegate(cur);
229  return true;
230  }
231  }
232  // Approximate case
233  else
234  {
235  // Insertion
236  // Only allow insertions if there is no match and we are not at the beginning of the query.
237  bool const allow_insertion = (cur.query_length() > 0) ? cur.last_rank() != seqan3::to_rank(query[query_pos]) : true;
238 
239  if (allow_insertion && (prev_error != error_type::deletion || error_left.substitution == 0) &&
240  error_left.insertion > 0)
241  {
242  search_param error_left2{error_left};
243  error_left2.insertion--;
244  error_left2.total--;
245 
246  // Always perform a recursive call. Abort recursion if and only if recursive call found a hit and
247  // abort_on_hit is set to true.
248  if (search_trivial<abort_on_hit>(cur,
249  query, query_pos + 1,
250  error_left2,
251  error_type::insertion) &&
252  abort_on_hit)
253  {
254  return true;
255  }
256  }
257 
258  // Do not allow deletions at the beginning of the query sequence
259  if (((query_pos > 0 && error_left.deletion > 0) || error_left.substitution > 0) && cur.extend_right())
260  {
261  do
262  {
263  // Match (when error_left.substitution > 0) and Mismatch
264  if (error_left.substitution > 0)
265  {
266  bool delta = cur.last_rank() != seqan3::to_rank(query[query_pos]);
267  search_param error_left2{error_left};
268  error_left2.total -= delta;
269  error_left2.substitution -= delta;
270 
271  if (search_trivial<abort_on_hit>(cur,
272  query,
273  query_pos + 1,
274  error_left2,
275  error_type::matchmm) &&
276  abort_on_hit)
277  {
278  return true;
279  }
280  }
281 
282  // Deletion (Do not allow deletions at the beginning of the query sequence.)
283  if (query_pos > 0)
284  {
285  // Match (when error_left.substitution == 0)
286  if (error_left.substitution == 0 && cur.last_rank() == seqan3::to_rank(query[query_pos]))
287  {
288  if (search_trivial<abort_on_hit>(cur,
289  query,
290  query_pos + 1,
291  error_left,
292  error_type::matchmm) &&
293  abort_on_hit)
294  {
295  return true;
296  }
297  }
298 
299  // Deletions at the end of the sequence are not allowed. When the algorithm
300  // arrives here, it cannot be at the end of the query and since deletions do not touch the query
301  // (i.e. increase query_pos) it won't be at the end of the query after the deletion.
302  // Do not allow deletions after an insertion.
303  if ((prev_error != error_type::insertion || error_left.substitution == 0) &&
304  error_left.deletion > 0)
305  {
306  search_param error_left2{error_left};
307  error_left2.total--;
308  error_left2.deletion--;
309  // Only search for characters different from the corresponding query character.
310  // (Same character is covered by a match.)
311  if (cur.last_rank() != seqan3::to_rank(query[query_pos]))
312  {
313  if (search_trivial<abort_on_hit>(cur,
314  query,
315  query_pos,
316  error_left2,
317  error_type::deletion) &&
318  abort_on_hit)
319  {
320  return true;
321  }
322  }
323  }
324  }
325  } while (cur.cycle_back());
326  }
327  else
328  {
329  // Match (when error_left.substitution == 0)
330  if (cur.extend_right(query[query_pos]))
331  {
332  if (search_trivial<abort_on_hit>(cur,
333  query,
334  query_pos + 1,
335  error_left,
336  error_type::matchmm) &&
337  abort_on_hit)
338  {
339  return true;
340  }
341  }
342  }
343  }
344 
345  return false;
346 }
348 
349 } // namespace seqan3::detail
Core alphabet concept and free function/type trait wrappers.
T clear(T... args)
T empty(T... args)
constexpr auto to_rank
Return the rank representation of a (semi-)alphabet object.
Definition: concept.hpp:155
@ none
No flag is set.
Definition: debug_stream_type.hpp:32
typename decltype(detail::split_after< i >(list_t{}))::second_type drop
Return a seqan3::type_list of the types in the input type list, except the first n.
Definition: traits.hpp:388
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:151
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:74
T push_back(T... args)
Adaptations of concepts from the Ranges TS.
Provides the concept for seqan3::detail::sdsl_index.
Provides data structures used by different search algorithms.
Provides seqan3::detail::search_traits.
Forward declares seqan3::detail::test_accessor.