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 ++it; // already checked `is_id`
199 for (; (it != e) && (is_blank)(*it); ++it)
202 bool at_delimiter = false;
203 for (; it != e; ++it)
205 if ((is_cntrl || is_blank)(*it))
210 id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
214 throw unexpected_end_of_input{"FASTA ID line did not end in newline."};
216 for (; (it != e) && ((!is_char<'\n
'>)(*it)); ++it)
219 #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
221 std::ranges::copy(stream_view | std::views::drop(1) // skip leading '>
' or ';
'
222 | std::views::drop_while(is_blank) // skip leading ' '
223 | detail::take_until_or_throw(is_cntrl || is_blank) // read ID until delimiter…
224 | views::char_to<std::ranges::range_value_t<id_type>>,
225 std::cpp20::back_inserter(id)); // … ^A is old delimiter
227 // consume rest of line
228 detail::consume(stream_view | detail::take_line_or_throw);
229 #endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
234 #if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
235 auto it = stream_view.begin();
236 auto e = stream_view.end();
237 ++it; // skip leading '>
' or ';
'
239 for (; (it != e) && (is_blank)(*it); ++it) // skip leading ' '
242 bool at_delimiter = false;
243 for (; it != e; ++it)
245 if ((is_char<'\n
'>)(*it))
250 id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
254 throw unexpected_end_of_input{"FASTA ID line did not end in newline."};
256 #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
258 std::ranges::copy(stream_view | detail::take_line_or_throw // read line
259 | std::views::drop(1) // skip leading '>
' or ';
'
260 | std::views::drop_while(is_blank) // skip leading ' '
261 | views::char_to<std::ranges::range_value_t<id_type>>,
262 std::cpp20::back_inserter(id));
263 #endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
268 detail::consume(stream_view | detail::take_line_or_throw);
273 template <typename stream_view_t,
274 typename seq_legal_alph_type,
276 void read_seq(stream_view_t & stream_view,
277 sequence_file_input_options<seq_legal_alph_type> const &,
280 auto constexpr is_id = is_char<'>
'> || is_char<';
'>;
282 if constexpr (!detail::decays_to_ignore_v<seq_type>)
284 auto constexpr is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
286 #if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
287 auto it = stream_view.begin();
288 auto e = stream_view.end();
291 throw unexpected_end_of_input{"No sequence information given!"};
293 for (; (it != e) && ((!is_id)(*it)); ++it)
295 if ((is_space || is_digit)(*it))
297 else if (!is_legal_alph(*it))
299 throw parse_error{std::string{"Encountered an unexpected letter: "} +
300 "char_is_valid_for<" +
301 detail::type_name_as_string<seq_legal_alph_type> +
302 "> evaluated to false on " +
303 detail::make_printable(*it)};
306 seq.push_back(assign_char_to(*it, std::ranges::range_value_t<seq_type>{}));
309 #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
311 if (std::ranges::begin(stream_view) == std::ranges::end(stream_view))
312 throw unexpected_end_of_input{"No sequence information given!"};
314 std::ranges::copy(stream_view | detail::take_until(is_id) // until next header (or end)
315 | std::views::filter(!(is_space || is_digit))// ignore whitespace and numbers
316 | std::views::transform([is_legal_alph] (char const c)
318 if (!is_legal_alph(c))
320 throw parse_error{std::string{"Encountered an unexpected letter: "} +
321 "char_is_valid_for<" +
322 detail::type_name_as_string<seq_legal_alph_type> +
323 "> evaluated to false on " +
324 detail::make_printable(c)};
327 }) // enforce legal alphabet
328 | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
329 std::cpp20::back_inserter(seq));
330 #endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
334 detail::consume(stream_view | detail::take_until(is_id));
339 template <typename stream_it_t, typename id_type>
340 void write_id(stream_it_t & stream_it, sequence_file_output_options const & options, id_type && id)
342 if (options.fasta_legacy_id_marker)
347 if (options.fasta_blank_before_id)
350 stream_it.write_range(id);
351 stream_it.write_end_of_line(options.add_carriage_return);
355 template <typename stream_it_t, typename seq_type>
356 void write_seq(stream_it_t & stream_it, sequence_file_output_options const & options, seq_type && seq)
358 auto char_sequence = seq | views::to_char;
360 if (options.fasta_letters_per_line > 0)
362 /* Using `views::interleave` is probably the way to go but that needs performance-tuning.*/
363 auto it = std::ranges::begin(char_sequence);
364 auto end = std::ranges::end(char_sequence);
368 /* Note: This solution is slightly suboptimal for sized but non-random-access ranges.*/
369 auto current_end = it;
370 size_t steps = std::ranges::advance(current_end, options.fasta_letters_per_line, end);
371 using subrange_t = std::ranges::subrange<decltype(it), decltype(it), std::ranges::subrange_kind::sized>;
372 it = stream_it.write_range(subrange_t{it, current_end, (options.fasta_letters_per_line - steps)});
373 stream_it.write_end_of_line(options.add_carriage_return);
378 stream_it.write_range(char_sequence);
379 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.