SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
align_pairwise.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 <concepts>
13#include <functional>
14#include <iostream>
15#include <ranges>
16#include <tuple>
17#include <type_traits>
18
29
30namespace seqan3
31{
32
127template <typename sequence_t, typename alignment_config_t>
128 requires detail::align_pairwise_single_input<sequence_t>
129 && std::copy_constructible<std::remove_reference_t<sequence_t>>
130 && detail::is_type_specialisation_of_v<alignment_config_t, configuration>
131constexpr auto align_pairwise(sequence_t && seq, alignment_config_t const & config)
132{
133 using std::get;
134
135 if constexpr (std::is_lvalue_reference_v<sequence_t>) // Forward tuple elements as references.
136 {
137 return align_pairwise(std::tie(get<0>(seq), get<1>(seq)), config);
138 }
139 else
140 {
141 static_assert(std::tuple_size_v<std::remove_reference_t<sequence_t>> == 2,
142 "Alignment configuration error: Expects exactly two sequences for pairwise alignments.");
143
144 static_assert(std::ranges::viewable_range<std::tuple_element_t<0, std::remove_reference_t<sequence_t>>>
145 && std::ranges::viewable_range<std::tuple_element_t<1, std::remove_reference_t<sequence_t>>>,
146 "Alignment configuration error: The tuple elements must model std::ranges::viewable_range.");
147
148 return align_pairwise(std::views::single(std::forward<sequence_t>(seq)), config);
149 }
150}
151
153template <typename sequence_t, typename alignment_config_t>
154 requires detail::align_pairwise_range_input<sequence_t>
155 && detail::is_type_specialisation_of_v<alignment_config_t, configuration>
156constexpr auto align_pairwise(sequence_t && sequences, alignment_config_t const & config)
157{
158 using first_seq_t = std::tuple_element_t<0, std::ranges::range_value_t<sequence_t>>;
159 using second_seq_t = std::tuple_element_t<1, std::ranges::range_value_t<sequence_t>>;
160
161 static_assert(std::ranges::random_access_range<first_seq_t> && std::ranges::sized_range<first_seq_t>,
162 "Alignment configuration error: The sequence must model random_access_range and sized_range.");
163 static_assert(std::ranges::random_access_range<second_seq_t> && std::ranges::sized_range<second_seq_t>,
164 "Alignment configuration error: The sequence must model random_access_range and sized_range.");
165
166 // Pipe with seqan3::detail::all to allow rvalue non-view ranges.
167 auto seq_view = std::forward<sequence_t>(sequences) | seqan3::detail::all;
168 // Configure the alignment algorithm.
169 auto && [algorithm, complete_config] = detail::alignment_configurator::configure<decltype(seq_view)>(config);
170
171 using complete_config_t = std::remove_cvref_t<decltype(complete_config)>;
172 using traits_t = detail::alignment_configuration_traits<complete_config_t>;
173
174 auto indexed_sequence_chunk_view =
175 views::zip(seq_view, std::views::iota(0)) | views::chunk(traits_t::alignments_per_vector);
176
177 using indexed_sequences_t = decltype(indexed_sequence_chunk_view);
178 using alignment_result_t = typename traits_t::alignment_result_type;
180 detail::execution_handler_parallel,
181 detail::execution_handler_sequential>;
182 using executor_t = detail::
183 algorithm_executor_blocking<indexed_sequences_t, decltype(algorithm), alignment_result_t, execution_handler_t>;
184
185 // Select the execution handler for the alignment configuration.
186 auto select_execution_handler = [parallel = complete_config.get_or(align_cfg::parallel{})]()
187 {
188 if constexpr (std::same_as<execution_handler_t, detail::execution_handler_parallel>)
189 {
190 auto thread_count = parallel.thread_count;
191 if (!thread_count)
192 throw std::runtime_error{"You must configure the number of threads in seqan3::align_cfg::parallel."};
193
194 return execution_handler_t{*thread_count};
195 }
196 else
197 {
198 return execution_handler_t{};
199 }
200 };
201
202 if constexpr (traits_t::is_one_way_execution) // Just compute alignment and wait until all alignments are computed.
203 select_execution_handler().bulk_execute(algorithm,
204 indexed_sequence_chunk_view,
205 get<align_cfg::on_result>(complete_config).callback);
206 else // Require two way execution: return the range over the alignments.
207 return algorithm_result_generator_range{executor_t{std::move(indexed_sequence_chunk_view),
208 std::move(algorithm),
209 alignment_result_t{},
210 select_execution_handler()}};
211}
213
214} // 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.
Provides seqan3::detail::all.
Provides various type traits on generic types.
seqan3::detail::parallel_mode< std::integral_constant< seqan3::detail::align_config_id, seqan3::detail::align_config_id::parallel > > parallel
Enables the parallel execution of the alignment algorithm if possible for the given configuration.
Definition align_config_parallel.hpp:35
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:131
seqan::stl::views::all all
Returns a view that includes all elements of the range argument.
Definition all_view.hpp:35
@ seq
The "sequence", usually a range of nucleotides or amino acids.
seqan::stl::views::zip zip
A view adaptor that takes several views and returns tuple-like values from every i-th element of each...
Definition zip.hpp:24
seqan::stl::views::chunk chunk
A view adaptor that divides a range into chunks. <dl class="no-api">This entity is not part of the Se...
Definition chunk.hpp:23
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
Provides seqan3::simd::simd_type.
Provides seqan3::simd::simd_traits.
T tie(T... args)
Hide me