SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
record.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 <limits>
13#include <tuple>
14
16
17namespace seqan3
18{
19
20// ----------------------------------------------------------------------------
21// enum field
22// ----------------------------------------------------------------------------
23
59enum class field
60{
61 // Fields used in multiple contexts ........................................
62 seq,
63 id,
64 qual,
65
66 offset,
67
68 // Fields unique to structure io ...........................................
69 bpp,
70 structure,
72 energy,
73 react,
74 react_err,
75 comment,
76
77 // Fields unique to alignment io ...........................................
78 alignment,
79 ref_id,
80 ref_seq,
83
84 flag,
85 mate,
86 mapq,
87 cigar,
88 tags,
89
90 bit_score,
91 evalue,
92
93 // User defined field aliases .. ...........................................
104};
105
106// ----------------------------------------------------------------------------
107// fields
108// ----------------------------------------------------------------------------
109
123template <field... fs>
124struct fields
125{
128 static constexpr std::array<field, sizeof...(fs)> as_array{fs...};
129
131 static constexpr size_t npos = std::numeric_limits<size_t>::max();
132
134 static constexpr size_t size = sizeof...(fs);
135
137 static constexpr size_t index_of(field f)
138 {
139 for (size_t i = 0; i < sizeof...(fs); ++i)
140 if (as_array[i] == f)
141 return i;
142 return npos;
143 }
144
146 static constexpr bool contains(field f)
147 {
148 return index_of(f) != npos;
149 }
150
151 static_assert(
152 []() constexpr
153 {
154 for (size_t i = 0; i < as_array.size(); ++i)
155 for (size_t j = i + 1; j < as_array.size(); ++j)
156 if (as_array[i] == as_array[j])
157 return false;
158
159 return true;
160 }(),
161 "You may not include a field twice into fields<>.");
162};
163
164// ----------------------------------------------------------------------------
165// record
166// ----------------------------------------------------------------------------
167
188template <typename field_types, typename field_ids>
189struct record : detail::transfer_template_args_onto_t<field_types, std::tuple>
190{
191public:
193 using base_type = detail::transfer_template_args_onto_t<field_types, std::tuple>;
194
195private:
197 template <typename t>
198 requires requires (t & v) { v.clear(); }
199 static constexpr void clear_element(t & v) noexcept(noexcept(v.clear()))
200 {
201 v.clear();
202 }
203
205 template <typename t>
206 static constexpr void clear_element(t & v) noexcept(noexcept(std::declval<t &>() = t{}))
207 {
208 v = {};
209 }
210
212 static constexpr auto expander = [](auto &... args)
213 {
214 (clear_element(args), ...);
215 };
216
218 base_type & as_base() noexcept
219 {
220 return *this;
221 }
222
223public:
227 record() = default;
228 record(record const &) = default;
229 record & operator=(record const &) = default;
230 record(record &&) = default;
231 record & operator=(record &&) = default;
232 ~record() = default;
233
235 using base_type::base_type;
237
238 static_assert(field_types::size() == field_ids::as_array.size(),
239 "You must give as many IDs as types to seqan3::record.");
240
242 void clear() noexcept(noexcept(std::apply(expander, std::declval<record &>().as_base())))
243 {
244 // PR2165 / __cpp_lib_tuple_like (C++23): std::apply requires tuple-like.
245 // In C++23, this means std::array, std::pair, std::tuple, and std::ranges::subranges. Nothing else.
246 // https://en.cppreference.com/w/cpp/utility/tuple/tuple-like
247 std::apply(expander, as_base());
248 }
249
250protected:
252
254 template <field f>
255 using field_constant = std::integral_constant<field, f>;
256
258 template <field f, typename tuple_t>
259 static decltype(auto) get_impl(field_constant<f>, tuple_t && record_as_tuple)
260 {
261 static_assert(field_ids::contains(f), "The record does not contain the field you wish to retrieve.");
262 return std::get<field_ids::index_of(f)>(std::forward<tuple_t>(record_as_tuple));
263 }
264};
265
266} // namespace seqan3
267
268namespace std
269{
270
276template <typename field_types, typename field_ids>
277struct tuple_size<seqan3::record<field_types, field_ids>> :
278 tuple_size<typename seqan3::record<field_types, field_ids>::base_type>
279{};
280
286template <size_t elem_no, typename field_types, typename field_ids>
287struct tuple_element<elem_no, seqan3::record<field_types, field_ids>> :
288 tuple_element<elem_no, typename seqan3::record<field_types, field_ids>::base_type>
289{};
290
291} // namespace std
T apply(T... args)
The seqan3::cigar semialphabet pairs a counter with a seqan3::cigar::operation letter.
Definition alphabet/cigar/cigar.hpp:57
field
An enumerator for the fields used in file formats.
Definition record.hpp:60
@ energy
Energy of a folded sequence, represented by one float number.
@ comment
Comment field of arbitrary content, usually a string.
@ structure
Fixed interactions, usually a string of structure alphabet characters.
@ bpp
Base pair probability matrix of interactions, usually a matrix of float numbers.
@ react
Reactivity values of the sequence characters given in a vector of float numbers.
@ flag
The alignment flag (bit information), uint16_t value.
@ react_err
Reactivity error values given in a vector corresponding to seqan3::field::react.
@ ref_offset
Sequence (seqan3::field::ref_seq) relative start position (0-based), unsigned value.
@ ref_seq
The (reference) "sequence" information, usually a range of nucleotides or amino acids.
@ alignment
The (pairwise) alignment stored in an object that models seqan3::detail::pairwise_alignment.
@ mapq
The mapping quality of the seqan3::field::seq alignment, usually a Phred-scaled score.
@ user_defined_2
Identifier for user defined file formats and specialisations.
@ user_defined_5
Identifier for user defined file formats and specialisations.
@ bit_score
The bit score (statistical significance indicator), unsigned value.
@ user_defined_0
Identifier for user defined file formats and specialisations.
@ user_defined_8
Identifier for user defined file formats and specialisations.
@ user_defined_3
Identifier for user defined file formats and specialisations.
@ offset
Sequence (seqan3::field::seq) relative start position (0-based), unsigned value.
@ mate
The mate pair information given as a std::tuple of reference name, offset and template length.
@ header_ptr
A pointer to the seqan3::sam_file_header object storing header information.
@ user_defined_7
Identifier for user defined file formats and specialisations.
@ user_defined_4
Identifier for user defined file formats and specialisations.
@ ref_id
The identifier of the (reference) sequence that seqan3::field::seq was aligned to.
@ structured_seq
Sequence and fixed interactions combined in one range.
@ evalue
The e-value (length normalized bit score), double value.
@ id
The identifier, usually a string.
@ user_defined_6
Identifier for user defined file formats and specialisations.
@ tags
The optional tags in the SAM format, stored in a dictionary.
@ user_defined_1
Identifier for user defined file formats and specialisations.
@ user_defined_9
Identifier for user defined file formats and specialisations.
@ seq
The "sequence", usually a range of nucleotides or amino acids.
@ qual
The qualities, usually in Phred score notation.
T max(T... args)
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
SeqAn specific customisations in the standard namespace.
T size(T... args)
A class template that holds a choice of seqan3::field.
Definition record.hpp:125
The class template that file records are based on; behaves like a std::tuple.
Definition record.hpp:190
void clear() noexcept(noexcept(std::apply(expander, std::declval< record & >().as_base())))
Clears containers that provide .clear() and (re-)initialises all other elements with = {}.
Definition record.hpp:242
record(record &&)=default
Defaulted.
~record()=default
Defaulted.
detail::transfer_template_args_onto_t< field_types, std::tuple > base_type
A specialisation of std::tuple.
Definition record.hpp:193
record & operator=(record const &)=default
Defaulted.
record()=default
Defaulted.
record & operator=(record &&)=default
Defaulted.
record(record const &)=default
Defaulted.
Provides type traits for working with templates.
Hide me