SeqAn3  3.1.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 <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 
79 {
80 public:
84  format_fasta() noexcept = default;
85  format_fasta(format_fasta const &) noexcept = default;
86  format_fasta & operator=(format_fasta const &) noexcept = default;
87  format_fasta(format_fasta &&) noexcept = default;
88  format_fasta & operator=(format_fasta &&) noexcept = default;
89  ~format_fasta() noexcept = default;
90 
92 
94  static inline std::vector<std::string> file_extensions
95  {
96  { "fasta" },
97  { "fa" },
98  { "fna" },
99  { "ffn" },
100  { "faa" },
101  { "frn" },
102  { "fas" },
103  };
104 
105 protected:
107  template <typename stream_type, // constraints checked by file
108  typename legal_alph_type,
109  typename seq_type, // other constraints checked inside function
110  typename id_type,
111  typename qual_type>
112  void read_sequence_record(stream_type & stream,
114  seq_type & sequence,
115  id_type & id,
116  qual_type & SEQAN3_DOXYGEN_ONLY(qualities))
117  {
118  auto stream_view = detail::istreambuf(stream);
119 
120  // ID
121  read_id(stream_view, options, id);
122 
123  // Sequence
124  read_seq(stream_view, options, sequence);
125  }
126 
128  template <typename stream_type, // constraints checked by file
129  typename seq_type, // other constraints checked inside function
130  typename id_type,
131  typename qual_type>
132  void write_sequence_record(stream_type & stream,
133  sequence_file_output_options const & options,
134  seq_type && sequence,
135  id_type && id,
136  qual_type && SEQAN3_DOXYGEN_ONLY(qualities))
137  {
138  seqan3::detail::fast_ostreambuf_iterator stream_it{*stream.rdbuf()};
139 
140  // ID
141  if constexpr (detail::decays_to_ignore_v<id_type>)
142  {
143  throw std::logic_error{"The ID field may not be set to ignore when writing FASTA files."};
144  }
145  else
146  {
147  if (std::ranges::empty(id)) //[[unlikely]]
148  throw std::runtime_error{"The ID field may not be empty when writing FASTA files."};
149 
150  write_id(stream_it, options, id);
151  }
152 
153  // Sequence
154  if constexpr (detail::decays_to_ignore_v<seq_type>) // sequence
155  {
156  throw std::logic_error{"The SEQ and SEQ_QUAL fields may not both be set to ignore when writing FASTA files."};
157  }
158  else
159  {
160  if (std::ranges::empty(sequence)) //[[unlikely]]
161  throw std::runtime_error{"The SEQ field may not be empty when writing FASTA files."};
162 
163  write_seq(stream_it, options, sequence);
164  }
165  }
166 
167 private:
170  template <typename stream_view_t,
171  typename seq_legal_alph_type,
172  typename id_type>
173  void read_id(stream_view_t & stream_view,
175  id_type & id)
176  {
177  auto const is_id = is_char<'>'> || is_char<';'>;
178 
179  if (!is_id(*begin(stream_view)))
180  throw parse_error{std::string{"Expected to be on beginning of ID, but "} + is_id.msg +
181  " evaluated to false on " + detail::make_printable(*begin(stream_view))};
182 
183  // read id
184  if constexpr (!detail::decays_to_ignore_v<id_type>)
185  {
186  if (options.truncate_ids)
187  {
188  #if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
189  auto it = stream_view.begin();
190  auto e = stream_view.end();
191  for (; (it != e) && (is_id || is_blank)(*it); ++it)
192  {}
193 
194  bool at_delimiter = false;
195  for (; it != e; ++it)
196  {
197  if ((is_cntrl || is_blank)(*it))
198  {
199  at_delimiter = true;
200  break;
201  }
202  id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
203  }
204 
205  if (!at_delimiter)
206  throw unexpected_end_of_input{"FastA ID line did not end in newline."};
207 
208  for (; (it != e) && ((!is_char<'\n'>)(*it)); ++it)
209  {}
210 
211  #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
212 
213  std::ranges::copy(stream_view | std::views::drop_while(is_id || is_blank) // skip leading >
214  | detail::take_until_or_throw(is_cntrl || is_blank) // read ID until delimiter…
215  | views::char_to<std::ranges::range_value_t<id_type>>,
216  std::cpp20::back_inserter(id)); // … ^A is old delimiter
217 
218  // consume rest of line
219  detail::consume(stream_view | detail::take_line_or_throw);
220  #endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
221 
222  }
223  else
224  {
225  #if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
226  auto it = stream_view.begin();
227  auto e = stream_view.end();
228  for (; (it != e) && (is_id || is_blank)(*it); ++it)
229  {}
230 
231  bool at_delimiter = false;
232  for (; it != e; ++it)
233  {
234  if ((is_char<'\n'>)(*it))
235  {
236  at_delimiter = true;
237  break;
238  }
239  id.push_back(assign_char_to(*it, std::ranges::range_value_t<id_type>{}));
240  }
241 
242  if (!at_delimiter)
243  throw unexpected_end_of_input{"FastA ID line did not end in newline."};
244 
245  #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
246 
247  std::ranges::copy(stream_view | detail::take_line_or_throw // read line
248  | std::views::drop_while(is_id || is_blank) // skip leading >
249  | views::char_to<std::ranges::range_value_t<id_type>>,
250  std::cpp20::back_inserter(id));
251  #endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
252  }
253  }
254  else
255  {
256  detail::consume(stream_view | detail::take_line_or_throw);
257  }
258  }
259 
261  template <typename stream_view_t,
262  typename seq_legal_alph_type,
263  typename seq_type>
264  void read_seq(stream_view_t & stream_view,
265  sequence_file_input_options<seq_legal_alph_type> const &,
266  seq_type & seq)
267  {
268  auto constexpr is_id = is_char<'>'> || is_char<';'>;
269 
270  if constexpr (!detail::decays_to_ignore_v<seq_type>)
271  {
272  auto constexpr is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
273 
274  #if SEQAN3_WORKAROUND_VIEW_PERFORMANCE
275  auto it = stream_view.begin();
276  auto e = stream_view.end();
277 
278  if (it == e)
279  throw unexpected_end_of_input{"No sequence information given!"};
280 
281  for (; (it != e) && ((!is_id)(*it)); ++it)
282  {
283  if ((is_space || is_digit)(*it))
284  continue;
285  else if (!is_legal_alph(*it))
286  {
287  throw parse_error{std::string{"Encountered an unexpected letter: "} +
288  "char_is_valid_for<" +
289  detail::type_name_as_string<seq_legal_alph_type> +
290  "> evaluated to false on " +
291  detail::make_printable(*it)};
292  }
293 
294  seq.push_back(assign_char_to(*it, std::ranges::range_value_t<seq_type>{}));
295  }
296 
297  #else // ↑↑↑ WORKAROUND | ORIGINAL ↓↓↓
298 
299  if (std::ranges::begin(stream_view) == std::ranges::end(stream_view))
300  throw unexpected_end_of_input{"No sequence information given!"};
301 
302  std::ranges::copy(stream_view | detail::take_until(is_id) // until next header (or end)
303  | std::views::filter(!(is_space || is_digit))// ignore whitespace and numbers
304  | std::views::transform([is_legal_alph] (char const c)
305  {
306  if (!is_legal_alph(c))
307  {
308  throw parse_error{std::string{"Encountered an unexpected letter: "} +
309  "char_is_valid_for<" +
310  detail::type_name_as_string<seq_legal_alph_type> +
311  "> evaluated to false on " +
312  detail::make_printable(c)};
313  }
314  return c;
315  }) // enforce legal alphabet
316  | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
317  std::cpp20::back_inserter(seq));
318  #endif // SEQAN3_WORKAROUND_VIEW_PERFORMANCE
319  }
320  else
321  {
322  detail::consume(stream_view | detail::take_until(is_id));
323  }
324  }
325 
327  template <typename stream_it_t, typename id_type>
328  void write_id(stream_it_t & stream_it, sequence_file_output_options const & options, id_type && id)
329  {
330  if (options.fasta_legacy_id_marker)
331  stream_it = ';';
332  else
333  stream_it = '>';
334 
335  if (options.fasta_blank_before_id)
336  stream_it = ' ';
337 
338  stream_it.write_range(id);
339  stream_it.write_end_of_line(options.add_carriage_return);
340  }
341 
343  template <typename stream_it_t, typename seq_type>
344  void write_seq(stream_it_t & stream_it, sequence_file_output_options const & options, seq_type && seq)
345  {
346  auto char_sequence = seq | views::to_char;
347 
348  if (options.fasta_letters_per_line > 0)
349  {
350  /* Using `views::interleave` is probably the way to go but that needs performance-tuning.*/
351  auto it = std::ranges::begin(char_sequence);
352  auto end = std::ranges::end(char_sequence);
353 
354  while (it != end)
355  {
356  /* Note: This solution is slightly suboptimal for sized but non-random-access ranges.*/
357  auto current_end = it;
358  size_t steps = std::ranges::advance(current_end, options.fasta_letters_per_line, end);
359  using subrange_t = std::ranges::subrange<decltype(it), decltype(it), std::ranges::subrange_kind::sized>;
360  it = stream_it.write_range(subrange_t{it, current_end, (options.fasta_letters_per_line - steps)});
361  stream_it.write_end_of_line(options.add_carriage_return);
362  }
363  }
364  else
365  {
366  stream_it.write_range(char_sequence);
367  stream_it.write_end_of_line(options.add_carriage_return);
368  }
369  }
370 };
371 
372 } // namespace seqan
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.
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:79
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:173
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_id(stream_it_t &stream_it, sequence_file_output_options const &options, id_type &&id)
Implementation of writing the ID.
Definition: format_fasta.hpp:328
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:132
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:264
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:344
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:112
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.
Adaptations of concepts from the Ranges TS.
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:25
The options type defines various option members that influence the behaviour of all or some formats.
Definition: 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.