SeqAn3 3.4.3-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
format_fasta.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2026 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2026 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
40
41namespace seqan3
42{
43
78{
79public:
89
91
94 {"fasta"},
95 {"fa"},
96 {"fna"},
97 {"ffn"},
98 {"faa"},
99 {"frn"},
100 {"fas"},
101 };
102
103protected:
105 template <typename stream_type, // constraints checked by file
106 typename legal_alph_type,
107 typename stream_pos_type,
108 typename seq_type, // other constraints checked inside function
109 typename id_type,
110 typename qual_type>
113 stream_pos_type & position_buffer,
114 seq_type & sequence,
115 id_type & id,
117 {
118 // Store current position in buffer
119 // Must happen before constructing the view.
120 // With libc++, tellg invalidates the I/O buffer.
121 position_buffer = stream.tellg();
122
123 auto stream_view = detail::istreambuf(stream);
124
125 // ID
126 read_id(stream_view, options, id);
127
128 // Sequence
129 read_seq(stream_view, options, sequence);
130 }
131
133 template <typename stream_type, // constraints checked by file
134 typename seq_type, // other constraints checked inside function
135 typename id_type,
136 typename qual_type>
138 sequence_file_output_options const & options,
139 seq_type && sequence,
140 id_type && id,
142 {
143 seqan3::detail::fast_ostreambuf_iterator stream_it{*stream.rdbuf()};
144
145 // ID
146 if constexpr (detail::decays_to_ignore_v<id_type>)
147 {
148 throw std::logic_error{"The ID field may not be set to ignore when writing FASTA files."};
149 }
150 else
151 {
152 if (std::ranges::empty(id)) //[[unlikely]]
153 throw std::runtime_error{"The ID field may not be empty when writing FASTA files."};
154
155 write_id(stream_it, options, id);
156 }
157
158 // Sequence
159 if constexpr (detail::decays_to_ignore_v<seq_type>) // sequence
160 {
161 throw std::logic_error{
162 "The SEQ and SEQ_QUAL fields may not both be set to ignore when writing FASTA files."};
163 }
164 else
165 {
166 if (std::ranges::empty(sequence)) //[[unlikely]]
167 throw std::runtime_error{"The SEQ field may not be empty when writing FASTA files."};
168
169 write_seq(stream_it, options, sequence);
170 }
171 }
172
173private:
176 template <typename stream_view_t, typename seq_legal_alph_type, typename id_type>
177 void
178 read_id(stream_view_t & stream_view, sequence_file_input_options<seq_legal_alph_type> const & options, id_type & id)
179 {
180 auto const is_id = is_char<'>'> || is_char<';'>;
181
182 if (!is_id(*begin(stream_view)))
183 throw parse_error{std::string{"Expected to be on beginning of ID, but "} + is_id.msg
184 + " evaluated to false on " + detail::make_printable(*begin(stream_view))};
185
186 if constexpr (detail::decays_to_ignore_v<id_type>) // Skip the ID, it is not requested by the user.
187 {
188 detail::consume(stream_view | detail::take_line_or_throw);
189 }
190 else // read ID
191 {
192 if (options.truncate_ids)
193 {
194#if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
195 auto it = stream_view.begin();
196 auto e = stream_view.end();
197 ++it; // already checked `is_id`
198
199 if (options.fasta_ignore_blanks_before_id)
200 {
201 for (; (it != e) && (is_blank)(*it); ++it) // skip leading ' '
202 {}
203 }
204
205 bool at_delimiter = false;
206 for (; it != e; ++it)
207 {
208 if ((is_cntrl || is_blank)(*it))
209 {
210 at_delimiter = true;
211 break;
212 }
213 id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
214 }
215
216 if (!at_delimiter)
217 throw unexpected_end_of_input{"FASTA ID line did not end in newline."};
218
219 for (; (it != e) && ((!is_char<'\n'>)(*it)); ++it)
220 {}
221
222#else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
223 if (options.fasta_ignore_blanks_before_id)
224 {
226 stream_view | std::views::drop(1) // skip leading '>' or ';'
227 | std::views::drop_while(is_blank) // skip leading ' '
228 | detail::take_until_or_throw(is_cntrl || is_blank) // read ID until delimiter…
229 | views::char_to<std::ranges::range_value_t<id_type>>,
230 std::back_inserter(id)); // … ^A is old delimiter
231 }
232 else
233 {
235 stream_view | std::views::drop(1) // skip leading '>' or ';'
236 | detail::take_until_or_throw(is_cntrl || is_blank) // read ID until delimiter…
237 | views::char_to<std::ranges::range_value_t<id_type>>,
238 std::back_inserter(id)); // … ^A is old delimiter
239 }
240
241 // consume rest of line
242 detail::consume(stream_view | detail::take_line_or_throw);
243#endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
244 }
245 else // options.truncate_ids
246 {
247#if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
248 auto it = stream_view.begin();
249 auto e = stream_view.end();
250 ++it; // skip leading '>' or ';'
251
252 if (options.fasta_ignore_blanks_before_id)
253 {
254 for (; (it != e) && (is_blank)(*it); ++it) // skip leading ' '
255 {}
256 }
257
258 bool at_delimiter = false;
259 for (; it != e; ++it)
260 {
261 if ((is_char<'\n'>)(*it))
262 {
263 at_delimiter = true;
264 break;
265 }
266 id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
267 }
268
269 if (!at_delimiter)
270 throw unexpected_end_of_input{"FASTA ID line did not end in newline."};
271
272#else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
273 if (options.fasta_ignore_blanks_before_id)
274 {
275 std::ranges::copy(stream_view | detail::take_line_or_throw // read line
276 | std::views::drop(1) // skip leading '>' or ';'
277 | std::views::drop_while(is_blank) // skip leading ' '
278 | views::char_to<std::ranges::range_value_t<id_type>>,
280 }
281 else
282 {
283 std::ranges::copy(stream_view | detail::take_line_or_throw // read line
284 | std::views::drop(1) // skip leading '>' or ';'
285 | views::char_to<std::ranges::range_value_t<id_type>>,
287 }
288#endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
289 }
290 }
291 }
292
294 template <typename stream_view_t, typename seq_legal_alph_type, typename seq_type>
295 void read_seq(stream_view_t & stream_view, sequence_file_input_options<seq_legal_alph_type> const &, seq_type & seq)
296 {
297 constexpr auto is_id = is_char<'>'> || is_char<';'>;
298
299 if constexpr (!detail::decays_to_ignore_v<seq_type>)
300 {
301 constexpr auto is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
302
303#if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
304 auto it = stream_view.begin();
305 auto e = stream_view.end();
306
307 if (it == e)
308 throw unexpected_end_of_input{"No sequence information given!"};
309
310 for (; (it != e) && ((!is_id)(*it)); ++it)
311 {
312 if ((is_space || is_digit)(*it))
313 {
314 continue;
315 }
316 else if (is_legal_alph(*it))
317 {
318 seq.push_back(assign_char_to(*it, std::ranges::range_value_t<seq_type>{}));
319 }
320 else
321 {
322
323 throw parse_error{std::string{"Encountered an unexpected letter: "} + "char_is_valid_for<"
324 + detail::type_name_as_string<seq_legal_alph_type>
325 + "> evaluated to false on " + detail::make_printable(*it)};
326 }
327 }
328
329#else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
330
331 if (std::ranges::begin(stream_view) == std::ranges::end(stream_view))
332 throw unexpected_end_of_input{"No sequence information given!"};
333
335 stream_view | detail::take_until(is_id) // until next header (or end)
336 | std::views::filter(!(is_space || is_digit)) // ignore whitespace and numbers
337 | std::views::transform(
338 [is_legal_alph](char const c)
339 {
340 if (!is_legal_alph(c))
341 {
342 throw parse_error{std::string{"Encountered an unexpected letter: "}
343 + "char_is_valid_for<"
344 + detail::type_name_as_string<seq_legal_alph_type>
345 + "> evaluated to false on " + detail::make_printable(c)};
346 }
347 return c;
348 }) // enforce legal alphabet
349 | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
351#endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
352 }
353 else
354 {
355 detail::consume(stream_view | detail::take_until(is_id));
356 }
357 }
358
360 template <typename stream_it_t, typename id_type>
361 void write_id(stream_it_t & stream_it, sequence_file_output_options const & options, id_type && id)
362 {
363 if (options.fasta_legacy_id_marker)
364 stream_it = ';';
365 else
366 stream_it = '>';
367
368 if (options.fasta_blank_before_id)
369 stream_it = ' ';
370
371 stream_it.write_range(id);
372 stream_it.write_end_of_line(options.add_carriage_return);
373 }
374
376 template <typename stream_it_t, typename seq_type>
377 void write_seq(stream_it_t & stream_it, sequence_file_output_options const & options, seq_type && seq)
378 {
379 auto char_sequence = seq | views::to_char;
380
381 if (options.fasta_letters_per_line > 0)
382 {
383 if constexpr (std::ranges::random_access_range<decltype(char_sequence)>
384 || std::ranges::sized_range<decltype(char_sequence)>)
385 {
386 for (auto && line : char_sequence | views::chunk(options.fasta_letters_per_line))
387 {
388 stream_it.write_range(line);
389 stream_it.write_end_of_line(options.add_carriage_return);
390 }
391 }
392 else // views::chunk is slow for ranges that are neither random access nor sized.
393 {
394 auto it = std::ranges::begin(char_sequence);
395 auto end = std::ranges::end(char_sequence);
396
397 while (it != end)
398 {
399 auto current_end = it;
400 size_t steps = std::ranges::advance(current_end, options.fasta_letters_per_line, end);
401 using subrange_t =
402 std::ranges::subrange<decltype(it), decltype(it), std::ranges::subrange_kind::sized>;
403 it = stream_it.write_range(subrange_t{it, current_end, (options.fasta_letters_per_line - steps)});
404 stream_it.write_end_of_line(options.add_carriage_return);
405 }
406 }
407 }
408 else
409 {
410 stream_it.write_range(char_sequence);
411 stream_it.write_end_of_line(options.add_carriage_return);
412 }
413 }
414};
415
416} // 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.
Provides seqan3::views::chunk.
A "pretty printer" for most SeqAn data structures and related types.
Definition debug_stream_type.hpp:79
The FASTA format.
Definition format_fasta.hpp:78
void read_sequence_record(stream_type &stream, sequence_file_input_options< 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_fasta.hpp:111
static std::vector< std::string > file_extensions
The valid file extensions for this format; note that you can modify this value.
Definition format_fasta.hpp:93
format_fasta() 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_fasta.hpp:137
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.
T end(T... args)
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
@ seq
The "sequence", usually a range of nucleotides or amino acids.
constexpr auto is_blank
Checks whether c is a blank character.
Definition predicate.hpp:139
constexpr auto is_digit
Checks whether c is a digital character.
Definition predicate.hpp:259
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
seqan::stl::views::chunk chunk
A view adaptor that divides a range into chunks. <dl class="no-api">This entity is not part of the Se...
Definition chunk.hpp:23
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
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.
Hide me