SeqAn3 3.4.3-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
io/sam_file/detail/cigar.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2026 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2026 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <seqan3/std/charconv>
13#include <algorithm>
14#include <concepts>
15#include <ranges>
16#include <sstream>
17#include <vector>
18
27
28namespace seqan3::detail
29{
32struct view_equality_fn
33{
35 template <std::ranges::forward_range rng1_type, std::ranges::forward_range rng2_type>
36 constexpr bool operator()(rng1_type && rng1, rng2_type && rng2) const
37 {
38 return std::ranges::equal(rng1, rng2);
39 }
40};
41
49inline void update_alignment_lengths(int32_t & ref_length,
50 int32_t & seq_length,
51 char const cigar_operation,
52 uint32_t const cigar_count)
53{
54 switch (cigar_operation)
55 {
56 case 'M':
57 case '=':
58 case 'X':
59 ref_length += cigar_count, seq_length += cigar_count;
60 break;
61 case 'D':
62 case 'N':
63 ref_length += cigar_count;
64 break;
65 case 'I':
66 seq_length += cigar_count;
67 break;
68 case 'S':
69 case 'H':
70 case 'P':
71 break; // no op (soft-clipping or padding does not increase either length)
72 default:
73 throw format_error{"Illegal cigar operation: " + std::string{cigar_operation}};
74 }
75}
76
89{
90 std::vector<seqan3::cigar> cigar_vector{};
91
92 if (cigar_str == "*")
93 return cigar_vector;
94
95 uint32_t cigar_count{};
96 char const * ptr = cigar_str.data();
97 char const * const end = ptr + cigar_str.size();
98
99 while (ptr < end)
100 {
101 auto const res = std::from_chars(ptr, end, cigar_count); // reads number up to next character
102
103 if (res.ec != std::errc{})
104 throw format_error{"Corrupted cigar string."};
105
106 ptr = res.ptr + 1; // skip cigar operation character
107
108 cigar_vector.emplace_back(cigar_count, seqan3::assign_char_strictly_to(*res.ptr, seqan3::cigar::operation{}));
109 }
110
111 return cigar_vector;
112}
113
119[[nodiscard]] inline std::string get_cigar_string(std::vector<cigar> const & cigar_vector)
120{
121 std::string result{};
122 std::ranges::for_each(cigar_vector,
123 [&result](auto & cig)
124 {
125 result.append(static_cast<std::string_view>(cig.to_string()));
126 });
127 return result;
128}
129
163template <seqan3::aligned_sequence ref_seq_type, seqan3::aligned_sequence query_seq_type>
164[[nodiscard]] inline std::string get_cigar_string(ref_seq_type && ref_seq,
165 query_seq_type && query_seq,
166 uint32_t const query_start_pos = 0,
167 uint32_t const query_end_pos = 0,
168 bool const extended_cigar = false)
169{
170 return get_cigar_string(std::tie(ref_seq, query_seq), query_start_pos, query_end_pos, extended_cigar);
171}
172
175struct access_restrictor_fn
176{
178 template <typename chr_t>
179 [[noreturn]] chr_t operator()(chr_t) const
180 {
181 throw std::logic_error{"Access is not allowed because there is no sequence information."};
182 }
183};
184
185} // namespace seqan3::detail
Provides the seqan3::cigar alphabet.
The <charconv> header from C++17's standard library.
The actual implementation of seqan3::cigar::operation for documentation purposes only.
Definition cigar_operation.hpp:45
T data(T... args)
T end(T... args)
T equal(T... args)
T for_each(T... args)
T from_chars(T... args)
constexpr auto assign_char_strictly_to
Assign a character to an alphabet object, throw if the character is not valid.
Definition alphabet/concept.hpp:721
@ ref_seq
The (reference) "sequence" information, usually a range of nucleotides or amino acids.
Provides seqan3::detail::pairwise_alignment and seqan3::detail::writable_pairwise_alignment.
#define SEQAN3_WORKAROUND_LITERAL
Our char literals returning std::vector should be constexpr if constexpr std::vector is supported.
Definition platform.hpp:310
Provides character predicates for tokenisation.
Provides seqan3::views::single_pass_input.
T size(T... args)
Provides seqan3::views::take_until and seqan3::views::take_until_or_throw.
T tie(T... args)
Auxiliary for pretty printing of exception messages.
Provides seqan3::tuple_like.
Provides seqan3::views::zip.
Hide me