SeqAn3  3.1.0
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 <seqan3/std/algorithm>
16 #include <iterator>
17 #include <seqan3/std/ranges>
18 #include <string>
19 #include <string_view>
20 #include <vector>
21 
42 
43 namespace seqan3
44 {
45 
80 {
81 public:
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 
106 protected:
108  template <typename stream_type, // constraints checked by file
109  typename legal_alph_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  seq_type & sequence,
116  id_type & id,
117  qual_type & SEQAN3_DOXYGEN_ONLY(qualities))
118  {
119  auto stream_view = detail::istreambuf(stream);
120 
121  // ID
122  read_id(stream_view, options, id);
123 
124  // Sequence
125  read_seq(stream_view, options, sequence);
126  }
127 
129  template <typename stream_type, // constraints checked by file
130  typename seq_type, // other constraints checked inside function
131  typename id_type,
132  typename qual_type>
133  void write_sequence_record(stream_type & stream,
134  sequence_file_output_options const & options,
135  seq_type && sequence,
136  id_type && id,
137  qual_type && SEQAN3_DOXYGEN_ONLY(qualities))
138  {
139  seqan3::detail::fast_ostreambuf_iterator stream_it{*stream.rdbuf()};
140 
141  // ID
142  if constexpr (detail::decays_to_ignore_v<id_type>)
143  {
144  throw std::logic_error{"The ID field may not be set to ignore when writing FASTA files."};
145  }
146  else
147  {
148  if (std::ranges::empty(id)) //[[unlikely]]
149  throw std::runtime_error{"The ID field may not be empty when writing FASTA files."};
150 
151  write_id(stream_it, options, id);
152  }
153 
154  // Sequence
155  if constexpr (detail::decays_to_ignore_v<seq_type>) // sequence
156  {
157  throw std::logic_error{"The SEQ and SEQ_QUAL fields may not both be set to ignore when writing FASTA files."};
158  }
159  else
160  {
161  if (std::ranges::empty(sequence)) //[[unlikely]]
162  throw std::runtime_error{"The SEQ field may not be empty when writing FASTA files."};
163 
164  write_seq(stream_it, options, sequence);
165  }
166  }
167 
168 private:
171  template <typename stream_view_t,
172  typename seq_legal_alph_type,
173  typename id_type>
174  void read_id(stream_view_t & stream_view,
176  id_type & id)
177  {
178  auto const is_id = is_char<'>'> || is_char<';'>;
179 
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))};
183 
184  // read id
185  if constexpr (!detail::decays_to_ignore_v<id_type>)
186  {
187  if (options.truncate_ids)
188  {
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)
193  {}
194 
195  bool at_delimiter = false;
196  for (; it != e; ++it)
197  {
198  if ((is_cntrl || is_blank)(*it))
199  {
200  at_delimiter = true;
201  break;
202  }
203  id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
204  }
205 
206  if (!at_delimiter)
207  throw unexpected_end_of_input{"FastA ID line did not end in newline."};
208 
209  for (; (it != e) && ((!is_char<'\n'>)(*it)); ++it)
210  {}
211 
212  #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
213 
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
218 
219  // consume rest of line
220  detail::consume(stream_view | detail::take_line_or_throw);
221  #endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
222 
223  }
224  else
225  {
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)
230  {}
231 
232  bool at_delimiter = false;
233  for (; it != e; ++it)
234  {
235  if ((is_char<'\n'>)(*it))
236  {
237  at_delimiter = true;
238  break;
239  }
240  id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
241  }
242 
243  if (!at_delimiter)
244  throw unexpected_end_of_input{"FastA ID line did not end in newline."};
245 
246  #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
247 
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
253  }
254  }
255  else
256  {
257  detail::consume(stream_view | detail::take_line_or_throw);
258  }
259  }
260 
262  template <typename stream_view_t,
263  typename seq_legal_alph_type,
264  typename seq_type>
265  void read_seq(stream_view_t & stream_view,
266  sequence_file_input_options<seq_legal_alph_type> const &,
267  seq_type & seq)
268  {
269  auto constexpr is_id = is_char<'>'> || is_char<';'>;
270 
271  if constexpr (!detail::decays_to_ignore_v<seq_type>)
272  {
273  auto constexpr is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
274 
275  #if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
276  auto it = stream_view.begin();
277  auto e = stream_view.end();
278 
279  if (it == e)
280  throw unexpected_end_of_input{"No sequence information given!"};
281 
282  for (; (it != e) && ((!is_id)(*it)); ++it)
283  {
284  if ((is_space || is_digit)(*it))
285  continue;
286  else if (!is_legal_alph(*it))
287  {
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)};
293  }
294 
295  seq.push_back(assign_char_to(*it, std::ranges::range_value_t<seq_type>{}));
296  }
297 
298  #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
299 
300  if (std::ranges::begin(stream_view) == std::ranges::end(stream_view))
301  throw unexpected_end_of_input{"No sequence information given!"};
302 
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)
306  {
307  if (!is_legal_alph(c))
308  {
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)};
314  }
315  return 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
320  }
321  else
322  {
323  detail::consume(stream_view | detail::take_until(is_id));
324  }
325  }
326 
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)
330  {
331  if (options.fasta_legacy_id_marker)
332  stream_it = ';';
333  else
334  stream_it = '>';
335 
336  if (options.fasta_blank_before_id)
337  stream_it = ' ';
338 
339  stream_it.write_range(id);
340  stream_it.write_end_of_line(options.add_carriage_return);
341  }
342 
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)
346  {
347  auto char_sequence = seq | views::to_char;
348 
349  if (options.fasta_letters_per_line > 0)
350  {
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);
354 
355  while (it != end)
356  {
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);
363  }
364  }
365  else
366  {
367  stream_it.write_range(char_sequence);
368  stream_it.write_end_of_line(options.add_carriage_return);
369  }
370  }
371 };
372 
373 } // namespace seqan
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.
The FastA format.
Definition: format_fasta.hpp:80
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_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:133
void read_sequence_record(stream_type &stream, sequence_file_input_options< legal_alph_type > const &options, 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
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: 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.