SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
align_pairwise.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/concepts>
16 #include <functional>
17 #include <iostream>
18 #include <seqan3/std/ranges>
19 #include <tuple>
20 #include <type_traits>
21 
22 #include <meta/meta.hpp>
23 
34 
35 namespace seqan3
36 {
37 
132 template <typename sequence_t, typename alignment_config_t>
134  requires detail::align_pairwise_single_input<sequence_t> &&
135  std::copy_constructible<std::remove_reference_t<sequence_t>> &&
136  detail::is_type_specialisation_of_v<alignment_config_t, configuration>
138 constexpr auto align_pairwise(sequence_t && seq, alignment_config_t const & config)
139 {
140  using std::get;
141 
142  if constexpr (std::is_lvalue_reference_v<sequence_t>) // Forward tuple elements as references.
143  {
144  return align_pairwise(std::tie(get<0>(seq), get<1>(seq)), config);
145  }
146  else
147  {
149  "Alignment configuration error: Expects exactly two sequences for pairwise alignments.");
150 
151  static_assert(std::ranges::viewable_range<std::tuple_element_t<0, std::remove_reference_t<sequence_t>>> &&
152  std::ranges::viewable_range<std::tuple_element_t<1, std::remove_reference_t<sequence_t>>>,
153  "Alignment configuration error: The tuple elements must model std::ranges::viewable_range.");
154 
155  return align_pairwise(std::views::single(std::forward<sequence_t>(seq)), config);
156  }
157 }
158 
160 template <typename sequence_t, typename alignment_config_t>
161  requires detail::align_pairwise_range_input<sequence_t> &&
162  detail::is_type_specialisation_of_v<alignment_config_t, configuration>
163 constexpr auto align_pairwise(sequence_t && sequences,
164  alignment_config_t const & config)
165 {
166  using first_seq_t = std::tuple_element_t<0, std::ranges::range_value_t<sequence_t>>;
167  using second_seq_t = std::tuple_element_t<1, std::ranges::range_value_t<sequence_t>>;
168 
169  static_assert(std::ranges::random_access_range<first_seq_t> && std::ranges::sized_range<first_seq_t>,
170  "Alignment configuration error: The sequence must model random_access_range and sized_range.");
171  static_assert(std::ranges::random_access_range<second_seq_t> && std::ranges::sized_range<second_seq_t>,
172  "Alignment configuration error: The sequence must model random_access_range and sized_range.");
173 
174  // Pipe with detail::persist to allow rvalue non-view ranges.
175  auto seq_view = std::forward<sequence_t>(sequences) | detail::persist;
176  // Configure the alignment algorithm.
177  auto && [algorithm, complete_config] = detail::alignment_configurator::configure<decltype(seq_view)>(config);
178 
179  using complete_config_t = std::remove_cvref_t<decltype(complete_config)>;
180  using traits_t = detail::alignment_configuration_traits<complete_config_t>;
181 
182  auto indexed_sequence_chunk_view = views::zip(seq_view, std::views::iota(0))
183  | views::chunk(traits_t::alignments_per_vector);
184 
185  using indexed_sequences_t = decltype(indexed_sequence_chunk_view);
186  using alignment_result_t = typename traits_t::alignment_result_type;
188  detail::execution_handler_parallel,
189  detail::execution_handler_sequential>;
190  using executor_t = detail::algorithm_executor_blocking<indexed_sequences_t,
191  decltype(algorithm),
192  alignment_result_t,
193  execution_handler_t>;
194 
195  // Select the execution handler for the alignment configuration.
196  auto select_execution_handler = [&] ()
197  {
198  if constexpr (std::same_as<execution_handler_t, detail::execution_handler_parallel>)
199  {
200  auto thread_count = get<align_cfg::parallel>(complete_config).thread_count;
201  if (!thread_count)
202  throw std::runtime_error{"You must configure the number of threads in seqan3::align_cfg::parallel."};
203 
204  return execution_handler_t{*thread_count};
205  }
206  else
207  {
208  return execution_handler_t{};
209  }
210  };
211 
212  if constexpr (traits_t::is_one_way_execution) // Just compute alignment and wait until all alignments are computed.
213  select_execution_handler().bulk_execute(algorithm,
214  indexed_sequence_chunk_view,
215  get<align_cfg::on_result>(complete_config).callback);
216  else // Require two way execution: return the range over the alignments.
217  return algorithm_result_generator_range{executor_t{std::move(indexed_sequence_chunk_view),
218  std::move(algorithm),
219  alignment_result_t{},
220  select_execution_handler()}};
221 }
223 
224 } // namespace seqan3
Provides seqan3::detail::algorithm_executor_blocking.
Provides seqan3::detail::algorithm_result_generator_range.
Provides concepts needed internally for the alignment algorithms.
Provides helper type traits for the configuration and execution of the alignment algorithm.
Provides seqan3::detail::alignment_selector.
Provides seqan3::alignment_result.
The Concepts library.
@ seq
The "sequence", usually a range of nucleotides or amino acids.
constexpr auto align_pairwise(sequence_t &&seq, alignment_config_t const &config)
Computes the pairwise alignment for a pair of sequences or a range over sequence pairs.
Definition: align_pairwise.hpp:138
constexpr simd_t iota(typename simd_traits< simd_t >::scalar_type const offset)
Fills a seqan3::simd::simd_type vector with the scalar values offset, offset+1, offset+2,...
Definition: algorithm.hpp:299
@ single
The text is a single range.
Definition: concept.hpp:84
constexpr auto chunk
A chunk view.
Definition: chunk.hpp:29
auto const get
A view calling std::get on each element in a range.
Definition: get.hpp:66
constexpr auto persist
A view adaptor that wraps rvalue references of non-views.
Definition: persist_view.hpp:228
constexpr auto zip
A zip view.
Definition: zip.hpp:29
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:70
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
Provides seqan3::detail::persist.
Adaptations of concepts from the Ranges TS.
T tie(T... args)
T tuple_size_v
Provides seqan3::simd::simd_type.
Provides seqan3::simd::simd_traits.
Provides various type traits on generic types.