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// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2006-2023, Knut Reinert & Freie Universität Berlin
3// Copyright (c) 2016-2023, Knut Reinert & MPI für molekulare Genetik
4// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5// shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6// -----------------------------------------------------------------------------------------------------
7
13#pragma once
14
15#include <algorithm>
16#include <iterator>
17#include <ranges>
18#include <string>
19#include <string_view>
20#include <vector>
21
42
43namespace seqan3
44{
45
80{
81public:
85 format_fasta() noexcept = default;
86 format_fasta(format_fasta const &) noexcept = default;
87 format_fasta & operator=(format_fasta const &) noexcept = default;
88 format_fasta(format_fasta &&) noexcept = default;
89 format_fasta & operator=(format_fasta &&) noexcept = default;
90 ~format_fasta() noexcept = default;
91
93
95 static inline std::vector<std::string> file_extensions{
96 {"fasta"},
97 {"fa"},
98 {"fna"},
99 {"ffn"},
100 {"faa"},
101 {"frn"},
102 {"fas"},
103 };
104
105protected:
107 template <typename stream_type, // constraints checked by file
108 typename legal_alph_type,
109 typename stream_pos_type,
110 typename seq_type, // other constraints checked inside function
111 typename id_type,
112 typename qual_type>
113 void read_sequence_record(stream_type & stream,
115 stream_pos_type & position_buffer,
116 seq_type & sequence,
117 id_type & id,
118 qual_type & SEQAN3_DOXYGEN_ONLY(qualities))
119 {
120 // Store current position in buffer
121 // Must happen before constructing the view.
122 // With libc++, tellg invalidates the I/O buffer.
123 position_buffer = stream.tellg();
124
125 auto stream_view = detail::istreambuf(stream);
126
127 // ID
128 read_id(stream_view, options, id);
129
130 // Sequence
131 read_seq(stream_view, options, sequence);
132 }
133
135 template <typename stream_type, // constraints checked by file
136 typename seq_type, // other constraints checked inside function
137 typename id_type,
138 typename qual_type>
139 void write_sequence_record(stream_type & stream,
140 sequence_file_output_options const & options,
141 seq_type && sequence,
142 id_type && id,
143 qual_type && SEQAN3_DOXYGEN_ONLY(qualities))
144 {
145 seqan3::detail::fast_ostreambuf_iterator stream_it{*stream.rdbuf()};
146
147 // ID
148 if constexpr (detail::decays_to_ignore_v<id_type>)
149 {
150 throw std::logic_error{"The ID field may not be set to ignore when writing FASTA files."};
151 }
152 else
153 {
154 if (std::ranges::empty(id)) //[[unlikely]]
155 throw std::runtime_error{"The ID field may not be empty when writing FASTA files."};
156
157 write_id(stream_it, options, id);
158 }
159
160 // Sequence
161 if constexpr (detail::decays_to_ignore_v<seq_type>) // sequence
162 {
163 throw std::logic_error{
164 "The SEQ and SEQ_QUAL fields may not both be set to ignore when writing FASTA files."};
165 }
166 else
167 {
168 if (std::ranges::empty(sequence)) //[[unlikely]]
169 throw std::runtime_error{"The SEQ field may not be empty when writing FASTA files."};
170
171 write_seq(stream_it, options, sequence);
172 }
173 }
174
175private:
178 template <typename stream_view_t, typename seq_legal_alph_type, typename id_type>
179 void
180 read_id(stream_view_t & stream_view, sequence_file_input_options<seq_legal_alph_type> const & options, id_type & id)
181 {
182 auto const is_id = is_char<'>'> || is_char<';'>;
183
184 if (!is_id(*begin(stream_view)))
185 throw parse_error{std::string{"Expected to be on beginning of ID, but "} + is_id.msg
186 + " evaluated to false on " + detail::make_printable(*begin(stream_view))};
187
188 if constexpr (detail::decays_to_ignore_v<id_type>) // Skip the ID, it is not requested by the user.
189 {
190 detail::consume(stream_view | detail::take_line_or_throw);
191 }
192 else // read ID
193 {
194 if (options.truncate_ids)
195 {
196#if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
197 auto it = stream_view.begin();
198 auto e = stream_view.end();
199 ++it; // already checked `is_id`
200
202 {
203 for (; (it != e) && (is_blank)(*it); ++it) // skip leading ' '
204 {}
205 }
206
207 bool at_delimiter = false;
208 for (; it != e; ++it)
209 {
210 if ((is_cntrl || is_blank)(*it))
211 {
212 at_delimiter = true;
213 break;
214 }
215 id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
216 }
217
218 if (!at_delimiter)
219 throw unexpected_end_of_input{"FASTA ID line did not end in newline."};
220
221 for (; (it != e) && ((!is_char<'\n'>)(*it)); ++it)
222 {}
223
224#else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
226 {
227 std::ranges::copy(
228 stream_view | std::views::drop(1) // skip leading '>' or ';'
229 | std::views::drop_while(is_blank) // skip leading ' '
230 | detail::take_until_or_throw(is_cntrl || is_blank) // read ID until delimiter…
231 | views::char_to<std::ranges::range_value_t<id_type>>,
232 std::back_inserter(id)); // … ^A is old delimiter
233 }
234 else
235 {
236 std::ranges::copy(
237 stream_view | std::views::drop(1) // skip leading '>' or ';'
238 | detail::take_until_or_throw(is_cntrl || is_blank) // read ID until delimiter…
239 | views::char_to<std::ranges::range_value_t<id_type>>,
240 std::back_inserter(id)); // … ^A is old delimiter
241 }
242
243 // consume rest of line
244 detail::consume(stream_view | detail::take_line_or_throw);
245#endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
246 }
247 else // options.truncate_ids
248 {
249#if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
250 auto it = stream_view.begin();
251 auto e = stream_view.end();
252 ++it; // skip leading '>' or ';'
253
255 {
256 for (; (it != e) && (is_blank)(*it); ++it) // skip leading ' '
257 {}
258 }
259
260 bool at_delimiter = false;
261 for (; it != e; ++it)
262 {
263 if ((is_char<'\n'>)(*it))
264 {
265 at_delimiter = true;
266 break;
267 }
268 id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
269 }
270
271 if (!at_delimiter)
272 throw unexpected_end_of_input{"FASTA ID line did not end in newline."};
273
274#else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
276 {
277 std::ranges::copy(stream_view | detail::take_line_or_throw // read line
278 | std::views::drop(1) // skip leading '>' or ';'
279 | std::views::drop_while(is_blank) // skip leading ' '
280 | views::char_to<std::ranges::range_value_t<id_type>>,
282 }
283 else
284 {
285 std::ranges::copy(stream_view | detail::take_line_or_throw // read line
286 | std::views::drop(1) // skip leading '>' or ';'
287 | views::char_to<std::ranges::range_value_t<id_type>>,
289 }
290#endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
291 }
292 }
293 }
294
296 template <typename stream_view_t, typename seq_legal_alph_type, typename seq_type>
297 void read_seq(stream_view_t & stream_view, sequence_file_input_options<seq_legal_alph_type> const &, seq_type & seq)
298 {
299 constexpr auto is_id = is_char<'>'> || is_char<';'>;
300
301 if constexpr (!detail::decays_to_ignore_v<seq_type>)
302 {
303 constexpr auto is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
304
305#if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
306 auto it = stream_view.begin();
307 auto e = stream_view.end();
308
309 if (it == e)
310 throw unexpected_end_of_input{"No sequence information given!"};
311
312 for (; (it != e) && ((!is_id)(*it)); ++it)
313 {
314 if ((is_space || is_digit)(*it))
315 {
316 continue;
317 }
318 else if (is_legal_alph(*it))
319 {
320 seq.push_back(assign_char_to(*it, std::ranges::range_value_t<seq_type>{}));
321 }
322 else
323 {
324
325 throw parse_error{std::string{"Encountered an unexpected letter: "} + "char_is_valid_for<"
326 + detail::type_name_as_string<seq_legal_alph_type>
327 + "> evaluated to false on " + detail::make_printable(*it)};
328 }
329 }
330
331#else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
332
333 if (std::ranges::begin(stream_view) == std::ranges::end(stream_view))
334 throw unexpected_end_of_input{"No sequence information given!"};
335
336 std::ranges::copy(
337 stream_view | detail::take_until(is_id) // until next header (or end)
338 | std::views::filter(!(is_space || is_digit)) // ignore whitespace and numbers
339 | std::views::transform(
340 [is_legal_alph](char const c)
341 {
342 if (!is_legal_alph(c))
343 {
344 throw parse_error{std::string{"Encountered an unexpected letter: "}
345 + "char_is_valid_for<"
346 + detail::type_name_as_string<seq_legal_alph_type>
347 + "> evaluated to false on " + detail::make_printable(c)};
348 }
349 return c;
350 }) // enforce legal alphabet
351 | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
353#endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
354 }
355 else
356 {
357 detail::consume(stream_view | detail::take_until(is_id));
358 }
359 }
360
362 template <typename stream_it_t, typename id_type>
363 void write_id(stream_it_t & stream_it, sequence_file_output_options const & options, id_type && id)
364 {
365 if (options.fasta_legacy_id_marker)
366 stream_it = ';';
367 else
368 stream_it = '>';
369
370 if (options.fasta_blank_before_id)
371 stream_it = ' ';
372
373 stream_it.write_range(id);
374 stream_it.write_end_of_line(options.add_carriage_return);
375 }
376
378 template <typename stream_it_t, typename seq_type>
379 void write_seq(stream_it_t & stream_it, sequence_file_output_options const & options, seq_type && seq)
380 {
381 auto char_sequence = seq | views::to_char;
382
383 if (options.fasta_letters_per_line > 0)
384 {
385 /* Using `views::interleave` is probably the way to go but that needs performance-tuning.*/
386 auto it = std::ranges::begin(char_sequence);
387 auto end = std::ranges::end(char_sequence);
388
389 while (it != end)
390 {
391 /* Note: This solution is slightly suboptimal for sized but non-random-access ranges.*/
392 auto current_end = it;
393 size_t steps = std::ranges::advance(current_end, options.fasta_letters_per_line, end);
394 using subrange_t = std::ranges::subrange<decltype(it), decltype(it), std::ranges::subrange_kind::sized>;
395 it = stream_it.write_range(subrange_t{it, current_end, (options.fasta_letters_per_line - steps)});
396 stream_it.write_end_of_line(options.add_carriage_return);
397 }
398 }
399 else
400 {
401 stream_it.write_range(char_sequence);
402 stream_it.write_end_of_line(options.add_carriage_return);
403 }
404 }
405};
406
407} // namespace seqan3
Provides aliases for qualified.
Core alphabet concept and free function/type trait wrappers.
T back_inserter(T... args)
Provides alphabet adaptations for standard char types.
Provides seqan3::views::char_to.
The FASTA format.
Definition format_fasta.hpp:80
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:113
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:95
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:139
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:63
auto const char_to
A view over an alphabet, given a range of characters.
Definition char_to.hpp:67
constexpr auto assign_char_to
Assign a character to an alphabet object.
Definition alphabet/concept.hpp:524
@ 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:142
constexpr auto is_digit
Checks whether c is a digital character.
Definition predicate.hpp:262
constexpr auto is_char
Checks whether a given letter is the same as the template non-type argument.
Definition predicate.hpp:63
constexpr auto is_space
Checks whether c is a space character.
Definition predicate.hpp:125
constexpr auto is_cntrl
Checks whether c is a control character.
Definition predicate.hpp:90
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.
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:48
The options type defines various option members that influence the behaviour of all or some formats.
Definition sequence_file/input_options.hpp:27
bool fasta_ignore_blanks_before_id
Remove spaces after ">" (or ";") before the actual ID.
Definition sequence_file/input_options.hpp:33
bool truncate_ids
Read the ID string only up until the first whitespace character.
Definition sequence_file/input_options.hpp:29
The options type defines various option members that influence the behaviour of all or some formats.
Definition sequence_file/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.