SeqAn3 3.2.0-rc.1
The Modern C++ library for sequence analysis.
format_fasta.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2006-2021, Knut Reinert & Freie Universität Berlin
3// Copyright (c) 2016-2021, 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 <seqan3/std/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 {
97 { "fasta" },
98 { "fa" },
99 { "fna" },
100 { "ffn" },
101 { "faa" },
102 { "frn" },
103 { "fas" },
104 };
105
106protected:
108 template <typename stream_type, // constraints checked by file
109 typename legal_alph_type,
110 typename stream_pos_type,
111 typename seq_type, // other constraints checked inside function
112 typename id_type,
113 typename qual_type>
114 void read_sequence_record(stream_type & stream,
116 stream_pos_type & position_buffer,
117 seq_type & sequence,
118 id_type & id,
119 qual_type & SEQAN3_DOXYGEN_ONLY(qualities))
120 {
121 auto stream_view = detail::istreambuf(stream);
122
123 // Store current position in buffer
124 position_buffer = stream.tellg();
125
126 // ID
127 read_id(stream_view, options, id);
128
129 // Sequence
130 read_seq(stream_view, options, sequence);
131 }
132
134 template <typename stream_type, // constraints checked by file
135 typename seq_type, // other constraints checked inside function
136 typename id_type,
137 typename qual_type>
138 void write_sequence_record(stream_type & stream,
139 sequence_file_output_options const & options,
140 seq_type && sequence,
141 id_type && id,
142 qual_type && SEQAN3_DOXYGEN_ONLY(qualities))
143 {
144 seqan3::detail::fast_ostreambuf_iterator stream_it{*stream.rdbuf()};
145
146 // ID
147 if constexpr (detail::decays_to_ignore_v<id_type>)
148 {
149 throw std::logic_error{"The ID field may not be set to ignore when writing FASTA files."};
150 }
151 else
152 {
153 if (std::ranges::empty(id)) //[[unlikely]]
154 throw std::runtime_error{"The ID field may not be empty when writing FASTA files."};
155
156 write_id(stream_it, options, id);
157 }
158
159 // Sequence
160 if constexpr (detail::decays_to_ignore_v<seq_type>) // sequence
161 {
162 throw std::logic_error{"The SEQ and SEQ_QUAL fields may not both be set to ignore when writing FASTA files."};
163 }
164 else
165 {
166 if (std::ranges::empty(sequence)) //[[unlikely]]
167 throw std::runtime_error{"The SEQ field may not be empty when writing FASTA files."};
168
169 write_seq(stream_it, options, sequence);
170 }
171 }
172
173private:
176 template <typename stream_view_t,
177 typename seq_legal_alph_type,
178 typename id_type>
179 void read_id(stream_view_t & stream_view,
181 id_type & id)
182 {
183 auto const is_id = is_char<'>'> || is_char<';'>;
184
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))};
188
189 // read id
190 if constexpr (!detail::decays_to_ignore_v<id_type>)
191 {
192 if (options.truncate_ids)
193 {
194 #if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
195 auto it = stream_view.begin();
196 auto e = stream_view.end();
197 ++it; // already checked `is_id`
198
199 for (; (it != e) && (is_blank)(*it); ++it)
200 {}
201
202 bool at_delimiter = false;
203 for (; it != e; ++it)
204 {
205 if ((is_cntrl || is_blank)(*it))
206 {
207 at_delimiter = true;
208 break;
209 }
210 id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
211 }
212
213 if (!at_delimiter)
214 throw unexpected_end_of_input{"FASTA ID line did not end in newline."};
215
216 for (; (it != e) && ((!is_char<'\n'>)(*it)); ++it)
217 {}
218
219 #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
220
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::back_inserter(id)); // … ^A is old delimiter
226
227 // consume rest of line
228 detail::consume(stream_view | detail::take_line_or_throw);
229 #endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
230
231 }
232 else
233 {
234 #if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
235 auto it = stream_view.begin();
236 auto e = stream_view.end();
237 ++it; // skip leading '>' or ';'
238
239 for (; (it != e) && (is_blank)(*it); ++it) // skip leading ' '
240 {}
241
242 bool at_delimiter = false;
243 for (; it != e; ++it)
244 {
245 if ((is_char<'\n'>)(*it))
246 {
247 at_delimiter = true;
248 break;
249 }
250 id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
251 }
252
253 if (!at_delimiter)
254 throw unexpected_end_of_input{"FASTA ID line did not end in newline."};
255
256 #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
257
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::back_inserter(id));
263 #endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
264 }
265 }
266 else
267 {
268 detail::consume(stream_view | detail::take_line_or_throw);
269 }
270 }
271
273 template <typename stream_view_t,
274 typename seq_legal_alph_type,
275 typename seq_type>
276 void read_seq(stream_view_t & stream_view,
277 sequence_file_input_options<seq_legal_alph_type> const &,
278 seq_type & seq)
279 {
280 auto constexpr is_id = is_char<'>'> || is_char<';'>;
281
282 if constexpr (!detail::decays_to_ignore_v<seq_type>)
283 {
284 auto constexpr is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
285
286 #if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
287 auto it = stream_view.begin();
288 auto e = stream_view.end();
289
290 if (it == e)
291 throw unexpected_end_of_input{"No sequence information given!"};
292
293 for (; (it != e) && ((!is_id)(*it)); ++it)
294 {
295 if ((is_space || is_digit)(*it))
296 continue;
297 else if (!is_legal_alph(*it))
298 {
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)};
304 }
305
306 seq.push_back(assign_char_to(*it, std::ranges::range_value_t<seq_type>{}));
307 }
308
309 #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
310
311 if (std::ranges::begin(stream_view) == std::ranges::end(stream_view))
312 throw unexpected_end_of_input{"No sequence information given!"};
313
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)
317 {
318 if (!is_legal_alph(c))
319 {
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)};
325 }
326 return c;
327 }) // enforce legal alphabet
328 | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
329 std::back_inserter(seq));
330 #endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
331 }
332 else
333 {
334 detail::consume(stream_view | detail::take_until(is_id));
335 }
336 }
337
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)
341 {
342 if (options.fasta_legacy_id_marker)
343 stream_it = ';';
344 else
345 stream_it = '>';
346
347 if (options.fasta_blank_before_id)
348 stream_it = ' ';
349
350 stream_it.write_range(id);
351 stream_it.write_end_of_line(options.add_carriage_return);
352 }
353
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)
357 {
358 auto char_sequence = seq | views::to_char;
359
360 if (options.fasta_letters_per_line > 0)
361 {
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);
365
366 while (it != end)
367 {
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);
374 }
375 }
376 else
377 {
378 stream_it.write_range(char_sequence);
379 stream_it.write_end_of_line(options.add_carriage_return);
380 }
381 }
382};
383
384} // namespace seqan
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
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:114
void read_id(stream_view_t &stream_view, sequence_file_input_options< seq_legal_alph_type > const &options, id_type &id)
Implementation of reading the ID.
Definition: format_fasta.hpp:179
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:96
format_fasta() noexcept=default
Defaulted.
void write_id(stream_it_t &stream_it, sequence_file_output_options const &options, id_type &&id)
Implementation of writing the ID.
Definition: format_fasta.hpp:340
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:138
void read_seq(stream_view_t &stream_view, sequence_file_input_options< seq_legal_alph_type > const &, seq_type &seq)
Implementation of reading the sequence.
Definition: format_fasta.hpp:276
void write_seq(stream_it_t &stream_it, sequence_file_output_options const &options, seq_type &&seq)
Implementation of writing the sequence.
Definition: format_fasta.hpp:356
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_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.
The options type defines various option members that influence the behaviour of all or some formats.
Definition: input_options.hpp:27
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.