SeqAn3
alignment_trace_matrix.hpp
Go to the documentation of this file.
1 // ============================================================================
2 // SeqAn - The Library for Sequence Analysis
3 // ============================================================================
4 //
5 // Copyright (c) 2006-2018, Knut Reinert & Freie Universitaet Berlin
6 // Copyright (c) 2016-2018, Knut Reinert & MPI Molekulare Genetik
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // * Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // * Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // * Neither the name of Knut Reinert or the FU Berlin nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 // ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
25 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 // DAMAGE.
32 //
33 // ============================================================================
34 
40 #pragma once
41 
45 
46 namespace seqan3::detail
47 {
48 
53 template <typename ...>
54 class alignment_trace_matrix;
55 
72 template <>
73 class alignment_trace_matrix<std::vector<trace_directions>>
74  : public row_wise_matrix<trace_directions>
75 {
76 public:
77  using row_wise_matrix<trace_directions>::row_wise_matrix;
78 };
79 
101 template <typename database_type, typename query_type, typename align_config_type, typename ...score_matrix_params_t>
103  requires matrix_concept<alignment_score_matrix<score_matrix_params_t...>> &&
104  std::Integral<typename alignment_score_matrix<score_matrix_params_t...>::entry_type>
106 class alignment_trace_matrix<database_type, query_type, align_config_type, alignment_score_matrix<score_matrix_params_t...>>
107  : public alignment_score_matrix<score_matrix_params_t...>
108 {
109 public:
111  using score_matrix_type = alignment_score_matrix<score_matrix_params_t...>;
112 
114  using score_type = typename score_matrix_type::entry_type;
115 
117  using entry_type = trace_directions;
118 
124  alignment_trace_matrix() = default;
125  alignment_trace_matrix(alignment_trace_matrix const &) = default;
126  alignment_trace_matrix(alignment_trace_matrix &&) = default;
127  alignment_trace_matrix & operator=(alignment_trace_matrix const &) = default;
128  alignment_trace_matrix & operator=(alignment_trace_matrix &&) = default;
129 
136  alignment_trace_matrix(database_type database, query_type query, align_config_type config, score_matrix_type score_matrix)
137  : score_matrix_type(std::move(score_matrix)),
138  _database{std::forward<database_type>(database)},
139  _query{std::forward<query_type>(query)},
140  _config{std::forward<align_config_type>(config)}
141  {}
143 
145  using score_matrix_type::rows;
147  using score_matrix_type::cols;
148 
150  entry_type at(size_t const row, size_t const col) const noexcept
151  {
152  entry_type direction{};
153 
154  if (is_trace_diagonal(row, col))
155  direction |= entry_type::diagonal;
156 
157  if (is_trace_up(row, col))
158  direction |= entry_type::up;
159 
160  if (is_trace_left(row, col))
161  direction |= entry_type::left;
162 
163  return direction;
164  }
165 
167  score_matrix_type const & score_matrix() const noexcept
168  {
169  return *this;
170  }
171 
172 private:
173 
175  bool is_trace_up(size_t const row, size_t const col) const noexcept
176  {
177  // TODO: use the alignment_config to calculate the score
178  score_type gap = 1;
179 
180  score_type curr = score_matrix().at(row, col);
181  score_type up = row == 0 ? col : score_matrix().at(row - 1, col);
182  return curr == up + gap;
183  }
184 
186  bool is_trace_left(size_t const row, size_t const col) const noexcept
187  {
188  // TODO: use the alignment_config to calculate the score
189  score_type gap = 1;
190 
191  score_type curr = score_matrix().at(row, col);
192  score_type left = col == 0 ? row : score_matrix().at(row, col - 1);
193  return curr == left + gap;
194  }
195 
197  bool is_trace_diagonal(size_t const row, size_t const col) const noexcept
198  {
199  // TODO: use the alignment_config to calculate the score
200  score_type match = 0;
201  score_type mismatch = 1;
202 
203  score_type curr = score_matrix().at(row, col);
204  if (col == 0 || row == 0)
205  return false;
206 
207  score_type diag = score_matrix().at(row - 1, col - 1);
208  bool is_match = _query[row - 1] == _database[col - 1];
209 
210  return (is_match && curr == diag + match) ||
211  (!is_match && curr == diag + mismatch);
212  }
213 
215  database_type _database;
217  query_type _query;
219  align_config_type _config;
220 };
221 
226 alignment_trace_matrix(std::vector<trace_directions>, size_t rows, size_t cols)
227  -> alignment_trace_matrix<std::vector<trace_directions>>;
228 
229 template <typename database_t, typename query_t, typename align_config_t, typename alignment_t, typename ...options_t>
230 alignment_trace_matrix(database_t && database, query_t && query, align_config_t && config, alignment_score_matrix<alignment_t, options_t...>)
231  -> alignment_trace_matrix<database_t, query_t, align_config_t, alignment_score_matrix<alignment_t, options_t...>>;
233 
234 } // namespace seqan3::detail
Contains the declaration of seqan3::detail::alignment_score_matrix.
constexpr detail::align_config_gap_adaptor< seqan3::detail::align_config_gap > gap
A configuration adaptor for gaps.
Definition: align_config_gap.hpp:162
SeqAn specific customisations in the standard namespace.
Definition: align_result.hpp:221
The concept Integral is satisfied if and only if T is an integral type.
Contains the declaration of seqan3::detail::trace_directions.
Definition: aligned_sequence_concept.hpp:288
Contains seqan3::detail::matrix_concept.