107 template <
typename stream_type,
108 typename legal_alph_type,
109 typename stream_pos_type,
115 stream_pos_type & position_buffer,
118 qual_type & SEQAN3_DOXYGEN_ONLY(qualities))
120 auto stream_view = detail::istreambuf(stream);
123 position_buffer = stream.tellg();
126 read_id(stream_view, options,
id);
129 read_seq(stream_view, options,
sequence);
133 template <
typename stream_type,
141 qual_type && SEQAN3_DOXYGEN_ONLY(qualities))
143 seqan3::detail::fast_ostreambuf_iterator stream_it{*stream.rdbuf()};
146 if constexpr (detail::decays_to_ignore_v<id_type>)
148 throw std::logic_error{
"The ID field may not be set to ignore when writing FASTA files."};
152 if (std::ranges::empty(
id))
155 write_id(stream_it, options,
id);
159 if constexpr (detail::decays_to_ignore_v<seq_type>)
162 "The SEQ and SEQ_QUAL fields may not both be set to ignore when writing FASTA files."};
169 write_seq(stream_it, options,
sequence);
176 template <
typename stream_view_t,
typename seq_legal_alph_type,
typename id_type>
180 auto const is_id = is_char<'>
'> || is_char<';
'>;
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))};
186 if constexpr (detail::decays_to_ignore_v<id_type>) // Skip the ID, it is not requested by the user.
188 detail::consume(stream_view | detail::take_line_or_throw);
192 if (options.truncate_ids)
194#if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
195 auto it = stream_view.begin();
196 auto e = stream_view.end();
197 ++it; // already checked `is_id`
199 if (options.fasta_ignore_blanks_before_id)
201 for (; (it != e) && (is_blank)(*it); ++it) // skip leading ' '
205 bool at_delimiter = false;
206 for (; it != e; ++it)
208 if ((is_cntrl || is_blank)(*it))
213 id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
217 throw unexpected_end_of_input{"FASTA ID line did not end in newline."};
219 for (; (it != e) && ((!is_char<'\n
'>)(*it)); ++it)
222#else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
223 if (options.fasta_ignore_blanks_before_id)
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
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
241 // consume rest of line
242 detail::consume(stream_view | detail::take_line_or_throw);
243#endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
245 else // options.truncate_ids
247#if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
248 auto it = stream_view.begin();
249 auto e = stream_view.end();
250 ++it; // skip leading '>
' or ';
'
252 if (options.fasta_ignore_blanks_before_id)
254 for (; (it != e) && (is_blank)(*it); ++it) // skip leading ' '
258 bool at_delimiter = false;
259 for (; it != e; ++it)
261 if ((is_char<'\n
'>)(*it))
266 id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
270 throw unexpected_end_of_input{"FASTA ID line did not end in newline."};
272#else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
273 if (options.fasta_ignore_blanks_before_id)
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>>,
279 std::back_inserter(id));
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>>,
286 std::back_inserter(id));
288#endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
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)
297 constexpr auto is_id = is_char<'>
'> || is_char<';
'>;
299 if constexpr (!detail::decays_to_ignore_v<seq_type>)
301 constexpr auto is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
303#if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
304 auto it = stream_view.begin();
305 auto e = stream_view.end();
308 throw unexpected_end_of_input{"No sequence information given!"};
310 for (; (it != e) && ((!is_id)(*it)); ++it)
312 if ((is_space || is_digit)(*it))
314 else if (!is_legal_alph(*it))
316 throw parse_error{std::string{"Encountered an unexpected letter: "} + "char_is_valid_for<"
317 + detail::type_name_as_string<seq_legal_alph_type>
318 + "> evaluated to false on " + detail::make_printable(*it)};
321 seq.push_back(assign_char_to(*it, std::ranges::range_value_t<seq_type>{}));
324#else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
326 if (std::ranges::begin(stream_view) == std::ranges::end(stream_view))
327 throw unexpected_end_of_input{"No sequence information given!"};
330 stream_view | detail::take_until(is_id) // until next header (or end)
331 | std::views::filter(!(is_space || is_digit)) // ignore whitespace and numbers
332 | std::views::transform(
333 [is_legal_alph](char const c)
335 if (!is_legal_alph(c))
337 throw parse_error{std::string{"Encountered an unexpected letter: "}
338 + "char_is_valid_for<"
339 + detail::type_name_as_string<seq_legal_alph_type>
340 + "> evaluated to false on " + detail::make_printable(c)};
343 }) // enforce legal alphabet
344 | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
345 std::back_inserter(seq));
346#endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
350 detail::consume(stream_view | detail::take_until(is_id));
355 template <typename stream_it_t, typename id_type>
356 void write_id(stream_it_t & stream_it, sequence_file_output_options const & options, id_type && id)
358 if (options.fasta_legacy_id_marker)
363 if (options.fasta_blank_before_id)
366 stream_it.write_range(id);
367 stream_it.write_end_of_line(options.add_carriage_return);
371 template <typename stream_it_t, typename seq_type>
372 void write_seq(stream_it_t & stream_it, sequence_file_output_options const & options, seq_type && seq)
374 auto char_sequence = seq | views::to_char;
376 if (options.fasta_letters_per_line > 0)
378 /* Using `views::interleave` is probably the way to go but that needs performance-tuning.*/
379 auto it = std::ranges::begin(char_sequence);
380 auto end = std::ranges::end(char_sequence);
384 /* Note: This solution is slightly suboptimal for sized but non-random-access ranges.*/
385 auto current_end = it;
386 size_t steps = std::ranges::advance(current_end, options.fasta_letters_per_line, end);
387 using subrange_t = std::ranges::subrange<decltype(it), decltype(it), std::ranges::subrange_kind::sized>;
388 it = stream_it.write_range(subrange_t{it, current_end, (options.fasta_letters_per_line - steps)});
389 stream_it.write_end_of_line(options.add_carriage_return);
394 stream_it.write_range(char_sequence);
395 stream_it.write_end_of_line(options.add_carriage_return);
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.
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.
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.
Provides seqan3::sequence_file_output_options.
The options type defines various option members that influence the behaviour of all or some formats.
Definition: output_options.hpp:26
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.