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))};
190 if constexpr (!detail::decays_to_ignore_v<id_type>)
192 if (options.truncate_ids)
194 #if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
195 auto it = stream_view.begin();
196 auto e = stream_view.end();
197 for (; (it != e) && (is_id || is_blank)(*it); ++it)
200 bool at_delimiter = false;
201 for (; it != e; ++it)
203 if ((is_cntrl || is_blank)(*it))
208 id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
212 throw unexpected_end_of_input{"FASTA ID line did not end in newline."};
214 for (; (it != e) && ((!is_char<'\n
'>)(*it)); ++it)
217 #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
219 std::ranges::copy(stream_view | std::views::drop_while(is_id || is_blank) // skip leading >
220 | detail::take_until_or_throw(is_cntrl || is_blank) // read ID until delimiter…
221 | views::char_to<std::ranges::range_value_t<id_type>>,
222 std::cpp20::back_inserter(id)); // … ^A is old delimiter
224 // consume rest of line
225 detail::consume(stream_view | detail::take_line_or_throw);
226 #endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
231 #if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
232 auto it = stream_view.begin();
233 auto e = stream_view.end();
234 for (; (it != e) && (is_id || is_blank)(*it); ++it)
237 bool at_delimiter = false;
238 for (; it != e; ++it)
240 if ((is_char<'\n
'>)(*it))
245 id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
249 throw unexpected_end_of_input{"FASTA ID line did not end in newline."};
251 #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
253 std::ranges::copy(stream_view | detail::take_line_or_throw // read line
254 | std::views::drop_while(is_id || is_blank) // skip leading >
255 | views::char_to<std::ranges::range_value_t<id_type>>,
256 std::cpp20::back_inserter(id));
257 #endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
262 detail::consume(stream_view | detail::take_line_or_throw);
267 template <typename stream_view_t,
268 typename seq_legal_alph_type,
270 void read_seq(stream_view_t & stream_view,
271 sequence_file_input_options<seq_legal_alph_type> const &,
274 auto constexpr is_id = is_char<'>
'> || is_char<';
'>;
276 if constexpr (!detail::decays_to_ignore_v<seq_type>)
278 auto constexpr is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
280 #if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
281 auto it = stream_view.begin();
282 auto e = stream_view.end();
285 throw unexpected_end_of_input{"No sequence information given!"};
287 for (; (it != e) && ((!is_id)(*it)); ++it)
289 if ((is_space || is_digit)(*it))
291 else if (!is_legal_alph(*it))
293 throw parse_error{std::string{"Encountered an unexpected letter: "} +
294 "char_is_valid_for<" +
295 detail::type_name_as_string<seq_legal_alph_type> +
296 "> evaluated to false on " +
297 detail::make_printable(*it)};
300 seq.push_back(assign_char_to(*it, std::ranges::range_value_t<seq_type>{}));
303 #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
305 if (std::ranges::begin(stream_view) == std::ranges::end(stream_view))
306 throw unexpected_end_of_input{"No sequence information given!"};
308 std::ranges::copy(stream_view | detail::take_until(is_id) // until next header (or end)
309 | std::views::filter(!(is_space || is_digit))// ignore whitespace and numbers
310 | std::views::transform([is_legal_alph] (char const c)
312 if (!is_legal_alph(c))
314 throw parse_error{std::string{"Encountered an unexpected letter: "} +
315 "char_is_valid_for<" +
316 detail::type_name_as_string<seq_legal_alph_type> +
317 "> evaluated to false on " +
318 detail::make_printable(c)};
321 }) // enforce legal alphabet
322 | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
323 std::cpp20::back_inserter(seq));
324 #endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
328 detail::consume(stream_view | detail::take_until(is_id));
333 template <typename stream_it_t, typename id_type>
334 void write_id(stream_it_t & stream_it, sequence_file_output_options const & options, id_type && id)
336 if (options.fasta_legacy_id_marker)
341 if (options.fasta_blank_before_id)
344 stream_it.write_range(id);
345 stream_it.write_end_of_line(options.add_carriage_return);
349 template <typename stream_it_t, typename seq_type>
350 void write_seq(stream_it_t & stream_it, sequence_file_output_options const & options, seq_type && seq)
352 auto char_sequence = seq | views::to_char;
354 if (options.fasta_letters_per_line > 0)
356 /* Using `views::interleave` is probably the way to go but that needs performance-tuning.*/
357 auto it = std::ranges::begin(char_sequence);
358 auto end = std::ranges::end(char_sequence);
362 /* Note: This solution is slightly suboptimal for sized but non-random-access ranges.*/
363 auto current_end = it;
364 size_t steps = std::ranges::advance(current_end, options.fasta_letters_per_line, end);
365 using subrange_t = std::ranges::subrange<decltype(it), decltype(it), std::ranges::subrange_kind::sized>;
366 it = stream_it.write_range(subrange_t{it, current_end, (options.fasta_letters_per_line - steps)});
367 stream_it.write_end_of_line(options.add_carriage_return);
372 stream_it.write_range(char_sequence);
373 stream_it.write_end_of_line(options.add_carriage_return);
The <algorithm> header from C++20's standard library.
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.