SeqAn3  3.1.0-rc.1
The Modern C++ library for sequence analysis.
format_embl.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 
40 
41 namespace seqan3
42 {
73 {
74 public:
78  format_embl() noexcept = default;
79  format_embl(format_embl const &) noexcept = default;
80  format_embl & operator=(format_embl const &) noexcept = default;
81  format_embl(format_embl &&) noexcept = default;
82  format_embl & operator=(format_embl &&) noexcept = default;
83  ~format_embl() noexcept = default;
84 
86 
88  static inline std::vector<std::string> file_extensions
89  {
90  { "embl" },
91  };
92 
93 protected:
95  template <typename stream_type, // constraints checked by file
96  typename seq_legal_alph_type,
97  typename seq_type, // other constraints checked inside function
98  typename id_type,
99  typename qual_type>
100  void read_sequence_record(stream_type & stream,
102  seq_type & sequence,
103  id_type & id,
104  qual_type & SEQAN3_DOXYGEN_ONLY(qualities))
105  {
106  auto stream_view = detail::istreambuf(stream);
107  auto stream_it = std::ranges::begin(stream_view);
108 
109  std::string idbuffer;
110  std::ranges::copy(stream_view | detail::take_until_or_throw(is_cntrl || is_blank),
111  std::cpp20::back_inserter(idbuffer));
112  if (idbuffer != "ID")
113  throw parse_error{"An entry has to start with the code word ID."};
114 
115  if constexpr (!detail::decays_to_ignore_v<id_type>)
116  {
117  if (options.embl_genbank_complete_header)
118  {
119  std::ranges::copy(idbuffer | views::char_to<std::ranges::range_value_t<id_type>>, std::cpp20::back_inserter(id));
120  do
121  {
122  std::ranges::copy(stream_view | detail::take_until_or_throw(is_char<'S'>)
123  | views::char_to<std::ranges::range_value_t<id_type>>,
124  std::cpp20::back_inserter(id));
125  id.push_back(*stream_it);
126  ++stream_it;
127  } while (*stream_it != 'Q');
128  id.pop_back(); // remove 'S' from id
129  idbuffer = "SQ";
130  }
131  else
132  {
133  // ID
134  detail::consume(stream_view | detail::take_until(!is_blank));
135 
136  // read id
137  if (options.truncate_ids)
138  {
139  std::ranges::copy(stream_view | detail::take_until_or_throw(is_blank || is_char<';'> || is_cntrl)
140  | views::char_to<std::ranges::range_value_t<id_type>>,
141  std::cpp20::back_inserter(id));
142  }
143  else
144  {
145  std::ranges::copy(stream_view | detail::take_until_or_throw(is_char<';'>)
146  | views::char_to<std::ranges::range_value_t<id_type>>,
147  std::cpp20::back_inserter(id));
148  }
149  }
150  }
151 
152  // Jump to sequence
153  if (idbuffer !="SQ")
154  {
155  do
156  {
157  detail::consume(stream_view | detail::take_until_or_throw(is_char<'S'>));
158  ++stream_it;
159  } while (*stream_it != 'Q');
160  }
161  detail::consume(stream_view | detail::take_line_or_throw); //Consume line with infos to sequence
162 
163  // Sequence
164  auto constexpr is_end = is_char<'/'> ;
165  if constexpr (!detail::decays_to_ignore_v<seq_type>)
166  {
167  auto seq_view = stream_view | std::views::filter(!(is_space || is_digit)) // ignore whitespace and numbers
168  | detail::take_until_or_throw(is_end); // until //
169 
170  auto constexpr is_legal_alph = char_is_valid_for<seq_legal_alph_type>;
171  std::ranges::copy(seq_view | std::views::transform([is_legal_alph] (char const c) // enforce legal alphabet
172  {
173  if (!is_legal_alph(c))
174  {
175  throw parse_error{std::string{"Encountered an unexpected letter: "} +
176  "char_is_valid_for<" +
177  detail::type_name_as_string<seq_legal_alph_type> +
178  "> evaluated to false on " +
179  detail::make_printable(c)};
180  }
181  return c;
182  })
183  | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
184  std::cpp20::back_inserter(sequence));
185  }
186  else
187  {
188  detail::consume(stream_view | detail::take_until(is_end));
189  }
190  //Jump over // and cntrl
191  ++stream_it;
192  ++stream_it;
193  ++stream_it;
194  }
195 
197  template <typename stream_type, // constraints checked by file
198  typename seq_type, // other constraints checked inside function
199  typename id_type,
200  typename qual_type>
201  void write_sequence_record(stream_type & stream,
202  sequence_file_output_options const & options,
203  seq_type && sequence,
204  id_type && id,
205  qual_type && SEQAN3_DOXYGEN_ONLY(qualities))
206  {
207  seqan3::detail::fast_ostreambuf_iterator stream_it{*stream.rdbuf()};
208 
209  [[maybe_unused]] size_t sequence_size = 0;
210 
211  if constexpr (!detail::decays_to_ignore_v<seq_type>)
212  sequence_size = std::ranges::distance(sequence);
213 
214  // ID
215  if constexpr (detail::decays_to_ignore_v<id_type>)
216  {
217  throw std::logic_error{"The ID field may not be set to ignore when writing embl files."};
218  }
219  else
220  {
221  if (ranges::empty(id)) //[[unlikely]]
222  throw std::runtime_error{"The ID field may not be empty when writing embl files."};
223 
224  if (options.embl_genbank_complete_header)
225  {
226  stream_it.write_range(id);
227  }
228  else
229  {
230  stream_it.write_range(std::string_view{"ID "});
231  stream_it.write_range(id);
232  stream_it.write_range(std::string_view{"; "});
233  stream_it.write_number(sequence_size);
234  stream_it.write_range(std::string_view{" BP.\n"});
235  }
236  }
237 
238  // Sequence
239  if constexpr (detail::decays_to_ignore_v<seq_type>) // sequence
240  {
241  throw std::logic_error{"The SEQ field may not be set to ignore when writing embl files."};
242  }
243  else
244  {
245  if (std::ranges::empty(sequence)) //[[unlikely]]
246  throw std::runtime_error{"The SEQ field may not be empty when writing embl files."};
247 
248  // write beginning of sequence record
249  stream_it.write_range(std::string_view{"SQ Sequence "});
250  stream_it.write_number(sequence_size);
251  stream_it.write_range(std::string_view{" BP;\n"});
252 
253  // write sequence in chunks of 60 bp's with a space after 10 bp's
254  auto char_sequence = sequence | views::to_char;
255  auto it = std::ranges::begin(char_sequence);
256  size_t written_chars{0};
257  uint8_t chunk_size{10u};
258 
259  while (it != std::ranges::end(char_sequence))
260  {
261  auto current_end = it;
262  size_t steps = std::ranges::advance(current_end, chunk_size, std::ranges::end(char_sequence));
263 
264  using subrange_t = std::ranges::subrange<decltype(it), decltype(it), std::ranges::subrange_kind::sized>;
265  it = stream_it.write_range(subrange_t{it, current_end, chunk_size - steps});
266  stream_it = ' ';
267  written_chars += chunk_size;
268 
269  if (written_chars % 60 == 0)
270  {
271  stream_it.write_number(written_chars);
272  stream_it.write_end_of_line(options.add_carriage_return);
273  }
274  }
275 
276  // fill last line
277  auto characters_in_last_line = sequence_size % 60;
278  auto number_of_padding_needed = 65 - characters_in_last_line - characters_in_last_line / chunk_size;
279  stream_it.write_range(views::repeat_n(' ', number_of_padding_needed));
280  stream_it.write_number(sequence_size);
281  stream_it.write_end_of_line(options.add_carriage_return);
282 
283  // write end-of-record-symbol
284  stream_it.write_range(std::string_view{"//"});
285  stream_it.write_end_of_line(options.add_carriage_return);
286  }
287  }
288 };
289 
290 } // namespace seqan
Adaptations of algorithms from the Ranges TS.
Core alphabet concept and free function/type trait wrappers.
Provides seqan3::views::char_to.
The EMBL format.
Definition: format_embl.hpp:73
static std::vector< std::string > file_extensions
The valid file extensions for this format; note that you can modify this value.
Definition: format_embl.hpp:89
void read_sequence_record(stream_type &stream, sequence_file_input_options< seq_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_embl.hpp:100
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_embl.hpp:201
format_embl() noexcept=default
Defaulted.
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.
auto const to_char
A view that calls seqan3::to_char() on each element in the input range.
Definition: to_char.hpp:63
auto const char_to
A view over an alphabet, given a range of characters.
Definition: char_to.hpp:67
constexpr auto is_blank
Checks whether c is a blank character.
Definition: predicate.hpp:145
constexpr auto is_digit
Checks whether c is a digital character.
Definition: predicate.hpp:269
constexpr auto is_char
Checks whether a given letter is the same as the template non-type argument.
Definition: predicate.hpp:65
constexpr auto is_space
Checks whether c is a space character.
Definition: predicate.hpp:128
constexpr auto is_cntrl
Checks whether c is a control character.
Definition: predicate.hpp:92
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:471
constexpr auto repeat_n
A view factory that repeats a given value n times.
Definition: repeat_n.hpp:95
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::views::repeat_n.
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:25
bool embl_genbank_complete_header
Read the complete_header into the seqan3::field::id for embl or genbank format.
Definition: input_options.hpp:29
bool truncate_ids
Read the ID string only up until the first whitespace character.
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:23
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:40
bool embl_genbank_complete_header
Complete header given for embl or genbank.
Definition: output_options.hpp:43
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.