SeqAn3 3.4.0-rc.4
The Modern C++ library for sequence analysis.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
aligned_sequence_concept.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
11#pragma once
12
13#include <algorithm>
14#include <ranges>
15
20
21// ---------------------------------------------------------------------------------------------------------------------
22// unaligned_seq transformation trait
23// ---------------------------------------------------------------------------------------------------------------------
24
26{
27
29template <template <typename...> typename container_type, typename seq_alph_t, typename... rest_t>
31constexpr auto
32 remove_gap_from_value_type(container_type<gapped<seq_alph_t>, rest_t...>) -> container_type<seq_alph_t, rest_t...>;
33
35template <template <typename...> typename container_type,
36 template <typename...>
37 typename allocator_type,
38 typename seq_alph_t,
39 typename... rest_t>
40 requires container<container_type<gapped<seq_alph_t>, allocator_type<gapped<seq_alph_t>>, rest_t...>>
41constexpr auto
42 remove_gap_from_value_type(container_type<gapped<seq_alph_t>, allocator_type<gapped<seq_alph_t>>, rest_t...>)
43 -> container_type<seq_alph_t, allocator_type<seq_alph_t>, rest_t...>;
44
46template <typename t>
48{};
49
51template <typename t>
52 requires (!requires { typename std::remove_reference_t<t>::unaligned_sequence_type; })
53 && requires { remove_gap_from_value_type(std::declval<t>()); }
55{
57 using type = decltype(remove_gap_from_value_type(std::declval<t>()));
58};
59
60// customisation point for our gap decorators.
62template <typename t>
63 requires requires { typename std::remove_reference_t<t>::unaligned_sequence_type; }
64struct unaligned_seq<t>
65{
67};
68
70template <typename t>
72
73} // namespace seqan3::detail
74
75// ---------------------------------------------------------------------------------------------------------------------
76// aligned_sequence
77// ---------------------------------------------------------------------------------------------------------------------
78
79namespace seqan3
80{
81
103template <typename t>
104concept aligned_sequence = sequence<t> && std::equality_comparable_with<std::ranges::range_reference_t<t>, gap>;
106
213template <typename t>
214concept writable_aligned_sequence = aligned_sequence<t> && std::ranges::forward_range<t> && requires {
216} && requires (t v, detail::unaligned_seq_t<t> unaligned) {
217 // global functions for generic usability
218 { insert_gap(v, std::ranges::begin(v)) } -> std::same_as<std::ranges::iterator_t<t>>;
219 { insert_gap(v, std::ranges::begin(v), 2) } -> std::same_as<std::ranges::iterator_t<t>>;
220 { erase_gap(v, std::ranges::begin(v)) } -> std::same_as<std::ranges::iterator_t<t>>;
221 { erase_gap(v, std::ranges::begin(v), std::ranges::end(v)) } -> std::same_as<std::ranges::iterator_t<t>>;
222 { assign_unaligned(v, unaligned) } -> std::same_as<void>;
223};
225
226// ---------------------------------------------------------------------------------------------------------------------
227// Aligned sequence interface for containers over the seqan3::gapped alphabet
228// ---------------------------------------------------------------------------------------------------------------------
229
250template <sequence_container aligned_seq_t>
251 requires detail::is_gapped_alphabet<std::iter_value_t<aligned_seq_t>>
252inline typename aligned_seq_t::iterator insert_gap(aligned_seq_t & aligned_seq,
253 typename aligned_seq_t::const_iterator pos_it)
254{
255 return aligned_seq.insert(pos_it, std::iter_value_t<aligned_seq_t>{gap{}});
256}
257
274template <sequence_container aligned_seq_t>
275 requires detail::is_gapped_alphabet<std::iter_value_t<aligned_seq_t>>
276inline typename aligned_seq_t::iterator insert_gap(aligned_seq_t & aligned_seq,
277 typename aligned_seq_t::const_iterator pos_it,
278 typename aligned_seq_t::size_type size)
279{
280 return aligned_seq.insert(pos_it, size, std::iter_value_t<aligned_seq_t>{gap{}});
281}
282
302template <sequence_container aligned_seq_t>
303 requires detail::is_gapped_alphabet<std::iter_value_t<aligned_seq_t>>
304inline typename aligned_seq_t::iterator erase_gap(aligned_seq_t & aligned_seq,
305 typename aligned_seq_t::const_iterator pos_it)
306{
307 if (*pos_it != gap{}) // [[unlikely]]
308 throw gap_erase_failure("The position to be erased does not contain a gap.");
309
310 return aligned_seq.erase(pos_it);
311}
312
333template <sequence_container aligned_seq_t>
334 requires detail::is_gapped_alphabet<std::iter_value_t<aligned_seq_t>>
335inline typename aligned_seq_t::iterator erase_gap(aligned_seq_t & aligned_seq,
336 typename aligned_seq_t::const_iterator first,
337 typename aligned_seq_t::const_iterator last)
338{
339 for (auto it = first; it != last; ++it)
340 if (*it != gap{}) // [[unlikely]]
341 throw gap_erase_failure("The range to be erased contains at least one non-gap character.");
342
343 return aligned_seq.erase(first, last);
344}
345
368template <sequence_container aligned_seq_t, std::ranges::forward_range unaligned_sequence_type>
369 requires detail::is_gapped_alphabet<std::iter_value_t<aligned_seq_t>>
371 std::ranges::range_reference_t<unaligned_sequence_type>>
372inline void assign_unaligned(aligned_seq_t & aligned_seq, unaligned_sequence_type && unaligned_seq)
373{
374 using std::swap;
375 aligned_seq_t tmp;
376 tmp.resize(std::ranges::distance(unaligned_seq));
378 std::ranges::copy(unaligned_seq, std::ranges::begin(tmp));
380 swap(aligned_seq, tmp);
381}
383
404template <typename range_type>
405 requires requires (range_type v) {
406 v.insert_gap(std::ranges::iterator_t<range_type>{});
407 v.insert_gap(std::ranges::iterator_t<range_type>{}, typename range_type::size_type{});
408 }
409std::ranges::iterator_t<range_type> insert_gap(range_type & rng,
410 std::ranges::iterator_t<range_type> const pos_it,
411 typename range_type::size_type const size = 1)
412{
413 return rng.insert_gap(pos_it, size);
414}
415
432template <typename range_type>
433 requires requires (range_type v) { v.erase_gap(std::ranges::iterator_t<range_type>{}); }
434std::ranges::iterator_t<range_type> erase_gap(range_type & rng, std::ranges::iterator_t<range_type> const pos_it)
435{
436 return rng.erase_gap(pos_it);
437}
438
457template <typename range_type>
458 requires requires (range_type v) {
459 v.erase_gap(std::ranges::iterator_t<range_type>{}, std::ranges::iterator_t<range_type>{});
460 }
461std::ranges::iterator_t<range_type> erase_gap(range_type & rng,
462 std::ranges::iterator_t<range_type> const first,
463 std::ranges::iterator_t<range_type> const last)
464{
465 return rng.erase_gap(first, last);
466}
468} // namespace seqan3
469
470namespace seqan3::detail
471{
472
476template <typename... elems>
477inline constexpr bool all_model_aligned_seq = (aligned_sequence<elems> && ...);
478
482template <typename... elems>
483inline constexpr bool all_model_aligned_seq<type_list<elems...>> = all_model_aligned_seq<elems...>;
484} // namespace seqan3::detail
Includes customized exception types for the alignment module .
T begin(T... args)
A combined alphabet that can hold values of either of its alternatives..
Definition alphabet_variant.hpp:127
The alphabet of a gap character '-'.
Definition gap.hpp:36
T copy(T... args)
Provides seqan3::gapped.
aligned_seq_t::iterator insert_gap(aligned_seq_t &aligned_seq, typename aligned_seq_t::const_iterator pos_it, typename aligned_seq_t::size_type size)
An implementation of seqan3::writable_aligned_sequence::insert_gap for sequence containers.
Definition aligned_sequence_concept.hpp:276
void assign_unaligned(aligned_seq_t &aligned_seq, unaligned_sequence_type &&unaligned_seq)
An implementation of seqan3::writable_aligned_sequence::assign_unaligned_sequence for sequence contai...
Definition aligned_sequence_concept.hpp:372
std::ranges::iterator_t< range_type > erase_gap(range_type &rng, std::ranges::iterator_t< range_type > const first, std::ranges::iterator_t< range_type > const last)
An implementation of seqan3::writable_aligned_sequence::erase_gap for ranges with the corresponding m...
Definition aligned_sequence_concept.hpp:461
aligned_seq_t::iterator erase_gap(aligned_seq_t &aligned_seq, typename aligned_seq_t::const_iterator pos_it)
An implementation of seqan3::writable_aligned_sequence::erase_gap for sequence containers.
Definition aligned_sequence_concept.hpp:304
std::ranges::iterator_t< range_type > erase_gap(range_type &rng, std::ranges::iterator_t< range_type > const pos_it)
An implementation of seqan3::writable_aligned_sequence::erase_gap for ranges with the corresponding m...
Definition aligned_sequence_concept.hpp:434
aligned_seq_t::iterator erase_gap(aligned_seq_t &aligned_seq, typename aligned_seq_t::const_iterator first, typename aligned_seq_t::const_iterator last)
An implementation of seqan3::writable_aligned_sequence::erase_gap for sequence containers.
Definition aligned_sequence_concept.hpp:335
std::ranges::iterator_t< range_type > insert_gap(range_type &rng, std::ranges::iterator_t< range_type > const pos_it, typename range_type::size_type const size=1)
An implementation of seqan3::writable_aligned_sequence::insert_gap for ranges with the corresponding ...
Definition aligned_sequence_concept.hpp:409
aligned_seq_t::iterator insert_gap(aligned_seq_t &aligned_seq, typename aligned_seq_t::const_iterator pos_it)
An implementation of seqan3::writable_aligned_sequence::insert_gap for sequence containers.
Definition aligned_sequence_concept.hpp:252
The generic concept for an aligned sequence.
The (most general) container concept as defined by the standard library.
The generic concept for a (biological) sequence.
Resolves to std::is_assignable_v<t>.
The generic concept for an aligned sequence that is writable.
The internal SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
constexpr auto remove_gap_from_value_type(container_type< gapped< seq_alph_t >, rest_t... >) -> container_type< seq_alph_t, rest_t... >
Helper function to deduce the unaligned sequence type from an aligned sequence container.
typename unaligned_seq< t >::type unaligned_seq_t
Helper type that delegates to seqan3::detail::unaligned_seq::type.
Definition aligned_sequence_concept.hpp:71
constexpr bool all_model_aligned_seq
True, if each type models seqan3::aligned_sequence; false otherwise.
Definition aligned_sequence_concept.hpp:477
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
#define SEQAN3_WORKAROUND_GCC_BOGUS_MEMCPY_START(...)
Denotes the start of a block where diagnostics are ignored.
Definition platform.hpp:268
#define SEQAN3_WORKAROUND_GCC_BOGUS_MEMCPY_STOP
Denotes the end of a block where diagnostics are ignored.
Definition platform.hpp:277
Additional non-standard concepts for ranges.
decltype(remove_gap_from_value_type(std::declval< t >())) type
The unaligned sequence type of t.
Definition aligned_sequence_concept.hpp:57
Default transformation trait that shall expose the unaligned sequence type of t when specialised.
Definition aligned_sequence_concept.hpp:48
Type that contains multiple types.
Definition type_list.hpp:26
T swap(T... args)
Adaptations of concepts from the standard library.
Hide me