SeqAn3 3.1.0
The Modern C++ library for sequence analysis.
output.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2006-2021, Knut Reinert & Freie Universität Berlin
3// Copyright (c) 2016-2021, 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 <seqan3/std/filesystem>
17#include <fstream>
18#include <seqan3/std/ranges>
19#include <string>
20#include <variant>
21#include <vector>
22
26#include <seqan3/io/detail/record.hpp>
29#include <seqan3/io/record.hpp>
43
44namespace seqan3
45{
46
47// ----------------------------------------------------------------------------
48// sequence_file_output
49// ----------------------------------------------------------------------------
50
65template <detail::fields_specialisation selected_field_ids_ = fields<field::seq, field::id, field::qual>,
66 detail::type_list_of_sequence_file_output_formats valid_formats_ =
67 type_list<format_embl, format_fasta, format_fastq, format_genbank, format_sam>>
69{
70public:
76 using selected_field_ids = selected_field_ids_;
78 using valid_formats = valid_formats_;
80 using stream_char_type = char;
82
85
86 static_assert([] () constexpr
87 {
88 for (field f : selected_field_ids::as_array)
89 if (!field_ids::contains(f))
90 return false;
91 return true;
92 }(),
93 "You selected a field that is not valid for sequence files, please refer to the documentation "
94 "of sequence_file_output::field_ids for the accepted values.");
95
102 using value_type = void;
104 using reference = void;
106 using const_reference = void;
108 using size_type = void;
112 using iterator = detail::out_file_iterator<sequence_file_output>;
114 using const_iterator = void;
116 using sentinel = std::default_sentinel_t;
118
134
151 selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
152 primary_stream{new std::ofstream{}, stream_deleter_default}
153 {
154 primary_stream->rdbuf()->pubsetbuf(stream_buffer.data(), stream_buffer.size());
155 static_cast<std::basic_ofstream<char> *>(primary_stream.get())->open(filename,
156 std::ios_base::out | std::ios::binary);
157
158 if (!primary_stream->good())
159 throw file_open_error{"Could not open file " + filename.string() + " for writing."};
160
161 // possibly add intermediate compression stream
162 secondary_stream = detail::make_secondary_ostream(*primary_stream, filename);
163
164 // initialise format handler or throw if format is not found
165 detail::set_format(format, filename);
166 }
167
183 template <output_stream stream_t,
184 sequence_file_output_format file_format>
186 requires std::same_as<typename std::remove_reference_t<stream_t>::char_type, stream_char_type>
188 sequence_file_output(stream_t & stream,
189 file_format const & SEQAN3_DOXYGEN_ONLY(format_tag),
190 selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
191 primary_stream{&stream, stream_deleter_noop},
192 secondary_stream{&stream, stream_deleter_noop},
193 format{detail::sequence_file_output_format_exposer<file_format>{}}
194 {
195 static_assert(list_traits::contains<file_format, valid_formats>,
196 "You selected a format that is not in the valid_formats of this file.");
197 }
198
200 template <output_stream stream_t,
201 sequence_file_output_format file_format>
203 requires std::same_as<typename std::remove_reference_t<stream_t>::char_type, stream_char_type>
205 sequence_file_output(stream_t && stream,
206 file_format const & SEQAN3_DOXYGEN_ONLY(format_tag),
207 selected_field_ids const & SEQAN3_DOXYGEN_ONLY(fields_tag) = selected_field_ids{}) :
208 primary_stream{new stream_t{std::move(stream)}, stream_deleter_default},
209 secondary_stream{&*primary_stream, stream_deleter_noop},
210 format{detail::sequence_file_output_format_exposer<file_format>{}}
211 {
212 static_assert(list_traits::contains<file_format, valid_formats>,
213 "You selected a format that is not in the valid_formats of this file.");
214 }
216
238 iterator begin() noexcept
239 {
240 return {*this};
241 }
242
257 sentinel end() noexcept
258 {
259 return {};
260 }
261
280 template <typename record_t>
281 void push_back(record_t && r)
283 requires detail::record_like<record_t>
285 {
286 write_record(detail::get_or_ignore<field::seq>(r),
287 detail::get_or_ignore<field::id>(r),
288 detail::get_or_ignore<field::qual>(r));
289 }
290
312 template <typename tuple_t>
313 void push_back(tuple_t && t)
315 requires tuple_like<tuple_t> && (!detail::record_like<tuple_t>)
317 {
318 // index_of might return npos, but this will be handled well by get_or_ignore (and just return ignore)
319 write_record(detail::get_or_ignore<selected_field_ids::index_of(field::seq)>(t),
320 detail::get_or_ignore<selected_field_ids::index_of(field::id)>(t),
321 detail::get_or_ignore<selected_field_ids::index_of(field::qual)>(t));
322 }
323
347 template <typename arg_t, typename ...arg_types>
348 void emplace_back(arg_t && arg, arg_types && ... args)
349 {
350 push_back(std::tie(arg, args...));
351 }
352
374 template <std::ranges::input_range rng_t>
379 {
380 for (auto && record : range)
381 push_back(std::forward<decltype(record)>(record));
382 return *this;
383 }
384
412 template <std::ranges::input_range rng_t>
417 {
418 f = range;
419 return f;
420 }
421
423 template <std::ranges::input_range rng_t>
428 {
429 #if defined(__GNUC__) && (__GNUC__ == 9) // an unreported build problem of GCC9
430 for (auto && record : range)
431 f.push_back(std::forward<decltype(record)>(record));
432 #else // ^^^ workaround | regular solution ↓↓↓
433 f = range;
434 #endif
435 return std::move(f);
436 }
438
441
446 {
447 return *secondary_stream;
448 }
450protected:
453 std::vector<char> stream_buffer{std::vector<char>(1'000'000)};
454
462 static void stream_deleter_noop(std::basic_ostream<stream_char_type> *) {}
464 static void stream_deleter_default(std::basic_ostream<stream_char_type> * ptr) { delete ptr; }
465
467 stream_ptr_t primary_stream{nullptr, stream_deleter_noop};
469 stream_ptr_t secondary_stream{nullptr, stream_deleter_noop};
470
472 using format_type = typename detail::variant_from_tags<valid_formats,
473 detail::sequence_file_output_format_exposer>::type;
475 format_type format;
477
479 template <typename seq_t, typename id_t, typename qual_t>
480 void write_record(seq_t && seq, id_t && id, qual_t && qual)
481 {
482 assert(!format.valueless_by_exception());
483 std::visit([&] (auto & f)
484 {
485 {
486 f.write_sequence_record(*secondary_stream,
487 options,
488 seq,
489 id,
490 qual);
491 }
492 }, format);
493 }
494
496 friend iterator;
497};
498
505template <output_stream stream_t,
506 sequence_file_output_format file_format>
508 file_format const &)
511
513template <output_stream stream_t,
514 sequence_file_output_format file_format>
516 file_format const &)
519
521template <output_stream stream_t,
522 sequence_file_output_format file_format,
523 detail::fields_specialisation selected_field_ids>
525 file_format const &,
526 selected_field_ids const &)
529
531template <output_stream stream_t,
532 sequence_file_output_format file_format,
533 detail::fields_specialisation selected_field_ids>
535 file_format const &,
536 selected_field_ids const &)
540} // namespace seqan3
A class for writing sequence files, e.g. FASTA, FASTQ ...
Definition: output.hpp:69
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.
void push_back(record_t &&r)
Write a seqan3::record to the file.
Definition: output.hpp:281
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: output.hpp:150
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: output.hpp:114
char stream_char_type
Character type of the stream(s).
Definition: output.hpp:80
std::default_sentinel_t sentinel
The type returned by end().
Definition: output.hpp:116
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.
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: output.hpp:424
sequence_file_output(sequence_file_output &&)=default
Move construction is defaulted.
void push_back(tuple_t &&t)
Write a record in form of a std::tuple to the file.
Definition: output.hpp:313
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.
friend sequence_file_output & operator|(rng_t &&range, sequence_file_output &f)
Write a range of records (or tuples) to the file.
Definition: output.hpp:413
valid_formats_ valid_formats
A seqan3::type_list with the possible formats.
Definition: output.hpp:78
selected_field_ids_ selected_field_ids
A seqan3::fields list with the fields selected for the record.
Definition: output.hpp:76
~sequence_file_output()=default
Destructor is defaulted.
sequence_file_output_options options
The options are public and its members can be set directly.
Definition: output.hpp:440
sequence_file_output & operator=(rng_t &&range)
Write a range of records (or tuples) to the file.
Definition: output.hpp:375
void emplace_back(arg_t &&arg, arg_types &&... args)
Write a record to the file by passing individual fields.
Definition: output.hpp:348
void value_type
The value type (void).
Definition: output.hpp:102
sentinel end() noexcept
Returns a sentinel for comparison with iterator.
Definition: output.hpp:257
void reference
The reference type (void).
Definition: output.hpp:104
void const_reference
The const reference type (void).
Definition: output.hpp:106
sequence_file_output()=delete
Default constructor is explicitly deleted, you need to give a stream or file name.
detail::out_file_iterator< sequence_file_output > iterator
The iterator type of this view (an output iterator).
Definition: output.hpp:112
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: output.hpp:188
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: output.hpp:205
iterator begin() noexcept
Returns an iterator to current position in the file.
Definition: output.hpp:238
void size_type
The size type (void).
Definition: output.hpp:108
Provides seqan3::views::elements.
The <filesystem> header from C++17's standard library.
Provides the seqan3::sequence_file_format_genbank class.
Provides the seqan3::format_sam.
T format(T... args)
T forward(T... args)
T get(T... args)
field
An enumerator for the fields used in file formats.
Definition: record.hpp:63
@ id
The identifier, usually a string.
@ seq
The "sequence", usually a range of nucleotides or amino acids.
@ qual
The qualities, usually in Phred score notation.
The generic concept for sequence file out formats.
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: cigar_operation_table.hpp:2
Provides the seqan3::detail::out_file_iterator class template.
The <ranges> header from C++20's standard library.
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.
A class template that holds a choice of seqan3::field.
Definition: record.hpp:128
The class template that file records are based on; behaves like a std::tuple.
Definition: record.hpp:191
The options type defines various option members that influence the behaviour of all or some formats.
Definition: output_options.hpp:26
Type that contains multiple types.
Definition: type_list.hpp:29
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.