SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
sam_tag_dictionary.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2021, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2021, 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 <seqan3/std/concepts>
16 #include <map>
17 #include <variant>
18 
23 
24 namespace seqan3::detail
25 {
28  using sam_tag_variant = std::variant<char, int32_t, float, std::string,
34 
36  char constexpr sam_tag_type_char[12] = {'A', 'i', 'f', 'Z', 'H', 'B', 'B', 'B', 'B', 'B', 'B', 'B'};
38  char constexpr sam_tag_type_char_extra[12] = {'\0', '\0', '\0', '\0', '\0', 'c', 'C', 's', 'S', 'i', 'I', 'f'};
39 }
40 
41 namespace seqan3
42 {
43 
44 inline namespace literals
45 {
46 
67 #ifdef __cpp_nontype_template_parameter_class
68 template <small_string<2> str> // TODO: better handling if too large string is provided?
69 constexpr uint16_t operator""_tag()
70 {
71 #else // GCC/Clang extension
72 #pragma GCC diagnostic push
73 #pragma GCC diagnostic ignored "-Wpedantic"
74 template <typename char_t, char_t ...s>
75 constexpr uint16_t operator""_tag()
76 {
77  static_assert(std::same_as<char_t, char>, "Illegal SAM tag: Type must be char.");
78  constexpr small_string<sizeof...(s)> str{std::array<char, sizeof...(s)>{s...}};
79 #pragma GCC diagnostic pop
80 #endif
81 
82  static_assert(str.size() == 2, "Illegal SAM tag: Exactly two characters must be given.");
83 
84  char constexpr char0 = str[0];
85  char constexpr char1 = str[1];
86 
87  static_assert((is_alpha(char0) && is_alnum(char1)),
88  "Illegal SAM tag: a SAM tag must match /[A-Za-z][A-Za-z0-9]/.");
89 
90  return static_cast<uint16_t>(char0) * 256 + static_cast<uint16_t>(char1);
91 }
93 
94 } // inline namespace literals
95 
172 template <uint16_t tag_value>
174 {
177 };
178 
181 template <uint16_t tag_value>
183 
185 template <> struct sam_tag_type<"AM"_tag> { using type = int32_t; };
186 template <> struct sam_tag_type<"AS"_tag> { using type = int32_t; };
187 template <> struct sam_tag_type<"BC"_tag> { using type = std::string; };
188 template <> struct sam_tag_type<"BQ"_tag> { using type = std::string; };
189 template <> struct sam_tag_type<"BZ"_tag> { using type = std::string; };
190 template <> struct sam_tag_type<"CB"_tag> { using type = std::string; };
191 template <> struct sam_tag_type<"CC"_tag> { using type = std::string; };
192 template <> struct sam_tag_type<"CG"_tag> { using type = std::vector<int32_t>; };
193 template <> struct sam_tag_type<"CM"_tag> { using type = int32_t; };
194 template <> struct sam_tag_type<"CO"_tag> { using type = std::string; };
195 template <> struct sam_tag_type<"CP"_tag> { using type = int32_t; };
196 template <> struct sam_tag_type<"CQ"_tag> { using type = std::string; };
197 template <> struct sam_tag_type<"CR"_tag> { using type = std::string; };
198 template <> struct sam_tag_type<"CS"_tag> { using type = std::string; };
199 template <> struct sam_tag_type<"CT"_tag> { using type = std::string; };
200 template <> struct sam_tag_type<"CY"_tag> { using type = std::string; };
201 template <> struct sam_tag_type<"E2"_tag> { using type = std::string; };
202 template <> struct sam_tag_type<"FI"_tag> { using type = int32_t; };
203 template <> struct sam_tag_type<"FS"_tag> { using type = std::string; };
204 template <> struct sam_tag_type<"FZ"_tag> { using type = std::vector<uint16_t>; };
205 
206 // template <> struct sam_tag_type<"GC"_tag> {};
207 // template <> struct sam_tag_type<"GQ"_tag> {};
208 // template <> struct sam_tag_type<"GS"_tag> {};
209 
210 template <> struct sam_tag_type<"H0"_tag> { using type = int32_t; };
211 template <> struct sam_tag_type<"H1"_tag> { using type = int32_t; };
212 template <> struct sam_tag_type<"H2"_tag> { using type = int32_t; };
213 template <> struct sam_tag_type<"HI"_tag> { using type = int32_t; };
214 template <> struct sam_tag_type<"IH"_tag> { using type = int32_t; };
215 template <> struct sam_tag_type<"LB"_tag> { using type = std::string; };
216 template <> struct sam_tag_type<"MC"_tag> { using type = std::string; };
217 template <> struct sam_tag_type<"MD"_tag> { using type = std::string; };
218 
219 // template <> struct sam_tag_type<"MF"_tag> {};
220 
221 template <> struct sam_tag_type<"MI"_tag> { using type = std::string; };
222 template <> struct sam_tag_type<"MQ"_tag> { using type = int32_t; };
223 template <> struct sam_tag_type<"NH"_tag> { using type = int32_t; };
224 template <> struct sam_tag_type<"NM"_tag> { using type = int32_t; };
225 template <> struct sam_tag_type<"OC"_tag> { using type = std::string; };
226 template <> struct sam_tag_type<"OP"_tag> { using type = int32_t; };
227 template <> struct sam_tag_type<"OQ"_tag> { using type = std::string; };
228 template <> struct sam_tag_type<"OX"_tag> { using type = std::string; };
229 template <> struct sam_tag_type<"PG"_tag> { using type = std::string; };
230 template <> struct sam_tag_type<"PQ"_tag> { using type = int32_t; };
231 template <> struct sam_tag_type<"PT"_tag> { using type = std::string; };
232 template <> struct sam_tag_type<"PU"_tag> { using type = std::string; };
233 template <> struct sam_tag_type<"Q2"_tag> { using type = std::string; };
234 template <> struct sam_tag_type<"QT"_tag> { using type = std::string; };
235 template <> struct sam_tag_type<"QX"_tag> { using type = std::string; };
236 template <> struct sam_tag_type<"R2"_tag> { using type = std::string; };
237 template <> struct sam_tag_type<"RG"_tag> { using type = std::string; };
238 template <> struct sam_tag_type<"RT"_tag> { using type = std::string; };
239 template <> struct sam_tag_type<"RX"_tag> { using type = std::string; };
240 
241 // template <> struct sam_tag_type<"S2"_tag> {};
242 
243 template <> struct sam_tag_type<"SA"_tag> { using type = std::string; };
244 template <> struct sam_tag_type<"SM"_tag> { using type = int32_t; };
245 
246 // template <> struct sam_tag_type<"SQ"_tag> {};
247 
248 template <> struct sam_tag_type<"TC"_tag> { using type = int32_t; };
249 template <> struct sam_tag_type<"U2"_tag> { using type = std::string; };
250 template <> struct sam_tag_type<"UQ"_tag> { using type = int32_t; };
252 
331 class sam_tag_dictionary : public std::map<uint16_t, detail::sam_tag_variant>
332 {
333 private:
336 
337 public:
340 
357  template <uint16_t tag>
359  requires (!std::same_as<sam_tag_type_t<tag>, variant_type>)
361  auto & get() &
362  {
363  if ((*this).count(tag) == 0)
364  (*this)[tag] = sam_tag_type_t<tag>{}; // set correct type if tag is not set yet on
365 
366  return std::get<sam_tag_type_t<tag>>((*this)[tag]);
367  }
368 
370  template <uint16_t tag>
372  requires (!std::same_as<sam_tag_type_t<tag>, variant_type>)
374  auto && get() &&
375  {
376  if ((*this).count(tag) == 0)
377  (*this)[tag] = sam_tag_type_t<tag>{}; // set correct type if tag is not set yet on
378 
379  return std::get<sam_tag_type_t<tag>>(std::move((*this)[tag]));
380  }
381 
384  template <uint16_t tag>
386  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>>((*this).at(tag));
391  }
392 
395  template <uint16_t tag>
397  requires (!std::same_as<sam_tag_type_t<tag>, variant_type>)
399  auto const && get() const &&
400  {
401  return std::get<sam_tag_type_t<tag>>(std::move((*this).at(tag)));
402  }
404 };
405 
406 } // namespace seqan3
The SAM tag dictionary class that stores all optional SAM fields.
Definition: sam_tag_dictionary.hpp:332
Implements a small string that can be used for compile time computations.
Definition: small_string.hpp:44
The Concepts library.
Provides type traits for working with templates.
Provides concepts for core language types and relations that don't have concepts in C++20 (yet).
constexpr auto is_alnum
Checks whether c is a alphanumeric character.
Definition: predicate.hpp:220
constexpr auto is_alpha
Checks whether c is a alphabetical character.
Definition: predicate.hpp:239
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:182
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:74
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:429
The generic base class.
Definition: sam_tag_dictionary.hpp:174
Provides character predicates for tokenisation.
A constexpr string implementation to manipulate string literals at compile time.