SeqAn3  3.1.0-rc.1
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 
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 
109 // ----------------------------------------------------------------------------
110 // fields
111 // ----------------------------------------------------------------------------
112 
126 template <field ...fs>
127 struct fields
128 {
131  static constexpr std::array<field, sizeof...(fs)> as_array{fs...};
132 
134  static constexpr size_t npos = std::numeric_limits<size_t>::max();
135 
137  static constexpr size_t size = sizeof...(fs);
138 
140  static constexpr size_t index_of(field f)
141  {
142  for (size_t i = 0; i < sizeof...(fs); ++i)
143  if (as_array[i] == f)
144  return i;
145  return npos;
146  }
147 
149  static constexpr bool contains(field f)
150  {
151  return index_of(f) != npos;
152  }
153 
154  static_assert([] () constexpr
155  {
156  for (size_t i = 0; i < as_array.size(); ++i)
157  for (size_t j = i + 1; j < as_array.size(); ++j)
158  if (as_array[i] == as_array[j])
159  return false;
160 
161  return true;
162  } (), "You may not include a field twice into fields<>.");
163 };
164 
165 // ----------------------------------------------------------------------------
166 // record
167 // ----------------------------------------------------------------------------
168 
189 template <typename field_types, typename field_ids>
190 struct record : detail::transfer_template_args_onto_t<field_types, std::tuple>
191 {
192 private:
194  template <typename t>
196  requires requires (t & v) { v.clear(); }
198  static constexpr void clear_element(t & v) noexcept(noexcept(v.clear()))
199  {
200  v.clear();
201  }
202 
204  template <typename t>
205  static constexpr void clear_element(t & v) noexcept(noexcept(std::declval<t &>() = t{}))
206  {
207  v = {};
208  }
209 
211  static constexpr auto expander = [] (auto & ...args) { (clear_element(args), ...); };
212 
213 public:
216 
220  record() = default;
221  record(record const &) = default;
222  record & operator=(record const &) = default;
223  record(record &&) = default;
224  record & operator=(record &&) = default;
225  ~record() = default;
226 
228  using base_type::base_type;
230 
231  static_assert(field_types::size() == field_ids::as_array.size(),
232  "You must give as many IDs as types to seqan3::record.");
233 
235  void clear() noexcept(noexcept(std::apply(expander, std::declval<record &>())))
236  {
237  std::apply(expander, *this);
238  }
239 
240 protected:
242 
244  template <field f>
246 
248  template <field f, typename tuple_t>
249  static decltype(auto) get_impl(field_constant<f>, tuple_t && record_as_tuple)
250  {
251  static_assert(field_ids::contains(f), "The record does not contain the field you wish to retrieve.");
252 #if SEQAN3_WORKAROUND_GCC_94967
253  // is_rvalue_reference_v can't be used, because tuple_t won't contain `&&` in the type due to reference
254  // collapsing
255  if constexpr (!std::is_lvalue_reference_v<tuple_t> && std::is_const_v<tuple_t>)
256  {
257  // A simple std::move(...) does not work, because it would mess up tuple_element types like `int const &`
258  using return_t = std::tuple_element_t<field_ids::index_of(f), tuple_t>;
259  return static_cast<return_t const &&>(std::get<field_ids::index_of(f)>(std::move(record_as_tuple)));
260  }
261  else
262  {
263  return std::get<field_ids::index_of(f)>(std::forward<tuple_t>(record_as_tuple));
264  }
265 #else // ^^^ workaround / no workaround vvv
266  return std::get<field_ids::index_of(f)>(std::forward<tuple_t>(record_as_tuple));
267 #endif // SEQAN3_WORKAROUND_GCC_94967
268  }
269 };
270 
271 } // namespace seqan3
272 
273 namespace std
274 {
275 
281 template <typename field_types, typename field_ids>
282 struct tuple_size<seqan3::record<field_types, field_ids>>
283  : tuple_size<typename seqan3::record<field_types, field_ids>::base_type>
284 {};
285 
291 template <size_t elem_no, typename field_types, typename field_ids>
292 struct tuple_element<elem_no, seqan3::record<field_types, field_ids>>
293  : tuple_element<elem_no, typename seqan3::record<field_types, field_ids>::base_type>
294 {};
295 
296 } // namespace std
T apply(T... args)
typename transfer_template_args_onto< source_type, target_template >::type transfer_template_args_onto_t
Shortcut for seqan3::detail::transfer_template_args_onto (transformation_trait shortcut).
Definition: template_inspection.hpp:77
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.
@ 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.
@ 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.
@ 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.
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
Subconcept definition for seqan3::tuple_like to test for std::tuple_size-interface.
T max(T... args)
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
SeqAn specific customisations in the standard namespace.
T size(T... args)
A class template that holds a choice of seqan3::field.
Definition: record.hpp:128
static constexpr std::array< field, sizeof...(fs)> as_array
The template parameters stored in an array for easy access.
Definition: record.hpp:131
static constexpr bool contains(field f)
Whether a field is contained in the parameter pack.
Definition: record.hpp:149
static constexpr size_t npos
Special value that indicates that index_of() failed.
Definition: record.hpp:134
static constexpr size_t index_of(field f)
Retrieve the position of field in the parameter pack.
Definition: record.hpp:140
static constexpr size_t size
The size of fields.
Definition: record.hpp:137
The class template that file records are based on; behaves like an std::tuple.
Definition: record.hpp:191
record(record &&)=default
Defaulted.
record & operator=(record &&)=default
Defaulted.
static decltype(auto) get_impl(field_constant< f >, tuple_t &&record_as_tuple)
This is basically the seqan3::get<f>(static_cast<tuple>(record)) implementation.
Definition: record.hpp:249
static constexpr auto expander
A lambda function that expands a pack and calls clear_element on every argument in the pack.
Definition: record.hpp:211
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:235
~record()=default
Defaulted.
detail::transfer_template_args_onto_t< field_types, std::tuple > base_type
A specialisation of std::tuple.
Definition: record.hpp:215
record & operator=(record const &)=default
Defaulted.
record()=default
Defaulted.
static constexpr void clear_element(t &v) noexcept(noexcept(std::declval< t & >()=t{}))
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: record.hpp:205
static constexpr void clear_element(t &v) noexcept(noexcept(v.clear()))
Auxiliary functions for clear().
Definition: record.hpp:198
record(record const &)=default
Defaulted.
Provides type traits for working with templates.