SeqAn3  3.0.2
The Modern C++ library for sequence analysis.
output.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 <cassert>
16 #include <fstream>
17 #include <optional>
18 #include <string>
19 #include <type_traits>
20 #include <variant>
21 #include <vector>
25 #include <seqan3/io/exception.hpp>
26 #include <seqan3/std/filesystem>
27 #include <seqan3/io/record.hpp>
30 #include <seqan3/io/detail/record.hpp>
37 #include <seqan3/std/ranges>
38 
39 namespace seqan3
40 {
41 
42 // ----------------------------------------------------------------------------
43 // structure_file_output
44 // ----------------------------------------------------------------------------
45 
158 template <detail::fields_specialisation selected_field_ids_ = fields<field::seq, field::id, field::structure>,
159  detail::type_list_of_structure_file_output_formats valid_formats_ = type_list<format_vienna>>
161 {
162 public:
167  using selected_field_ids = selected_field_ids_;
170  using valid_formats = valid_formats_;
172  using stream_char_type = char;
174 
177  field::id,
178  field::bpp,
179  field::structure,
180  field::structured_seq,
181  field::energy,
182  field::react,
183  field::react_err,
184  field::comment,
185  field::offset>;
186 
187  static_assert([] () constexpr
188  {
189  for (field f : selected_field_ids::as_array)
190  if (!field_ids::contains(f))
191  return false;
192  return true;
193  }(),
194  "You selected a field that is not valid for structure files, please refer to the documentation "
195  "of structure_file_output::field_ids for the accepted values.");
196 
197  static_assert([] () constexpr
198  {
199  return !(selected_field_ids::contains(field::structured_seq) &&
201  (selected_field_ids::contains(field::structure))));
202  }(), "You may not select field::structured_seq and either of field::seq and field::structure "
203  "at the same time.");
204 
210  using value_type = void;
213  using reference = void;
215  using const_reference = void;
217  using size_type = void;
221  using iterator = detail::out_file_iterator<structure_file_output>;
223  using const_iterator = void;
225  using sentinel = std::default_sentinel_t;
227 
231  structure_file_output() = delete;
243 
260  selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
261  primary_stream{new std::ofstream{}, stream_deleter_default}
262  {
263  primary_stream->rdbuf()->pubsetbuf(stream_buffer.data(), stream_buffer.size());
264  static_cast<std::basic_ofstream<char> *>(primary_stream.get())->open(filename,
265  std::ios_base::out | std::ios::binary);
266 
267  if (!primary_stream->good())
268  throw file_open_error{"Could not open file " + filename.string() + " for writing."};
269 
270  // possibly add intermediate compression stream
271  secondary_stream = detail::make_secondary_ostream(*primary_stream, filename);
272 
273  // initialise format handler or throw if format is not found
274  detail::set_format(format, filename);
275  }
276 
293  template <output_stream stream_t, structure_file_output_format file_format>
295  requires std::same_as<typename std::remove_reference_t<stream_t>::char_type, char>
297  structure_file_output(stream_t & stream,
298  file_format const & SEQAN3_DOXYGEN_ONLY(format_tag),
299  selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
300  primary_stream{&stream, stream_deleter_noop},
301  secondary_stream{&stream, stream_deleter_noop},
302  format{detail::structure_file_output_format_exposer<file_format>{}}
303  {
304  static_assert(list_traits::contains<file_format, valid_formats>,
305  "You selected a format that is not in the valid_formats of this file.");
306  }
307 
309  template <output_stream stream_t, structure_file_output_format file_format>
311  requires std::same_as<typename std::remove_reference_t<stream_t>::char_type, char>
313  structure_file_output(stream_t && stream,
314  file_format const & SEQAN3_DOXYGEN_ONLY(format_tag),
315  selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
316  primary_stream{new stream_t{std::move(stream)}, stream_deleter_default},
317  secondary_stream{&*primary_stream, stream_deleter_noop},
318  format{detail::structure_file_output_format_exposer<file_format>{}}
319  {
320  static_assert(list_traits::contains<file_format, valid_formats>,
321  "You selected a format that is not in the valid_formats of this file.");
322  }
324 
346  iterator begin() noexcept
347  {
348  return {*this};
349  }
350 
365  sentinel end() noexcept
366  {
367  return {};
368  }
369 
388  template <typename record_t>
389  void push_back(record_t && r)
391  requires tuple_like<record_t> &&
392  requires { requires detail::is_type_specialisation_of_v<std::remove_cvref_t<record_t>, record>; }
394  {
395  write_record(detail::get_or_ignore<field::seq>(r),
396  detail::get_or_ignore<field::id>(r),
397  detail::get_or_ignore<field::bpp>(r),
398  detail::get_or_ignore<field::structure>(r),
399  detail::get_or_ignore<field::structured_seq>(r),
400  detail::get_or_ignore<field::energy>(r),
401  detail::get_or_ignore<field::react>(r),
402  detail::get_or_ignore<field::react_err>(r),
403  detail::get_or_ignore<field::comment>(r),
404  detail::get_or_ignore<field::offset>(r));
405  }
406 
428  template <typename tuple_t>
429  void push_back(tuple_t && t)
431  requires tuple_like<tuple_t>
433  {
434  // index_of might return npos, but this will be handled well by get_or_ignore (and just return ignore)
435  write_record(detail::get_or_ignore<selected_field_ids::index_of(field::seq)>(t),
436  detail::get_or_ignore<selected_field_ids::index_of(field::id)>(t),
437  detail::get_or_ignore<selected_field_ids::index_of(field::bpp)>(t),
438  detail::get_or_ignore<selected_field_ids::index_of(field::structure)>(t),
439  detail::get_or_ignore<selected_field_ids::index_of(field::structured_seq)>(t),
440  detail::get_or_ignore<selected_field_ids::index_of(field::energy)>(t),
441  detail::get_or_ignore<selected_field_ids::index_of(field::react)>(t),
442  detail::get_or_ignore<selected_field_ids::index_of(field::react_err)>(t),
443  detail::get_or_ignore<selected_field_ids::index_of(field::comment)>(t),
444  detail::get_or_ignore<selected_field_ids::index_of(field::offset)>(t));
445  }
446 
470  template <typename arg_t, typename ...arg_types>
471  void emplace_back(arg_t && arg, arg_types && ... args)
472  {
473  push_back(std::tie(arg, args...));
474  }
475 
497  template <std::ranges::input_range rng_t>
502  {
503  for (auto && record : range)
504  push_back(std::forward<decltype(record)>(record));
505  return *this;
506  }
507 
535  template <std::ranges::input_range rng_t>
540  {
541  f = range;
542  return f;
543  }
544 
546  template <std::ranges::input_range rng_t>
551  {
552  f = range;
553  return std::move(f);
554  }
556 
559 
564  {
565  return *secondary_stream;
566  }
568 protected:
571  std::vector<char> stream_buffer{std::vector<char>(1'000'000)};
572 
580  static void stream_deleter_noop(std::basic_ostream<stream_char_type> *) {}
582  static void stream_deleter_default(std::basic_ostream<stream_char_type> * ptr) { delete ptr; }
583 
585  stream_ptr_t primary_stream{nullptr, stream_deleter_noop};
587  stream_ptr_t secondary_stream{nullptr, stream_deleter_noop};
588 
590  using format_type = typename detail::variant_from_tags<valid_formats,
591  detail::structure_file_output_format_exposer>::type;
593  format_type format;
595 
597  template <typename seq_type,
598  typename id_type,
599  typename bpp_type,
600  typename structure_type,
601  typename structured_seq_type,
602  typename energy_type,
603  typename react_type,
604  typename comment_type,
605  typename offset_type>
606  void write_record(seq_type && seq,
607  id_type && id,
608  bpp_type && bpp,
609  structure_type && structure,
610  structured_seq_type && structured_seq,
611  energy_type && energy,
612  react_type && react,
613  react_type && react_error,
614  comment_type && comment,
615  offset_type && offset)
616  {
617  static_assert(detail::decays_to_ignore_v<structured_seq_type> ||
618  (detail::decays_to_ignore_v<seq_type> && detail::decays_to_ignore_v<structure_type>),
619  "You may not select field::structured_seq and either of field::seq and field::structure "
620  "at the same time.");
621 
622  assert(!format.valueless_by_exception());
623  std::visit([&] (auto & f)
624  {
625  if constexpr (!detail::decays_to_ignore_v<structured_seq_type>)
626  {
627  f.write_structure_record(*secondary_stream,
628  options,
629  structured_seq | views::get<0>,
630  id,
631  bpp,
632  structured_seq | views::get<1>,
633  energy,
634  react,
635  react_error,
636  comment,
637  offset);
638  }
639  else
640  {
641  f.write_structure_record(*secondary_stream,
642  options,
643  seq,
644  id,
645  bpp,
646  structure,
647  energy,
648  react,
649  react_error,
650  comment,
651  offset);
652  }
653  }, format);
654  }
655 
657  friend iterator;
658 };
659 
665 template <output_stream stream_t,
667  structure_file_output_format file_format,
668  detail::fields_specialisation selected_field_ids>
669 structure_file_output(stream_t &&, file_format const &, selected_field_ids const &)
672 
674 template <output_stream stream_t,
675  structure_file_output_format file_format,
676  detail::fields_specialisation selected_field_ids>
677 structure_file_output(stream_t &, file_format const &, selected_field_ids const &)
681 
682 } // namespace seqan3
seqan3::structure_file_output::emplace_back
void emplace_back(arg_t &&arg, arg_types &&... args)
Write a record to the file by passing individual fields.
Definition: output.hpp:471
zip.hpp
Provides seqan3::views::zip.
format_vienna.hpp
Provides the seqan3::format_vienna.
seqan3::structure_file_output::sentinel
std::default_sentinel_t sentinel
The type returned by end().
Definition: output.hpp:225
seqan3::structure_file_output::structure_file_output
structure_file_output(stream_t &, file_format const &, selected_field_ids const &) -> structure_file_output< selected_field_ids, type_list< file_format >>
This is an overloaded member function, provided for convenience. It differs from the above function o...
fstream
seqan3::structure_file_output::structure_file_output
structure_file_output(stream_t &stream, file_format const &format_tag, selected_field_ids const &fields_tag=selected_field_ids{})
Construct from an existing stream and with specified format.
Definition: output.hpp:297
misc_output.hpp
Provides various utility functions required only for output.
seqan3::structure_file_output::valid_formats
valid_formats_ valid_formats
A seqan3::type_list with the possible formats.
Definition: output.hpp:170
seqan3::type_list
meta::list< types... > type_list
Type that contains multiple types, an alias for meta::list.
Definition: type_list.hpp:31
tuple.hpp
Provides seqan3::tuple_like.
seqan3::field::offset
@ offset
Sequence (SEQ) relative start position (0-based), unsigned value.
concept.hpp
Stream concepts.
seqan3::structure_file_output::const_iterator
void const_iterator
The const iterator type is void, because files are not const-iterable.
Definition: output.hpp:223
seqan3::structure_file_output::stream_char_type
char stream_char_type
Character type of the stream(s).
Definition: output.hpp:172
seqan3::structure_file_output::reference
void reference
The reference type (void).
Definition: output.hpp:213
vector
std::vector::size
T size(T... args)
seqan3::structure_file_output::operator=
structure_file_output & operator=(structure_file_output &&)=default
Move assignment is defaulted.
seqan3::structure_file_output::selected_field_ids
selected_field_ids_ selected_field_ids
A seqan3::fields list with the fields selected for the record.
Definition: output.hpp:168
convert.hpp
Provides seqan3::views::convert.
seqan3::field::id
@ id
The identifier, usually a string.
std::unique_ptr::get
T get(T... args)
seqan3::structure_file_output
A class for writing structured sequence files, e.g. Stockholm, Connect, Vienna, ViennaRNA bpp matrix ...
Definition: output.hpp:161
record.hpp
Provides the seqan3::record template and the seqan3::field enum.
std::function
filesystem
This header includes C++17 filesystem support and imports it into namespace std::filesystem (independ...
seqan3::structure_file_output::operator=
structure_file_output & operator=(structure_file_output const &)=delete
Copy assignment is explicitly deleted, because you can't have multiple access to the same file.
std::filesystem::path
seqan3::structure_file_output::operator|
friend structure_file_output operator|(rng_t &&range, structure_file_output &&f)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: output.hpp:547
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
std::tie
T tie(T... args)
seqan3::seq
constexpr sequenced_policy seq
Global execution policy object for sequenced execution policy.
Definition: execution.hpp:54
seqan3::structure_file_output::begin
iterator begin() noexcept
Returns an iterator to current position in the file.
Definition: output.hpp:346
seqan3::structure_file_output::push_back
void push_back(record_t &&r)
Write a seqan3::record to the file.
Definition: output.hpp:389
seqan3::structure_file_output::structure_file_output
structure_file_output(stream_t &&stream, file_format const &format_tag, selected_field_ids const &fields_tag=selected_field_ids{})
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: output.hpp:313
output_options.hpp
Provides seqan3::structure_file_output_options.
seqan3::views::move
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:68
seqan3::structure_file_output::value_type
void value_type
The value type (void).
Definition: output.hpp:211
seqan3::structure_file_output::structure_file_output
structure_file_output(structure_file_output &&)=default
Move construction is defaulted.
std::basic_ostream
std::forward
T forward(T... args)
std::ofstream
seqan3::structure_file_output::structure_file_output
structure_file_output()=delete
Default constructor is explicitly deleted, you need to give a stream or file name.
exception.hpp
Provides exceptions used in the I/O module.
seqan3::structure_file_output::~structure_file_output
~structure_file_output()=default
Destructor is defaulted.
seqan3
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
seqan3::structure_file_output::structure_file_output
structure_file_output(structure_file_output const &)=delete
Copy construction is explicitly deleted, because you can't have multiple access to the same file.
seqan3::structure_file_output::structure_file_output
structure_file_output(stream_t &&, file_format const &, selected_field_ids const &) -> structure_file_output< selected_field_ids, type_list< file_format >>
Deduction of the selected fields, the file format and the stream type.
seqan3::structure_file_output::push_back
void push_back(tuple_t &&t)
Write a record in form of a std::tuple to the file.
Definition: output.hpp:429
structure_file_output_format
The generic concept for structure file out formats.
std::format
T format(T... args)
output_format_concept.hpp
Provides seqan3::structure_file_output_format and auxiliary classes.
tuple_like
Whether a type behaves like a tuple.
seqan3::structure_file_output::end
sentinel end() noexcept
Returns a sentinel for comparison with iterator.
Definition: output.hpp:365
seqan3::structure_file_output::const_reference
void const_reference
The const reference type (void).
Definition: output.hpp:215
ranges
Adaptations of concepts from the Ranges TS.
cassert
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
std::ptrdiff_t
std::visit
T visit(T... args)
seqan3::structure_file_output_options
The options type defines various option members that influence the behaviour of all or some formats.
Definition: output_options.hpp:24
seqan3::structure_file_output::operator|
friend structure_file_output & operator|(rng_t &&range, structure_file_output &f)
Write a range of records (or tuples) to the file.
Definition: output.hpp:536
optional
out_file_iterator.hpp
Provides the seqan3::detail::out_file_iterator class template.
traits.hpp
Provides traits for seqan3::type_list.
seqan3::structure_file_output::size_type
void size_type
The size type (void).
Definition: output.hpp:217
std::unique_ptr
seqan3::structure_file_output::operator=
structure_file_output & operator=(rng_t &&range)
Write a range of records (or tuples) to the file.
Definition: output.hpp:498
std::vector::data
T data(T... args)
seqan3::structure_file_output::structure_file_output
structure_file_output(std::filesystem::path filename, selected_field_ids const &fields_tag=selected_field_ids{})
Construct from filename.
Definition: output.hpp:259
seqan3::structure_file_output::iterator
detail::out_file_iterator< structure_file_output > iterator
The iterator type of this view (an output iterator).
Definition: output.hpp:221
get.hpp
Provides seqan3::views::get.
seqan3::structure_file_output::options
structure_file_output_options options
The options are public and its members can be set directly.
Definition: output.hpp:558
variant
string