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-2020, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2020, 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 <tuple>
16 
18 
19 namespace seqan3
20 {
21 
22 // ----------------------------------------------------------------------------
23 // enum field
24 // ----------------------------------------------------------------------------
25 
61 enum class field
62 {
63  // Fields used in multiple contexts ........................................
64  seq,
65  id,
66  qual,
68 
69  offset,
70 
71  // Fields unique to structure io ...........................................
72  bpp,
73  structure,
75  energy,
76  react,
77  react_err,
78  comment,
79 
80  // Fields unique to alignment io ...........................................
81  alignment,
82  ref_id,
83  ref_seq,
84  ref_offset,
85  header_ptr,
86 
87  flag,
88  mate,
89  mapq,
90  cigar,
91  tags,
92 
93  bit_score,
94  evalue,
95 
96  // User defined field aliases .. ...........................................
107 
108  // deprecated lowercase:
110 
111  // deprecated uppercase:
146 };
147 
148 // ----------------------------------------------------------------------------
149 // fields
150 // ----------------------------------------------------------------------------
151 
165 template <field ...fs>
166 struct fields
167 {
170  static constexpr std::array<field, sizeof...(fs)> as_array{fs...};
171 
173  static constexpr size_t npos = std::numeric_limits<size_t>::max();
174 
176  static constexpr size_t index_of(field f)
177  {
178  for (size_t i = 0; i < sizeof...(fs); ++i)
179  if (as_array[i] == f)
180  return i;
181  return npos;
182  }
183 
185  static constexpr bool contains(field f)
186  {
187  return index_of(f) != npos;
188  }
189 
190  static_assert([] () constexpr
191  {
192  for (size_t i = 0; i < as_array.size(); ++i)
193  for (size_t j = i + 1; j < as_array.size(); ++j)
194  if (as_array[i] == as_array[j])
195  return false;
196 
197  return true;
198  } (), "You may not include a field twice into fields<>.");
199 };
200 
201 // ----------------------------------------------------------------------------
202 // record
203 // ----------------------------------------------------------------------------
204 
225 template <typename field_types, typename field_ids>
226 struct record : detail::transfer_template_args_onto_t<field_types, std::tuple>
227 {
228 private:
230  template <typename t>
232  requires requires (t & v) { v.clear(); }
234  static constexpr void clear_element(t & v) noexcept(noexcept(v.clear()))
235  {
236  v.clear();
237  }
238 
240  template <typename t>
241  static constexpr void clear_element(t & v) noexcept(noexcept(std::declval<t &>() = t{}))
242  {
243  v = {};
244  }
245 
247  static constexpr auto expander = [] (auto & ...args) { (clear_element(args), ...); };
248 
249 public:
251  using base_type = detail::transfer_template_args_onto_t<field_types, std::tuple>;
252 
256  record() = default;
257  record(record const &) = default;
258  record & operator=(record const &) = default;
259  record(record &&) = default;
260  record & operator=(record &&) = default;
261  ~record() = default;
262 
264  using base_type::base_type;
266 
267  static_assert(field_types::size() == field_ids::as_array.size(),
268  "You must give as many IDs as types to seqan3::record.");
269 
271  void clear() noexcept(noexcept(std::apply(expander, std::declval<record &>())))
272  {
273  std::apply(expander, *this);
274  }
275 
276 protected:
278 
280  template <field f>
281  using field_constant = std::integral_constant<field, f>;
282 
284  template <field f, typename tuple_t>
285  static decltype(auto) get_impl(field_constant<f>, tuple_t && record_as_tuple)
286  {
287  static_assert(field_ids::contains(f), "The record does not contain the field you wish to retrieve.");
288 #if SEQAN3_WORKAROUND_GCC_94967
289  // is_rvalue_reference_v can't be used, because tuple_t won't contain `&&` in the type due to reference
290  // collapsing
291  if constexpr (!std::is_lvalue_reference_v<tuple_t> && std::is_const_v<tuple_t>)
292  {
293  // A simple std::move(...) does not work, because it would mess up tuple_element types like `int const &`
294  using return_t = std::tuple_element_t<field_ids::index_of(f), tuple_t>;
295  return static_cast<return_t const &&>(std::get<field_ids::index_of(f)>(std::move(record_as_tuple)));
296  }
297  else
298  {
299  return std::get<field_ids::index_of(f)>(std::forward<tuple_t>(record_as_tuple));
300  }
301 #else // ^^^ workaround / no workaround vvv
302  return std::get<field_ids::index_of(f)>(std::forward<tuple_t>(record_as_tuple));
303 #endif // SEQAN3_WORKAROUND_GCC_94967
304  }
305 };
306 
307 } // namespace seqan3
308 
309 namespace std
310 {
311 
317 template <typename field_types, typename field_ids>
318 struct tuple_size<seqan3::record<field_types, field_ids>>
319  : tuple_size<typename seqan3::record<field_types, field_ids>::base_type>
320 {};
321 
327 template <size_t elem_no, typename field_types, typename field_ids>
328 struct tuple_element<elem_no, seqan3::record<field_types, field_ids>>
329  : tuple_element<elem_no, typename seqan3::record<field_types, field_ids>::base_type>
330 {};
331 
332 } // namespace std
333 
334 namespace seqan3
335 {
336 
344 template <field f, typename field_types, typename field_ids>
346 {
347  static_assert(field_ids::contains(f), "The record does not contain the field you wish to retrieve.");
348  return std::get<field_ids::index_of(f)>(r);
349 }
350 
352 template <field f, typename field_types, typename field_ids>
354 {
355  static_assert(field_ids::contains(f), "The record does not contain the field you wish to retrieve.");
356  return std::get<field_ids::index_of(f)>(r);
357 }
358 
360 template <field f, typename field_types, typename field_ids>
362 {
363  static_assert(field_ids::contains(f), "The record does not contain the field you wish to retrieve.");
364  return std::get<field_ids::index_of(f)>(std::move(r));
365 }
366 
368 template <field f, typename field_types, typename field_ids>
370 {
371  static_assert(field_ids::contains(f), "The record does not contain the field you wish to retrieve.");
372 #if SEQAN3_WORKAROUND_GCC_94967
373  // A simple std::move(...) does not work, because it would mess up tuple_element types like `int const &`
374  using return_t = std::tuple_element_t<field_ids::index_of(f), record<field_types, field_ids>>;
375  return static_cast<return_t const &&>(std::get<field_ids::index_of(f)>(std::move(r)));
376 #else // ^^^ workaround / no workaround vvv
377  return std::get<field_ids::index_of(f)>(std::move(r));
378 #endif // SEQAN3_WORKAROUND_GCC_94967
379 }
381 
382 } // namespace seqan3
T apply(T... args)
Provides type traits for working with templates.
field
An enumerator for the fields used in file formats.
Definition: record.hpp:62
@ 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:194
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:150
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:70
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:202
T size(T... args)
A class template that holds a choice of seqan3::field.
Definition: record.hpp:167
The class template that file records are based on; behaves like an std::tuple.
Definition: record.hpp:227
auto & get(record< field_types, field_ids > &r)
Free function get() for seqan3::record based on seqan3::field.
Definition: record.hpp:345
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:361
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:271
~record()=default
Defaulted.
detail::transfer_template_args_onto_t< field_types, std::tuple > base_type
A specialisation of std::tuple.
Definition: record.hpp:251
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:353
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:369
record(record const &)=default
Defaulted.