SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
combined_score_and_trace_matrix.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 <seqan3/std/ranges>
16 
22 
23 namespace seqan3::detail
24 {
44 template <std::ranges::input_range score_matrix_t, std::ranges::input_range trace_matrix_t>
46  requires (std::ranges::input_range<std::ranges::range_reference_t<score_matrix_t>> &&
47  std::ranges::input_range<std::ranges::range_reference_t<trace_matrix_t>>)
50 {
51 private:
54 
55  class iterator;
56  class sentinel;
57 
59  score_matrix_t score_matrix{};
61  trace_matrix_t trace_matrix{};
62 
63 public:
73 
75 
96  template <std::integral column_index_t, std::integral row_index_t>
97  void resize(column_index_type<column_index_t> const column_count,
98  row_index_type<row_index_t> const row_count,
99  score_type const initial_score = score_type{})
100  {
101  score_matrix_t tmp_score_matrix{};
102  tmp_score_matrix.resize(column_count, row_count, initial_score);
103 
104  trace_matrix_t tmp_trace_matrix{};
105  tmp_trace_matrix.resize(column_count, row_count);
106 
107  score_matrix = std::move(tmp_score_matrix);
108  trace_matrix = std::move(tmp_trace_matrix);
109  }
110 
116  {
117  return iterator{score_matrix.begin(), trace_matrix.begin()};
118  }
119 
121  iterator begin() const = delete;
122 
125  {
126  return sentinel{score_matrix.end()};
127  }
128 
130  sentinel end() const = delete;
132 
141  auto trace_path(matrix_coordinate const & from_coordinate) const
142  {
143  return trace_matrix.trace_path(from_coordinate);
144  }
145 };
146 
158 template <std::ranges::input_range score_matrix_t, std::ranges::input_range trace_matrix_t>
160  requires (std::ranges::input_range<std::ranges::range_reference_t<score_matrix_t>> &&
161  std::ranges::input_range<std::ranges::range_reference_t<trace_matrix_t>>)
163 class combined_score_and_trace_matrix<score_matrix_t, trace_matrix_t>::iterator
164 {
165 private:
167  using score_matrix_reference_type = std::ranges::range_reference_t<score_matrix_t>;
169  using trace_matrix_reference_type = std::ranges::range_reference_t<trace_matrix_t>;
170 
171  static_assert(std::ranges::viewable_range<score_matrix_reference_type>);
172  static_assert(std::ranges::viewable_range<trace_matrix_reference_type>);
173 
175  using combined_column_type = decltype(views::zip(std::declval<score_matrix_reference_type>(),
176  std::declval<trace_matrix_reference_type>()));
178  using score_matrix_iter_type = std::ranges::iterator_t<score_matrix_t>;
180  using trace_matrix_iter_type = std::ranges::iterator_t<trace_matrix_t>;
181 
182  // Befriend the base class.
183  template <std::ranges::input_range other_score_matrix_t, std::ranges::input_range other_trace_matrix_t>
185  requires (std::ranges::input_range<std::ranges::range_reference_t<other_score_matrix_t>> &&
186  std::ranges::input_range<std::ranges::range_reference_t<other_trace_matrix_t>>)
188  friend class combined_score_and_trace_matrix;
189 
191  static constexpr auto transform_to_combined_matrix_cell = std::views::transform([] (auto && tpl)
192  -> affine_cell_proxy<std::remove_cvref_t<decltype(tpl)>>
193  {
194  using fwd_tuple_t = decltype(tpl);
195  return affine_cell_proxy<std::remove_cvref_t<fwd_tuple_t>>{std::forward<fwd_tuple_t>(tpl)};
196  });
197 
202 
203 public:
208  using value_type = decltype(std::declval<combined_column_type>() | transform_to_combined_matrix_cell);
212  using pointer = void;
218 
222  iterator() = default;
223  iterator(iterator const &) = default;
224  iterator(iterator &&) = default;
225  iterator & operator=(iterator const &) = default;
226  iterator & operator=(iterator &&) = default;
227  ~iterator() = default;
228 
234  explicit iterator(score_matrix_iter_type score_matrix_it,
235  trace_matrix_iter_type trace_matrix_it) noexcept :
236  score_matrix_it{std::move(score_matrix_it)},
237  trace_matrix_it{std::move(trace_matrix_it)}
238  {}
240 
246  {
247  return views::zip(*score_matrix_it, *trace_matrix_it) | transform_to_combined_matrix_cell;
248  }
250 
256  {
257  ++score_matrix_it;
258  ++trace_matrix_it;
259  return *this;
260  }
261 
263  void operator++(int)
264  {
265  ++(*this);
266  }
268 };
269 
277 template <std::ranges::input_range score_matrix_t, std::ranges::input_range trace_matrix_t>
279  requires (std::ranges::input_range<std::ranges::range_reference_t<score_matrix_t>> &&
280  std::ranges::input_range<std::ranges::range_reference_t<trace_matrix_t>>)
282 class combined_score_and_trace_matrix<score_matrix_t, trace_matrix_t>::sentinel
283 {
284 private:
286  using score_matrix_sentinel_type = std::ranges::sentinel_t<score_matrix_t>;
287 
289  score_matrix_sentinel_type score_matrix_sentinel{};
290 
291 public:
295  sentinel() = default;
296  sentinel(sentinel const &) = default;
297  sentinel(sentinel &&) = default;
298  sentinel & operator=(sentinel const &) = default;
299  sentinel & operator=(sentinel &&) = default;
300  ~sentinel() = default;
301 
306  explicit sentinel(score_matrix_sentinel_type score_matrix_sentinel) noexcept :
307  score_matrix_sentinel{std::move(score_matrix_sentinel)}
308  {}
310 
315  friend bool operator==(iterator const & lhs, sentinel const & rhs) noexcept
316  {
317  return rhs.equal(lhs);
318  }
319 
321  friend bool operator==(sentinel const & lhs, iterator const & rhs) noexcept
322  {
323  return rhs == lhs;
324  }
325 
327  friend bool operator!=(iterator const & lhs, sentinel const & rhs) noexcept
328  {
329  return !(lhs == rhs);
330  }
331 
333  friend bool operator!=(sentinel const & lhs, iterator const & rhs) noexcept
334  {
335  return !(lhs == rhs);
336  }
338 
339 private:
349  constexpr bool equal(iterator const & iter) const noexcept
350  {
351  return iter.score_matrix_it == score_matrix_sentinel;
352  }
353 };
354 
355 } // namespace seqan3::detail
Provides seqan3::detail::affine_cell_proxy.
A proxy for an affine score matrix cell.
Definition: affine_cell_proxy.hpp:122
Combined score and trace matrix iterator for the pairwise sequence alignment.
Definition: combined_score_and_trace_matrix.hpp:164
void operator++(int)
Advance the iterator to the next alignment matrix column.
Definition: combined_score_and_trace_matrix.hpp:263
std::ranges::iterator_t< trace_matrix_t > trace_matrix_iter_type
The type of the trace matrix iterator.
Definition: combined_score_and_trace_matrix.hpp:180
value_type reference
The reference type.
Definition: combined_score_and_trace_matrix.hpp:210
iterator & operator=(iterator &&)=default
Defaulted.
reference operator*() const
Returns the range over the current column.
Definition: combined_score_and_trace_matrix.hpp:245
void pointer
The pointer type.
Definition: combined_score_and_trace_matrix.hpp:212
std::ranges::range_reference_t< score_matrix_t > score_matrix_reference_type
The reference type of the score matrix.
Definition: combined_score_and_trace_matrix.hpp:167
iterator & operator++()
Advance the iterator to the next alignment matrix column.
Definition: combined_score_and_trace_matrix.hpp:255
decltype(std::declval< combined_column_type >()|transform_to_combined_matrix_cell) value_type
The value type.
Definition: combined_score_and_trace_matrix.hpp:208
score_matrix_iter_type score_matrix_it
The current iterator over the score matrix.
Definition: combined_score_and_trace_matrix.hpp:199
std::ranges::iterator_t< score_matrix_t > score_matrix_iter_type
The type of the score matrix iterator.
Definition: combined_score_and_trace_matrix.hpp:178
iterator(score_matrix_iter_type score_matrix_it, trace_matrix_iter_type trace_matrix_it) noexcept
Initialises the iterator from the underlying matrix.
Definition: combined_score_and_trace_matrix.hpp:234
std::ranges::range_reference_t< trace_matrix_t > trace_matrix_reference_type
The reference type of the trace matrix.
Definition: combined_score_and_trace_matrix.hpp:169
iterator & operator=(iterator const &)=default
Defaulted.
trace_matrix_iter_type trace_matrix_it
The current iterator over the trace matrix.
Definition: combined_score_and_trace_matrix.hpp:201
decltype(views::zip(std::declval< score_matrix_reference_type >(), std::declval< trace_matrix_reference_type >())) combined_column_type
The combined column type.
Definition: combined_score_and_trace_matrix.hpp:176
The sentinel type for the seqan3::detail::combined_score_and_trace_matrix.
Definition: combined_score_and_trace_matrix.hpp:283
friend bool operator==(iterator const &lhs, sentinel const &rhs) noexcept
Checks if the iterator reached the end of the matrix.
Definition: combined_score_and_trace_matrix.hpp:315
friend bool operator!=(iterator const &lhs, sentinel const &rhs) noexcept
Checks if the iterator did not reach the end of the matrix.
Definition: combined_score_and_trace_matrix.hpp:327
sentinel & operator=(sentinel const &)=default
Defaulted.
constexpr bool equal(iterator const &iter) const noexcept
Compares the stored score matrix sentinel with the given iterator.
Definition: combined_score_and_trace_matrix.hpp:349
friend bool operator!=(sentinel const &lhs, iterator const &rhs) noexcept
Checks if the iterator did not reach the end of the matrix.
Definition: combined_score_and_trace_matrix.hpp:333
friend bool operator==(sentinel const &lhs, iterator const &rhs) noexcept
Checks if the iterator reached the end of the matrix.
Definition: combined_score_and_trace_matrix.hpp:321
sentinel & operator=(sentinel &&)=default
Defaulted.
sentinel(score_matrix_sentinel_type score_matrix_sentinel) noexcept
Initialises the sentinel from the underlying matrix.
Definition: combined_score_and_trace_matrix.hpp:306
std::ranges::sentinel_t< score_matrix_t > score_matrix_sentinel_type
The sentinel type of the underlying score matrix.
Definition: combined_score_and_trace_matrix.hpp:286
An alignment matrix that combines a score matrix with a trace matrix into a common interface.
Definition: combined_score_and_trace_matrix.hpp:50
sentinel end() const =delete
This alignment matrix is not const-iterable.
auto trace_path(matrix_coordinate const &from_coordinate) const
Returns a trace path starting from the given coordinate and ending in the cell with seqan3::detail::t...
Definition: combined_score_and_trace_matrix.hpp:141
combined_score_and_trace_matrix & operator=(combined_score_and_trace_matrix const &)=default
Defaulted.
void resize(column_index_type< column_index_t > const column_count, row_index_type< row_index_t > const row_count, score_type const initial_score=score_type{})
Resizes the matrix.
Definition: combined_score_and_trace_matrix.hpp:97
combined_score_and_trace_matrix(combined_score_and_trace_matrix &&)=default
Defaulted.
iterator begin()
Returns the iterator pointing to the first alignment column.
Definition: combined_score_and_trace_matrix.hpp:115
combined_score_and_trace_matrix & operator=(combined_score_and_trace_matrix &&)=default
Defaulted.
combined_score_and_trace_matrix(combined_score_and_trace_matrix const &)=default
Defaulted.
iterator begin() const =delete
This score matrix is not const-iterable.
sentinel end()
Returns the sentinel pointing behind the last alignment column.
Definition: combined_score_and_trace_matrix.hpp:124
Provides various transformation traits used by the range module.
Provides concepts for core language types and relations that don't have concepts in C++20 (yet).
decltype(detail::transform< trait_t >(list_t{})) transform
Apply a transformation trait to every type in the list and return a seqan3::type_list of the results.
Definition: traits.hpp:434
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:70
Provides seqan3::detail::alignment_coordinate and associated strong types.
The internal SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
SeqAn specific customisations in the standard namespace.
Adaptations of concepts from the Ranges TS.
A strong type for designated initialisation of the column index of a matrix.
Definition: matrix_coordinate.hpp:34
A strong type for designated initialisation of the row index of a matrix.
Definition: matrix_coordinate.hpp:65
Provides seqan3::views::zip.