SeqAn3 3.2.0-rc.1
The Modern C++ library for sequence analysis.
format_fastq.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 <algorithm>
16#include <iterator>
17#include <seqan3/std/ranges>
18#include <string>
19#include <string_view>
20#include <vector>
21
43
44namespace seqan3
45{
46
79{
80public:
84 format_fastq() noexcept = default;
85 format_fastq(format_fastq const &) noexcept = default;
86 format_fastq & operator=(format_fastq const &) noexcept = default;
87 format_fastq(format_fastq &&) noexcept = default;
88 format_fastq & operator=(format_fastq &&) noexcept = default;
89 ~format_fastq() noexcept = default;
90
92
94 static inline std::vector<std::string> file_extensions
95 {
96 { "fastq" },
97 { "fq" }
98 };
99
100protected:
102 template <typename stream_type, // constraints checked by file
103 typename seq_legal_alph_type,
104 typename stream_pos_type,
105 typename seq_type, // other constraints checked inside function
106 typename id_type,
107 typename qual_type>
108 void read_sequence_record(stream_type & stream,
110 stream_pos_type & position_buffer,
111 seq_type & sequence,
112 id_type & id,
113 qual_type & qualities)
114 {
115 auto stream_view = detail::istreambuf(stream);
116 auto stream_it = begin(stream_view);
117
118 // cache the begin position so we write quals to the same position as seq in seq_qual case
119 size_t sequence_size_before = 0;
120 size_t sequence_size_after = 0;
121 if constexpr (!detail::decays_to_ignore_v<seq_type>)
122 sequence_size_before = size(sequence);
123 position_buffer = stream.tellg();
124
125 /* ID */
126 if (*stream_it != '@') // [[unlikely]]
127 {
128 throw parse_error{std::string{"Expected '@' on beginning of ID line, got: "} +
129 detail::make_printable(*stream_it)};
130 }
131 ++stream_it; // skip '@'
132
133 if constexpr (!detail::decays_to_ignore_v<id_type>)
134 {
135 if (options.truncate_ids)
136 {
137 std::ranges::copy(stream_view | detail::take_until_or_throw(is_cntrl || is_blank)
138 | views::char_to<std::ranges::range_value_t<id_type>>,
141 }
142 else
143 {
144 std::ranges::copy(stream_view | detail::take_line_or_throw
145 | views::char_to<std::ranges::range_value_t<id_type>>,
147 }
148 }
149 else
150 {
152 }
153
154 /* Sequence */
155 auto seq_view = stream_view | detail::take_until_or_throw(is_char<'+'>) // until 2nd ID line
156 | std::views::filter(!is_space); // ignore whitespace
157 if constexpr (!detail::decays_to_ignore_v<seq_type>)
158 {
159 auto constexpr is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
160 std::ranges::copy(seq_view | std::views::transform([is_legal_alph] (char const c) // enforce legal alphabet
161 {
162 if (!is_legal_alph(c))
163 {
164 throw parse_error{std::string{"Encountered an unexpected letter: "} +
165 "char_is_valid_for<" +
166 detail::type_name_as_string<seq_legal_alph_type> +
167 "> evaluated to false on " +
169 }
170 return c;
171 })
172 | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
174 sequence_size_after = size(sequence);
175 }
176 else // consume, but count
177 {
178 auto it = begin(seq_view);
179 auto it_end = end(seq_view);
180 while (it != it_end)
181 {
182 ++it;
183 ++sequence_size_after;
184 }
185 }
186
188
189 /* Qualities */
190 auto qview = stream_view | std::views::filter(!is_space) // this consumes trailing newline
191 | detail::take_exactly_or_throw(sequence_size_after - sequence_size_before);
192 if constexpr (!detail::decays_to_ignore_v<qual_type>)
193 {
194 std::ranges::copy(qview | views::char_to<std::ranges::range_value_t<qual_type>>,
195 std::back_inserter(qualities));
196 }
197 else
198 {
199 detail::consume(qview);
200 }
201 }
202
204 template <typename stream_type, // constraints checked by file
205 typename seq_type, // other constraints checked inside function
206 typename id_type,
207 typename qual_type>
208 void write_sequence_record(stream_type & stream,
209 sequence_file_output_options const & options,
210 seq_type && sequence,
211 id_type && id,
212 qual_type && qualities)
213 {
214 seqan3::detail::fast_ostreambuf_iterator stream_it{*stream.rdbuf()};
215
216 // ID
217 if constexpr (detail::decays_to_ignore_v<id_type>)
218 {
219 throw std::logic_error{"The ID field may not be set to ignore when writing FASTQ files."};
220 }
221 else
222 {
223 if (std::ranges::empty(id)) //[[unlikely]]
224 throw std::runtime_error{"The ID field may not be empty when writing FASTQ files."};
225
226 stream_it = '@';
227 stream_it.write_range(id);
228 stream_it.write_end_of_line(options.add_carriage_return);
229 }
230
231 // Sequence
232 if constexpr (detail::decays_to_ignore_v<seq_type>)
233 {
234 throw std::logic_error{"The SEQ and SEQ_QUAL fields may not both be set to ignore when writing FASTQ files."};
235 }
236 else
237 {
238 if (std::ranges::empty(sequence)) //[[unlikely]]
239 throw std::runtime_error{"The SEQ field may not be empty when writing FASTQ files."};
240
241 stream_it.write_range(sequence | views::to_char);
242 stream_it.write_end_of_line(options.add_carriage_return);
243 }
244
245 // 2nd ID-line
246 if constexpr (!detail::decays_to_ignore_v<id_type>)
247 {
248 stream_it = '+';
249
250 if (options.fastq_double_id)
251 stream_it.write_range(id);
252
253 stream_it.write_end_of_line(options.add_carriage_return);
254 }
255
256 // Quality line
257 if constexpr (detail::decays_to_ignore_v<qual_type>)
258 {
259 throw std::logic_error{"The QUAL and SEQ_QUAL fields may not both be set to ignore when writing FASTQ files."};
260 }
261 else
262 {
263 if (std::ranges::empty(qualities)) //[[unlikely]]
264 throw std::runtime_error{"The SEQ field may not be empty when writing FASTQ files."};
265
266 if constexpr (std::ranges::sized_range<seq_type> && std::ranges::sized_range<qual_type>)
267 {
268 assert(std::ranges::size(sequence) == std::ranges::size(qualities));
269 }
270
271 stream_it.write_range(qualities | views::to_char);
272 stream_it.write_end_of_line(options.add_carriage_return);
273 }
274 }
275};
276
277} // namespace seqan
Provides aliases for qualified.
Core alphabet concept and free function/type trait wrappers.
T back_inserter(T... args)
Provides alphabet adaptations for standard char types.
Provides seqan3::views::char_to.
Functionally the same as std::ostreambuf_iterator, but offers writing a range more efficiently.
Definition: fast_ostreambuf_iterator.hpp:39
The FASTQ format.
Definition: format_fastq.hpp:79
void read_sequence_record(stream_type &stream, sequence_file_input_options< seq_legal_alph_type > const &options, stream_pos_type &position_buffer, seq_type &sequence, id_type &id, qual_type &qualities)
Read from the specified stream and back-insert into the given field buffers.
Definition: format_fastq.hpp:108
format_fastq() noexcept=default
Defaulted.
void write_sequence_record(stream_type &stream, sequence_file_output_options const &options, seq_type &&sequence, id_type &&id, qual_type &&qualities)
Write the given fields to the specified stream.
Definition: format_fastq.hpp:208
static std::vector< std::string > file_extensions
The valid file extensions for this format; note that you can modify this value.
Definition: format_fastq.hpp:95
Provides various utility functions.
Provides various transformation traits used by the range module.
Provides seqan3::dna5, container aliases and string literals.
Provides seqan3::detail::fast_ostreambuf_iterator.
auto const to_char
A view that calls seqan3::to_char() on each element in the input range.
Definition: to_char.hpp:63
auto const char_to
A view over an alphabet, given a range of characters.
Definition: char_to.hpp:67
constexpr void consume(rng_t &&rng)
Iterate over a range (consumes single-pass input ranges).
Definition: misc.hpp:28
auto constexpr take_line_or_throw
A view adaptor that returns a single line from the underlying range (throws if there is no end-of-lin...
Definition: take_line_view.hpp:85
auto constexpr take_until_or_throw
A view adaptor that returns elements from the underlying range until the functor evaluates to true (t...
Definition: take_until_view.hpp:584
auto constexpr take_exactly_or_throw
A view adaptor that returns the first size elements from the underlying range and also exposes size i...
Definition: take_exactly_view.hpp:617
constexpr auto istreambuf
A view factory that returns a view over the stream buffer of an input stream.
Definition: istreambuf_view.hpp:110
auto constexpr is_blank
Checks whether c is a blank character.
Definition: predicate.hpp:145
std::string make_printable(char const c)
Returns a printable value for the given character c.
Definition: pretty_print.hpp:48
auto constexpr is_cntrl
Checks whether c is a control character.
Definition: predicate.hpp:92
auto constexpr is_space
Checks whether c is a space character.
Definition: predicate.hpp:128
decltype(detail::transform< trait_t >(list_t{})) transform
Apply a transformation trait to every type in the list and return a seqan3::type_list of the results.
Definition: traits.hpp:495
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:151
Provides seqan3::detail::ignore_output_iterator for writing to null stream.
The generic concept for a (biological) sequence.
Provides various utility functions.
Provides seqan3::detail::istreambuf.
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
SeqAn specific customisations in the standard namespace.
Provides character predicates for tokenisation.
The <ranges> header from C++20's standard library.
Provides seqan3::sequence_file_input_format and auxiliary classes.
Provides seqan3::sequence_file_input_options.
Provides seqan3::sequence_file_output_format and auxiliary classes.
Provides seqan3::sequence_file_output_options.
Thrown if there is a parse error, such as reading an unexpected character from an input stream.
Definition: exception.hpp:48
The options type defines various option members that influence the behaviour of all or some formats.
Definition: input_options.hpp:27
bool truncate_ids
Read the ID string only up until the first whitespace character.
Definition: input_options.hpp:29
The options type defines various option members that influence the behaviour of all or some formats.
Definition: output_options.hpp:26
bool add_carriage_return
The default plain text line-ending is "\n", but on Windows an additional carriage return is recommend...
Definition: output_options.hpp:43
bool fastq_double_id
Whether to write the ID only '@' or also after '+' line.
Definition: output_options.hpp:38
Provides seqan3::views::take_exactly and seqan3::views::take_exactly_or_throw.
Provides seqan3::detail::take_line and seqan3::detail::take_line_or_throw.
Provides seqan3::views::take_until and seqan3::views::take_until_or_throw.
Provides seqan3::views::to_char.
Provides traits to inspect some information of a type, for example its name.