108 template <
typename stream_type,
109 typename legal_alph_type,
117 qual_type & SEQAN3_DOXYGEN_ONLY(qualities))
119 auto stream_view = detail::istreambuf(stream);
122 read_id(stream_view, options,
id);
125 read_seq(stream_view, options,
sequence);
129 template <
typename stream_type,
137 qual_type && SEQAN3_DOXYGEN_ONLY(qualities))
139 seqan3::detail::fast_ostreambuf_iterator stream_it{*stream.rdbuf()};
142 if constexpr (detail::decays_to_ignore_v<id_type>)
144 throw std::logic_error{
"The ID field may not be set to ignore when writing FASTA files."};
148 if (std::ranges::empty(
id))
151 write_id(stream_it, options,
id);
155 if constexpr (detail::decays_to_ignore_v<seq_type>)
157 throw std::logic_error{
"The SEQ and SEQ_QUAL fields may not both be set to ignore when writing FASTA files."};
164 write_seq(stream_it, options,
sequence);
171 template <
typename stream_view_t,
172 typename seq_legal_alph_type,
174 void read_id(stream_view_t & stream_view,
178 auto const is_id = is_char<'>
'> || is_char<';
'>;
180 if (!is_id(*begin(stream_view)))
181 throw parse_error{std::string{"Expected to be on beginning of ID, but "} + is_id.msg +
182 " evaluated to false on " + detail::make_printable(*begin(stream_view))};
185 if constexpr (!detail::decays_to_ignore_v<id_type>)
187 if (options.truncate_ids)
189 #if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
190 auto it = stream_view.begin();
191 auto e = stream_view.end();
192 for (; (it != e) && (is_id || is_blank)(*it); ++it)
195 bool at_delimiter = false;
196 for (; it != e; ++it)
198 if ((is_cntrl || is_blank)(*it))
203 id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
207 throw unexpected_end_of_input{"FastA ID line did not end in newline."};
209 for (; (it != e) && ((!is_char<'\n
'>)(*it)); ++it)
212 #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
214 std::ranges::copy(stream_view | std::views::drop_while(is_id || is_blank) // skip leading >
215 | detail::take_until_or_throw(is_cntrl || is_blank) // read ID until delimiter…
216 | views::char_to<std::ranges::range_value_t<id_type>>,
217 std::cpp20::back_inserter(id)); // … ^A is old delimiter
219 // consume rest of line
220 detail::consume(stream_view | detail::take_line_or_throw);
221 #endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
226 #if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
227 auto it = stream_view.begin();
228 auto e = stream_view.end();
229 for (; (it != e) && (is_id || is_blank)(*it); ++it)
232 bool at_delimiter = false;
233 for (; it != e; ++it)
235 if ((is_char<'\n
'>)(*it))
240 id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
244 throw unexpected_end_of_input{"FastA ID line did not end in newline."};
246 #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
248 std::ranges::copy(stream_view | detail::take_line_or_throw // read line
249 | std::views::drop_while(is_id || is_blank) // skip leading >
250 | views::char_to<std::ranges::range_value_t<id_type>>,
251 std::cpp20::back_inserter(id));
252 #endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
257 detail::consume(stream_view | detail::take_line_or_throw);
262 template <typename stream_view_t,
263 typename seq_legal_alph_type,
265 void read_seq(stream_view_t & stream_view,
266 sequence_file_input_options<seq_legal_alph_type> const &,
269 auto constexpr is_id = is_char<'>
'> || is_char<';
'>;
271 if constexpr (!detail::decays_to_ignore_v<seq_type>)
273 auto constexpr is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
275 #if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
276 auto it = stream_view.begin();
277 auto e = stream_view.end();
280 throw unexpected_end_of_input{"No sequence information given!"};
282 for (; (it != e) && ((!is_id)(*it)); ++it)
284 if ((is_space || is_digit)(*it))
286 else if (!is_legal_alph(*it))
288 throw parse_error{std::string{"Encountered an unexpected letter: "} +
289 "char_is_valid_for<" +
290 detail::type_name_as_string<seq_legal_alph_type> +
291 "> evaluated to false on " +
292 detail::make_printable(*it)};
295 seq.push_back(assign_char_to(*it, std::ranges::range_value_t<seq_type>{}));
298 #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
300 if (std::ranges::begin(stream_view) == std::ranges::end(stream_view))
301 throw unexpected_end_of_input{"No sequence information given!"};
303 std::ranges::copy(stream_view | detail::take_until(is_id) // until next header (or end)
304 | std::views::filter(!(is_space || is_digit))// ignore whitespace and numbers
305 | std::views::transform([is_legal_alph] (char const c)
307 if (!is_legal_alph(c))
309 throw parse_error{std::string{"Encountered an unexpected letter: "} +
310 "char_is_valid_for<" +
311 detail::type_name_as_string<seq_legal_alph_type> +
312 "> evaluated to false on " +
313 detail::make_printable(c)};
316 }) // enforce legal alphabet
317 | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
318 std::cpp20::back_inserter(seq));
319 #endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
323 detail::consume(stream_view | detail::take_until(is_id));
328 template <typename stream_it_t, typename id_type>
329 void write_id(stream_it_t & stream_it, sequence_file_output_options const & options, id_type && id)
331 if (options.fasta_legacy_id_marker)
336 if (options.fasta_blank_before_id)
339 stream_it.write_range(id);
340 stream_it.write_end_of_line(options.add_carriage_return);
344 template <typename stream_it_t, typename seq_type>
345 void write_seq(stream_it_t & stream_it, sequence_file_output_options const & options, seq_type && seq)
347 auto char_sequence = seq | views::to_char;
349 if (options.fasta_letters_per_line > 0)
351 /* Using `views::interleave` is probably the way to go but that needs performance-tuning.*/
352 auto it = std::ranges::begin(char_sequence);
353 auto end = std::ranges::end(char_sequence);
357 /* Note: This solution is slightly suboptimal for sized but non-random-access ranges.*/
358 auto current_end = it;
359 size_t steps = std::ranges::advance(current_end, options.fasta_letters_per_line, end);
360 using subrange_t = std::ranges::subrange<decltype(it), decltype(it), std::ranges::subrange_kind::sized>;
361 it = stream_it.write_range(subrange_t{it, current_end, (options.fasta_letters_per_line - steps)});
362 stream_it.write_end_of_line(options.add_carriage_return);
367 stream_it.write_range(char_sequence);
368 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.
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: cigar_operation_table.hpp:2
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.