SeqAn3 3.4.0-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-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 <algorithm>
13#include <concepts>
14#include <ranges>
15#include <seqan3/std/charconv>
16#include <sstream>
17
26
27namespace seqan3::detail
28{
31struct view_equality_fn
32{
34 template <std::ranges::forward_range rng1_type, std::ranges::forward_range rng2_type>
35 constexpr bool operator()(rng1_type && rng1, rng2_type && rng2) const
36 {
37 return std::ranges::equal(rng1, rng2);
38 }
39};
40
48inline void update_alignment_lengths(int32_t & ref_length,
49 int32_t & seq_length,
50 char const cigar_operation,
51 uint32_t const cigar_count)
52{
53 switch (cigar_operation)
54 {
55 case 'M':
56 case '=':
57 case 'X':
58 ref_length += cigar_count, seq_length += cigar_count;
59 break;
60 case 'D':
61 case 'N':
62 ref_length += cigar_count;
63 break;
64 case 'I':
65 seq_length += cigar_count;
66 break;
67 case 'S':
68 case 'H':
69 case 'P':
70 break; // no op (soft-clipping or padding does not increase either length)
71 default:
72 throw format_error{"Illegal cigar operation: " + std::string{cigar_operation}};
73 }
74}
75
88{
89 std::vector<seqan3::cigar> cigar_vector{};
90
91 if (cigar_str == "*")
92 return cigar_vector;
93
94 uint32_t cigar_count{};
95 char const * ptr = cigar_str.data();
96 char const * const end = ptr + cigar_str.size();
97
98 while (ptr < end)
99 {
100 auto const res = std::from_chars(ptr, end, cigar_count); // reads number up to next character
101
102 if (res.ec != std::errc{})
103 throw format_error{"Corrupted cigar string."};
104
105 ptr = res.ptr + 1; // skip cigar operation character
106
107 cigar_vector.emplace_back(cigar_count, seqan3::assign_char_strictly_to(*res.ptr, seqan3::cigar::operation{}));
108 }
109
110 return cigar_vector;
111}
112
118[[nodiscard]] inline std::string get_cigar_string(std::vector<cigar> const & cigar_vector)
119{
120 std::string result{};
121 std::ranges::for_each(cigar_vector,
122 [&result](auto & cig)
123 {
124 result.append(static_cast<std::string_view>(cig.to_string()));
125 });
126 return result;
127}
128
162template <seqan3::aligned_sequence ref_seq_type, seqan3::aligned_sequence query_seq_type>
163[[nodiscard]] inline std::string get_cigar_string(ref_seq_type && ref_seq,
164 query_seq_type && query_seq,
165 uint32_t const query_start_pos = 0,
166 uint32_t const query_end_pos = 0,
167 bool const extended_cigar = false)
168{
169 return get_cigar_string(std::tie(ref_seq, query_seq), query_start_pos, query_end_pos, extended_cigar);
170}
171
174struct access_restrictor_fn
175{
177 template <typename chr_t>
178 [[noreturn]] chr_t operator()(chr_t) const
179 {
180 throw std::logic_error{"Access is not allowed because there is no sequence information."};
181 }
182};
183
184} // 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:731
@ 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:269
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