SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
format_fastq.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 <algorithm>
13#include <iterator>
14#include <ranges>
15#include <string>
16#include <string_view>
17#include <vector>
18
41
42namespace seqan3
43{
44
77{
78public:
88
90
92 static inline std::vector<std::string> file_extensions{{"fastq"}, {"fq"}};
93
94protected:
96 template <typename stream_type, // constraints checked by file
97 typename seq_legal_alph_type,
98 typename stream_pos_type,
99 typename seq_type, // other constraints checked inside function
100 typename id_type,
101 typename qual_type>
104 stream_pos_type & position_buffer,
105 seq_type & sequence,
106 id_type & id,
108 {
109 // Store current position in buffer
110 // Must happen before constructing the view.
111 // With libc++, tellg invalidates the I/O buffer.
112 position_buffer = stream.tellg();
113
114 auto stream_view = detail::istreambuf(stream);
116
117 // cache the begin position so we write quals to the same position as seq in seq_qual case
118 size_t sequence_size_before = 0;
119 size_t sequence_size_after = 0;
120 if constexpr (!detail::decays_to_ignore_v<seq_type>)
122
123 /* ID */
124 if (*stream_it != '@') // [[unlikely]]
125 {
126 throw parse_error{std::string{"Expected '@' on beginning of ID line, got: "}
127 + detail::make_printable(*stream_it)};
128 }
129 ++stream_it; // skip '@'
130
131#if SEQAN3_WORKAROUND_VIEW_PERFORMANCE // can't have nice things :'(
132 auto e = std::ranges::end(stream_view);
133 if constexpr (!detail::decays_to_ignore_v<id_type>)
134 {
135 if (options.truncate_ids)
136 {
137 for (; (stream_it != e) && (!(is_cntrl || is_blank))(*stream_it); ++stream_it)
138 {
140 id.push_back(*stream_it);
141 else
142 id.push_back(assign_char_to(*stream_it, std::ranges::range_value_t<id_type>{}));
143 }
144 for (; (stream_it != e) && (!is_char<'\n'>)(*stream_it); ++stream_it)
145 {}
146 }
147 else
148 {
149 for (; (stream_it != e) && (!is_char<'\n'>)(*stream_it); ++stream_it)
150 {
152 id.push_back(*stream_it);
153 else
154 id.push_back(assign_char_to(*stream_it, std::ranges::range_value_t<id_type>{}));
155 }
156 }
157 }
158 else
159 {
160 for (; (stream_it != e) && (!is_char<'\n'>)(*stream_it); ++stream_it)
161 {}
162 }
163
164 if (stream_it == e)
165 {
166 throw unexpected_end_of_input{"Expected end of ID-line, got end-of-file."};
167 }
168 ++stream_it; // skip newline
169
170 /* Sequence */
171 if constexpr (!detail::decays_to_ignore_v<seq_type>)
172 {
173 for (; (stream_it != e) && (!is_char<'+'>)(*stream_it); ++stream_it)
174 {
175 if ((!is_space)(*stream_it))
176 {
178 {
179 sequence.push_back(*stream_it);
180 }
181 else
182 {
184 {
185 throw parse_error{std::string{"Encountered bad letter for seq: "}
186 + detail::make_printable(*stream_it)};
187 }
188 sequence.push_back(assign_char_to(*stream_it, std::ranges::range_value_t<seq_type>{}));
189 }
190 }
191 }
193 }
194 else // consume, but count
195 {
196 for (; (stream_it != e) && (!is_char<'+'>)(*stream_it); ++stream_it)
197 if ((!is_space)(*stream_it))
199 }
200
201 /* 2nd ID line */
202 if (stream_it == e)
203 throw unexpected_end_of_input{"Expected second ID-line, got end-of-file."};
204
205 if (*stream_it != '+')
206 {
207 throw parse_error{std::string{"Expected '+' on beginning of 2nd ID line, got: "}
208 + detail::make_printable(*stream_it)};
209 }
210
211 for (; (stream_it != e) && (!is_char<'\n'>)(*stream_it); ++stream_it)
212 {}
213
214 if (stream_it == e)
215 throw unexpected_end_of_input{"Expected end of second ID-line, got end-of-file."};
216
217 ++stream_it;
218
219 /* Qualities */
220 if constexpr (!detail::decays_to_ignore_v<qual_type>)
221 {
223 {
224 if (stream_it == e)
225 throw unexpected_end_of_input{"Expected qualities, got end-of-file."};
226
227 if ((!is_space)(*stream_it))
228 {
231 {
232 qualities.push_back(*stream_it);
233 }
234 else
235 {
236 if (!char_is_valid_for<std::ranges::range_value_t<qual_type>>(*stream_it))
237 {
238 throw parse_error{std::string{"Encountered bad letter for qual: "}
239 + detail::make_printable(*stream_it)};
240 }
241 qualities.push_back(assign_char_to(*stream_it, std::ranges::range_value_t<qual_type>{}));
242 }
243 }
244 ++stream_it;
245 }
246 }
247 else // consume
248 {
250 {
251 if (stream_it == e)
252 throw unexpected_end_of_input{"File ended before expected number of qualities could be read."};
253
254 if ((!is_space)(*stream_it))
256 ++stream_it;
257 }
258 }
259
260 if (stream_it != e)
261 {
262 if ((!is_char<'\n'>)(*stream_it))
263 throw parse_error{"Qualitites longer than sequence."};
264 else
265 ++stream_it;
266 }
267
268#else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
269
270 if constexpr (!detail::decays_to_ignore_v<id_type>)
271 {
272 if (options.truncate_ids)
273 {
274 std::ranges::copy(stream_view | detail::take_until_or_throw(is_cntrl || is_blank)
275 | views::char_to<std::ranges::range_value_t<id_type>>,
277 detail::consume(stream_view | detail::take_line_or_throw);
278 }
279 else
280 {
281 std::ranges::copy(stream_view | detail::take_line_or_throw
282 | views::char_to<std::ranges::range_value_t<id_type>>,
284 }
285 }
286 else
287 {
288 detail::consume(stream_view | detail::take_line_or_throw);
289 }
290
291 /* Sequence */
292 auto seq_view = stream_view | detail::take_until_or_throw(is_char<'+'>) // until 2nd ID line
293 | std::views::filter(!is_space); // ignore whitespace
294 if constexpr (!detail::decays_to_ignore_v<seq_type>)
295 {
299 | std::views::transform(
300 [is_legal_alph](char const c) // enforce legal alphabet
301 {
302 if (!is_legal_alph(c))
303 {
304 throw parse_error{std::string{"Encountered an unexpected letter: "}
305 + "char_is_valid_for<"
306 + detail::type_name_as_string<seq_legal_alph_type>
307 + "> evaluated to false on " + detail::make_printable(c)};
308 }
309 return c;
310 })
311 | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
314 }
315 else // consume, but count
316 {
317 auto it = begin(seq_view);
318 auto it_end = end(seq_view);
319 while (it != it_end)
320 {
321 ++it;
323 }
324 }
325
326 detail::consume(stream_view | detail::take_line_or_throw);
327
328 /* Qualities */
329 auto qview = stream_view | std::views::filter(!is_space) // this consumes trailing newline
330 | detail::take_exactly_or_throw(sequence_size_after - sequence_size_before);
331 if constexpr (!detail::decays_to_ignore_v<qual_type>)
332 {
333 std::ranges::copy(qview | views::char_to<std::ranges::range_value_t<qual_type>>,
335 }
336 else
337 {
338 detail::consume(qview);
339 }
340#endif
341 }
342
344 template <typename stream_type, // constraints checked by file
345 typename seq_type, // other constraints checked inside function
346 typename id_type,
347 typename qual_type>
349 sequence_file_output_options const & options,
350 seq_type && sequence,
351 id_type && id,
353 {
354 seqan3::detail::fast_ostreambuf_iterator stream_it{*stream.rdbuf()};
355
356 // ID
357 if constexpr (detail::decays_to_ignore_v<id_type>)
358 {
359 throw std::logic_error{"The ID field may not be set to ignore when writing FASTQ files."};
360 }
361 else
362 {
363 if (std::ranges::empty(id)) //[[unlikely]]
364 throw std::runtime_error{"The ID field may not be empty when writing FASTQ files."};
365
366 stream_it = '@';
367 stream_it.write_range(id);
368 stream_it.write_end_of_line(options.add_carriage_return);
369 }
370
371 // Sequence
372 if constexpr (detail::decays_to_ignore_v<seq_type>)
373 {
374 throw std::logic_error{
375 "The SEQ and SEQ_QUAL fields may not both be set to ignore when writing FASTQ files."};
376 }
377 else
378 {
379 if (std::ranges::empty(sequence)) //[[unlikely]]
380 throw std::runtime_error{"The SEQ field may not be empty when writing FASTQ files."};
381
382 stream_it.write_range(sequence | views::to_char);
383 stream_it.write_end_of_line(options.add_carriage_return);
384 }
385
386 // 2nd ID-line
387 if constexpr (!detail::decays_to_ignore_v<id_type>)
388 {
389 stream_it = '+';
390
391 if (options.fastq_double_id)
392 stream_it.write_range(id);
393
394 stream_it.write_end_of_line(options.add_carriage_return);
395 }
396
397 // Quality line
398 if constexpr (detail::decays_to_ignore_v<qual_type>)
399 {
400 throw std::logic_error{
401 "The QUAL and SEQ_QUAL fields may not both be set to ignore when writing FASTQ files."};
402 }
403 else
404 {
405 if (std::ranges::empty(qualities)) //[[unlikely]]
406 throw std::runtime_error{"The SEQ field may not be empty when writing FASTQ files."};
407
408 if constexpr (std::ranges::sized_range<seq_type> && std::ranges::sized_range<qual_type>)
409 {
410 assert(std::ranges::size(sequence) == std::ranges::size(qualities));
411 }
412
413 stream_it.write_range(qualities | views::to_char);
414 stream_it.write_end_of_line(options.add_carriage_return);
415 }
416 }
417};
418
419} // namespace seqan3
Provides aliases for qualified.
Core alphabet concept and free function/type trait wrappers.
T back_inserter(T... args)
T begin(T... args)
Provides alphabet adaptations for standard char types.
Provides seqan3::views::char_to.
A "pretty printer" for most SeqAn data structures and related types.
Definition debug_stream_type.hpp:79
The FASTQ format.
Definition format_fastq.hpp:77
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:102
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:348
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:92
T copy(T... args)
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:60
auto const char_to
A view over an alphabet, given a range of characters.
Definition char_to.hpp:64
constexpr auto assign_char_to
Assign a character to an alphabet object.
Definition alphabet/concept.hpp:517
constexpr auto char_is_valid_for
Returns whether a character is in the valid set of a seqan3::alphabet (usually implies a bijective ma...
Definition alphabet/concept.hpp:661
constexpr auto is_blank
Checks whether c is a blank character.
Definition predicate.hpp:139
constexpr auto is_char
Checks whether a given letter is the same as the template non-type argument.
Definition predicate.hpp:60
constexpr auto is_space
Checks whether c is a space character.
Definition predicate.hpp:122
constexpr auto is_cntrl
Checks whether c is a control character.
Definition predicate.hpp:87
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:26
SeqAn specific customisations in the standard namespace.
Provides character predicates for tokenisation.
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 io/exception.hpp:45
The options type defines various option members that influence the behaviour of all or some formats.
Definition sequence_file/output_options.hpp:23
bool add_carriage_return
The default plain text line-ending is "\n", but on Windows an additional carriage return is recommend...
Definition sequence_file/output_options.hpp:39
bool fastq_double_id
Whether to write the ID only '@' or also after '+' line.
Definition sequence_file/output_options.hpp:34
Thrown if I/O was expecting more input (e.g. a delimiter or a new line), but the end of input was rea...
Definition io/exception.hpp:75
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.
Provides concepts that do not have equivalents in C++20.
Hide me