SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
format_fastq.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2020, 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 
44 
45 namespace seqan3
46 {
47 
79 {
80 public:
84  format_fastq() noexcept = default;
85  format_fastq(format_fastq const &) noexcept = default;
86  format_fastq & operator=(format_fastq const &) noexcept = default;
87  format_fastq(format_fastq &&) noexcept = default;
88  format_fastq & operator=(format_fastq &&) noexcept = default;
89  ~format_fastq() noexcept = default;
90 
92 
94  static inline std::vector<std::string> file_extensions
95  {
96  { "fastq" },
97  { "fq" }
98  };
99 
100 protected:
102  template <typename stream_type, // constraints checked by file
103  typename seq_legal_alph_type, bool seq_qual_combined,
104  typename seq_type, // other constraints checked inside function
105  typename id_type,
106  typename qual_type>
107  void read_sequence_record(stream_type & stream,
109  seq_type & sequence,
110  id_type & id,
111  qual_type & qualities)
112  {
113  auto stream_view = views::istreambuf(stream);
114  auto stream_it = begin(stream_view);
115 
116  // cache the begin position so we write quals to the same position as seq in seq_qual case
117  size_t sequence_size_before = 0;
118  size_t sequence_size_after = 0;
119  if constexpr (!detail::decays_to_ignore_v<seq_type>)
120  sequence_size_before = size(sequence);
121 
122  /* ID */
123  if (*stream_it != '@') // [[unlikely]]
124  {
125  throw parse_error{std::string{"Expected '@' on beginning of ID line, got: "} +
126  detail::make_printable(*stream_it)};
127  }
128  ++stream_it; // skip '@'
129 
130  if constexpr (!detail::decays_to_ignore_v<id_type>)
131  {
132  if (options.truncate_ids)
133  {
134  std::ranges::copy(stream_view | views::take_until_or_throw(is_cntrl || is_blank)
135  | views::char_to<std::ranges::range_value_t<id_type>>,
136  std::cpp20::back_inserter(id));
137  detail::consume(stream_view | views::take_line_or_throw);
138  }
139  else
140  {
141  std::ranges::copy(stream_view | views::take_line_or_throw
142  | views::char_to<std::ranges::range_value_t<id_type>>,
143  std::cpp20::back_inserter(id));
144  }
145  }
146  else
147  {
148  detail::consume(stream_view | views::take_line_or_throw);
149  }
150 
151  /* Sequence */
152  auto seq_view = stream_view | views::take_until_or_throw(is_char<'+'>) // until 2nd ID line
153  | std::views::filter(!is_space); // ignore whitespace
154  if constexpr (!detail::decays_to_ignore_v<seq_type>)
155  {
156  auto constexpr is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
157  std::ranges::copy(seq_view | std::views::transform([is_legal_alph] (char const c) // enforce legal alphabet
158  {
159  if (!is_legal_alph(c))
160  {
161  throw parse_error{std::string{"Encountered an unexpected letter: "} +
162  "char_is_valid_for<" +
163  detail::type_name_as_string<seq_legal_alph_type> +
164  "> evaluated to false on " +
165  detail::make_printable(c)};
166  }
167  return c;
168  })
169  | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
170  std::cpp20::back_inserter(sequence));
171  sequence_size_after = size(sequence);
172  }
173  else // consume, but count
174  {
175  auto it = begin(seq_view);
176  auto it_end = end(seq_view);
177  while (it != it_end)
178  {
179  ++it;
180  ++sequence_size_after;
181  }
182  }
183 
184  detail::consume(stream_view | views::take_line_or_throw);
185 
186  /* Qualities */
187  auto qview = stream_view | std::views::filter(!is_space) // this consumes trailing newline
188  | views::take_exactly_or_throw(sequence_size_after - sequence_size_before);
189  if constexpr (seq_qual_combined)
190  {
191  // seq_qual field implies that they are the same variable
192  assert(std::addressof(sequence) == std::addressof(qualities));
193  std::ranges::copy(qview | views::char_to<typename std::ranges::range_value_t<qual_type>::quality_alphabet_type>,
194  begin(qualities) + sequence_size_before);
195  }
196  else if constexpr (!detail::decays_to_ignore_v<qual_type>)
197  {
198  std::ranges::copy(qview | views::char_to<std::ranges::range_value_t<qual_type>>,
199  std::cpp20::back_inserter(qualities));
200  }
201  else
202  {
203  detail::consume(qview);
204  }
205  }
206 
208  template <typename stream_type, // constraints checked by file
209  typename seq_type, // other constraints checked inside function
210  typename id_type,
211  typename qual_type>
212  void write_sequence_record(stream_type & stream,
213  sequence_file_output_options const & options,
214  seq_type && sequence,
215  id_type && id,
216  qual_type && qualities)
217  {
218  seqan3::detail::fast_ostreambuf_iterator stream_it{*stream.rdbuf()};
219 
220  // ID
221  if constexpr (detail::decays_to_ignore_v<id_type>)
222  {
223  throw std::logic_error{"The ID field may not be set to ignore when writing FASTQ files."};
224  }
225  else
226  {
227  if (std::ranges::empty(id)) //[[unlikely]]
228  throw std::runtime_error{"The ID field may not be empty when writing FASTQ files."};
229 
230  stream_it = '@';
231  stream_it.write_range(id);
232  stream_it.write_end_of_line(options.add_carriage_return);
233  }
234 
235  // Sequence
236  if constexpr (detail::decays_to_ignore_v<seq_type>)
237  {
238  throw std::logic_error{"The SEQ and SEQ_QUAL fields may not both be set to ignore when writing FASTQ files."};
239  }
240  else
241  {
242  if (std::ranges::empty(sequence)) //[[unlikely]]
243  throw std::runtime_error{"The SEQ field may not be empty when writing FASTQ files."};
244 
245  stream_it.write_range(sequence | views::to_char);
246  stream_it.write_end_of_line(options.add_carriage_return);
247  }
248 
249  // 2nd ID-line
250  if constexpr (!detail::decays_to_ignore_v<id_type>)
251  {
252  stream_it = '+';
253 
254  if (options.fastq_double_id)
255  stream_it.write_range(id);
256 
257  stream_it.write_end_of_line(options.add_carriage_return);
258  }
259 
260  // Quality line
261  if constexpr (detail::decays_to_ignore_v<qual_type>)
262  {
263  throw std::logic_error{"The QUAL and SEQ_QUAL fields may not both be set to ignore when writing FASTQ files."};
264  }
265  else
266  {
267  if (std::ranges::empty(qualities)) //[[unlikely]]
268  throw std::runtime_error{"The SEQ field may not be empty when writing FASTQ files."};
269 
270  if constexpr (std::ranges::sized_range<seq_type> && std::ranges::sized_range<qual_type>)
271  {
272  assert(std::ranges::size(sequence) == std::ranges::size(qualities));
273  }
274 
275  stream_it.write_range(qualities | views::to_char);
276  stream_it.write_end_of_line(options.add_carriage_return);
277  }
278  }
279 };
280 
281 } // namespace seqan
T addressof(T... args)
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.
The FastQ format.
Definition: format_fastq.hpp:79
void read_sequence_record(stream_type &stream, sequence_file_input_options< seq_legal_alph_type, seq_qual_combined > 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_fastq.hpp:107
format_fastq() 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_fastq.hpp:212
static std::vector< std::string > file_extensions
The valid file extensions for this format; note that you can modify this value.
Definition: format_fastq.hpp:95
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 is_blank
Checks whether c is a blank character.
Definition: predicate.hpp:161
constexpr auto is_space
Checks whether c is a space character.
Definition: predicate.hpp:144
constexpr auto is_cntrl
Checks whether c is a control character.
Definition: predicate.hpp:108
decltype(detail::transform< trait_t >(list_t{})) transform
Apply a transformation trait to every type in the list and return a seqan3::type_list of the results.
Definition: traits.hpp:434
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:150
auto const to_char
A view that calls seqan3::to_char() on each element in the input range.
Definition: to_char.hpp:65
constexpr auto take_until_or_throw
A view adaptor that returns elements from the underlying range until the functor evaluates to true (t...
Definition: take_until.hpp:614
constexpr auto istreambuf
A view factory that returns a view over the stream buffer of an input stream.
Definition: istreambuf.hpp:114
constexpr auto take_exactly_or_throw
A view adaptor that returns the first size elements from the underlying range and also exposes size i...
Definition: take_exactly.hpp:91
auto const char_to
A view over an alphabet, given a range of characters.
Definition: char_to.hpp:70
constexpr auto take_line_or_throw
A view adaptor that returns a single line from the underlying range (throws if there is no end-of-lin...
Definition: take_line.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::views::istreambuf.
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
SeqAn specific customisations in the standard namespace.
Provides various utility functions.
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.
Thrown if there is a parse error, such as reading an unexpected character from an input stream.
Definition: exception.hpp:48
The options type defines various option members that influence the behaviour of all or some formats.
Definition: input_options.hpp:30
bool truncate_ids
Read the ID string only up until the first whitespace character.
Definition: input_options.hpp:32
The options type defines various option members that influence the behaviour of all or some formats.
Definition: output_options.hpp:22
bool add_carriage_return
The default plain text line-ending is "\n", but on Windows an additional carriage return is recommend...
Definition: output_options.hpp:39
bool fastq_double_id
Whether to write the ID only '@' or also after '+' line.
Definition: output_options.hpp:34
Provides seqan3::views::take.
Provides seqan3::views::take_exactly and seqan3::views::take_exactly_or_throw.
Provides seqan3::views::take_line and seqan3::views::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.
Provides character predicates for tokenisation.