SeqAn3 3.4.3-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
sam_tag_dictionary.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 <concepts>
13#include <cstddef>
14#include <map>
15#include <variant>
16#include <vector>
17
21
22namespace seqan3::detail
23{
26using sam_tag_variant = std::variant<char,
27 int32_t,
28 float,
38
41constexpr char sam_tag_type_char[12] = {'A', 'i', 'f', 'Z', 'H', 'B', 'B', 'B', 'B', 'B', 'B', 'B'};
44constexpr char sam_tag_type_char_extra[12] = {'\0', '\0', '\0', '\0', '\0', 'c', 'C', 's', 'S', 'i', 'I', 'f'};
45} // namespace seqan3::detail
46
47namespace seqan3
48{
49
50inline namespace literals
51{
52
72template <small_string<2> str> // TODO: better handling if too large string is provided?
73constexpr uint16_t operator""_tag()
74{
75 static_assert(str.size() == 2, "Illegal SAM tag: Exactly two characters must be given.");
76
77 constexpr char char0 = str[0];
78 constexpr char char1 = str[1];
79
80 static_assert((is_alpha(char0) && is_alnum(char1)), "Illegal SAM tag: a SAM tag must match /[A-Za-z][A-Za-z0-9]/.");
81
82 return static_cast<uint16_t>(char0) * 256 + static_cast<uint16_t>(char1);
83}
85
86} // namespace literals
87
165template <uint16_t tag_value>
171
174template <uint16_t tag_value>
176
177// clang-format off
179template <> struct sam_tag_type<"AM"_tag> { using type = int32_t; };
180template <> struct sam_tag_type<"AS"_tag> { using type = int32_t; };
181template <> struct sam_tag_type<"BC"_tag> { using type = std::string; };
182template <> struct sam_tag_type<"BQ"_tag> { using type = std::string; };
183template <> struct sam_tag_type<"BZ"_tag> { using type = std::string; };
184template <> struct sam_tag_type<"CB"_tag> { using type = std::string; };
185template <> struct sam_tag_type<"CC"_tag> { using type = std::string; };
186template <> struct sam_tag_type<"CG"_tag> { using type = std::vector<int32_t>; };
187template <> struct sam_tag_type<"CM"_tag> { using type = int32_t; };
188template <> struct sam_tag_type<"CO"_tag> { using type = std::string; };
189template <> struct sam_tag_type<"CP"_tag> { using type = int32_t; };
190template <> struct sam_tag_type<"CQ"_tag> { using type = std::string; };
191template <> struct sam_tag_type<"CR"_tag> { using type = std::string; };
192template <> struct sam_tag_type<"CS"_tag> { using type = std::string; };
193template <> struct sam_tag_type<"CT"_tag> { using type = std::string; };
194template <> struct sam_tag_type<"CY"_tag> { using type = std::string; };
195template <> struct sam_tag_type<"E2"_tag> { using type = std::string; };
196template <> struct sam_tag_type<"FI"_tag> { using type = int32_t; };
197template <> struct sam_tag_type<"FS"_tag> { using type = std::string; };
198template <> struct sam_tag_type<"FZ"_tag> { using type = std::vector<uint16_t>; };
199
200// template <> struct sam_tag_type<"GC"_tag> {};
201// template <> struct sam_tag_type<"GQ"_tag> {};
202// template <> struct sam_tag_type<"GS"_tag> {};
203
204template <> struct sam_tag_type<"H0"_tag> { using type = int32_t; };
205template <> struct sam_tag_type<"H1"_tag> { using type = int32_t; };
206template <> struct sam_tag_type<"H2"_tag> { using type = int32_t; };
207template <> struct sam_tag_type<"HI"_tag> { using type = int32_t; };
208template <> struct sam_tag_type<"IH"_tag> { using type = int32_t; };
209template <> struct sam_tag_type<"LB"_tag> { using type = std::string; };
210template <> struct sam_tag_type<"MC"_tag> { using type = std::string; };
211template <> struct sam_tag_type<"MD"_tag> { using type = std::string; };
212
213// template <> struct sam_tag_type<"MF"_tag> {};
214
215template <> struct sam_tag_type<"MI"_tag> { using type = std::string; };
216template <> struct sam_tag_type<"MQ"_tag> { using type = int32_t; };
217template <> struct sam_tag_type<"NH"_tag> { using type = int32_t; };
218template <> struct sam_tag_type<"NM"_tag> { using type = int32_t; };
219template <> struct sam_tag_type<"OC"_tag> { using type = std::string; };
220template <> struct sam_tag_type<"OP"_tag> { using type = int32_t; };
221template <> struct sam_tag_type<"OQ"_tag> { using type = std::string; };
222template <> struct sam_tag_type<"OX"_tag> { using type = std::string; };
223template <> struct sam_tag_type<"PG"_tag> { using type = std::string; };
224template <> struct sam_tag_type<"PQ"_tag> { using type = int32_t; };
225template <> struct sam_tag_type<"PT"_tag> { using type = std::string; };
226template <> struct sam_tag_type<"PU"_tag> { using type = std::string; };
227template <> struct sam_tag_type<"Q2"_tag> { using type = std::string; };
228template <> struct sam_tag_type<"QT"_tag> { using type = std::string; };
229template <> struct sam_tag_type<"QX"_tag> { using type = std::string; };
230template <> struct sam_tag_type<"R2"_tag> { using type = std::string; };
231template <> struct sam_tag_type<"RG"_tag> { using type = std::string; };
232template <> struct sam_tag_type<"RT"_tag> { using type = std::string; };
233template <> struct sam_tag_type<"RX"_tag> { using type = std::string; };
234
235// template <> struct sam_tag_type<"S2"_tag> {};
236
237template <> struct sam_tag_type<"SA"_tag> { using type = std::string; };
238template <> struct sam_tag_type<"SM"_tag> { using type = int32_t; };
239
240// template <> struct sam_tag_type<"SQ"_tag> {};
241
242template <> struct sam_tag_type<"TC"_tag> { using type = int32_t; };
243template <> struct sam_tag_type<"U2"_tag> { using type = std::string; };
244template <> struct sam_tag_type<"UQ"_tag> { using type = int32_t; };
246// clang-format on
247
328class sam_tag_dictionary : public std::map<uint16_t, detail::sam_tag_variant>
329{
330private:
333
334public:
337
354 template <uint16_t tag>
355 requires (!std::same_as<sam_tag_type_t<tag>, variant_type>)
356 auto & get() &
357 {
358 if ((*this).count(tag) == 0)
359 (*this)[tag] = sam_tag_type_t<tag>{}; // set correct type if tag is not set yet on
360
361 return std::get<sam_tag_type_t<tag>>((*this)[tag]);
362 }
363
365 template <uint16_t tag>
366 requires (!std::same_as<sam_tag_type_t<tag>, variant_type>)
367 auto && get() &&
368 {
369 if ((*this).count(tag) == 0)
370 (*this)[tag] = sam_tag_type_t<tag>{}; // set correct type if tag is not set yet on
371
372 return std::get<sam_tag_type_t<tag>>(std::move((*this)[tag]));
373 }
374
377 template <uint16_t tag>
378 requires (!std::same_as<sam_tag_type_t<tag>, variant_type>)
379 auto const & get() const &
380 {
381 return std::get<sam_tag_type_t<tag>>((*this).at(tag));
382 }
383
386 template <uint16_t tag>
387 requires (!std::same_as<sam_tag_type_t<tag>, variant_type>)
388 auto const && get() const &&
389 {
390 return std::get<sam_tag_type_t<tag>>(std::move((*this).at(tag)));
391 }
393};
394
395} // namespace seqan3
A "pretty printer" for most SeqAn data structures and related types.
Definition debug_stream_type.hpp:79
The SAM tag dictionary class that stores all optional SAM fields.
Definition sam_tag_dictionary.hpp:329
detail::sam_tag_variant variant_type
The variant type defining all valid SAM tag field types.
Definition sam_tag_dictionary.hpp:336
auto && get() &&
Uses std::map::operator[] for access and default initializes new keys.
Definition sam_tag_dictionary.hpp:367
auto & get() &
Uses std::map::operator[] for access and default initializes new keys.
Definition sam_tag_dictionary.hpp:356
auto const && get() const &&
Uses std::map::at() for access and throws when the key is unknown.
Definition sam_tag_dictionary.hpp:388
auto const & get() const &
Uses std::map::at() for access and throws when the key is unknown.
Definition sam_tag_dictionary.hpp:379
constexpr auto is_alnum
Checks whether c is a alphanumeric character.
Definition predicate.hpp:194
constexpr auto is_alpha
Checks whether c is a alphabetical character.
Definition predicate.hpp:211
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
Provides character predicates for tokenisation.
A constexpr string implementation to manipulate string literals at compile time.
The generic base class.
Definition sam_tag_dictionary.hpp:167
detail::sam_tag_variant type
The type for all unknown tags with no extra overload defaults to a std::variant.
Definition sam_tag_dictionary.hpp:169
typename sam_tag_type< tag_value >::type sam_tag_type_t
Short cut helper for seqan3::sam_tag_type::type.
Definition sam_tag_dictionary.hpp:175
Provides type traits for working with templates.
Hide me