SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
alignment_score_matrix_one_column_banded.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 <cassert>
13#include <iterator>
14#include <ranges>
15#include <span>
16
21
22namespace seqan3::detail
23{
24
38template <typename score_t>
39class alignment_score_matrix_one_column_banded :
40 protected alignment_score_matrix_one_column_base<score_t>,
41 public alignment_matrix_column_major_range_base<alignment_score_matrix_one_column_banded<score_t>>
42{
43private:
45 using matrix_base_t = alignment_score_matrix_one_column_base<score_t>;
47 using range_base_t = alignment_matrix_column_major_range_base<alignment_score_matrix_one_column_banded<score_t>>;
48
50 friend range_base_t;
51
52protected:
53 using typename matrix_base_t::element_type;
54 using typename range_base_t::alignment_column_type;
56 using column_data_view_type = std::span<element_type>;
57
58public:
59 using matrix_base_t::num_cols;
60 using matrix_base_t::num_rows;
61
66 using value_type = alignment_score_matrix_proxy<score_t>;
68 using reference = value_type;
70 using iterator = typename range_base_t::iterator;
72 using sentinel = typename range_base_t::sentinel;
73 using typename matrix_base_t::size_type;
74 using typename matrix_base_t::underlying_type;
76
81 constexpr alignment_score_matrix_one_column_banded() = default;
83 constexpr alignment_score_matrix_one_column_banded(alignment_score_matrix_one_column_banded const &) = default;
85 constexpr alignment_score_matrix_one_column_banded(alignment_score_matrix_one_column_banded &&) = default;
87 constexpr alignment_score_matrix_one_column_banded &
88 operator=(alignment_score_matrix_one_column_banded const &) = default;
90 constexpr alignment_score_matrix_one_column_banded &
91 operator=(alignment_score_matrix_one_column_banded &&) = default;
93 ~alignment_score_matrix_one_column_banded() = default;
94
109 template <std::ranges::forward_range first_sequence_t, std::ranges::forward_range second_sequence_t>
110 constexpr alignment_score_matrix_one_column_banded(first_sequence_t && first,
111 second_sequence_t && second,
112 align_cfg::band_fixed_size const & band,
113 score_t const initial_value = score_t{})
114 {
115 matrix_base_t::num_cols = static_cast<size_type>(std::ranges::distance(first) + 1);
116 matrix_base_t::num_rows = static_cast<size_type>(std::ranges::distance(second) + 1);
117
118 band_col_index = std::min<int32_t>(std::max<int32_t>(band.upper_diagonal, 0), matrix_base_t::num_cols - 1);
119 band_row_index =
120 std::min<int32_t>(std::abs(std::min<int32_t>(band.lower_diagonal, 0)), matrix_base_t::num_rows - 1);
121
122 band_size = band_col_index + band_row_index + 1;
123 // Reserve one more cell to deal with last cell in the banded column which needs only the diagonal and up cell.
124 matrix_base_t::pool.resize(band_size + 1, element_type{initial_value, initial_value});
125 }
127
129 int32_t band_col_index{};
131 int32_t band_row_index{};
133 int32_t band_size{};
134
135private:
137 constexpr alignment_column_type initialise_column(size_type const column_index) noexcept
138 {
139 int32_t slice_begin = std::max<int32_t>(0, band_col_index - column_index);
140 int32_t row_end_index = column_index - band_col_index + band_size;
141 int32_t slice_end = band_size - std::max<int32_t>(row_end_index - matrix_base_t::num_rows, 0);
142
143 assert(row_end_index >= 0);
144 assert(slice_begin >= 0);
145 assert(slice_end > 0);
146 assert(slice_begin < slice_end);
147
148 return alignment_column_type{*this,
149 column_data_view_type{std::addressof(matrix_base_t::pool[slice_begin]),
150 std::addressof(matrix_base_t::pool[slice_end])}};
151 }
152
154 template <std::random_access_iterator iter_t>
155 constexpr value_type make_proxy(iter_t host_iter) noexcept
156 {
157 return {std::get<0>(*host_iter), // current
158 std::get<0>(matrix_base_t::cache), // last diagonal
159 std::get<1>(*(host_iter + 1)), // last left (read)
160 std::get<1>(*(host_iter)), // next left (write)
161 std::get<1>(matrix_base_t::cache)}; // last up
162 }
163
165 template <std::random_access_iterator iter_t>
166 constexpr void on_column_iterator_creation(iter_t host_iter) noexcept
167 {
168 // Cache the last diagonal value.
169 std::get<0>(matrix_base_t::cache) = std::get<0>(*host_iter);
170 }
171
173 template <std::random_access_iterator iter_t>
174 constexpr void before_column_iterator_increment(iter_t SEQAN3_DOXYGEN_ONLY(host_iter)) noexcept
175 {
176 // noop.
177 }
178
180 template <std::random_access_iterator iter_t>
181 constexpr void after_column_iterator_increment(iter_t host_iter) noexcept
182 {
183 // Cache the last diagonal value.
184 std::get<0>(matrix_base_t::cache) = std::get<0>(*host_iter);
185 }
186};
187
188} // namespace seqan3::detail
T addressof(T... args)
Provides seqan3::detail::align_config_band.
Provides seqan3::detail::alignment_matrix_column_major_range_base.
Provides seqan3::detail::alignment_score_matrix_one_column_base.
Provides seqan3::detail::alignment_score_matrix_proxy.
Hide me