Loading...
Searching...
No Matches
io/structure_file/output.hpp
Go to the documentation of this file.
// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
#include <cassert>
#include <filesystem>
#include <fstream>
#include <optional>
#include <ranges>
#include <string>
#include <type_traits>
#include <variant>
#include <vector>
#include <seqan3/io/detail/misc_output.hpp>
#include <seqan3/io/detail/out_file_iterator.hpp>
#include <seqan3/io/detail/record.hpp>
#include <seqan3/io/detail/record_like.hpp>
#include <seqan3/io/exception.hpp>
#include <seqan3/io/record.hpp>
#include <seqan3/io/stream/concept.hpp>
#include <seqan3/io/structure_file/format_vienna.hpp>
#include <seqan3/io/structure_file/output_format_concept.hpp>
#include <seqan3/io/structure_file/output_options.hpp>
#include <seqan3/utility/tuple/concept.hpp>
#include <seqan3/utility/type_list/traits.hpp>
#include <seqan3/utility/views/convert.hpp>
#include <seqan3/utility/views/elements.hpp>
#include <seqan3/utility/views/zip.hpp>
namespace seqan3
{
// ----------------------------------------------------------------------------
// structure_file_output
// ----------------------------------------------------------------------------
template <detail::fields_specialisation selected_field_ids_ = fields<field::seq, field::id, field::structure>,
detail::type_list_of_structure_file_output_formats valid_formats_ = type_list<format_vienna>>
class structure_file_output
{
public:
using selected_field_ids = selected_field_ids_;
using valid_formats = valid_formats_;
using stream_char_type = char;
using field_ids = fields<field::seq,
field::id,
field::bpp,
field::structure,
field::structured_seq,
field::energy,
field::react,
field::react_err,
field::comment,
field::offset>;
static_assert(
[]() constexpr
{
for (field f : selected_field_ids::as_array)
if (!field_ids::contains(f))
return false;
return true;
}(),
"You selected a field that is not valid for structure files, please refer to the documentation "
"of structure_file_output::field_ids for the accepted values.");
static_assert(
[]() constexpr
{
return !(selected_field_ids::contains(field::structured_seq)
&& (selected_field_ids::contains(field::seq) || (selected_field_ids::contains(field::structure))));
}(),
"You may not select field::structured_seq and either of field::seq and field::structure "
"at the same time.");
using value_type = void;
using reference = void;
using const_reference = void;
using size_type = void;
using difference_type = std::ptrdiff_t;
using iterator = detail::out_file_iterator<structure_file_output>;
using const_iterator = void;
using sentinel = std::default_sentinel_t;
structure_file_output() = delete;
structure_file_output(structure_file_output const &) = delete;
structure_file_output & operator=(structure_file_output const &) = delete;
structure_file_output(structure_file_output &&) = default;
structure_file_output & operator=(structure_file_output &&) = default;
~structure_file_output() = default;
structure_file_output(std::filesystem::path filename,
selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
primary_stream{new std::ofstream{}, stream_deleter_default}
{
primary_stream->rdbuf()->pubsetbuf(stream_buffer.data(), stream_buffer.size());
static_cast<std::basic_ofstream<char> *>(primary_stream.get())
->open(filename, std::ios_base::out | std::ios::binary);
if (!primary_stream->good())
throw file_open_error{"Could not open file " + filename.string() + " for writing."};
// possibly add intermediate compression stream
secondary_stream = detail::make_secondary_ostream(*primary_stream, filename);
// initialise format handler or throw if format is not found
detail::set_format(format, filename);
}
template <output_stream stream_t, structure_file_output_format file_format>
requires std::same_as<typename std::remove_reference_t<stream_t>::char_type, char>
structure_file_output(stream_t & stream,
file_format const & SEQAN3_DOXYGEN_ONLY(format_tag),
selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
primary_stream{&stream, stream_deleter_noop},
secondary_stream{&stream, stream_deleter_noop},
format{detail::structure_file_output_format_exposer<file_format>{}}
{
static_assert(list_traits::contains<file_format, valid_formats>,
"You selected a format that is not in the valid_formats of this file.");
}
template <output_stream stream_t, structure_file_output_format file_format>
requires std::same_as<typename std::remove_reference_t<stream_t>::char_type, char>
structure_file_output(stream_t && stream,
file_format const & SEQAN3_DOXYGEN_ONLY(format_tag),
selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
primary_stream{new stream_t{std::move(stream)}, stream_deleter_default},
secondary_stream{&*primary_stream, stream_deleter_noop},
format{detail::structure_file_output_format_exposer<file_format>{}}
{
static_assert(list_traits::contains<file_format, valid_formats>,
"You selected a format that is not in the valid_formats of this file.");
}
iterator begin() noexcept
{
return {*this};
}
sentinel end() noexcept
{
return {};
}
template <typename record_t>
void push_back(record_t && r)
requires detail::record_like<record_t>
{
write_record(detail::get_or_ignore<field::seq>(r),
detail::get_or_ignore<field::id>(r),
detail::get_or_ignore<field::bpp>(r),
detail::get_or_ignore<field::structure>(r),
detail::get_or_ignore<field::structured_seq>(r),
detail::get_or_ignore<field::energy>(r),
detail::get_or_ignore<field::react>(r),
detail::get_or_ignore<field::react_err>(r),
detail::get_or_ignore<field::comment>(r),
detail::get_or_ignore<field::offset>(r));
}
template <typename tuple_t>
void push_back(tuple_t && t)
requires tuple_like<tuple_t> && (!detail::record_like<tuple_t>)
{
// index_of might return npos, but this will be handled well by get_or_ignore (and just return ignore)
write_record(detail::get_or_ignore<selected_field_ids::index_of(field::seq)>(t),
detail::get_or_ignore<selected_field_ids::index_of(field::id)>(t),
detail::get_or_ignore<selected_field_ids::index_of(field::bpp)>(t),
detail::get_or_ignore<selected_field_ids::index_of(field::structure)>(t),
detail::get_or_ignore<selected_field_ids::index_of(field::structured_seq)>(t),
detail::get_or_ignore<selected_field_ids::index_of(field::energy)>(t),
detail::get_or_ignore<selected_field_ids::index_of(field::react)>(t),
detail::get_or_ignore<selected_field_ids::index_of(field::react_err)>(t),
detail::get_or_ignore<selected_field_ids::index_of(field::comment)>(t),
detail::get_or_ignore<selected_field_ids::index_of(field::offset)>(t));
}
template <typename arg_t, typename... arg_types>
void emplace_back(arg_t && arg, arg_types &&... args)
{
push_back(std::tie(arg, args...));
}
template <std::ranges::input_range rng_t>
structure_file_output & operator=(rng_t && range)
requires tuple_like<std::ranges::range_reference_t<rng_t>>
{
for (auto && record : range)
push_back(std::forward<decltype(record)>(record));
return *this;
}
template <std::ranges::input_range rng_t>
friend structure_file_output & operator|(rng_t && range, structure_file_output & f)
requires tuple_like<std::ranges::range_reference_t<rng_t>>
{
f = range;
return f;
}
template <std::ranges::input_range rng_t>
friend structure_file_output operator|(rng_t && range, structure_file_output && f)
requires tuple_like<std::ranges::range_reference_t<rng_t>>
{
f = range;
return std::move(f);
}
structure_file_output_options options;
std::basic_ostream<stream_char_type> & get_stream()
{
return *secondary_stream;
}
protected:
std::vector<char> stream_buffer{std::vector<char>(1'000'000)};
using stream_ptr_t = std::unique_ptr<std::basic_ostream<stream_char_type>,
std::function<void(std::basic_ostream<stream_char_type> *)>>;
static void stream_deleter_noop(std::basic_ostream<stream_char_type> *)
{}
static void stream_deleter_default(std::basic_ostream<stream_char_type> * ptr)
{
delete ptr;
}
stream_ptr_t primary_stream{nullptr, stream_deleter_noop};
stream_ptr_t secondary_stream{nullptr, stream_deleter_noop};
using format_type =
typename detail::variant_from_tags<valid_formats, detail::structure_file_output_format_exposer>::type;
format_type format;
template <typename seq_type,
typename id_type,
typename bpp_type,
typename structure_type,
typename structured_seq_type,
typename energy_type,
typename react_type,
typename comment_type,
typename offset_type>
void write_record(seq_type && seq,
id_type && id,
bpp_type && bpp,
structure_type && structure,
structured_seq_type && structured_seq,
energy_type && energy,
react_type && react,
react_type && react_error,
comment_type && comment,
offset_type && offset)
{
static_assert(detail::decays_to_ignore_v<structured_seq_type>
|| (detail::decays_to_ignore_v<seq_type> && detail::decays_to_ignore_v<structure_type>),
"You may not select field::structured_seq and either of field::seq and field::structure "
"at the same time.");
assert(!format.valueless_by_exception());
std::visit(
[&](auto & f)
{
if constexpr (!detail::decays_to_ignore_v<structured_seq_type>)
{
f.write_structure_record(*secondary_stream,
options,
structured_seq | views::elements<0>,
id,
bpp,
structured_seq | views::elements<1>,
energy,
react,
react_error,
comment,
offset);
}
else
{
f.write_structure_record(*secondary_stream,
options,
seq,
id,
bpp,
structure,
energy,
react,
react_error,
comment,
offset);
}
},
format);
}
friend iterator;
};
template <output_stream stream_t,
structure_file_output_format file_format,
detail::fields_specialisation selected_field_ids>
structure_file_output(stream_t &&,
file_format const &,
selected_field_ids const &) -> structure_file_output<selected_field_ids, type_list<file_format>>;
template <output_stream stream_t,
structure_file_output_format file_format,
detail::fields_specialisation selected_field_ids>
structure_file_output(stream_t &,
file_format const &,
selected_field_ids const &) -> structure_file_output<selected_field_ids, type_list<file_format>>;
} // namespace seqan3
std::basic_ofstream
std::basic_ostream
cassert
seqan3::structure_file_output_format
The generic concept for structure file out formats.
Definition structure_file/output_format_concept.hpp:137
seqan3::structure_file_output::sentinel
std::default_sentinel_t sentinel
The type returned by end().
Definition io/structure_file/output.hpp:126
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 io/structure_file/output.hpp:323
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 io/structure_file/output.hpp:426
seqan3::structure_file_output::const_reference
void const_reference
The const reference type (void).
Definition io/structure_file/output.hpp:116
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 io/structure_file/output.hpp:435
seqan3::structure_file_output::iterator
detail::out_file_iterator< structure_file_output > iterator
The iterator type of this view (an output iterator).
Definition io/structure_file/output.hpp:122
seqan3::structure_file_output::stream_char_type
char stream_char_type
Character type of the stream(s).
Definition io/structure_file/output.hpp:71
seqan3::structure_file_output::operator=
structure_file_output & operator=(structure_file_output &&)=default
Move assignment is defaulted.
seqan3::structure_file_output::options
structure_file_output_options options
The options are public and its members can be set directly.
Definition io/structure_file/output.hpp:444
seqan3::structure_file_output::difference_type
std::ptrdiff_t difference_type
A signed integer type, usually std::ptrdiff_t.
Definition io/structure_file/output.hpp:120
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 io/structure_file/output.hpp:67
seqan3::structure_file_output::push_back
void push_back(record_t &&r)
Write a seqan3::record to the file.
Definition io/structure_file/output.hpp:286
seqan3::structure_file_output::field_ids
fields< field::seq, field::id, field::bpp, field::structure, field::structured_seq, field::energy, field::react, field::react_err, field::comment, field::offset > field_ids
The subset of seqan3::field IDs that are valid for this file.
Definition io/structure_file/output.hpp:75
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::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 io/structure_file/output.hpp:160
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 io/structure_file/output.hpp:363
seqan3::structure_file_output::operator=
structure_file_output & operator=(rng_t &&range)
Write a range of records (or tuples) to the file.
Definition io/structure_file/output.hpp:390
seqan3::structure_file_output::size_type
void size_type
The size type (void).
Definition io/structure_file/output.hpp:118
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::reference
void reference
The reference type (void).
Definition io/structure_file/output.hpp:114
seqan3::structure_file_output::begin
iterator begin() noexcept
Returns an iterator to current position in the file.
Definition io/structure_file/output.hpp:243
seqan3::structure_file_output::end
sentinel end() noexcept
Returns a sentinel for comparison with iterator.
Definition io/structure_file/output.hpp:262
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...
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.
seqan3::structure_file_output::structure_file_output
structure_file_output(structure_file_output &&)=default
Move construction is defaulted.
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 io/structure_file/output.hpp:210
seqan3::structure_file_output::value_type
void value_type
The value type (void).
Definition io/structure_file/output.hpp:112
seqan3::structure_file_output::valid_formats
valid_formats_ valid_formats
A seqan3::type_list with the possible formats.
Definition io/structure_file/output.hpp:69
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 io/structure_file/output.hpp:196
seqan3::structure_file_output::const_iterator
void const_iterator
The const iterator type is void, because files are not const-iterable.
Definition io/structure_file/output.hpp:124
seqan3::structure_file_output::~structure_file_output
~structure_file_output()=default
Destructor is defaulted.
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.
std::vector::data
T data(T... args)
elements.hpp
Provides seqan3::views::elements.
filesystem
format_vienna.hpp
Provides the seqan3::format_vienna.
std::forward
T forward(T... args)
fstream
std::function
std::unique_ptr::get
T get(T... args)
seqan3::field
field
An enumerator for the fields used in file formats.
Definition record.hpp:60
seqan3::field::energy
@ energy
Energy of a folded sequence, represented by one float number.
Definition record.hpp:72
seqan3::field::comment
@ comment
Comment field of arbitrary content, usually a string.
Definition record.hpp:75
seqan3::field::structure
@ structure
Fixed interactions, usually a string of structure alphabet characters.
Definition record.hpp:70
seqan3::field::bpp
@ bpp
Base pair probability matrix of interactions, usually a matrix of float numbers.
Definition record.hpp:69
seqan3::field::react
@ react
Reactivity values of the sequence characters given in a vector of float numbers.
Definition record.hpp:73
seqan3::field::react_err
@ react_err
Reactivity error values given in a vector corresponding to seqan3::field::react.
Definition record.hpp:74
seqan3::field::offset
@ offset
Sequence (seqan3::field::seq) relative start position (0-based), unsigned value.
Definition record.hpp:66
seqan3::field::structured_seq
@ structured_seq
Sequence and fixed interactions combined in one range.
Definition record.hpp:71
seqan3::field::id
@ id
The identifier, usually a string.
Definition record.hpp:63
seqan3::field::seq
@ seq
The "sequence", usually a range of nucleotides or amino acids.
Definition record.hpp:62
seqan3::list_traits::contains
constexpr bool contains
Whether a type occurs in a type list or not.
Definition type_list/traits.hpp:249
tuple_like
Whether a type behaves like a tuple.
exception.hpp
Provides exceptions used in the I/O module.
concept.hpp
Stream concepts.
misc_output.hpp
Provides various utility functions required only for output.
seqan3
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
std
SeqAn specific customisations in the standard namespace.
std::basic_ofstream::open
T open(T... args)
optional
out_file_iterator.hpp
Provides the seqan3::detail::out_file_iterator class template.
std::filesystem::path
std::ptrdiff_t
record.hpp
Provides the seqan3::record template and the seqan3::field enum.
record_like.hpp
Provides seqan3::detail::record_like.
std::vector::size
T size(T... args)
string
seqan3::fields
A class template that holds a choice of seqan3::field.
Definition record.hpp:125
seqan3::file_open_error
Thrown if there is an unspecified filesystem or stream error while opening, e.g. permission problem.
Definition io/exception.hpp:36
seqan3::record
The class template that file records are based on; behaves like a std::tuple.
Definition record.hpp:190
seqan3::structure_file_output_options
The options type defines various option members that influence the behaviour of all or some formats.
Definition structure_file/output_options.hpp:23
output_format_concept.hpp
Provides seqan3::structure_file_output_format and auxiliary classes.
output_options.hpp
Provides seqan3::structure_file_output_options.
std::tie
T tie(T... args)
traits.hpp
Provides traits for seqan3::type_list.
type_traits
std::unique_ptr
concept.hpp
Provides seqan3::tuple_like.
convert.hpp
Provides seqan3::views::convert.
variant
vector
std::visit
T visit(T... args)
zip.hpp
Provides seqan3::views::zip.
Hide me