SeqAn3 3.3.0-rc.1
The Modern C++ library for sequence analysis.
alignment_from_cigar.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2006-2022, Knut Reinert & Freie Universität Berlin
3// Copyright (c) 2016-2022, 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 <vector>
16
21
22namespace seqan3
23{
24
82template <typename reference_type, typename sequence_type>
83inline auto alignment_from_cigar(std::vector<cigar> const & cigar_vector,
84 reference_type const & reference,
85 uint32_t const zero_based_reference_start_position,
86 sequence_type const & query)
87{
88 if (cigar_vector.empty())
89 throw std::logic_error{"An empty CIGAR is not a valid alignment representation."};
90
91 // compute the length of the aligned region in the reference sequence
92 // -------------------------------------------------------------------------
93 // this requires a first stream over the cigar vector.
94 uint32_t reference_length{0};
95 uint32_t query_length{0};
96
97 for (auto [cigar_count, cigar_operation] : cigar_vector)
98 {
99 if (('M'_cigar_operation == cigar_operation) || ('='_cigar_operation == cigar_operation)
100 || ('X'_cigar_operation == cigar_operation))
101 {
102 reference_length += cigar_count;
103 query_length += cigar_count;
104 }
105 else if ('D'_cigar_operation == cigar_operation)
106 {
107 reference_length += cigar_count;
108 }
109 else if ('I'_cigar_operation == cigar_operation)
110 {
111 query_length += cigar_count;
112 }
113 }
114
115 if (static_cast<size_t>(zero_based_reference_start_position + reference_length) > std::ranges::size(reference))
116 throw std::logic_error{"The CIGAR string indicates a reference length of at least "
117 + std::to_string(zero_based_reference_start_position + reference_length)
118 + ", but the supplied reference sequence is only of size"
119 + std::to_string(std::ranges::size(reference)) + "."};
120
121 // Get soft clipping at the start and end of the CIGAR string
122 // -------------------------------------------------------------------------
123 uint32_t soft_clipping_start{0};
124 uint32_t soft_clipping_end{0};
125
126 // Checks whether the given index in the cigar vector is a soft clip.
127 auto soft_clipping_at = [&cigar_vector](size_t const index)
128 {
129 return cigar_vector[index] == 'S'_cigar_operation;
130 };
131 // Checks whether the given index in the cigar vector is a hard clip.
132 auto hard_clipping_at = [&](size_t const index)
133 {
134 return cigar_vector[index] == 'H'_cigar_operation;
135 };
136 // Checks whether the given cigar vector has at least min_size many elements.
137 auto vector_size_at_least = [&](size_t const min_size)
138 {
139 return cigar_vector.size() >= min_size;
140 };
141 // Returns the cigar count of the ith cigar element in the given cigar vector.
142 auto cigar_count_at = [&](size_t const index)
143 {
144 return get<0>(cigar_vector[index]);
145 };
146
147 // check for soft clipping at the first two positions
148 // cigar is non-empty, checked at the very beginning.
149 if (soft_clipping_at(0))
150 soft_clipping_start = cigar_count_at(0);
151 else if (vector_size_at_least(2) && hard_clipping_at(0) && soft_clipping_at(1))
152 soft_clipping_start = cigar_count_at(1);
153
154 // Check for soft clipping at the last two positions to validate the CIGAR string.
155 // Even if the two following arithmetics overflow, they are protected by the corresponding if expressions below.
156 auto const last_index = cigar_vector.size() - 1;
157 auto const second_last_index = last_index - 1;
158
159 if (vector_size_at_least(2) && soft_clipping_at(last_index))
160 soft_clipping_end = cigar_count_at(last_index);
161 else if (vector_size_at_least(3) && hard_clipping_at(last_index) && soft_clipping_at(second_last_index))
162 soft_clipping_end = cigar_count_at(second_last_index);
163
164 if (soft_clipping_start + query_length + soft_clipping_end != std::ranges::size(query))
165 throw std::logic_error{"The CIGAR string indicates a query/read sequence length of "
166 + std::to_string(soft_clipping_start + query_length + soft_clipping_end)
167 + ", but the supplied query/read sequence is of size"
168 + std::to_string(std::ranges::size(query)) + "."};
169
170 // Assign the sequence to the alignment (a tuple of 2 gap decorators)
171 // -------------------------------------------------------------------------
172 using gapped_reference_type = gap_decorator<decltype(reference | views::slice(0, 0))>;
173 using gapped_sequence_type = gap_decorator<decltype(query | views::slice(0, 0))>;
175
176 alignment_type alignment{};
177
178 assign_unaligned(get<0>(alignment),
179 reference
180 | views::slice(zero_based_reference_start_position,
181 zero_based_reference_start_position + reference_length));
182 // query_length already accounts for soft clipping at begin and end
183 assign_unaligned(get<1>(alignment), query | views::slice(soft_clipping_start, soft_clipping_start + query_length));
184
185 // Insert gaps into the alignment based on the cigar vector
186 // -------------------------------------------------------------------------
187 using std::get;
188 auto current_ref_pos = std::ranges::begin(get<0>(alignment));
189 auto current_read_pos = std::ranges::begin(get<1>(alignment));
190
191 for (auto [cigar_count, cigar_operation] : cigar_vector)
192 {
193 if (('M'_cigar_operation == cigar_operation) || ('='_cigar_operation == cigar_operation)
194 || ('X'_cigar_operation == cigar_operation))
195 {
196 std::ranges::advance(current_ref_pos, cigar_count);
197 std::ranges::advance(current_read_pos, cigar_count);
198 }
199 else if (('D'_cigar_operation == cigar_operation) || ('N'_cigar_operation == cigar_operation))
200 {
201 // insert gaps into query
202 current_read_pos = get<1>(alignment).insert_gap(current_read_pos, cigar_count);
203 ++current_read_pos;
204 std::ranges::advance(current_ref_pos, cigar_count);
205 }
206 else if (('I'_cigar_operation == cigar_operation)) // Insert gaps into ref
207 {
208 current_ref_pos = get<0>(alignment).insert_gap(current_ref_pos, cigar_count);
209 ++current_ref_pos;
210 std::ranges::advance(current_read_pos, cigar_count);
211 }
212 else if (('P'_cigar_operation == cigar_operation)) // skip padding
213 {
214 current_ref_pos = get<0>(alignment).insert_gap(current_ref_pos, cigar_count);
215 ++current_ref_pos;
216
217 current_read_pos = get<1>(alignment).insert_gap(current_read_pos, cigar_count);
218 ++current_read_pos;
219 }
220 // S and H are ignored as they are handled by cropping the sequence
221 }
222
223 return alignment;
224}
225
226} // namespace seqan3
Includes the aligned_sequence and the related insert_gap and erase_gap functions to enable stl contai...
Provides the seqan3::cigar alphabet.
T begin(T... args)
A gap decorator allows the annotation of sequences with gap symbols while leaving the underlying sequ...
Definition: gap_decorator.hpp:81
T empty(T... args)
Provides seqan3::gap_decorator.
auto alignment_from_cigar(std::vector< cigar > const &cigar_vector, reference_type const &reference, uint32_t const zero_based_reference_start_position, sequence_type const &query)
Construct an alignment from a CIGAR string and the corresponding sequences.
Definition: alignment_from_cigar.hpp:83
@ alignment
The (pairwise) alignment stored in an object that models seqan3::detail::pairwise_alignment.
constexpr size_t size
The size of a type pack.
Definition: type_pack/traits.hpp:146
constexpr auto slice
A view adaptor that returns a half-open interval on the underlying range.
Definition: slice.hpp:178
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
constexpr auto const & get(configuration< configs_t... > const &config) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: configuration.hpp:415
T size(T... args)
Provides seqan3::views::slice.
T to_string(T... args)