SeqAn3
The Modern C++ library for sequence analysis.
affine_gap_banded_init_policy.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2019, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2019, 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 <tuple>
16 
20 
21 namespace seqan3::detail
22 {
23 
30 template <typename derived_t, typename traits_type = default_affine_init_traits>
31 class affine_gap_banded_init_policy :
32  public affine_gap_init_policy<affine_gap_banded_init_policy<derived_t, traits_type>, traits_type>
33 {
34 private:
35 
37  using base_t = affine_gap_init_policy<affine_gap_banded_init_policy<derived_t, traits_type>, traits_type>;
38 
40  friend derived_t;
41 
45  constexpr affine_gap_banded_init_policy() noexcept = default;
46  constexpr affine_gap_banded_init_policy(affine_gap_banded_init_policy const &) noexcept = default;
47  constexpr affine_gap_banded_init_policy(affine_gap_banded_init_policy &&) noexcept = default;
48  constexpr affine_gap_banded_init_policy & operator=(affine_gap_banded_init_policy const &) noexcept = default;
51  constexpr affine_gap_banded_init_policy & operator=(affine_gap_banded_init_policy &&) noexcept = default;
52  ~affine_gap_banded_init_policy() noexcept = default;
53 
61  template <typename cell_t, typename cache_t>
62  constexpr auto init_origin_cell(cell_t && current_cell, cache_t & cache) const noexcept
63  {
64  using std::get;
65 
66  // Call get twice since the banded score column is a zipped range and we need to access the main column.
67  auto & [main_score, hz_score, hz_trace] = get<0>(get<0>(current_cell));
68  auto & trace_value = get<2>(current_cell);
69  auto & vt_score = get<1>(get<0>(cache));
70  auto & vt_trace = get<2>(get<0>(cache));
71 
72  main_score = 0;
73 
74  // Initialise the vertical matrix cell according to the traits settings.
75  if constexpr (traits_type::free_second_leading_t::value)
76  {
77  vt_score = 0;
78  vt_trace = trace_directions::none;
79  }
80  else
81  {
82  vt_score = get<1>(cache);
83  vt_trace = trace_directions::up_open;
84  }
85  // Initialise the horizontal matrix cell according to the traits settings.
86  if constexpr (traits_type::free_first_leading_t::value)
87  {
88  hz_score = 0;
89  hz_trace = trace_directions::none;
90  }
91  else
92  {
93  hz_score = get<1>(cache);
94  hz_trace = trace_directions::left_open;
95  }
96  trace_value = trace_directions::none;
97  }
98 
105  template <typename cell_t, typename cache_t>
106  constexpr auto init_column_cell(cell_t && current_cell, cache_t & cache) const noexcept
107  {
108  using std::get;
109 
110  // Call get twice since the banded score column is a zipped range and we need to access the main column.
111  auto & [main_score, hz_score, hz_trace] = get<0>(get<0>(current_cell));
112  auto & trace_value = get<2>(current_cell);
113  auto & vt_score = get<1>(get<0>(cache));
114  auto & vt_trace = get<2>(get<0>(cache));
115 
116  main_score = vt_score;
117  trace_value = vt_trace;
118 
119  // Initialise the vertical matrix cell according to the traits settings.
120  if constexpr (traits_type::free_second_leading_t::value)
121  {
122  vt_score = 0;
123  vt_trace = trace_directions::none;
124  }
125  else
126  { // previous vertical + gap extension
127  vt_score += get<2>(cache);
128  vt_trace = trace_directions::up;
129  }
130  hz_score = main_score + get<1>(cache); // gap_opening cost
131  hz_trace = trace_directions::left_open;
132  }
133 
140  template <typename cell_t, typename cache_t>
141  constexpr auto init_row_cell(cell_t && current_cell, cache_t & cache) const noexcept
142  {
143  auto & [current_entry, next_entry] = get<0>(current_cell); // Split into current entry and next entry.
144  auto & trace_value = get<2>(current_cell); // the trace value to store.
145  auto & main_score = get<0>(current_entry); // current_entry stores current score to be updated.
146  auto & hz_trace = get<2>(current_entry); // store trace for the next horizontal computation.
147  auto const & prev_hz_score = get<1>(next_entry); // get the previous horizontal score.
148  auto const & prev_hz_trace = get<2>(next_entry); // get the previous horizontal trace.
149  auto & vt_score = get<1>(get<0>(cache));
150  auto & vt_trace = get<2>(get<0>(cache));
151 
152  main_score = prev_hz_score;
153  trace_value = prev_hz_trace;
154  vt_score += main_score + get<1>(cache); // gap opening cost
155  vt_trace = trace_directions::up_open;
156 
157  // Initialise the horizontal matrix cell according to the traits settings.
158  if constexpr (traits_type::free_first_leading_t::value)
159  {
160  get<1>(current_entry) = 0;
161  hz_trace = trace_directions::none;
162  }
163  else
164  {
165  get<1>(current_entry) = prev_hz_score + get<2>(cache);
166  hz_trace = trace_directions::left;
167  }
168  }
169 
183  template <typename optimum_type, typename band_type, typename gap_scheme_type>
184  constexpr void balance_leading_gaps(optimum_type & total,
185  band_type const & band,
186  gap_scheme_type const & scheme) const noexcept
187  {
188  if constexpr (!traits_type::free_second_leading_t::value)
189  { // Band starts inside of second sequence.
190  if (0 > band.upper_bound)
191  total.score += scheme.score(std::abs(band.upper_bound));
192  }
193  if constexpr (!traits_type::free_first_leading_t::value)
194  { // Band starts inside of first sequence.
195  if (band.lower_bound > 0)
196  total.score += scheme.score(band.lower_bound);
197  }
198  }
199 };
200 
201 } // namespace seqan3::detail
Provides seqan3::align_cfg::aligned_ends.
Provides seqan3::detail::affine_gap_init_policy.
Definition: aligned_sequence_concept.hpp:35
auto const get
A view calling std::get on each element in a range.
Definition: get.hpp:66
Provides seqan3::detail::configuration and utility functions.