SeqAn3  3.0.2
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 
17 #include <meta/meta.hpp>
18 
21 
22 namespace seqan3
23 {
24 
25 // ----------------------------------------------------------------------------
26 // enum field
27 // ----------------------------------------------------------------------------
28 
64 enum class field
65 {
66  // Fields used in multiple contexts ........................................
67  seq,
68  id,
69  qual,
70  seq_qual,
71  offset,
72 
73  // Fields unique to structure io ...........................................
74  bpp,
75  structure,
76  structured_seq,
77  energy,
78  react,
79  react_err,
80  comment,
81 
82  // Fields unique to alignment io ...........................................
83  alignment,
84  ref_id,
85  ref_seq,
86  ref_offset,
87  header_ptr,
88 
89  flag,
90  mate,
91  mapq,
92  cigar,
93  tags,
94 
95  bit_score,
96  evalue,
97 
98  // User defined field aliases .. ...........................................
99  user_defined_0,
100  user_defined_1,
101  user_defined_2,
102  user_defined_3,
103  user_defined_4,
104  user_defined_5,
105  user_defined_6,
106  user_defined_7,
107  user_defined_8,
108  user_defined_9,
109 
110  // deprecated uppercase:
111  SEQ SEQAN3_DEPRECATED_310 = seq,
113  QUAL SEQAN3_DEPRECATED_310 = qual,
114  SEQ_QUAL SEQAN3_DEPRECATED_310 = seq_qual,
115  OFFSET SEQAN3_DEPRECATED_310 = offset,
116  BPP SEQAN3_DEPRECATED_310 = bpp,
117  STRUCTURE SEQAN3_DEPRECATED_310 = structure,
118  STRUCTURED_SEQ SEQAN3_DEPRECATED_310 = structured_seq,
119  ENERGY SEQAN3_DEPRECATED_310 = energy,
120  REACT SEQAN3_DEPRECATED_310 = react,
121  REACT_ERR SEQAN3_DEPRECATED_310 = react_err,
122  COMMENT SEQAN3_DEPRECATED_310 = comment,
123  ALIGNMENT SEQAN3_DEPRECATED_310 = alignment,
124  REF_ID SEQAN3_DEPRECATED_310 = ref_id,
125  REF_SEQ SEQAN3_DEPRECATED_310 = ref_seq,
126  REF_OFFSET SEQAN3_DEPRECATED_310 = ref_offset,
127  HEADER_PTR SEQAN3_DEPRECATED_310 = header_ptr,
128  FLAG SEQAN3_DEPRECATED_310 = flag,
129  MATE SEQAN3_DEPRECATED_310 = mate,
130  MAPQ SEQAN3_DEPRECATED_310 = mapq,
131  CIGAR SEQAN3_DEPRECATED_310 = cigar,
132  TAGS SEQAN3_DEPRECATED_310 = tags,
133  BIT_SCORE SEQAN3_DEPRECATED_310 = bit_score,
134  EVALUE SEQAN3_DEPRECATED_310 = evalue,
135  USER_DEFINED_0 SEQAN3_DEPRECATED_310 = user_defined_0,
136  USER_DEFINED_1 SEQAN3_DEPRECATED_310 = user_defined_1,
137  USER_DEFINED_2 SEQAN3_DEPRECATED_310 = user_defined_2,
138  USER_DEFINED_3 SEQAN3_DEPRECATED_310 = user_defined_3,
139  USER_DEFINED_4 SEQAN3_DEPRECATED_310 = user_defined_4,
140  USER_DEFINED_5 SEQAN3_DEPRECATED_310 = user_defined_5,
141  USER_DEFINED_6 SEQAN3_DEPRECATED_310 = user_defined_6,
142  USER_DEFINED_7 SEQAN3_DEPRECATED_310 = user_defined_7,
143  USER_DEFINED_8 SEQAN3_DEPRECATED_310 = user_defined_8,
144  USER_DEFINED_9 SEQAN3_DEPRECATED_310 = user_defined_9,
145 };
146 
147 // ----------------------------------------------------------------------------
148 // fields
149 // ----------------------------------------------------------------------------
150 
164 template <field ...fs>
165 struct fields
166 {
169  static constexpr std::array<field, sizeof...(fs)> as_array{fs...};
170 
172  static constexpr size_t npos = std::numeric_limits<size_t>::max();
173 
175  static constexpr size_t index_of(field f)
176  {
177  for (size_t i = 0; i < sizeof...(fs); ++i)
178  if (as_array[i] == f)
179  return i;
180  return npos;
181  }
182 
184  static constexpr bool contains(field f)
185  {
186  return index_of(f) != npos;
187  }
188 
189  static_assert([] () constexpr
190  {
191  for (size_t i = 0; i < as_array.size(); ++i)
192  for (size_t j = i + 1; j < as_array.size(); ++j)
193  if (as_array[i] == as_array[j])
194  return false;
195 
196  return true;
197  } (), "You may not include a field twice into fields<>.");
198 };
199 
200 // ----------------------------------------------------------------------------
201 // record
202 // ----------------------------------------------------------------------------
203 
224 template <typename field_types, typename field_ids>
225 struct record : detail::transfer_template_args_onto_t<field_types, std::tuple>
226 {
227 private:
229  template <typename t>
231  requires requires (t & v) { v.clear(); }
233  static constexpr void clear_element(t & v) noexcept(noexcept(v.clear()))
234  {
235  v.clear();
236  }
237 
239  template <typename t>
240  static constexpr void clear_element(t & v) noexcept(noexcept(std::declval<t &>() = t{}))
241  {
242  v = {};
243  }
244 
246  static constexpr auto expander = [] (auto & ...args) { (clear_element(args), ...); };
247 
248 public:
250  using base_type = detail::transfer_template_args_onto_t<field_types, std::tuple>;
251 
255  record() = default;
256  record(record const &) = default;
257  record & operator=(record const &) = default;
258  record(record &&) = default;
259  record & operator=(record &&) = default;
260  ~record() = default;
261 
263  using base_type::base_type;
265 
266  static_assert(field_types::size() == field_ids::as_array.size(),
267  "You must give as many IDs as types to seqan3::record.");
268 
270  void clear() noexcept(noexcept(std::apply(expander, std::declval<record &>())))
271  {
272  std::apply(expander, *this);
273  }
274 };
275 
276 } // namespace seqan3
277 
278 namespace std
279 {
280 
286 template <typename field_types, typename field_ids>
287 struct tuple_size<seqan3::record<field_types, field_ids>>
288 {
290  static constexpr size_t value = tuple_size_v<typename seqan3::record<field_types, field_ids>::base_type>;
291 };
292 
298 template <size_t elem_no, typename field_types, typename field_ids>
299 struct tuple_element<elem_no, seqan3::record<field_types, field_ids>>
300 {
302  using type = std::tuple_element_t<elem_no, typename seqan3::record<field_types, field_ids>::base_type>;
303 };
304 
305 } // namespace std
306 
307 namespace seqan3
308 {
309 
316 template <field f, typename field_types, typename field_ids>
319 {
320  static_assert(field_ids::contains(f), "The record does not contain the field you wish to retrieve.");
321  return std::get<field_ids::index_of(f)>(r);
322 }
323 
325 template <field f, typename field_types, typename field_ids>
326 auto const & get(record<field_types, field_ids> const & r)
327 {
328  static_assert(field_ids::contains(f), "The record does not contain the field you wish to retrieve.");
329  return std::get<field_ids::index_of(f)>(r);
330 }
331 
333 template <field f, typename field_types, typename field_ids>
335 {
336  static_assert(field_ids::contains(f), "The record does not contain the field you wish to retrieve.");
337  return std::get<field_ids::index_of(f)>(std::move(r));
338 }
339 
341 template <field f, typename field_types, typename field_ids>
342 auto const && get(record<field_types, field_ids> const && r)
343 {
344  static_assert(field_ids::contains(f), "The record does not contain the field you wish to retrieve.");
345  return std::get<field_ids::index_of(f)>(std::move(r));
346 }
348 
349 } // namespace seqan3
std::tuple_element< elem_no, seqan3::record< field_types, field_ids > >::type
std::tuple_element_t< elem_no, typename seqan3::record< field_types, field_ids >::base_type > type
The member type. Delegates to same type on base_type.
Definition: record.hpp:302
std::apply
T apply(T... args)
seqan3::record::record
record(record const &)=default
Defaulted.
seqan3::record::record
record()=default
Defaulted.
seqan3::field::seq
@ seq
The "sequence", usually a range of nucleotides or amino acids.
seqan3::field::offset
@ offset
Sequence (SEQ) relative start position (0-based), unsigned value.
seqan3::record::operator=
record & operator=(record &&)=default
Defaulted.
std::array::size
T size(T... args)
seqan3::record::get
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:326
seqan3::field::id
@ id
The identifier, usually a string.
template_inspection.hpp
Provides seqan3::type_list and auxiliary type traits.
tuple
seqan3::record::get
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:342
seqan3::pack_traits::contains
constexpr bool contains
Whether a type occurs in a pack or not.
Definition: traits.hpp:193
seqan3::fields
A class template that holds a choice of seqan3::field.
Definition: record.hpp:166
SEQAN3_DEPRECATED_310
#define SEQAN3_DEPRECATED_310
Deprecation message for SeqAn 3.1.0 release.
Definition: platform.hpp:194
seqan3::seq
constexpr sequenced_policy seq
Global execution policy object for sequenced execution policy.
Definition: execution.hpp:54
seqan3::field::cigar
@ cigar
The cigar vector (std::vector<seqan3::cigar>) representing the alignment in SAM/BAM format.
seqan3::views::move
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:68
seqan3::field::mapq
@ mapq
The mapping quality of the SEQ alignment, usually a ohred-scaled score.
seqan3::record::get
auto & get(record< field_types, field_ids > &r)
Free function get() for seqan3::record based on seqan3::field.
Definition: record.hpp:318
std::array
seqan3
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
seqan3::record::record
record(record &&)=default
Defaulted.
seqan3::record::operator=
record & operator=(record const &)=default
Defaulted.
seqan3::record::get
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:334
seqan3::pack_traits::size
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:116
std
SeqAn specific customisations in the standard namespace.
seqan3::field
field
An enumerator for the fields used in file formats.
Definition: record.hpp:65
seqan3::record
The class template that file records are based on; behaves like an std::tuple.
Definition: record.hpp:226
seqan3::record::base_type
detail::transfer_template_args_onto_t< field_types, std::tuple > base_type
A specialisation of std::tuple.
Definition: record.hpp:250
seqan3::field::flag
@ flag
The alignment flag (bit information), uint16_t value.
std::numeric_limits::max
T max(T... args)
seqan3::record::~record
~record()=default
Defaulted.
seqan3::record::clear
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:270
seqan3::field::header_ptr
@ header_ptr
A pointer to the seqan3::alignment_file_header object storing header information.
seqan3::field::alignment
@ alignment
The (pairwise) alignment stored in an seqan3::alignment object.
type_list.hpp
Provides seqan3::type_list.