SeqAn3 3.1.0-rc.2
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 <seqan3/std/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
78{
79public:
83 format_fastq() noexcept = default;
84 format_fastq(format_fastq const &) noexcept = default;
85 format_fastq & operator=(format_fastq const &) noexcept = default;
86 format_fastq(format_fastq &&) noexcept = default;
87 format_fastq & operator=(format_fastq &&) noexcept = default;
88 ~format_fastq() noexcept = default;
89
91
93 static inline std::vector<std::string> file_extensions
94 {
95 { "fastq" },
96 { "fq" }
97 };
98
99protected:
101 template <typename stream_type, // constraints checked by file
102 typename seq_legal_alph_type,
103 typename seq_type, // other constraints checked inside function
104 typename id_type,
105 typename qual_type>
106 void read_sequence_record(stream_type & stream,
108 seq_type & sequence,
109 id_type & id,
110 qual_type & qualities)
111 {
112 auto stream_view = detail::istreambuf(stream);
113 auto stream_it = begin(stream_view);
114
115 // cache the begin position so we write quals to the same position as seq in seq_qual case
116 size_t sequence_size_before = 0;
117 size_t sequence_size_after = 0;
118 if constexpr (!detail::decays_to_ignore_v<seq_type>)
119 sequence_size_before = size(sequence);
120
121 /* ID */
122 if (*stream_it != '@') // [[unlikely]]
123 {
124 throw parse_error{std::string{"Expected '@' on beginning of ID line, got: "} +
125 detail::make_printable(*stream_it)};
126 }
127 ++stream_it; // skip '@'
128
129 if constexpr (!detail::decays_to_ignore_v<id_type>)
130 {
131 if (options.truncate_ids)
132 {
133 std::ranges::copy(stream_view | detail::take_until_or_throw(is_cntrl || is_blank)
134 | views::char_to<std::ranges::range_value_t<id_type>>,
135 std::cpp20::back_inserter(id));
136 detail::consume(stream_view | detail::take_line_or_throw);
137 }
138 else
139 {
140 std::ranges::copy(stream_view | detail::take_line_or_throw
141 | views::char_to<std::ranges::range_value_t<id_type>>,
142 std::cpp20::back_inserter(id));
143 }
144 }
145 else
146 {
147 detail::consume(stream_view | detail::take_line_or_throw);
148 }
149
150 /* Sequence */
151 auto seq_view = stream_view | detail::take_until_or_throw(is_char<'+'>) // until 2nd ID line
152 | std::views::filter(!is_space); // ignore whitespace
153 if constexpr (!detail::decays_to_ignore_v<seq_type>)
154 {
155 auto constexpr is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
156 std::ranges::copy(seq_view | std::views::transform([is_legal_alph] (char const c) // enforce legal alphabet
157 {
158 if (!is_legal_alph(c))
159 {
160 throw parse_error{std::string{"Encountered an unexpected letter: "} +
161 "char_is_valid_for<" +
162 detail::type_name_as_string<seq_legal_alph_type> +
163 "> evaluated to false on " +
164 detail::make_printable(c)};
165 }
166 return c;
167 })
168 | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
169 std::cpp20::back_inserter(sequence));
170 sequence_size_after = size(sequence);
171 }
172 else // consume, but count
173 {
174 auto it = begin(seq_view);
175 auto it_end = end(seq_view);
176 while (it != it_end)
177 {
178 ++it;
179 ++sequence_size_after;
180 }
181 }
182
183 detail::consume(stream_view | detail::take_line_or_throw);
184
185 /* Qualities */
186 auto qview = stream_view | std::views::filter(!is_space) // this consumes trailing newline
187 | detail::take_exactly_or_throw(sequence_size_after - sequence_size_before);
188 if constexpr (!detail::decays_to_ignore_v<qual_type>)
189 {
190 std::ranges::copy(qview | views::char_to<std::ranges::range_value_t<qual_type>>,
191 std::cpp20::back_inserter(qualities));
192 }
193 else
194 {
195 detail::consume(qview);
196 }
197 }
198
200 template <typename stream_type, // constraints checked by file
201 typename seq_type, // other constraints checked inside function
202 typename id_type,
203 typename qual_type>
204 void write_sequence_record(stream_type & stream,
205 sequence_file_output_options const & options,
206 seq_type && sequence,
207 id_type && id,
208 qual_type && qualities)
209 {
210 seqan3::detail::fast_ostreambuf_iterator stream_it{*stream.rdbuf()};
211
212 // ID
213 if constexpr (detail::decays_to_ignore_v<id_type>)
214 {
215 throw std::logic_error{"The ID field may not be set to ignore when writing FASTQ files."};
216 }
217 else
218 {
219 if (std::ranges::empty(id)) //[[unlikely]]
220 throw std::runtime_error{"The ID field may not be empty when writing FASTQ files."};
221
222 stream_it = '@';
223 stream_it.write_range(id);
224 stream_it.write_end_of_line(options.add_carriage_return);
225 }
226
227 // Sequence
228 if constexpr (detail::decays_to_ignore_v<seq_type>)
229 {
230 throw std::logic_error{"The SEQ and SEQ_QUAL fields may not both be set to ignore when writing FASTQ files."};
231 }
232 else
233 {
234 if (std::ranges::empty(sequence)) //[[unlikely]]
235 throw std::runtime_error{"The SEQ field may not be empty when writing FASTQ files."};
236
237 stream_it.write_range(sequence | views::to_char);
238 stream_it.write_end_of_line(options.add_carriage_return);
239 }
240
241 // 2nd ID-line
242 if constexpr (!detail::decays_to_ignore_v<id_type>)
243 {
244 stream_it = '+';
245
246 if (options.fastq_double_id)
247 stream_it.write_range(id);
248
249 stream_it.write_end_of_line(options.add_carriage_return);
250 }
251
252 // Quality line
253 if constexpr (detail::decays_to_ignore_v<qual_type>)
254 {
255 throw std::logic_error{"The QUAL and SEQ_QUAL fields may not both be set to ignore when writing FASTQ files."};
256 }
257 else
258 {
259 if (std::ranges::empty(qualities)) //[[unlikely]]
260 throw std::runtime_error{"The SEQ field may not be empty when writing FASTQ files."};
261
262 if constexpr (std::ranges::sized_range<seq_type> && std::ranges::sized_range<qual_type>)
263 {
264 assert(std::ranges::size(sequence) == std::ranges::size(qualities));
265 }
266
267 stream_it.write_range(qualities | views::to_char);
268 stream_it.write_end_of_line(options.add_carriage_return);
269 }
270 }
271};
272
273} // namespace seqan
Adaptations of algorithms from the Ranges TS.
Provides aliases for qualified.
Core alphabet concept and free function/type trait wrappers.
Provides alphabet adaptations for standard char types.
Provides seqan3::views::char_to.
The FastQ format.
Definition: format_fastq.hpp:78
void read_sequence_record(stream_type &stream, sequence_file_input_options< seq_legal_alph_type > const &options, 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:106
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:204
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:94
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 auto is_blank
Checks whether c is a blank character.
Definition: predicate.hpp:145
constexpr auto is_space
Checks whether c is a space character.
Definition: predicate.hpp:128
constexpr auto is_cntrl
Checks whether c is a control character.
Definition: predicate.hpp:92
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:471
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: cigar_operation_table.hpp:2
SeqAn specific customisations in the standard namespace.
Provides character predicates for tokenisation.
Adaptations of concepts from the Ranges TS.
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:25
bool truncate_ids
Read the ID string only up until the first whitespace character.
Definition: input_options.hpp:27
The options type defines various option members that influence the behaviour of all or some formats.
Definition: 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: output_options.hpp:40
bool fastq_double_id
Whether to write the ID only '@' or also after '+' line.
Definition: output_options.hpp:35
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.