SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
record.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 <limits>
16 #include <tuple>
17 
19 
20 namespace seqan3
21 {
22 
23 // ----------------------------------------------------------------------------
24 // enum field
25 // ----------------------------------------------------------------------------
26 
62 enum class field
63 {
64  // Fields used in multiple contexts ........................................
65  seq,
66  id,
67  qual,
68 #ifdef SEQAN3_DEPRECATED_310
70 #endif // SEQAN3_DEPRECATED_310
71 
72  offset,
73 
74  // Fields unique to structure io ...........................................
75  bpp,
76  structure,
78  energy,
79  react,
80  react_err,
81  comment,
82 
83  // Fields unique to alignment io ...........................................
84  alignment,
85  ref_id,
86  ref_seq,
87  ref_offset,
88  header_ptr,
89 
90  flag,
91  mate,
92  mapq,
93  cigar,
94  tags,
95 
96  bit_score,
97  evalue,
98 
99  // User defined field aliases .. ...........................................
110 
111 #ifdef SEQAN3_DEPRECATED_310
112  // deprecated lowercase:
114 
115  // deprecated uppercase:
150 #endif // SEQAN3_DEPRECATED_310
151 };
152 
153 // ----------------------------------------------------------------------------
154 // fields
155 // ----------------------------------------------------------------------------
156 
170 template <field ...fs>
171 struct fields
172 {
175  static constexpr std::array<field, sizeof...(fs)> as_array{fs...};
176 
178  static constexpr size_t npos = std::numeric_limits<size_t>::max();
179 
181  static constexpr size_t size = sizeof...(fs);
182 
184  static constexpr size_t index_of(field f)
185  {
186  for (size_t i = 0; i < sizeof...(fs); ++i)
187  if (as_array[i] == f)
188  return i;
189  return npos;
190  }
191 
193  static constexpr bool contains(field f)
194  {
195  return index_of(f) != npos;
196  }
197 
198  static_assert([] () constexpr
199  {
200  for (size_t i = 0; i < as_array.size(); ++i)
201  for (size_t j = i + 1; j < as_array.size(); ++j)
202  if (as_array[i] == as_array[j])
203  return false;
204 
205  return true;
206  } (), "You may not include a field twice into fields<>.");
207 };
208 
209 // ----------------------------------------------------------------------------
210 // record
211 // ----------------------------------------------------------------------------
212 
233 template <typename field_types, typename field_ids>
234 struct record : detail::transfer_template_args_onto_t<field_types, std::tuple>
235 {
236 private:
238  template <typename t>
240  requires requires (t & v) { v.clear(); }
242  static constexpr void clear_element(t & v) noexcept(noexcept(v.clear()))
243  {
244  v.clear();
245  }
246 
248  template <typename t>
249  static constexpr void clear_element(t & v) noexcept(noexcept(std::declval<t &>() = t{}))
250  {
251  v = {};
252  }
253 
255  static constexpr auto expander = [] (auto & ...args) { (clear_element(args), ...); };
256 
257 public:
259  using base_type = detail::transfer_template_args_onto_t<field_types, std::tuple>;
260 
264  record() = default;
265  record(record const &) = default;
266  record & operator=(record const &) = default;
267  record(record &&) = default;
268  record & operator=(record &&) = default;
269  ~record() = default;
270 
272  using base_type::base_type;
274 
275  static_assert(field_types::size() == field_ids::as_array.size(),
276  "You must give as many IDs as types to seqan3::record.");
277 
279  void clear() noexcept(noexcept(std::apply(expander, std::declval<record &>())))
280  {
281  std::apply(expander, *this);
282  }
283 
284 protected:
286 
288  template <field f>
289  using field_constant = std::integral_constant<field, f>;
290 
292  template <field f, typename tuple_t>
293  static decltype(auto) get_impl(field_constant<f>, tuple_t && record_as_tuple)
294  {
295  static_assert(field_ids::contains(f), "The record does not contain the field you wish to retrieve.");
296 #if SEQAN3_WORKAROUND_GCC_94967
297  // is_rvalue_reference_v can't be used, because tuple_t won't contain `&&` in the type due to reference
298  // collapsing
299  if constexpr (!std::is_lvalue_reference_v<tuple_t> && std::is_const_v<tuple_t>)
300  {
301  // A simple std::move(...) does not work, because it would mess up tuple_element types like `int const &`
302  using return_t = std::tuple_element_t<field_ids::index_of(f), tuple_t>;
303  return static_cast<return_t const &&>(std::get<field_ids::index_of(f)>(std::move(record_as_tuple)));
304  }
305  else
306  {
307  return std::get<field_ids::index_of(f)>(std::forward<tuple_t>(record_as_tuple));
308  }
309 #else // ^^^ workaround / no workaround vvv
310  return std::get<field_ids::index_of(f)>(std::forward<tuple_t>(record_as_tuple));
311 #endif // SEQAN3_WORKAROUND_GCC_94967
312  }
313 };
314 
315 } // namespace seqan3
316 
317 namespace std
318 {
319 
325 template <typename field_types, typename field_ids>
326 struct tuple_size<seqan3::record<field_types, field_ids>>
327  : tuple_size<typename seqan3::record<field_types, field_ids>::base_type>
328 {};
329 
335 template <size_t elem_no, typename field_types, typename field_ids>
336 struct tuple_element<elem_no, seqan3::record<field_types, field_ids>>
337  : tuple_element<elem_no, typename seqan3::record<field_types, field_ids>::base_type>
338 {};
339 
340 } // namespace std
341 
342 #ifdef SEQAN3_DEPRECATED_310
343 namespace seqan3
344 {
345 
353 template <field f, typename field_types, typename field_ids>
355 {
356  static_assert(field_ids::contains(f), "The record does not contain the field you wish to retrieve.");
357  return std::get<field_ids::index_of(f)>(r);
358 }
359 
361 template <field f, typename field_types, typename field_ids>
363 {
364  static_assert(field_ids::contains(f), "The record does not contain the field you wish to retrieve.");
365  return std::get<field_ids::index_of(f)>(r);
366 }
367 
369 template <field f, typename field_types, typename field_ids>
371 {
372  static_assert(field_ids::contains(f), "The record does not contain the field you wish to retrieve.");
373  return std::get<field_ids::index_of(f)>(std::move(r));
374 }
375 
377 template <field f, typename field_types, typename field_ids>
379 {
380  static_assert(field_ids::contains(f), "The record does not contain the field you wish to retrieve.");
381 #if SEQAN3_WORKAROUND_GCC_94967
382  // A simple std::move(...) does not work, because it would mess up tuple_element types like `int const &`
383  using return_t = std::tuple_element_t<field_ids::index_of(f), record<field_types, field_ids>>;
384  return static_cast<return_t const &&>(std::get<field_ids::index_of(f)>(std::move(r)));
385 #else // ^^^ workaround / no workaround vvv
386  return std::get<field_ids::index_of(f)>(std::move(r));
387 #endif // SEQAN3_WORKAROUND_GCC_94967
388 }
390 
391 } // namespace seqan3
392 #endif // SEQAN3_DEPRECATED_310
T apply(T... args)
Provides type traits for working with templates.
field
An enumerator for the fields used in file formats.
Definition: record.hpp:63
@ energy
Energy of a folded sequence, represented by one float number.
@ comment
Comment field of arbitrary content, usually a string.
@ CIGAR
Please use the field name in lower case.
@ structure
Fixed interactions, usually a string of structure alphabet characters.
@ bpp
Base pair probability matrix of interactions, usually a matrix of float numbers.
@ FLAG
Please use the field name in lower case.
@ REF_SEQ
Please use the field name in lower case.
@ OFFSET
Please use the field name in lower case.
@ react
Reactivity values of the sequence characters given in a vector of float numbers.
@ flag
The alignment flag (bit information), uint16_t value.
@ USER_DEFINED_6
Please use the field name in lower case.
@ TAGS
Please use the field name in lower case.
@ react_err
Reactivity error values given in a vector corresponding to seqan3::field::react.
@ QUAL
Please use the field name in lower case.
@ USER_DEFINED_8
Please use the field name in lower case.
@ MATE
Please use the field name in lower case.
@ _seq_qual_deprecated
[DEPRECATED] Sequence and qualities combined in one range. Use field::seq and field::qual instead.
@ 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.
@ SEQ
Please use the field name in lower case.
@ alignment
The (pairwise) alignment stored in an object that models seqan3::detail::pairwise_alignment.
@ cigar
The cigar vector (std::vector<seqan3::cigar>) representing the alignment in SAM/BAM format.
@ 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.
@ STRUCTURE
Please use the field name in lower case.
@ USER_DEFINED_2
Please use the field name in lower case.
@ REACT_ERR
Please use the field name in lower case.
@ 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.
@ STRUCTURED_SEQ
Please use the field name in lower case.
@ user_defined_3
Identifier for user defined file formats and specialisations.
@ offset
Sequence (seqan3::field::seq) relative start position (0-based), unsigned value.
@ MAPQ
Please use the field name in lower case.
@ mate
The mate pair information given as a std::tuple of reference name, offset and template length.
@ SEQ_QUAL
[DEPRECATED] Sequence and qualities combined in one range. Use field::seq and field::qual instead.
@ 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.
@ ENERGY
Please use the field name in lower case.
@ ref_id
The identifier of the (reference) sequence that seqan3::field::seq was aligned to.
@ BIT_SCORE
Please use the field name in lower case.
@ REACT
Please use the field name in lower case.
@ structured_seq
Sequence and fixed interactions combined in one range.
@ ALIGNMENT
Please use the field name in lower case.
@ HEADER_PTR
Please use the field name in lower case.
@ evalue
The e-value (length normalized bit score), double value.
@ ID
Please use the field name in lower case.
@ id
The identifier, usually a string.
@ USER_DEFINED_1
Please use the field name in lower case.
@ REF_OFFSET
Please use the field name in lower case.
@ user_defined_6
Identifier for user defined file formats and specialisations.
@ USER_DEFINED_3
Please use the field name in lower case.
@ seq_qual
[DEPRECATED] Sequence and qualities combined in one range. Use field::seq and field::qual instead.
@ 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.
@ USER_DEFINED_9
Please use the field name in lower case.
@ USER_DEFINED_5
Please use the field name in lower case.
@ USER_DEFINED_7
Please use the field name in lower case.
@ qual
The qualities, usually in Phred score notation.
@ BPP
Please use the field name in lower case.
@ COMMENT
Please use the field name in lower case.
@ EVALUE
Please use the field name in lower case.
@ REF_ID
Please use the field name in lower case.
@ USER_DEFINED_4
Please use the field name in lower case.
@ USER_DEFINED_0
Please use the field name in lower case.
constexpr bool contains
Whether a type occurs in a type list or not.
Definition: traits.hpp:231
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:151
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:74
T max(T... args)
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
SeqAn specific customisations in the standard namespace.
#define SEQAN3_DEPRECATED_310
Deprecation message for SeqAn 3.1.0 release.
Definition: platform.hpp:203
T size(T... args)
A class template that holds a choice of seqan3::field.
Definition: record.hpp:172
The class template that file records are based on; behaves like an std::tuple.
Definition: record.hpp:235
auto & get(record< field_types, field_ids > &r)
Free function get() for seqan3::record based on seqan3::field.
Definition: record.hpp:354
auto && get(record< field_types, field_ids > &&r)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: record.hpp:370
record(record &&)=default
Defaulted.
record & operator=(record &&)=default
Defaulted.
void clear() noexcept(noexcept(std::apply(expander, std::declval< record & >())))
Clears containers that provide .clear() and (re-)initialises all other elements with = {}.
Definition: record.hpp:279
~record()=default
Defaulted.
detail::transfer_template_args_onto_t< field_types, std::tuple > base_type
A specialisation of std::tuple.
Definition: record.hpp:259
auto const & get(record< field_types, field_ids > const &r)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: record.hpp:362
record & operator=(record const &)=default
Defaulted.
record()=default
Defaulted.
auto const && get(record< field_types, field_ids > const &&r)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: record.hpp:378
record(record const &)=default
Defaulted.