SeqAn3  3.0.1
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 <iterator>
16 #include <string>
17 #include <string_view>
18 #include <vector>
19 
41 #include <seqan3/std/algorithm>
42 #include <seqan3/std/ranges>
43 
44 namespace seqan3
45 {
46 
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 
94  {
95  { "fastq" },
96  { "fq" }
97  };
98 
99 protected:
101  template <typename stream_type, // constraints checked by file
102  typename seq_legal_alph_type, bool seq_qual_combined,
103  typename seq_type, // other constraints checked inside function
104  typename id_type,
105  typename qual_type>
106  void read_sequence_record(stream_type & stream,
108  seq_type & sequence,
109  id_type & id,
110  qual_type & qualities)
111  {
112  auto stream_view = views::istreambuf(stream);
113  auto stream_it = begin(stream_view);
114 
115  // cache the begin position so we write quals to the same position as seq in seq_qual case
116  size_t sequence_size_before = 0;
117  size_t sequence_size_after = 0;
118  if constexpr (!detail::decays_to_ignore_v<seq_type>)
119  sequence_size_before = size(sequence);
120 
121  /* ID */
122  if (*stream_it != '@') // [[unlikely]]
123  {
124  throw parse_error{std::string{"Expected '@' on beginning of ID line, got: "} +
125  detail::make_printable(*stream_it)};
126  }
127  ++stream_it; // skip '@'
128 
129  if constexpr (!detail::decays_to_ignore_v<id_type>)
130  {
131  if (options.truncate_ids)
132  {
133  std::ranges::copy(stream_view | views::take_until_or_throw(is_cntrl || is_blank)
135  std::ranges::back_inserter(id));
136  detail::consume(stream_view | views::take_line_or_throw);
137  }
138  else
139  {
140  std::ranges::copy(stream_view | views::take_line_or_throw
142  std::ranges::back_inserter(id));
143  }
144  }
145  else
146  {
147  detail::consume(stream_view | views::take_line_or_throw);
148  }
149 
150  /* Sequence */
151  auto seq_view = stream_view | views::take_until_or_throw(is_char<'+'>) // until 2nd ID line
152  | std::views::filter(!is_space); // ignore whitespace
153  if constexpr (!detail::decays_to_ignore_v<seq_type>)
154  {
155  auto constexpr is_legal_alph = is_in_alphabet<seq_legal_alph_type>;
156  std::ranges::copy(seq_view | std::views::transform([is_legal_alph] (char const c) // enforce legal alphabet
157  {
158  if (!is_legal_alph(c))
159  {
160  throw parse_error{std::string{"Encountered an unexpected letter: "} +
161  is_legal_alph.msg +
162  " evaluated to false on " +
163  detail::make_printable(c)};
164  }
165  return c;
166  })
167  | views::char_to<value_type_t<seq_type>>, // convert to actual target alphabet
168  std::ranges::back_inserter(sequence));
169  sequence_size_after = size(sequence);
170  }
171  else // consume, but count
172  {
173  auto it = begin(seq_view);
174  auto it_end = end(seq_view);
175  while (it != it_end)
176  {
177  ++it;
178  ++sequence_size_after;
179  }
180  }
181 
182  /* 2nd ID line */
183  if (*stream_it != '+') // [[unlikely]]
184  {
185  throw parse_error{std::string{"Expected '+' on beginning of 2nd ID line, got: "} +
186  detail::make_printable(*stream_it)};
187  }
188  detail::consume(stream_view | views::take_line_or_throw);
189 
190  /* Qualities */
191  auto qview = stream_view | std::views::filter(!is_space) // this consumes trailing newline
192  | views::take_exactly_or_throw(sequence_size_after - sequence_size_before);
193  if constexpr (seq_qual_combined)
194  {
195  // seq_qual field implies that they are the same variable
196  assert(std::addressof(sequence) == std::addressof(qualities));
197  std::ranges::copy(qview | views::char_to<typename value_type_t<qual_type>::quality_alphabet_type>,
198  begin(qualities) + sequence_size_before);
199  }
200  else if constexpr (!detail::decays_to_ignore_v<qual_type>)
201  {
202  std::ranges::copy(qview | views::char_to<value_type_t<qual_type>>,
203  std::ranges::back_inserter(qualities));
204  }
205  else
206  {
207  detail::consume(qview);
208  }
209  }
210 
212  template <typename stream_type, // constraints checked by file
213  typename seq_type, // other constraints checked inside function
214  typename id_type,
215  typename qual_type>
216  void write_sequence_record(stream_type & stream,
217  sequence_file_output_options const & options,
218  seq_type && sequence,
219  id_type && id,
220  qual_type && qualities)
221  {
222  seqan3::ostreambuf_iterator stream_it{stream};
223 
224  // ID
225  if constexpr (detail::decays_to_ignore_v<id_type>)
226  {
227  throw std::logic_error{"The ID field may not be set to ignore when writing FASTQ files."};
228  }
229  else
230  {
231  if (empty(id)) //[[unlikely]]
232  throw std::runtime_error{"The ID field may not be empty when writing FASTQ files."};
233 
234  stream_it = '@';
235  std::ranges::copy(id, stream_it);
236 
237  detail::write_eol(stream_it, options.add_carriage_return);
238  }
239 
240  // Sequence
241  if constexpr (detail::decays_to_ignore_v<seq_type>)
242  {
243  throw std::logic_error{"The SEQ and SEQ_QUAL fields may not both be set to ignore when writing FASTQ files."};
244  }
245  else
246  {
247  if (empty(sequence)) //[[unlikely]]
248  throw std::runtime_error{"The SEQ field may not be empty when writing FASTQ files."};
249 
250  std::ranges::copy(sequence | views::to_char, stream_it);
251 
252  detail::write_eol(stream_it, options.add_carriage_return);
253  }
254 
255  // 2nd ID-line
256  if constexpr (!detail::decays_to_ignore_v<id_type>)
257  {
258  stream_it = '+';
259 
260  if (options.fastq_double_id)
261  std::ranges::copy(id, stream_it);
262 
263  detail::write_eol(stream_it, options.add_carriage_return);
264  }
265 
266  // Quality line
267  if constexpr (detail::decays_to_ignore_v<qual_type>)
268  {
269  throw std::logic_error{"The QUAL and SEQ_QUAL fields may not both be set to ignore when writing FASTQ files."};
270  }
271  else
272  {
273  if (empty(qualities)) //[[unlikely]]
274  throw std::runtime_error{"The SEQ field may not be empty when writing FASTQ files."};
275 
276  if constexpr (std::ranges::sized_range<seq_type> && std::ranges::sized_range<qual_type>)
277  {
278  assert(size(sequence) == size(qualities));
279  }
280 
281  std::ranges::copy(qualities | views::to_char, stream_it);
282 
283  detail::write_eol(stream_it, options.add_carriage_return);
284  }
285  }
286 };
287 
288 } // namespace seqan
seqan3::views::take_exactly_or_throw
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
shortcuts.hpp
Provides various shortcuts for common std::ranges functions.
take_until.hpp
Provides seqan3::views::take_until and seqan3::views::take_until_or_throw.
input_format_concept.hpp
Provides seqan3::sequence_file_input_format and auxiliary classes.
seqan3::views::to_char
const auto to_char
A view that calls seqan3::to_char() on each element in the input range.
Definition: to_char.hpp:65
std::string
seqan3::sequence_file_input_options::truncate_ids
bool truncate_ids
Read the ID string only up until the first whitespace character.
Definition: input_options.hpp:28
seqan3::format_fastq::format_fastq
format_fastq() noexcept=default
Defaulted.
vector
seqan3::views::char_to
const auto char_to
A view over an alphabet, given a range of characters.
Definition: char_to.hpp:69
seqan3::sequence_file_output_options::fastq_double_id
bool fastq_double_id
Whether to write the ID only '@' or also after '+' line.
Definition: output_options.hpp:34
seqan3::is_space
constexpr auto is_space
Checks whether c is a space character.
Definition: predicate.hpp:146
seqan3::format_fastq::file_extensions
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:94
algorithm
Adaptations of algorithms from the Ranges TS.
seqan3::format_fastq::~format_fastq
~format_fastq() noexcept=default
seqan3::sequence_file_input_options
The options type defines various option members that influence the behaviour of all or some formats.
Definition: input_options.hpp:25
std::addressof
T addressof(T... args)
seqan3::format_fastq::write_sequence_record
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:216
seqan3::parse_error
Thrown if there is a parse error, such as reading an unexpected character from an input stream.
Definition: exception.hpp:47
range.hpp
Provides various transformation traits used by the range module.
seqan3::value_type_t
typename value_type< t >::type value_type_t
Shortcut for seqan3::value_type (transformation_trait shortcut).
Definition: pre.hpp:48
dna5.hpp
Provides seqan3::dna5, container aliases and string literals.
seqan3::ostreambuf_iterator
::ranges::ostreambuf_iterator ostreambuf_iterator
Alias for ranges::ostreambuf_iterator. Writes successive characters onto the output stream from which...
Definition: iterator.hpp:204
seqan3
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:36
std::runtime_error
seqan3::sequence_file_output_options
The options type defines various option members that influence the behaviour of all or some formats.
Definition: output_options.hpp:21
std::logic_error
seqan3::sequence_file_output_options::add_carriage_return
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
output_options.hpp
Provides seqan3::sequence_file_output_options.
seqan3::is_cntrl
constexpr auto is_cntrl
Checks whether c is a control character.
Definition: predicate.hpp:110
seqan3::pack_traits::size
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:116
output_format_concept.hpp
Provides seqan3::SequenceFileFormatOut and auxiliary classes.
char.hpp
Provides alphabet adaptations for standard char types.
to_char.hpp
Provides seqan3::views::to_char.
take.hpp
Provides seqan3::views::take.
ranges
Adaptations of concepts from the Ranges TS.
seqan3::views::take_line_or_throw
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
seqan3::views::take_until_or_throw
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:599
take_exactly.hpp
Provides seqan3::views::take_exactly and seqan3::views::take_exactly_or_throw.
seqan3::format_fastq
The FastQ format.
Definition: format_fastq.hpp:78
predicate.hpp
Provides character predicates for tokenisation.
input_options.hpp
Provides seqan3::sequence_file_input_options.
ignore_output_iterator.hpp
Provides seqan3::detail::ignore_output_iterator for writing to null stream.
aliases.hpp
Provides aliases for qualified.
istreambuf.hpp
Provides seqan3::views::istreambuf.
seqan3::pack_traits::transform
seqan3::type_list< trait_t< pack_t >... > transform
Apply a transformation trait to every type in the pack and return a seqan3::type_list of the results.
Definition: traits.hpp:307
take_line.hpp
Provides seqan3::views::take_line and seqan3::views::take_line_or_throw.
iterator.hpp
Provides seqan3::ostream and seqan3::ostreambuf iterator.
misc.hpp
Provides various utility functions.
seqan3::format_fastq::read_sequence_record
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:106
seqan3::is_blank
constexpr auto is_blank
Checks whether c is a blank character.
Definition: predicate.hpp:163
seqan3::format_fastq::operator=
format_fastq & operator=(format_fastq const &) noexcept=default
Defaulted.
misc.hpp
Provides various utility functions.
string_view
char_to.hpp
Provides seqan3::views::char_to.
seqan3::views::istreambuf
constexpr auto istreambuf
A view factory that returns a view over the stream buffer of an input stream.
Definition: istreambuf.hpp:113
string