SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
format_fasta.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <algorithm>
13#include <iterator>
14#include <ranges>
15#include <string>
16#include <string_view>
17#include <vector>
18
39
40namespace seqan3
41{
42
77{
78public:
88
90
93 {"fasta"},
94 {"fa"},
95 {"fna"},
96 {"ffn"},
97 {"faa"},
98 {"frn"},
99 {"fas"},
100 };
101
102protected:
104 template <typename stream_type, // constraints checked by file
105 typename legal_alph_type,
106 typename stream_pos_type,
107 typename seq_type, // other constraints checked inside function
108 typename id_type,
109 typename qual_type>
112 stream_pos_type & position_buffer,
113 seq_type & sequence,
114 id_type & id,
116 {
117 // Store current position in buffer
118 // Must happen before constructing the view.
119 // With libc++, tellg invalidates the I/O buffer.
120 position_buffer = stream.tellg();
121
122 auto stream_view = detail::istreambuf(stream);
123
124 // ID
125 read_id(stream_view, options, id);
126
127 // Sequence
128 read_seq(stream_view, options, sequence);
129 }
130
132 template <typename stream_type, // constraints checked by file
133 typename seq_type, // other constraints checked inside function
134 typename id_type,
135 typename qual_type>
137 sequence_file_output_options const & options,
138 seq_type && sequence,
139 id_type && id,
141 {
142 seqan3::detail::fast_ostreambuf_iterator stream_it{*stream.rdbuf()};
143
144 // ID
145 if constexpr (detail::decays_to_ignore_v<id_type>)
146 {
147 throw std::logic_error{"The ID field may not be set to ignore when writing FASTA files."};
148 }
149 else
150 {
151 if (std::ranges::empty(id)) //[[unlikely]]
152 throw std::runtime_error{"The ID field may not be empty when writing FASTA files."};
153
154 write_id(stream_it, options, id);
155 }
156
157 // Sequence
158 if constexpr (detail::decays_to_ignore_v<seq_type>) // sequence
159 {
160 throw std::logic_error{
161 "The SEQ and SEQ_QUAL fields may not both be set to ignore when writing FASTA files."};
162 }
163 else
164 {
165 if (std::ranges::empty(sequence)) //[[unlikely]]
166 throw std::runtime_error{"The SEQ field may not be empty when writing FASTA files."};
167
168 write_seq(stream_it, options, sequence);
169 }
170 }
171
172private:
175 template <typename stream_view_t, typename seq_legal_alph_type, typename id_type>
176 void
177 read_id(stream_view_t & stream_view, sequence_file_input_options<seq_legal_alph_type> const & options, id_type & id)
178 {
179 auto const is_id = is_char<'>'> || is_char<';'>;
180
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))};
184
185 if constexpr (detail::decays_to_ignore_v<id_type>) // Skip the ID, it is not requested by the user.
186 {
187 detail::consume(stream_view | detail::take_line_or_throw);
188 }
189 else // read ID
190 {
191 if (options.truncate_ids)
192 {
193#if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
194 auto it = stream_view.begin();
195 auto e = stream_view.end();
196 ++it; // already checked `is_id`
197
198 if (options.fasta_ignore_blanks_before_id)
199 {
200 for (; (it != e) && (is_blank)(*it); ++it) // skip leading ' '
201 {}
202 }
203
204 bool at_delimiter = false;
205 for (; it != e; ++it)
206 {
207 if ((is_cntrl || is_blank)(*it))
208 {
209 at_delimiter = true;
210 break;
211 }
212 id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
213 }
214
215 if (!at_delimiter)
216 throw unexpected_end_of_input{"FASTA ID line did not end in newline."};
217
218 for (; (it != e) && ((!is_char<'\n'>)(*it)); ++it)
219 {}
220
221#else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
222 if (options.fasta_ignore_blanks_before_id)
223 {
225 stream_view | std::views::drop(1) // skip leading '>' or ';'
226 | std::views::drop_while(is_blank) // skip leading ' '
227 | detail::take_until_or_throw(is_cntrl || is_blank) // read ID until delimiter…
228 | views::char_to<std::ranges::range_value_t<id_type>>,
229 std::back_inserter(id)); // … ^A is old delimiter
230 }
231 else
232 {
234 stream_view | std::views::drop(1) // skip leading '>' or ';'
235 | detail::take_until_or_throw(is_cntrl || is_blank) // read ID until delimiter…
236 | views::char_to<std::ranges::range_value_t<id_type>>,
237 std::back_inserter(id)); // … ^A is old delimiter
238 }
239
240 // consume rest of line
241 detail::consume(stream_view | detail::take_line_or_throw);
242#endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
243 }
244 else // options.truncate_ids
245 {
246#if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
247 auto it = stream_view.begin();
248 auto e = stream_view.end();
249 ++it; // skip leading '>' or ';'
250
251 if (options.fasta_ignore_blanks_before_id)
252 {
253 for (; (it != e) && (is_blank)(*it); ++it) // skip leading ' '
254 {}
255 }
256
257 bool at_delimiter = false;
258 for (; it != e; ++it)
259 {
260 if ((is_char<'\n'>)(*it))
261 {
262 at_delimiter = true;
263 break;
264 }
265 id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
266 }
267
268 if (!at_delimiter)
269 throw unexpected_end_of_input{"FASTA ID line did not end in newline."};
270
271#else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
272 if (options.fasta_ignore_blanks_before_id)
273 {
274 std::ranges::copy(stream_view | detail::take_line_or_throw // read line
275 | std::views::drop(1) // skip leading '>' or ';'
276 | std::views::drop_while(is_blank) // skip leading ' '
277 | views::char_to<std::ranges::range_value_t<id_type>>,
279 }
280 else
281 {
282 std::ranges::copy(stream_view | detail::take_line_or_throw // read line
283 | std::views::drop(1) // skip leading '>' or ';'
284 | views::char_to<std::ranges::range_value_t<id_type>>,
286 }
287#endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
288 }
289 }
290 }
291
293 template <typename stream_view_t, typename seq_legal_alph_type, typename seq_type>
294 void read_seq(stream_view_t & stream_view, sequence_file_input_options<seq_legal_alph_type> const &, seq_type & seq)
295 {
296 constexpr auto is_id = is_char<'>'> || is_char<';'>;
297
298 if constexpr (!detail::decays_to_ignore_v<seq_type>)
299 {
300 constexpr auto is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
301
302#if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
303 auto it = stream_view.begin();
304 auto e = stream_view.end();
305
306 if (it == e)
307 throw unexpected_end_of_input{"No sequence information given!"};
308
309 for (; (it != e) && ((!is_id)(*it)); ++it)
310 {
311 if ((is_space || is_digit)(*it))
312 {
313 continue;
314 }
315 else if (is_legal_alph(*it))
316 {
317 seq.push_back(assign_char_to(*it, std::ranges::range_value_t<seq_type>{}));
318 }
319 else
320 {
321
322 throw parse_error{std::string{"Encountered an unexpected letter: "} + "char_is_valid_for<"
323 + detail::type_name_as_string<seq_legal_alph_type>
324 + "> evaluated to false on " + detail::make_printable(*it)};
325 }
326 }
327
328#else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
329
330 if (std::ranges::begin(stream_view) == std::ranges::end(stream_view))
331 throw unexpected_end_of_input{"No sequence information given!"};
332
334 stream_view | detail::take_until(is_id) // until next header (or end)
335 | std::views::filter(!(is_space || is_digit)) // ignore whitespace and numbers
336 | std::views::transform(
337 [is_legal_alph](char const c)
338 {
339 if (!is_legal_alph(c))
340 {
341 throw parse_error{std::string{"Encountered an unexpected letter: "}
342 + "char_is_valid_for<"
343 + detail::type_name_as_string<seq_legal_alph_type>
344 + "> evaluated to false on " + detail::make_printable(c)};
345 }
346 return c;
347 }) // enforce legal alphabet
348 | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
350#endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
351 }
352 else
353 {
354 detail::consume(stream_view | detail::take_until(is_id));
355 }
356 }
357
359 template <typename stream_it_t, typename id_type>
360 void write_id(stream_it_t & stream_it, sequence_file_output_options const & options, id_type && id)
361 {
362 if (options.fasta_legacy_id_marker)
363 stream_it = ';';
364 else
365 stream_it = '>';
366
367 if (options.fasta_blank_before_id)
368 stream_it = ' ';
369
370 stream_it.write_range(id);
371 stream_it.write_end_of_line(options.add_carriage_return);
372 }
373
375 template <typename stream_it_t, typename seq_type>
376 void write_seq(stream_it_t & stream_it, sequence_file_output_options const & options, seq_type && seq)
377 {
378 auto char_sequence = seq | views::to_char;
379
380 if (options.fasta_letters_per_line > 0)
381 {
382 /* Using `views::interleave` is probably the way to go but that needs performance-tuning.*/
383 auto it = std::ranges::begin(char_sequence);
384 auto end = std::ranges::end(char_sequence);
385
386 while (it != end)
387 {
388 /* Note: This solution is slightly suboptimal for sized but non-random-access ranges.*/
389 auto current_end = it;
390 size_t steps = std::ranges::advance(current_end, options.fasta_letters_per_line, end);
391 using subrange_t = std::ranges::subrange<decltype(it), decltype(it), std::ranges::subrange_kind::sized>;
392 it = stream_it.write_range(subrange_t{it, current_end, (options.fasta_letters_per_line - steps)});
393 stream_it.write_end_of_line(options.add_carriage_return);
394 }
395 }
396 else
397 {
398 stream_it.write_range(char_sequence);
399 stream_it.write_end_of_line(options.add_carriage_return);
400 }
401 }
402};
403
404} // namespace seqan3
Provides aliases for qualified.
Core alphabet concept and free function/type trait wrappers.
T back_inserter(T... args)
T begin(T... args)
Provides alphabet adaptations for standard char types.
Provides seqan3::views::char_to.
A "pretty printer" for most SeqAn data structures and related types.
Definition debug_stream_type.hpp:79
The FASTA format.
Definition format_fasta.hpp:77
void read_sequence_record(stream_type &stream, sequence_file_input_options< legal_alph_type > const &options, stream_pos_type &position_buffer, seq_type &sequence, id_type &id, qual_type &qualities)
Read from the specified stream and back-insert into the given field buffers.
Definition format_fasta.hpp:110
static std::vector< std::string > file_extensions
The valid file extensions for this format; note that you can modify this value.
Definition format_fasta.hpp:92
format_fasta() noexcept=default
Defaulted.
void write_sequence_record(stream_type &stream, sequence_file_output_options const &options, seq_type &&sequence, id_type &&id, qual_type &&qualities)
Write the given fields to the specified stream.
Definition format_fasta.hpp:136
T copy(T... args)
Provides various utility functions.
Provides various transformation traits used by the range module.
Provides seqan3::dna5, container aliases and string literals.
T end(T... args)
Provides seqan3::detail::fast_ostreambuf_iterator.
auto const to_char
A view that calls seqan3::to_char() on each element in the input range.
Definition to_char.hpp:60
auto const char_to
A view over an alphabet, given a range of characters.
Definition char_to.hpp:64
constexpr auto assign_char_to
Assign a character to an alphabet object.
Definition alphabet/concept.hpp:517
@ seq
The "sequence", usually a range of nucleotides or amino acids.
constexpr auto is_blank
Checks whether c is a blank character.
Definition predicate.hpp:139
constexpr auto is_digit
Checks whether c is a digital character.
Definition predicate.hpp:259
constexpr auto is_char
Checks whether a given letter is the same as the template non-type argument.
Definition predicate.hpp:60
constexpr auto is_space
Checks whether c is a space character.
Definition predicate.hpp:122
constexpr auto is_cntrl
Checks whether c is a control character.
Definition predicate.hpp:87
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:26
SeqAn specific customisations in the standard namespace.
Provides character predicates for tokenisation.
Provides seqan3::sequence_file_input_format and auxiliary classes.
Provides seqan3::sequence_file_input_options.
Provides seqan3::sequence_file_output_format and auxiliary classes.
Provides seqan3::sequence_file_output_options.
Thrown if there is a parse error, such as reading an unexpected character from an input stream.
Definition io/exception.hpp:45
The options type defines various option members that influence the behaviour of all or some formats.
Definition sequence_file/output_options.hpp:23
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.
Hide me