SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
io/sequence_file/output.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <cassert>
13#include <filesystem>
14#include <fstream>
15#include <ranges>
16#include <string>
17#include <variant>
18#include <vector>
19
26#include <seqan3/io/record.hpp>
40
41namespace seqan3
42{
43
44// ----------------------------------------------------------------------------
45// sequence_file_output
46// ----------------------------------------------------------------------------
47
62template <detail::fields_specialisation selected_field_ids_ = fields<field::seq, field::id, field::qual>,
63 detail::type_list_of_sequence_file_output_formats valid_formats_ =
64 type_list<format_embl, format_fasta, format_fastq, format_genbank, format_sam>>
66{
67public:
73 using selected_field_ids = selected_field_ids_;
75 using valid_formats = valid_formats_;
77 using stream_char_type = char;
79
82
83 static_assert(
84 []() constexpr
85 {
86 for (field f : selected_field_ids::as_array)
87 if (!field_ids::contains(f))
88 return false;
89 return true;
90 }(),
91 "You selected a field that is not valid for sequence files, please refer to the documentation "
92 "of sequence_file_output::field_ids for the accepted values.");
93
100 using value_type = void;
102 using reference = void;
104 using const_reference = void;
106 using size_type = void;
112 using const_iterator = void;
114 using sentinel = std::default_sentinel_t;
116
132
149 selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
151 {
152 primary_stream->rdbuf()->pubsetbuf(stream_buffer.data(), stream_buffer.size());
154 ->open(filename, std::ios_base::out | std::ios::binary);
155
156 if (!primary_stream->good())
157 throw file_open_error{"Could not open file " + filename.string() + " for writing."};
158
159 // possibly add intermediate compression stream
161
162 // initialise format handler or throw if format is not found
163 detail::set_format(format, filename);
164 }
165
181 template <output_stream stream_t, sequence_file_output_format file_format>
182 requires std::same_as<typename std::remove_reference_t<stream_t>::char_type, stream_char_type>
183 sequence_file_output(stream_t & stream,
184 file_format const & SEQAN3_DOXYGEN_ONLY(format_tag),
185 selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
188 format{detail::sequence_file_output_format_exposer<file_format>{}}
189 {
190 static_assert(list_traits::contains<file_format, valid_formats>,
191 "You selected a format that is not in the valid_formats of this file.");
192 }
193
195 template <output_stream stream_t, sequence_file_output_format file_format>
196 requires std::same_as<typename std::remove_reference_t<stream_t>::char_type, stream_char_type>
197 sequence_file_output(stream_t && stream,
198 file_format const & SEQAN3_DOXYGEN_ONLY(format_tag),
199 selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
200 primary_stream{new stream_t{std::move(stream)}, stream_deleter_default},
201 secondary_stream{&*primary_stream, stream_deleter_noop},
202 format{detail::sequence_file_output_format_exposer<file_format>{}}
203 {
204 static_assert(list_traits::contains<file_format, valid_formats>,
205 "You selected a format that is not in the valid_formats of this file.");
206 }
208
230 iterator begin() noexcept
231 {
232 return {*this};
233 }
234
249 sentinel end() noexcept
250 {
251 return {};
252 }
253
272 template <typename record_t>
273 void push_back(record_t && r)
274 requires detail::record_like<record_t>
275 {
276 write_record(detail::get_or_ignore<field::seq>(r),
277 detail::get_or_ignore<field::id>(r),
278 detail::get_or_ignore<field::qual>(r));
279 }
280
302 template <typename tuple_t>
303 void push_back(tuple_t && t)
304 requires tuple_like<tuple_t> && (!detail::record_like<tuple_t>)
305 {
306 // index_of might return npos, but this will be handled well by get_or_ignore (and just return ignore)
307 write_record(detail::get_or_ignore<selected_field_ids::index_of(field::seq)>(t),
308 detail::get_or_ignore<selected_field_ids::index_of(field::id)>(t),
309 detail::get_or_ignore<selected_field_ids::index_of(field::qual)>(t));
310 }
311
335 template <typename arg_t, typename... arg_types>
336 void emplace_back(arg_t && arg, arg_types &&... args)
337 {
338 push_back(std::tie(arg, args...));
339 }
340
362 template <std::ranges::input_range rng_t>
365 {
366 for (auto && record : range)
367 push_back(std::forward<decltype(record)>(record));
368 return *this;
369 }
370
398 template <std::ranges::input_range rng_t>
401 {
402 f = range;
403 return f;
404 }
405
407 template <std::ranges::input_range rng_t>
410 {
411 f = range;
412 return std::move(f);
413 }
415
418
423 {
424 return *secondary_stream;
425 }
427
428protected:
431 std::vector<char> stream_buffer{std::vector<char>(1'000'000)};
432
444 {
445 delete ptr;
446 }
447
449 stream_ptr_t primary_stream{nullptr, stream_deleter_noop};
451 stream_ptr_t secondary_stream{nullptr, stream_deleter_noop};
452
459
461 template <typename seq_t, typename id_t, typename qual_t>
462 void write_record(seq_t && seq, id_t && id, qual_t && qual)
463 {
464 assert(!format.valueless_by_exception());
466 [&](auto & f)
467 {
468 {
469 f.write_sequence_record(*secondary_stream, options, seq, id, qual);
470 }
471 },
472 format);
473 }
474
476 friend iterator;
477};
478
485template <output_stream stream_t, sequence_file_output_format file_format>
487 file_format const &)
490
492template <output_stream stream_t, sequence_file_output_format file_format>
494 file_format const &)
497
499template <output_stream stream_t,
500 sequence_file_output_format file_format,
502sequence_file_output(stream_t &&, file_format const &, selected_field_ids const &)
504
506template <output_stream stream_t,
507 sequence_file_output_format file_format,
509sequence_file_output(stream_t &, file_format const &, selected_field_ids const &)
512} // namespace seqan3
Output iterator necessary for providing a range-like interface in output file.
Definition out_file_iterator.hpp:44
The generic concept for sequence file out formats.
Definition sequence_file/output_format_concept.hpp:94
A class for writing sequence files, e.g. FASTA, FASTQ ...
Definition io/sequence_file/output.hpp:66
static void stream_deleter_noop(std::basic_ostream< stream_char_type > *)
Stream deleter that does nothing (no ownership assumed).
Definition io/sequence_file/output.hpp:440
sequence_file_output & operator=(sequence_file_output const &)=delete
Copy assignment is explicitly deleted, because you can't have multiple access to the same file.
sequence_file_output(stream_t &, file_format const &, selected_field_ids const &) -> sequence_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...
sequence_file_output(std::filesystem::path filename, selected_field_ids const &fields_tag=selected_field_ids{})
Construct from filename.
Definition io/sequence_file/output.hpp:148
sequence_file_output(stream_t &&, file_format const &) -> sequence_file_output< typename sequence_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...
void const_iterator
The const iterator type is void, because files are not const-iterable.
Definition io/sequence_file/output.hpp:112
char stream_char_type
Character type of the stream(s).
Definition io/sequence_file/output.hpp:77
std::default_sentinel_t sentinel
The type returned by end().
Definition io/sequence_file/output.hpp:114
typename detail::variant_from_tags< valid_formats, detail::sequence_file_output_format_exposer >::type format_type
Type of the format, a std::variant over the valid_formats.
Definition io/sequence_file/output.hpp:455
friend iterator
Befriend iterator so it can access the buffers.
Definition io/sequence_file/output.hpp:476
sequence_file_output(stream_t &&, file_format const &, selected_field_ids const &) -> sequence_file_output< selected_field_ids, type_list< file_format > >
Deduction guide for given stream, file format and field ids.
void push_back(tuple_t &&t)
Write a record in form of a std::tuple to the file.
Definition io/sequence_file/output.hpp:303
sequence_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/sequence_file/output.hpp:183
void push_back(record_t &&r)
Write a seqan3::record to the file.
Definition io/sequence_file/output.hpp:273
format_type format
The actual std::variant holding a pointer to the detected/selected format.
Definition io/sequence_file/output.hpp:457
sequence_file_output(sequence_file_output &&)=default
Move construction is defaulted.
stream_ptr_t primary_stream
The primary stream is the user provided stream or the file stream if constructed from filename.
Definition io/sequence_file/output.hpp:449
sequence_file_output & operator=(sequence_file_output &&)=default
Move assignment is defaulted.
sequence_file_output(sequence_file_output const &)=delete
Copy construction is explicitly deleted, because you can't have multiple access to the same file.
sequence_file_output(stream_t &, file_format const &) -> sequence_file_output< typename sequence_file_output<>::selected_field_ids, type_list< file_format > >
Deduction guide for given stream and file format.
valid_formats_ valid_formats
A seqan3::type_list with the possible formats.
Definition io/sequence_file/output.hpp:75
selected_field_ids_ selected_field_ids
A seqan3::fields list with the fields selected for the record.
Definition io/sequence_file/output.hpp:73
sequence_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/sequence_file/output.hpp:197
friend sequence_file_output operator|(rng_t &&range, sequence_file_output &&f)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition io/sequence_file/output.hpp:408
~sequence_file_output()=default
Destructor is defaulted.
std::basic_ostream< stream_char_type > & get_stream()
Expose a reference to the secondary stream. [public, but not documented as part of the API].
Definition io/sequence_file/output.hpp:422
void emplace_back(arg_t &&arg, arg_types &&... args)
Write a record to the file by passing individual fields.
Definition io/sequence_file/output.hpp:336
sequence_file_output & operator=(rng_t &&range)
Write a range of records (or tuples) to the file.
Definition io/sequence_file/output.hpp:363
void value_type
The value type (void).
Definition io/sequence_file/output.hpp:100
sentinel end() noexcept
Returns a sentinel for comparison with iterator.
Definition io/sequence_file/output.hpp:249
void reference
The reference type (void).
Definition io/sequence_file/output.hpp:102
std::vector< char > stream_buffer
A larger (compared to stl default) stream buffer to use when reading from a file.
Definition io/sequence_file/output.hpp:431
static void stream_deleter_default(std::basic_ostream< stream_char_type > *ptr)
Stream deleter with default behaviour (ownership assumed).
Definition io/sequence_file/output.hpp:443
friend sequence_file_output & operator|(rng_t &&range, sequence_file_output &f)
Write a range of records (or tuples) to the file.
Definition io/sequence_file/output.hpp:399
stream_ptr_t secondary_stream
The secondary stream is a compression layer on the primary or just points to the primary (no compress...
Definition io/sequence_file/output.hpp:451
void const_reference
The const reference type (void).
Definition io/sequence_file/output.hpp:104
void write_record(seq_t &&seq, id_t &&id, qual_t &&qual)
Write record to format.
Definition io/sequence_file/output.hpp:462
sequence_file_output()=delete
Default constructor is explicitly deleted, you need to give a stream or file name.
iterator begin() noexcept
Returns an iterator to current position in the file.
Definition io/sequence_file/output.hpp:230
void size_type
The size type (void).
Definition io/sequence_file/output.hpp:106
Auxiliary concept that checks whether a type is a specialisation of seqan3::fields.
Definition detail/record.hpp:32
T data(T... args)
Provides auxiliary data structures and functions for seqan3::record and seqan3::fields.
Provides seqan3::views::elements.
Provides the seqan3::sequence_file_format_genbank class.
Provides the seqan3::format_sam.
T forward(T... args)
T get(T... args)
field
An enumerator for the fields used in file formats.
Definition record.hpp:60
void set_format(format_variant_type &format, std::filesystem::path const &file_name)
Sets the file format according to the file name extension.
Definition io/detail/misc.hpp:65
auto make_secondary_ostream(std::basic_ostream< char_t > &primary_stream, std::filesystem::path &filename) -> std::unique_ptr< std::basic_ostream< char_t >, std::function< void(std::basic_ostream< char_t > *)> >
Depending on the given filename/extension, create a compression stream or just forward the primary st...
Definition misc_output.hpp:40
@ seq
The "sequence", usually a range of nucleotides or amino acids.
@ qual
The qualities, usually in Phred score notation.
Whether a type behaves like a tuple.
Provides various utility functions.
Provides exceptions used in the I/O module.
Stream concepts.
Provides various utility functions required only for output.
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
SeqAn specific customisations in the standard namespace.
Provides the seqan3::detail::out_file_iterator class template.
Provides the seqan3::record template and the seqan3::field enum.
Provides seqan3::detail::record_like.
Provides seqan3::sequence_file_output_format and auxiliary classes.
Provides seqan3::sequence_file_output_options.
T size(T... args)
Base class to deduce the std::variant type from format tags.
Definition io/detail/misc.hpp:28
A class template that holds a choice of seqan3::field.
Definition record.hpp:125
static constexpr bool contains(field f)
Whether a field is contained in the parameter pack.
Definition record.hpp:146
Thrown if there is an unspecified filesystem or stream error while opening, e.g. permission problem.
Definition io/exception.hpp:36
The class template that file records are based on; behaves like a std::tuple.
Definition record.hpp:190
The options type defines various option members that influence the behaviour of all or some formats.
Definition sequence_file/output_options.hpp:23
Type that contains multiple types.
Definition type_list.hpp:26
T tie(T... args)
Provides traits for seqan3::type_list.
Provides seqan3::tuple_like.
Provides seqan3::views::convert.
T visit(T... args)
Provides seqan3::views::zip.
Hide me