SeqAn3  3.0.1
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>
22 
23 // remove the following after range-v3 is updated to 1.0
24 #pragma GCC diagnostic push
25 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
26 
31 #include <seqan3/io/exception.hpp>
32 #include <seqan3/std/filesystem>
33 #include <seqan3/io/record.hpp>
36 #include <seqan3/io/detail/record.hpp>
43 #include <seqan3/std/ranges>
44 
45 namespace seqan3
46 {
47 
48 // ----------------------------------------------------------------------------
49 // structure_file_output
50 // ----------------------------------------------------------------------------
51 
164 template <detail::fields_specialisation selected_field_ids_ = fields<field::seq, field::id, field::structure>,
165  detail::type_list_of_structure_file_output_formats valid_formats_ = type_list<format_vienna>>
167 {
168 public:
173  using selected_field_ids = selected_field_ids_;
176  using valid_formats = valid_formats_;
178  using stream_char_type = char;
180 
182  using field_ids = fields<field::seq,
183  field::id,
184  field::bpp,
188  field::react,
192 
193  static_assert([] () constexpr
194  {
195  for (field f : selected_field_ids::as_array)
196  if (!field_ids::contains(f))
197  return false;
198  return true;
199  }(),
200  "You selected a field that is not valid for structure files, please refer to the documentation "
201  "of structure_file_output::field_ids for the accepted values.");
202 
203  static_assert([] () constexpr
204  {
208  }(), "You may not select field::structured_seq and either of field::seq and field::structure "
209  "at the same time.");
210 
216  using value_type = void;
219  using reference = void;
221  using const_reference = void;
223  using size_type = void;
227  using iterator = detail::out_file_iterator<structure_file_output>;
229  using const_iterator = void;
231  using sentinel = std::ranges::default_sentinel_t;
233 
237  structure_file_output() = delete;
248  ~structure_file_output() = default;
249 
266  selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
267  primary_stream{new std::ofstream{filename, std::ios_base::out | std::ios::binary}, stream_deleter_default}
268  {
269  if (!primary_stream->good())
270  throw file_open_error{"Could not open file " + filename.string() + " for writing."};
271 
272  // possibly add intermediate compression stream
273  secondary_stream = detail::make_secondary_ostream(*primary_stream, filename);
274 
275  // initialise format handler or throw if format is not found
276  detail::set_format(format, filename);
277  }
278 
295  template <output_stream stream_t, structure_file_output_format file_format>
299  structure_file_output(stream_t & stream,
300  file_format const & SEQAN3_DOXYGEN_ONLY(format_tag),
301  selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
302  primary_stream{&stream, stream_deleter_noop},
303  secondary_stream{&stream, stream_deleter_noop},
304  format{detail::structure_file_output_format_exposer<file_format>{}}
305  {
306  static_assert(list_traits::contains<file_format, valid_formats>,
307  "You selected a format that is not in the valid_formats of this file.");
308  }
309 
311  template <output_stream stream_t, structure_file_output_format file_format>
315  structure_file_output(stream_t && stream,
316  file_format const & SEQAN3_DOXYGEN_ONLY(format_tag),
317  selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
318  primary_stream{new stream_t{std::move(stream)}, stream_deleter_default},
319  secondary_stream{&*primary_stream, stream_deleter_noop},
320  format{detail::structure_file_output_format_exposer<file_format>{}}
321  {
322  static_assert(list_traits::contains<file_format, valid_formats>,
323  "You selected a format that is not in the valid_formats of this file.");
324  }
326 
348  iterator begin() noexcept
349  {
350  return {*this};
351  }
352 
367  sentinel end() noexcept
368  {
369  return {};
370  }
371 
390  template <typename record_t>
391  void push_back(record_t && r)
392  requires tuple_like<record_t> &&
393  requires { requires detail::is_type_specialisation_of_v<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)
430  requires tuple_like<tuple_t>
431  {
432  // index_of might return npos, but this will be handled well by get_or_ignore (and just return ignore)
433  write_record(detail::get_or_ignore<selected_field_ids::index_of(field::seq)>(t),
434  detail::get_or_ignore<selected_field_ids::index_of(field::id)>(t),
435  detail::get_or_ignore<selected_field_ids::index_of(field::bpp)>(t),
436  detail::get_or_ignore<selected_field_ids::index_of(field::structure)>(t),
437  detail::get_or_ignore<selected_field_ids::index_of(field::structured_seq)>(t),
438  detail::get_or_ignore<selected_field_ids::index_of(field::energy)>(t),
439  detail::get_or_ignore<selected_field_ids::index_of(field::react)>(t),
440  detail::get_or_ignore<selected_field_ids::index_of(field::react_err)>(t),
441  detail::get_or_ignore<selected_field_ids::index_of(field::comment)>(t),
442  detail::get_or_ignore<selected_field_ids::index_of(field::offset)>(t));
443  }
444 
468  template <typename arg_t, typename ...arg_types>
469  void emplace_back(arg_t && arg, arg_types && ... args)
470  {
471  push_back(std::tie(arg, args...));
472  }
473 
495  template <std::ranges::input_range rng_t>
498  {
499  for (auto && record : range)
500  push_back(std::forward<decltype(record)>(record));
501  return *this;
502  }
503 
531  template <std::ranges::input_range rng_t>
534  {
535  f = range;
536  return f;
537  }
538 
540  template <std::ranges::input_range rng_t>
543  {
544  f = range;
545  return std::move(f);
546  }
548 
551 
556  {
557  return *secondary_stream;
558  }
560 protected:
562 
570  static void stream_deleter_noop(std::basic_ostream<stream_char_type> *) {}
572  static void stream_deleter_default(std::basic_ostream<stream_char_type> * ptr) { delete ptr; }
573 
575  stream_ptr_t primary_stream{nullptr, stream_deleter_noop};
577  stream_ptr_t secondary_stream{nullptr, stream_deleter_noop};
578 
580  using format_type = typename detail::variant_from_tags<valid_formats,
581  detail::structure_file_output_format_exposer>::type;
583  format_type format;
585 
587  template <typename seq_type,
588  typename id_type,
589  typename bpp_type,
590  typename structure_type,
591  typename structured_seq_type,
592  typename energy_type,
593  typename react_type,
594  typename comment_type,
595  typename offset_type>
596  void write_record(seq_type && seq,
597  id_type && id,
598  bpp_type && bpp,
599  structure_type && structure,
600  structured_seq_type && structured_seq,
601  energy_type && energy,
602  react_type && react,
603  react_type && react_error,
604  comment_type && comment,
605  offset_type && offset)
606  {
607  static_assert(detail::decays_to_ignore_v<structured_seq_type> ||
608  (detail::decays_to_ignore_v<seq_type> && detail::decays_to_ignore_v<structure_type>),
609  "You may not select field::structured_seq and either of field::seq and field::structure "
610  "at the same time.");
611 
612  assert(!format.valueless_by_exception());
613  std::visit([&] (auto & f)
614  {
615  if constexpr (!detail::decays_to_ignore_v<structured_seq_type>)
616  {
617  f.write_structure_record(*secondary_stream,
618  options,
619  structured_seq | views::get<0>,
620  id,
621  bpp,
622  structured_seq | views::get<1>,
623  energy,
624  react,
625  react_error,
626  comment,
627  offset);
628  }
629  else
630  {
631  f.write_structure_record(*secondary_stream,
632  options,
633  seq,
634  id,
635  bpp,
636  structure,
637  energy,
638  react,
639  react_error,
640  comment,
641  offset);
642  }
643  }, format);
644  }
645 
647  friend iterator;
648 };
649 
655 template <output_stream stream_t,
657  structure_file_output_format file_format,
658  detail::fields_specialisation selected_field_ids>
659 structure_file_output(stream_t &&, file_format const &, selected_field_ids const &)
660  -> structure_file_output<selected_field_ids,
661  type_list<file_format>>;
662 
664 template <output_stream stream_t,
665  structure_file_output_format file_format,
666  detail::fields_specialisation selected_field_ids>
667 structure_file_output(stream_t &, file_format const &, selected_field_ids const &)
668  -> structure_file_output<selected_field_ids,
669  type_list<file_format>>;
671 
672 } // namespace seqan3
673 #pragma GCC diagnostic pop
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:469
zip.hpp
Provides seqan3::views::zip.
seqan3::field::seq
The "sequence", usually a range of nucleotides or amino acids.
seqan3::structure_file_output::push_back
void push_back(record_t &&r) requires tuple_like< record_t > &&requires
Write a seqan3::record to the file.
Definition: output.hpp:391
format_vienna.hpp
Provides the seqan3::format_vienna.
seqan3::structure_file_output::operator|
friend structure_file_output operator|(rng_t &&range, structure_file_output &&f) requires tuple_like< reference_t< rng_t >>
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: output.hpp:541
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:299
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:176
tuple.hpp
Provides seqan3::tuple_like.
seqan3::field::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:229
seqan3::structure_file_output::stream_char_type
char stream_char_type
Character type of the stream(s).
Definition: output.hpp:178
seqan3::field::bpp
Base pair probability matrix of interactions, usually a matrix of float numbers.
seqan3::structure_file_output::reference
void reference
The reference type (void).
Definition: output.hpp:219
vector
seqan3::structure_file_output::push_back
void push_back(tuple_t &&t) requires tuple_like< tuple_t >
Write a record in form of a std::tuple to the file.
Definition: output.hpp:429
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:174
convert.hpp
Provides seqan3::views::convert.
seqan3::field::id
The identifier, usually a string.
seqan3::structure_file_output
A class for writing structured sequence files, e.g. Stockholm, Connect, Vienna, ViennaRNA bpp matrix ...
Definition: output.hpp:166
seqan3::views::move
const auto move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:68
record.hpp
Provides the seqan3::record template and the seqan3::field enum.
std::function
seqan3::field::structure
Fixed interactions, usually a string of structure alphabet characters.
filesystem
This header includes C++17 filesystem support and imports it into namespace seqan3::filesystem (indep...
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::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:165
seqan3::field::energy
Energy of a folded sequence, represented by one float number.
std::tie
T tie(T... args)
same_as
The concept std::same_as<T, U> is satisfied if and only if T and U denote the same type.
seqan3::structure_file_output::begin
iterator begin() noexcept
Returns an iterator to current position in the file.
Definition: output.hpp:348
seqan3::structure_file_output::sentinel
std::ranges::default_sentinel_t sentinel
The type returned by end().
Definition: output.hpp:231
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:315
all.hpp
Meta-header for the structure module. It includes all headers from alphabet/structure/.
output_options.hpp
Provides seqan3::structure_file_output_options.
seqan3::structure_file_output::value_type
void value_type
The value type (void).
Definition: output.hpp:217
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.
seqan3::field::comment
Comment field of arbitrary content, usually a string.
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:36
structure_file_output_format
The generic concept for structure file out formats.
seqan3::field::structured_seq
Sequence and fixed interactions combined in one range.
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:367
seqan3::structure_file_output::const_reference
void const_reference
The const reference type (void).
Definition: output.hpp:221
ranges
Adaptations of concepts from the Ranges TS.
seqan3::structure_file_output::operator|
friend structure_file_output & operator|(rng_t &&range, structure_file_output &f) requires tuple_like< reference_t< rng_t >>
Write a range of records (or tuples) to the file.
Definition: output.hpp:532
seqan3::field::react
Reactivity values of the sequence characters given in a vector of float numbers.
cassert
seqan3::field
field
An enumerator for the fields used in file formats.
Definition: record.hpp:64
seqan3::record
The class template that file records are based on; behaves like an std::tuple.
Definition: record.hpp:225
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:23
seqan3::field::react_err
Reactivity error values given in a vector corresponding to REACT.
optional
seqan3::structure_file_output::operator=
structure_file_output & operator=(rng_t &&range) requires tuple_like< reference_t< rng_t >>
Write a range of records (or tuples) to the file.
Definition: output.hpp:496
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:223
std::unique_ptr
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:265
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:227
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:550
variant
string