109 template <
typename stream_type,
110 typename legal_alph_type,
bool seq_qual_combined,
118 qual_type & SEQAN3_DOXYGEN_ONLY(qualities))
123 read_id(stream_view, options,
id);
126 read_seq(stream_view, options,
sequence);
130 template <
typename stream_type,
138 qual_type && SEQAN3_DOXYGEN_ONLY(qualities))
140 seqan3::detail::fast_ostreambuf_iterator stream_it{*stream.rdbuf()};
143 if constexpr (detail::decays_to_ignore_v<id_type>)
145 throw std::logic_error{
"The ID field may not be set to ignore when writing FASTA files."};
149 if (std::ranges::empty(
id))
152 write_id(stream_it, options,
id);
156 if constexpr (detail::decays_to_ignore_v<seq_type>)
158 throw std::logic_error{
"The SEQ and SEQ_QUAL fields may not both be set to ignore when writing FASTA files."};
165 write_seq(stream_it, options,
sequence);
172 template <
typename stream_view_t,
173 typename seq_legal_alph_type,
bool seq_qual_combined,
175 void read_id(stream_view_t & stream_view,
179 auto const is_id = is_char<'>
'> || is_char<';
'>;
181 if (!is_id(*begin(stream_view)))
182 throw parse_error{std::string{"Expected to be on beginning of ID, but "} + is_id.msg +
183 " evaluated to false on " + detail::make_printable(*begin(stream_view))};
186 if constexpr (!detail::decays_to_ignore_v<id_type>)
188 if (options.truncate_ids)
190 #if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
191 auto it = stream_view.begin();
192 auto e = stream_view.end();
193 for (; (it != e) && (is_id || is_blank)(*it); ++it)
196 bool at_delimiter = false;
197 for (; it != e; ++it)
199 if ((is_cntrl || is_blank)(*it))
204 id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
208 throw unexpected_end_of_input{"FastA ID line did not end in newline."};
210 for (; (it != e) && ((!is_char<'\n
'>)(*it)); ++it)
213 #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
215 std::ranges::copy(stream_view | std::views::drop_while(is_id || is_blank) // skip leading >
216 | views::take_until_or_throw(is_cntrl || is_blank) // read ID until delimiter…
217 | views::char_to<std::ranges::range_value_t<id_type>>,
218 std::cpp20::back_inserter(id)); // … ^A is old delimiter
220 // consume rest of line
221 detail::consume(stream_view | views::take_line_or_throw);
222 #endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
227 #if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
228 auto it = stream_view.begin();
229 auto e = stream_view.end();
230 for (; (it != e) && (is_id || is_blank)(*it); ++it)
233 bool at_delimiter = false;
234 for (; it != e; ++it)
236 if ((is_char<'\n
'>)(*it))
241 id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
245 throw unexpected_end_of_input{"FastA ID line did not end in newline."};
247 #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
249 std::ranges::copy(stream_view | views::take_line_or_throw // read line
250 | std::views::drop_while(is_id || is_blank) // skip leading >
251 | views::char_to<std::ranges::range_value_t<id_type>>,
252 std::cpp20::back_inserter(id));
253 #endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
258 detail::consume(stream_view | views::take_line_or_throw);
263 template <typename stream_view_t,
264 typename seq_legal_alph_type, bool seq_qual_combined,
266 void read_seq(stream_view_t & stream_view,
267 sequence_file_input_options<seq_legal_alph_type, seq_qual_combined> const &,
270 auto constexpr is_id = is_char<'>
'> || is_char<';
'>;
272 if constexpr (!detail::decays_to_ignore_v<seq_type>)
274 auto constexpr is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
276 #if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
277 auto it = stream_view.begin();
278 auto e = stream_view.end();
281 throw unexpected_end_of_input{"No sequence information given!"};
283 for (; (it != e) && ((!is_id)(*it)); ++it)
285 if ((is_space || is_digit)(*it))
287 else if (!is_legal_alph(*it))
289 throw parse_error{std::string{"Encountered an unexpected letter: "} +
290 "char_is_valid_for<" +
291 detail::type_name_as_string<seq_legal_alph_type> +
292 "> evaluated to false on " +
293 detail::make_printable(*it)};
296 seq.push_back(assign_char_to(*it, std::ranges::range_value_t<seq_type>{}));
299 #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
301 if (std::ranges::begin(stream_view) == std::ranges::end(stream_view))
302 throw unexpected_end_of_input{"No sequence information given!"};
304 std::ranges::copy(stream_view | views::take_until(is_id) // until next header (or end)
305 | std::views::filter(!(is_space || is_digit))// ignore whitespace and numbers
306 | std::views::transform([is_legal_alph] (char const c)
308 if (!is_legal_alph(c))
310 throw parse_error{std::string{"Encountered an unexpected letter: "} +
311 "char_is_valid_for<" +
312 detail::type_name_as_string<seq_legal_alph_type> +
313 "> evaluated to false on " +
314 detail::make_printable(c)};
317 }) // enforce legal alphabet
318 | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
319 std::cpp20::back_inserter(seq));
320 #endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
324 detail::consume(stream_view | views::take_until(is_id));
329 template <typename stream_it_t, typename id_type>
330 void write_id(stream_it_t & stream_it, sequence_file_output_options const & options, id_type && id)
332 if (options.fasta_legacy_id_marker)
337 if (options.fasta_blank_before_id)
340 stream_it.write_range(id);
341 stream_it.write_end_of_line(options.add_carriage_return);
345 template <typename stream_it_t, typename seq_type>
346 void write_seq(stream_it_t & stream_it, sequence_file_output_options const & options, seq_type && seq)
348 auto char_sequence = seq | views::to_char;
350 if (options.fasta_letters_per_line > 0)
352 /* Using `views::interleave` is probably the way to go but that needs performance-tuning.*/
353 auto it = std::ranges::begin(char_sequence);
354 auto end = std::ranges::end(char_sequence);
358 /* Note: This solution is slightly suboptimal for sized but non-random-access ranges.*/
359 auto current_end = it;
360 size_t steps = std::ranges::advance(current_end, options.fasta_letters_per_line, end);
361 using subrange_t = std::ranges::subrange<decltype(it), decltype(it), std::ranges::subrange_kind::sized>;
362 it = stream_it.write_range(subrange_t{it, current_end, (options.fasta_letters_per_line - steps)});
363 stream_it.write_end_of_line(options.add_carriage_return);
368 stream_it.write_range(char_sequence);
369 stream_it.write_end_of_line(options.add_carriage_return);
Adaptations of algorithms from the Ranges TS.
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 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.hpp:114
Provides seqan3::detail::ignore_output_iterator for writing to null stream.
The generic concept for a (biological) sequence.
Provides various utility functions.
Provides seqan3::views::istreambuf.
Provides seqan3::views::join.
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
SeqAn specific customisations in the standard namespace.
Provides various utility functions.
Adaptations of concepts from the Ranges TS.
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:22
Provides seqan3::views::take.
Provides seqan3::views::take_exactly and seqan3::views::take_exactly_or_throw.
Provides seqan3::views::take_line and seqan3::views::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.
Provides character predicates for tokenisation.