108 template <
typename stream_type,
109 typename legal_alph_type,
110 typename stream_pos_type,
116 stream_pos_type & position_buffer,
119 qual_type & SEQAN3_DOXYGEN_ONLY(qualities))
124 position_buffer = stream.tellg();
127 read_id(stream_view, options,
id);
134 template <
typename stream_type,
142 qual_type && SEQAN3_DOXYGEN_ONLY(qualities))
147 if constexpr (detail::decays_to_ignore_v<id_type>)
149 throw std::logic_error{
"The ID field may not be set to ignore when writing FASTA files."};
153 if (std::ranges::empty(
id))
160 if constexpr (detail::decays_to_ignore_v<seq_type>)
162 throw std::logic_error{
"The SEQ and SEQ_QUAL fields may not both be set to ignore when writing FASTA files."};
176 template <
typename stream_view_t,
177 typename seq_legal_alph_type,
183 auto const is_id = is_char<'>
'> || is_char<';
'>;
185 if (!is_id(*begin(stream_view)))
186 throw parse_error{std::string{"Expected to be on beginning of ID, but "} + is_id.msg +
187 " evaluated to false on " + detail::make_printable(*begin(stream_view))};
189 if constexpr (detail::decays_to_ignore_v<id_type>) // Skip the ID, it is not requested by the user.
191 detail::consume(stream_view | detail::take_line_or_throw);
195 if (options.truncate_ids)
197 #if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
198 auto it = stream_view.begin();
199 auto e = stream_view.end();
200 ++it; // already checked `is_id`
202 if (options.fasta_ignore_blanks_before_id)
204 for (; (it != e) && (is_blank)(*it); ++it) // skip leading ' '
208 bool at_delimiter = false;
209 for (; it != e; ++it)
211 if ((is_cntrl || is_blank)(*it))
216 id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
220 throw unexpected_end_of_input{"FASTA ID line did not end in newline."};
222 for (; (it != e) && ((!is_char<'\n
'>)(*it)); ++it)
225 #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
226 if (options.fasta_ignore_blanks_before_id)
228 std::ranges::copy(stream_view | std::views::drop(1) // skip leading '>
' or ';
'
229 | std::views::drop_while(is_blank) // skip leading ' '
230 | detail::take_until_or_throw(is_cntrl || is_blank) // read ID until delimiter…
231 | views::char_to<std::ranges::range_value_t<id_type>>,
232 std::back_inserter(id)); // … ^A is old delimiter
236 std::ranges::copy(stream_view | std::views::drop(1) // skip leading '>
' or ';
'
237 | detail::take_until_or_throw(is_cntrl || is_blank) // read ID until delimiter…
238 | views::char_to<std::ranges::range_value_t<id_type>>,
239 std::back_inserter(id)); // … ^A is old delimiter
242 // consume rest of line
243 detail::consume(stream_view | detail::take_line_or_throw);
244 #endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
247 else // options.truncate_ids
249 #if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
250 auto it = stream_view.begin();
251 auto e = stream_view.end();
252 ++it; // skip leading '>
' or ';
'
254 if (options.fasta_ignore_blanks_before_id)
256 for (; (it != e) && (is_blank)(*it); ++it) // skip leading ' '
260 bool at_delimiter = false;
261 for (; it != e; ++it)
263 if ((is_char<'\n
'>)(*it))
268 id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
272 throw unexpected_end_of_input{"FASTA ID line did not end in newline."};
274 #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
275 if (options.fasta_ignore_blanks_before_id)
277 std::ranges::copy(stream_view | detail::take_line_or_throw // read line
278 | std::views::drop(1) // skip leading '>
' or ';
'
279 | std::views::drop_while(is_blank) // skip leading ' '
280 | views::char_to<std::ranges::range_value_t<id_type>>,
281 std::back_inserter(id));
285 std::ranges::copy(stream_view | detail::take_line_or_throw // read line
286 | std::views::drop(1) // skip leading '>
' or ';
'
287 | views::char_to<std::ranges::range_value_t<id_type>>,
288 std::back_inserter(id));
290 #endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
296 template <typename stream_view_t,
297 typename seq_legal_alph_type,
299 void read_seq(stream_view_t & stream_view,
300 sequence_file_input_options<seq_legal_alph_type> const &,
303 auto constexpr is_id = is_char<'>
'> || is_char<';
'>;
305 if constexpr (!detail::decays_to_ignore_v<seq_type>)
307 auto constexpr is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
309 #if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
310 auto it = stream_view.begin();
311 auto e = stream_view.end();
314 throw unexpected_end_of_input{"No sequence information given!"};
316 for (; (it != e) && ((!is_id)(*it)); ++it)
318 if ((is_space || is_digit)(*it))
320 else if (!is_legal_alph(*it))
322 throw parse_error{std::string{"Encountered an unexpected letter: "} +
323 "char_is_valid_for<" +
324 detail::type_name_as_string<seq_legal_alph_type> +
325 "> evaluated to false on " +
326 detail::make_printable(*it)};
329 seq.push_back(assign_char_to(*it, std::ranges::range_value_t<seq_type>{}));
332 #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
334 if (std::ranges::begin(stream_view) == std::ranges::end(stream_view))
335 throw unexpected_end_of_input{"No sequence information given!"};
337 std::ranges::copy(stream_view | detail::take_until(is_id) // until next header (or end)
338 | std::views::filter(!(is_space || is_digit))// ignore whitespace and numbers
339 | std::views::transform([is_legal_alph] (char const c)
341 if (!is_legal_alph(c))
343 throw parse_error{std::string{"Encountered an unexpected letter: "} +
344 "char_is_valid_for<" +
345 detail::type_name_as_string<seq_legal_alph_type> +
346 "> evaluated to false on " +
347 detail::make_printable(c)};
350 }) // enforce legal alphabet
351 | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
352 std::back_inserter(seq));
353 #endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
357 detail::consume(stream_view | detail::take_until(is_id));
362 template <typename stream_it_t, typename id_type>
363 void write_id(stream_it_t & stream_it, sequence_file_output_options const & options, id_type && id)
365 if (options.fasta_legacy_id_marker)
370 if (options.fasta_blank_before_id)
373 stream_it.write_range(id);
374 stream_it.write_end_of_line(options.add_carriage_return);
378 template <typename stream_it_t, typename seq_type>
379 void write_seq(stream_it_t & stream_it, sequence_file_output_options const & options, seq_type && seq)
381 auto char_sequence = seq | views::to_char;
383 if (options.fasta_letters_per_line > 0)
385 /* Using `views::interleave` is probably the way to go but that needs performance-tuning.*/
386 auto it = std::ranges::begin(char_sequence);
387 auto end = std::ranges::end(char_sequence);
391 /* Note: This solution is slightly suboptimal for sized but non-random-access ranges.*/
392 auto current_end = it;
393 size_t steps = std::ranges::advance(current_end, options.fasta_letters_per_line, end);
394 using subrange_t = std::ranges::subrange<decltype(it), decltype(it), std::ranges::subrange_kind::sized>;
395 it = stream_it.write_range(subrange_t{it, current_end, (options.fasta_letters_per_line - steps)});
396 stream_it.write_end_of_line(options.add_carriage_return);
401 stream_it.write_range(char_sequence);
402 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.
Functionally the same as std::ostreambuf_iterator, but offers writing a range more efficiently.
Definition: fast_ostreambuf_iterator.hpp:39
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.
constexpr auto istreambuf
A view factory that returns a view over the stream buffer of an input stream.
Definition: istreambuf_view.hpp:110
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_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.