SeqAn3  3.0.2
The Modern C++ library for sequence analysis.
edit_distance_unbanded.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2020, 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 
15 #include <algorithm>
16 #include <seqan3/std/algorithm>
17 #include <bitset>
18 #include <seqan3/std/ranges>
19 #include <utility>
20 
28 
29 namespace seqan3::detail
30 {
34 template <typename derived_t, typename edit_traits>
35 class edit_distance_unbanded_max_errors_policy :
37  edit_traits
39 {
40 protected:
41  static_assert(edit_traits::use_max_errors, "This policy assumes that edit_traits::use_max_errors is true.");
42 
44  friend derived_t;
45 
49  edit_distance_unbanded_max_errors_policy() noexcept = default;
50  edit_distance_unbanded_max_errors_policy(edit_distance_unbanded_max_errors_policy const &) noexcept
51  = default;
52  edit_distance_unbanded_max_errors_policy(edit_distance_unbanded_max_errors_policy &&) noexcept
53  = default;
54  edit_distance_unbanded_max_errors_policy & operator=(edit_distance_unbanded_max_errors_policy const &) noexcept
55  = default;
56  edit_distance_unbanded_max_errors_policy & operator=(edit_distance_unbanded_max_errors_policy &&) noexcept
57  = default;
58  ~edit_distance_unbanded_max_errors_policy() noexcept = default;
59 
61  using typename edit_traits::word_type;
62  using typename edit_traits::score_type;
63 
68  score_type max_errors{255};
71  size_t last_block{0u};
73  word_type last_score_mask{};
75 
80  void max_errors_init(size_t block_count) noexcept
82  {
83  derived_t * self = static_cast<derived_t *>(this);
84 
85  max_errors = -get<align_cfg::min_score>(self->config).score;
86 
87  assert(max_errors >= score_type{0});
88 
89  if (std::ranges::empty(self->query)) // [[unlikely]]
90  {
91  last_block = 0u;
92  self->score_mask = 0u;
93  last_score_mask = self->score_mask;
94  return;
95  }
96 
97  last_block = block_count - 1u;
98  last_score_mask = self->score_mask;
99 
100  // local_max_errors either stores the maximal number of _score (me.max_errors) or the needle size minus one. It
101  // is used for the mask computation and setting the initial score (the minus one is there because of the Ukkonen
102  // trick).
103  size_t const local_max_errors = std::min<size_t>(max_errors, std::ranges::size(self->query) - 1u);
104  self->score_mask = word_type{1u} << (local_max_errors % self->word_size);
105  last_block = std::min(local_max_errors / self->word_size, last_block);
106  self->_score = local_max_errors + 1u;
107  }
108 
110  bool is_last_active_cell_within_last_row() const noexcept
111  {
112  derived_t const * self = static_cast<derived_t const *>(this);
113  return (self->score_mask == this->last_score_mask) && (this->last_block == self->vp.size() - 1u);
114  }
115 
117  bool prev_last_active_cell() noexcept
118  {
119  derived_t * self = static_cast<derived_t *>(this);
120  self->score_mask >>= 1u;
121  if (self->score_mask != 0u)
122  return true;
123 
124  if constexpr (edit_traits::is_global)
125  {
126  if (last_block == 0u) // [[unlikely]]
127  return false;
128  }
129 
130  last_block--;
131 
132  self->score_mask = word_type{1u} << (edit_traits::word_size - 1u);
133  return true;
134  }
135 
137  void next_last_active_cell() noexcept
138  {
139  derived_t * self = static_cast<derived_t *>(this);
140  self->score_mask <<= 1u;
141  if (self->score_mask)
142  return;
143 
144  self->score_mask = 1u;
145  last_block++;
146  }
147 
151  bool update_last_active_cell() noexcept
152  {
153  derived_t * self = static_cast<derived_t *>(this);
154  // update the last active cell
155  while (!(self->_score <= max_errors))
156  {
157  self->advance_score(self->vn[last_block], self->vp[last_block], self->score_mask);
158  if (!prev_last_active_cell())
159  {
160  // prev_last_active_cell = false can only happen for global alignments
161  assert(edit_traits::is_global);
162  // we abort here if we don't need to compute a matrix, because the continued
163  // computation can't produce an alignment.
164  return !edit_traits::compute_matrix;
165  }
166  }
167 
168  if (is_last_active_cell_within_last_row())
169  {
170  assert(self->_score <= max_errors);
171 
172  if constexpr(edit_traits::is_semi_global)
173  self->update_best_score();
174 
175  return self->on_hit();
176  }
177  else
178  {
179  next_last_active_cell();
180  self->advance_score(self->vp[last_block], self->vn[last_block], self->score_mask);
181  }
182 
183  return false;
184  }
186 
188  static size_t max_rows(word_type const score_mask, unsigned const last_block,
189  score_type const score, score_type const max_errors) noexcept
190  {
191  using score_matrix_type = typename edit_traits::score_matrix_type;
192  return score_matrix_type::max_rows(score_mask,
193  last_block,
194  score,
195  max_errors);
196  }
197 };
198 
202 template <typename derived_t, typename edit_traits>
203 class edit_distance_unbanded_global_policy :
205  edit_traits
207 {
208 protected:
209  static_assert(edit_traits::is_global || edit_traits::is_semi_global,
210  "This policy assumes that edit_traits::is_global or edit_traits::is_semi_global is true.");
211 
213  friend derived_t;
214 
218  edit_distance_unbanded_global_policy() noexcept = default;
219  edit_distance_unbanded_global_policy(edit_distance_unbanded_global_policy const &) noexcept
220  = default;
221  edit_distance_unbanded_global_policy(edit_distance_unbanded_global_policy &&) noexcept
222  = default;
223  edit_distance_unbanded_global_policy & operator=(edit_distance_unbanded_global_policy const &) noexcept
224  = default;
225  edit_distance_unbanded_global_policy & operator=(edit_distance_unbanded_global_policy &&) noexcept
226  = default;
227  ~edit_distance_unbanded_global_policy() noexcept = default;
228 
231  using typename edit_traits::score_type;
232 
241  score_type _best_score{};
243 
248  void score_init() noexcept
250  {
251  derived_t const * self = static_cast<derived_t const *>(this);
252  _best_score = self->_score;
253  }
254 
256  bool is_valid() const noexcept
257  {
258  [[maybe_unused]] derived_t const * self = static_cast<derived_t const *>(this);
259  // This condition uses the observation that after each computation of a column, _score has either the initial
260  // value of the first row (i.e. the entire column consist of INF's), has the value _score = max_errors + 1
261  // (there exists a cell within the column that has value <= max_errors, but is not on the last row) or _score <=
262  // max_errors (the score of the last active cell is <= max_errors)
263  if constexpr(edit_traits::use_max_errors)
264  return _best_score <= self->max_errors;
265 
266  // When not using max_errors there is always a valid alignment, because the last row will always be updated and
267  // with it the score.
268  return true;
269  }
270 
272  alignment_coordinate invalid_coordinate() const noexcept
273  {
274  derived_t const * self = static_cast<derived_t const *>(this);
275  return {column_index_type{std::ranges::size(self->database)}, row_index_type{std::ranges::size(self->query)}};
276  }
277 
279  void update_best_score() noexcept
280  {
281  derived_t const * self = static_cast<derived_t const *>(this);
282  _best_score = self->_score;
283  }
284 
286  size_t end_positions_first() const noexcept
287  {
288  derived_t const * self = static_cast<derived_t const *>(this);
289  return std::ranges::size(self->database);
290  }
292 
293 public:
298  std::optional<score_type> score() const noexcept
301  {
302  derived_t const * self = static_cast<derived_t const *>(this);
303  static_assert(edit_traits::compute_score, "score() can only be computed if you specify the result type within "
304  "your alignment config.");
305  if (!self->is_valid())
306  return std::nullopt;
307 
308  return -_best_score;
309  }
310 
313  alignment_coordinate end_positions() const noexcept
314  {
315  derived_t const * self = static_cast<derived_t const *>(this);
316  static_assert(edit_traits::compute_end_positions, "end_positions() can only be computed if you specify the "
317  "result type within your alignment config.");
318  if (!self->is_valid())
319  return self->invalid_coordinate();
320 
321  column_index_type const first{self->end_positions_first()};
322  row_index_type const second{std::ranges::size(self->query)};
323  return {first, second};
324  }
326 };
327 
329 template <typename derived_t, typename edit_traits>
330 class edit_distance_unbanded_semi_global_policy :
331  public edit_distance_unbanded_global_policy<derived_t, edit_traits>
332 {
333 protected:
334  static_assert(edit_traits::is_semi_global, "This policy assumes that edit_traits::is_semi_global is true.");
335 
337  friend derived_t;
338 
342  edit_distance_unbanded_semi_global_policy() noexcept = default;
343  edit_distance_unbanded_semi_global_policy(edit_distance_unbanded_semi_global_policy const &) noexcept
344  = default;
345  edit_distance_unbanded_semi_global_policy(edit_distance_unbanded_semi_global_policy &&) noexcept
346  = default;
347  edit_distance_unbanded_semi_global_policy & operator=(edit_distance_unbanded_semi_global_policy const &) noexcept
348  = default;
349  edit_distance_unbanded_semi_global_policy & operator=(edit_distance_unbanded_semi_global_policy &&) noexcept
350  = default;
351  ~edit_distance_unbanded_semi_global_policy() noexcept = default;
352 
355  using base_t = edit_distance_unbanded_global_policy<derived_t, edit_traits>;
357  using database_iterator = typename edit_traits::database_iterator;
358  using base_t::_best_score;
360 
372  database_iterator _best_score_col{};
374 
379  void score_init() noexcept
381  {
382  derived_t const * self = static_cast<derived_t const *>(this);
383  base_t::score_init();
384  _best_score_col = self->database_it_end;
385  }
386 
388  void update_best_score() noexcept
389  {
390  derived_t const * self = static_cast<derived_t const *>(this);
391  // we have to make sure that update_best_score is only called after a score update within the last row.
392  if constexpr(edit_traits::use_max_errors)
393  {
394  assert(std::ranges::empty(self->query) || self->is_last_active_cell_within_last_row());
395  }
396 
397  _best_score_col = (self->_score <= _best_score) ? self->database_it : _best_score_col;
398  _best_score = (self->_score <= _best_score) ? self->_score : _best_score;
399  }
400 
402  size_t end_positions_first() const noexcept
403  {
404  derived_t const * self = static_cast<derived_t const *>(this);
405  // offset == 0u is a special case if database sequence is empty, because in this case the best column is zero.
406  size_t offset = std::ranges::empty(self->database) ? 0u : 1u;
407  return std::ranges::distance(std::ranges::begin(self->database), _best_score_col) + offset;
408  }
410 };
411 
415 template <typename derived_t, typename edit_traits>
416 class edit_distance_unbanded_score_matrix_policy :
418  edit_traits
420 {
421 protected:
422  static_assert(edit_traits::compute_score_matrix,
423  "This policy assumes that edit_traits::compute_score_matrix is true.");
424 
426  friend derived_t;
427 
431  edit_distance_unbanded_score_matrix_policy() noexcept = default;
432  edit_distance_unbanded_score_matrix_policy(edit_distance_unbanded_score_matrix_policy const &) noexcept
433  = default;
434  edit_distance_unbanded_score_matrix_policy(edit_distance_unbanded_score_matrix_policy &&) noexcept
435  = default;
436  edit_distance_unbanded_score_matrix_policy & operator=(edit_distance_unbanded_score_matrix_policy const &) noexcept
437  = default;
438  edit_distance_unbanded_score_matrix_policy & operator=(edit_distance_unbanded_score_matrix_policy &&) noexcept
439  = default;
440  ~edit_distance_unbanded_score_matrix_policy() noexcept = default;
441 
443  using typename edit_traits::score_matrix_type;
444 
449  score_matrix_type _score_matrix{};
452 
465  void score_matrix_init()
466  {
467  derived_t const * self = static_cast<derived_t const *>(this);
468 
469  _score_matrix = score_matrix_type{std::ranges::size(self->query) + 1u};
470  _score_matrix.reserve(std::ranges::size(self->database) + 1u);
471  }
473 
474 public:
482  score_matrix_type const & score_matrix() const noexcept
483  {
484  static_assert(edit_traits::compute_score_matrix, "score_matrix() can only be computed if you specify the "
485  "result type within your alignment config.");
486  return _score_matrix;
487  }
489 };
490 
494 template <typename derived_t, typename edit_traits>
495 class edit_distance_unbanded_trace_matrix_policy :
497  edit_traits
499 {
500 protected:
501  static_assert(edit_traits::compute_trace_matrix,
502  "This policy assumes that edit_traits::compute_trace_matrix is true.");
503 
505  friend derived_t;
506 
510  edit_distance_unbanded_trace_matrix_policy() noexcept = default;
511  edit_distance_unbanded_trace_matrix_policy(edit_distance_unbanded_trace_matrix_policy const &) noexcept
512  = default;
513  edit_distance_unbanded_trace_matrix_policy(edit_distance_unbanded_trace_matrix_policy &&) noexcept
514  = default;
515  edit_distance_unbanded_trace_matrix_policy & operator=(edit_distance_unbanded_trace_matrix_policy const &) noexcept
516  = default;
517  edit_distance_unbanded_trace_matrix_policy & operator=(edit_distance_unbanded_trace_matrix_policy &&) noexcept
518  = default;
519  ~edit_distance_unbanded_trace_matrix_policy() noexcept = default;
520 
522  using typename edit_traits::word_type;
523  using typename edit_traits::trace_matrix_type;
524  using typename edit_traits::alignment_result_type;
525 
530  std::vector<word_type> hp{};
534 
536  trace_matrix_type _trace_matrix{};
538 
551  void trace_matrix_init(size_t block_count)
552  {
553  derived_t const * self = static_cast<derived_t const *>(this);
554 
555  _trace_matrix = trace_matrix_type{std::ranges::size(self->query) + 1u};
556  _trace_matrix.reserve(std::ranges::size(self->database) + 1u);
557 
558  hp.resize(block_count, 0u);
559  db.resize(block_count, 0u);
560  }
562 
563 public:
568  trace_matrix_type const & trace_matrix() const noexcept
570  {
571  static_assert(edit_traits::compute_trace_matrix, "trace_matrix() can only be computed if you specify the "
572  "result type within your alignment config.");
573  return _trace_matrix;
574  }
575 
578  alignment_coordinate begin_positions() const noexcept
579  {
580  derived_t const * self = static_cast<derived_t const *>(this);
581  static_assert(edit_traits::compute_begin_positions, "begin_positions() can only be computed if you specify the "
582  "result type within your alignment config.");
583  if (!self->is_valid())
584  return self->invalid_coordinate();
585 
586  alignment_coordinate const back = self->end_positions();
587  return alignment_begin_positions(trace_matrix(), back);
588  }
589 
592  auto alignment() const noexcept
593  {
594  using alignment_t = std::remove_cvref_t<decltype(std::declval<alignment_result_type &>().alignment())>;
595 
596  derived_t const * self = static_cast<derived_t const *>(this);
597  static_assert(edit_traits::compute_sequence_alignment, "alignment() can only be computed if you specify the "
598  "result type within your alignment config.");
599 
600  if (!self->is_valid())
601  return alignment_t{};
602 
603  return alignment_trace<alignment_t>(self->database,
604  self->query,
605  trace_matrix(),
606  self->end_positions(),
607  begin_positions());
608  }
610 };
611 
615 template <typename value_t>
616 class proxy_reference
617 {
618 public:
622  proxy_reference() noexcept = default;
623  proxy_reference(proxy_reference const &) noexcept = default;
624  proxy_reference(proxy_reference &&) noexcept = default;
625  proxy_reference & operator=(proxy_reference const &) noexcept = default;
626  proxy_reference & operator=(proxy_reference &&) noexcept = default;
627  ~proxy_reference() = default;
628 
630  proxy_reference(value_t & t) noexcept
631  : ptr(std::addressof(t))
632  {}
633 
634  proxy_reference(value_t &&) = delete;
635 
637  template <typename other_value_t>
639  requires std::convertible_to<other_value_t, value_t>
641  proxy_reference & operator=(other_value_t && u) noexcept
642  {
643  get() = std::forward<other_value_t>(u);
644  return *this;
645  }
647 
649  value_t & get() const noexcept
650  {
651  assert(ptr != nullptr);
652  return *ptr;
653  }
654 
656  operator value_t & () const noexcept
657  {
658  return get();
659  }
660 
661 private:
663  value_t * ptr{nullptr};
664 };
665 
677 template <std::ranges::viewable_range database_t,
678  std::ranges::viewable_range query_t,
679  typename align_config_t,
680  typename edit_traits>
681 class edit_distance_unbanded :
683 // Hide this section in doxygen, because it messes up the inheritance.
684  public edit_distance_base<
685  edit_traits::use_max_errors,
686  edit_distance_unbanded_max_errors_policy,
687  edit_traits,
688  edit_distance_unbanded<database_t, query_t, align_config_t, edit_traits>>,
689  public edit_distance_base<
690  edit_traits::is_global,
691  edit_distance_unbanded_global_policy,
692  edit_traits,
693  edit_distance_unbanded<database_t, query_t, align_config_t, edit_traits>>,
694  public edit_distance_base<
695  edit_traits::is_semi_global,
696  edit_distance_unbanded_semi_global_policy,
697  edit_traits,
698  edit_distance_unbanded<database_t, query_t, align_config_t, edit_traits>>,
699  public edit_distance_base<
700  edit_traits::compute_score_matrix,
701  edit_distance_unbanded_score_matrix_policy,
702  edit_traits,
703  edit_distance_unbanded<database_t, query_t, align_config_t, edit_traits>>,
704  public edit_distance_base<
705  edit_traits::compute_trace_matrix,
706  edit_distance_unbanded_trace_matrix_policy,
707  edit_traits,
708  edit_distance_unbanded<database_t, query_t, align_config_t, edit_traits>>
710 {
711 public:
712  using typename edit_traits::word_type;
713  using typename edit_traits::score_type;
714  using typename edit_traits::database_type;
715  using typename edit_traits::query_type;
716  using typename edit_traits::align_config_type;
717  using edit_traits::word_size;
718 
719 private:
721  template <typename other_derived_t, typename other_edit_traits>
722  friend class edit_distance_unbanded_max_errors_policy;
724  template <typename other_derived_t, typename other_edit_traits>
725  friend class edit_distance_unbanded_global_policy;
727  template <typename other_derived_t, typename other_edit_traits>
728  friend class edit_distance_unbanded_semi_global_policy;
730  template <typename other_derived_t, typename other_edit_traits>
731  friend class edit_distance_unbanded_score_matrix_policy;
733  template <typename other_derived_t, typename other_edit_traits>
734  friend class edit_distance_unbanded_trace_matrix_policy;
735 
736  using typename edit_traits::database_iterator;
737  using typename edit_traits::query_alphabet_type;
738  using typename edit_traits::alignment_result_type;
739  using edit_traits::use_max_errors;
740  using edit_traits::is_semi_global;
741  using edit_traits::is_global;
742  using edit_traits::compute_score;
743  using edit_traits::compute_end_positions;
744  using edit_traits::compute_begin_positions;
745  using edit_traits::compute_sequence_alignment;
746  using edit_traits::compute_score_matrix;
747  using edit_traits::compute_trace_matrix;
748  using edit_traits::compute_matrix;
749  using typename edit_traits::score_matrix_type;
750  using typename edit_traits::trace_matrix_type;
751 
753  database_t database;
755  query_t query;
757  align_config_t config;
758 
760  static constexpr word_type hp0 = is_global ? 1u : 0u;
762  static constexpr word_type hn0 = 0u;
764  static constexpr word_type vp0 = ~word_type{0u};
766  static constexpr word_type vn0 = 0u;
767 
769  score_type _score{};
776  word_type score_mask{0u};
787  std::vector<word_type> bit_masks{};
788 
790  database_iterator database_it{};
792  database_iterator database_it_end{};
793 
795  struct compute_state_trace_matrix
796  {
798  proxy_reference<word_type> db{};
799  };
800 
802  struct compute_state : enable_state_t<compute_trace_matrix, compute_state_trace_matrix>
803  {
806 
808  word_type b{};
810  word_type d0{};
812  hp_type hp{};
814  word_type hn{};
816  proxy_reference<word_type> vp{};
818  proxy_reference<word_type> vn{};
820  word_type carry_d0{};
822  word_type carry_hp{hp0};
824  word_type carry_hn{};
825  };
826 
828  void add_state()
829  {
830  if constexpr(!use_max_errors && compute_score_matrix)
831  this->_score_matrix.add_column(vp, vn);
832 
833  if constexpr(!use_max_errors && compute_trace_matrix)
834  this->_trace_matrix.add_column(this->hp, this->db, vp);
835 
836  if constexpr(use_max_errors && compute_matrix)
837  {
838  size_t max_rows = this->max_rows(score_mask, this->last_block, _score, this->max_errors);
839  if constexpr(compute_score_matrix)
840  this->_score_matrix.add_column(vp, vn, max_rows);
841 
842  if constexpr(compute_trace_matrix)
843  this->_trace_matrix.add_column(this->hp, this->db, vp, max_rows);
844  }
845  }
846 
847 public:
851  edit_distance_unbanded() = delete;
853  edit_distance_unbanded(edit_distance_unbanded const &) = default;
854  edit_distance_unbanded(edit_distance_unbanded &&) = default;
855  edit_distance_unbanded & operator=(edit_distance_unbanded const &) = default;
856  edit_distance_unbanded & operator=(edit_distance_unbanded &&) = default;
857  ~edit_distance_unbanded() = default;
858 
865  edit_distance_unbanded(database_t _database,
866  query_t _query,
867  align_config_t _config,
868  edit_traits const & SEQAN3_DOXYGEN_ONLY(_traits)) :
869  database{std::forward<database_t>(_database)},
870  query{std::forward<query_t>(_query)},
871  config{std::forward<align_config_t>(_config)},
872  _score{static_cast<score_type>(std::ranges::size(query))},
873  database_it{ranges::begin(database)},
874  database_it_end{ranges::end(database)}
875  {
876  static constexpr size_t alphabet_size_ = alphabet_size<query_alphabet_type>;
877 
878  size_t const block_count = (std::ranges::size(query) - 1u + word_size) / word_size;
879  score_mask = word_type{1u} << ((std::ranges::size(query) - 1u + word_size) % word_size);
880 
881  this->score_init();
882  if constexpr(use_max_errors)
883  this->max_errors_init(block_count);
884 
885  if constexpr(compute_score_matrix)
886  this->score_matrix_init();
887 
888  if constexpr(compute_trace_matrix)
889  this->trace_matrix_init(block_count);
890 
891  vp.resize(block_count, vp0);
892  vn.resize(block_count, vn0);
893  bit_masks.resize((alphabet_size_ + 1u) * block_count, 0u);
894 
895  // encoding the letters as bit-vectors
896  for (size_t j = 0u; j < std::ranges::size(query); j++)
897  {
898  size_t const i = block_count * seqan3::to_rank(query[j]) + j / word_size;
899  bit_masks[i] |= word_type{1u} << (j % word_size);
900  }
901 
902  add_state();
903  }
905 
906 private:
908  template <bool with_carry>
909  static void compute_step(compute_state & state) noexcept
910  {
911  word_type x, t;
912  assert(state.carry_d0 <= 1u);
913  assert(state.carry_hp <= 1u);
914  assert(state.carry_hn <= 1u);
915 
916  x = state.b | state.vn;
917  t = state.vp + (x & state.vp) + state.carry_d0;
918 
919  state.d0 = (t ^ state.vp) | x;
920  state.hn = state.vp & state.d0;
921  state.hp = state.vn | ~(state.vp | state.d0);
922 
923  if constexpr(with_carry)
924  state.carry_d0 = (state.carry_d0 != 0u) ? t <= state.vp : t < state.vp;
925 
926  x = (state.hp << 1u) | state.carry_hp;
927  state.vn = x & state.d0;
928  state.vp = (state.hn << 1u) | ~(x | state.d0) | state.carry_hn;
929 
930  if constexpr(with_carry)
931  {
932  state.carry_hp = state.hp >> (word_size - 1u);
933  state.carry_hn = state.hn >> (word_size - 1u);
934  }
935  }
936 
938  template <bool with_carry>
939  void compute_kernel(compute_state & state, size_t const block_offset, size_t const current_block) noexcept
940  {
941  state.vp = proxy_reference<word_type>{this->vp[current_block]};
942  state.vn = proxy_reference<word_type>{this->vn[current_block]};
943  if constexpr(compute_trace_matrix)
944  {
945  state.hp = proxy_reference<word_type>{this->hp[current_block]};
946  state.db = proxy_reference<word_type>{this->db[current_block]};
947  }
948  state.b = bit_masks[block_offset + current_block];
949 
950  compute_step<with_carry>(state);
951  if constexpr(compute_trace_matrix)
952  state.db = ~(state.b ^ state.d0);
953  }
954 
956  void advance_score(word_type P, word_type N, word_type mask) noexcept
957  {
958  if ((P & mask) != word_type{0u})
959  _score++;
960  else if ((N & mask) != word_type{0u})
961  _score--;
962  }
963 
965  bool on_hit() noexcept
966  {
967  // TODO: call external on_hit functor
968  return false;
969  }
970 
972  inline bool small_patterns();
973 
975  inline bool large_patterns();
976 
978  inline void compute_empty_query_sequence()
979  {
980  assert(std::ranges::empty(query));
981 
982  bool abort_computation = false;
983 
984  for (; database_it != database_it_end; ++database_it)
985  {
986  if constexpr(is_global)
987  ++_score;
988  else // is_semi_global
989  this->update_best_score();
990 
991  // call on_hit
992  if constexpr(use_max_errors)
993  abort_computation = on_hit();
994 
995  this->add_state();
996  if (abort_computation)
997  break;
998  }
999  }
1000 
1002  void compute()
1003  {
1004  // limit search width for prefix search (if no matrix needs to be computed)
1005  if constexpr(use_max_errors && is_global && !compute_matrix)
1006  {
1007  // Note: For global alignments we know that the database can only be max_length long to have a score less
1008  // than or equal max_errors in the last cell.
1009  //
1010  // The following matrix shows a minimal value for each entry (because a diagonal must be +0 or +1, each
1011  // diagonal is at least the value of the initial value in the first row)
1012  // 0 1 2 3 4 5 6...
1013  // 1 0 1 2 3 4 5...
1014  // ...
1015  // m ... 3 2 1 0 1 2 3 4 5
1016  // Thus, after |query| + max_errors entries the score will always be higher than max_errors.
1017  size_t const max_length = std::ranges::size(query) + this->max_errors + 1u;
1018  size_t const haystack_length = std::min(std::ranges::size(database), max_length);
1019  database_it_end -= std::ranges::size(database) - haystack_length;
1020  }
1021 
1022  // distinguish between the version for needles not longer than
1023  // one machine word and the version for longer needles
1024  // A special cases is if the second sequence is empty (vp.size() == 0u).
1025  if (vp.size() == 0u) // [[unlikely]]
1026  compute_empty_query_sequence();
1027  else if (vp.size() == 1u)
1028  small_patterns();
1029  else
1030  large_patterns();
1031 
1032  if constexpr(is_global)
1033  this->update_best_score();
1034  }
1035 
1036 public:
1041  template <typename callback_t>
1042  void operator()([[maybe_unused]] size_t const idx, callback_t && callback)
1043  {
1044  using traits_type = alignment_configuration_traits<align_config_t>;
1045  using result_value_type = typename alignment_result_value_type_accessor<alignment_result_type>::type;
1046 
1047  compute();
1048 
1049  // First cache the begin and end positions if enabled by the edit distance traits.
1050  // Note, that they might be activated even if the user did not configure them, but in order to
1051  // compute for example the alignment both information are required and enabled internally.
1052  auto cached_end_positions = this->invalid_coordinate();
1053  auto cached_begin_positions = this->invalid_coordinate();
1054 
1055  if constexpr (compute_end_positions)
1056  cached_end_positions = this->end_positions();
1057 
1058  if constexpr (compute_begin_positions)
1059  {
1060  static_assert(compute_end_positions, "End positions required to compute the begin positions.");
1061  if (this->is_valid())
1062  cached_begin_positions = alignment_begin_positions(this->trace_matrix(), cached_end_positions);
1063  }
1064 
1065  // Fill the result value type. Note we need to ask what was enabled on the user side in order to store
1066  // the correct information in the alignment result type. This information is stored in the alignment
1067  // configuration traits and not in the edit distance traits.
1068  result_value_type res_vt{};
1069 
1070  if constexpr (traits_type::output_sequence1_id)
1071  res_vt.sequence1_id = idx;
1072 
1073  if constexpr (traits_type::output_sequence2_id)
1074  res_vt.sequence2_id = idx;
1075 
1076  if constexpr (traits_type::compute_score)
1077  res_vt.score = this->score().value_or(matrix_inf<score_type>);
1078 
1079  if constexpr (traits_type::compute_sequence_alignment)
1080  {
1081  if (this->is_valid())
1082  {
1083  using alignment_t = decltype(res_vt.alignment);
1084  res_vt.alignment = alignment_trace<alignment_t>(database,
1085  query,
1086  this->trace_matrix(),
1087  cached_end_positions,
1088  cached_begin_positions);
1089  }
1090  }
1091 
1092  if constexpr (traits_type::compute_end_positions)
1093  res_vt.end_positions = std::move(cached_end_positions);
1094 
1095  if constexpr (traits_type::compute_begin_positions)
1096  res_vt.begin_positions = std::move(cached_begin_positions);
1097 
1098  callback(alignment_result_type{std::move(res_vt)});
1099  }
1100 };
1101 
1102 template <typename database_t, typename query_t, typename align_config_t, typename traits_t>
1103 bool edit_distance_unbanded<database_t, query_t, align_config_t, traits_t>::small_patterns()
1104 {
1105  bool abort_computation = false;
1106 
1107  // computing the blocks
1108  while (database_it != database_it_end)
1109  {
1110  compute_state state{};
1111  size_t const block_offset = seqan3::to_rank((query_alphabet_type) *database_it);
1112 
1113  compute_kernel<false>(state, block_offset, 0u);
1114  advance_score(state.hp, state.hn, score_mask);
1115 
1116  // semi-global without max_errors guarantees that the score stays within the last row
1117  if constexpr(is_semi_global && !use_max_errors)
1118  this->update_best_score();
1119 
1120  // updating the last active cell
1121  if constexpr(use_max_errors)
1122  abort_computation = this->update_last_active_cell();
1123 
1124  add_state();
1125  ++database_it;
1126  if (abort_computation)
1127  return true;
1128  }
1129 
1130  return false;
1131 }
1132 
1133 template <typename database_t, typename query_t, typename align_config_t, typename traits_t>
1134 bool edit_distance_unbanded<database_t, query_t, align_config_t, traits_t>::large_patterns()
1135 {
1136  bool abort_computation = false;
1137 
1138  while (database_it != database_it_end)
1139  {
1140  compute_state state{};
1141  size_t const block_offset = vp.size() * seqan3::to_rank((query_alphabet_type) *database_it);
1142 
1143  size_t block_count = vp.size();
1144  if constexpr(use_max_errors)
1145  block_count = this->last_block + 1;
1146 
1147  // compute each block in the current column; carries between blocks will be propagated.
1148  for (size_t current_block = 0u; current_block < block_count; current_block++)
1149  compute_kernel<true>(state, block_offset, current_block);
1150 
1151  advance_score(state.hp, state.hn, score_mask);
1152 
1153  // semi-global without max_errors guarantees that the score stays within the last row
1154  if constexpr(is_semi_global && !use_max_errors)
1155  this->update_best_score();
1156 
1157  if constexpr(use_max_errors)
1158  {
1159  // if the last active cell reached the end within the current block we have to compute the next block.
1160  bool additional_block = score_mask >> (word_size - 1u);
1161  bool reached_last_block = this->last_block + 1u == vp.size();
1162  // If there is no next block we skip the computation.
1163  if (reached_last_block)
1164  additional_block = false;
1165 
1166  if (additional_block)
1167  {
1168  size_t const current_block = this->last_block + 1u;
1169  // this might not be necessary, but carry_d0 = 1u might have an influence on the result of vn and vp.
1170  vp[current_block] = vp0;
1171  vn[current_block] = vn0;
1172  compute_kernel<false>(state, block_offset, current_block);
1173  }
1174 
1175  // updating the last active cell
1176  abort_computation = this->update_last_active_cell();
1177  }
1178 
1179  add_state();
1180  ++database_it;
1181 
1182  if (abort_computation)
1183  return true;
1184  }
1185 
1186  return false;
1187 }
1188 
1194 template <typename database_t, typename query_t, typename config_t, typename traits_t>
1196 edit_distance_unbanded(database_t && database, query_t && query, config_t config, traits_t)
1197  -> edit_distance_unbanded<database_t, query_t, config_t, traits_t>;
1199 
1200 } // namespace seqan3::detail
bitset
utility
std::vector
configuration.hpp
Provides seqan3::detail::configuration and utility functions.
seqan3::get
constexpr auto const & get(configuration< configs_t... > const &config) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: configuration.hpp:627
seqan3::to_rank
constexpr auto to_rank
Return the rank representation of a (semi-)alphabet object.
Definition: concept.hpp:143
algorithm
alignment_trace_algorithms.hpp
Provides algorithms that operate on seqan3::detail::alignment_trace_matrix.
seqan3::views::move
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:68
edit_distance_score_matrix_full.hpp
Provides seqan3::detail::edit_distance_score_matrix_full.
alignment_result.hpp
Provides seqan3::alignment_result.
seqan3::pack_traits::size
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:116
std::min
T min(T... args)
seqan3::pack_traits::back
typename decltype((std::type_identity< pack_t >{},...))::type back
Return the last type from the type pack.
Definition: traits.hpp:262
ranges
Adaptations of concepts from the Ranges TS.
edit_distance_fwd.hpp
Forwards for seqan3::edit_distance_unbanded related types.
std
SeqAn specific customisations in the standard namespace.
std::remove_cvref_t
std::optional
alignment_coordinate.hpp
Provides seqan3::detail::alignment_coordinate.
std::conditional_t
edit_distance_trace_matrix_full.hpp
Provides seqan3::detail::edit_distance_trace_matrix_full.