SeqAn3  3.0.1
The Modern C++ library for sequence analysis.
format_embl.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 
20 #include <range/v3/view/chunk.hpp>
21 
41 #include <seqan3/std/algorithm>
42 #include <seqan3/std/charconv>
43 #include <seqan3/std/ranges>
44 
45 namespace seqan3
46 {
77 {
78 public:
82  format_embl() noexcept = default;
83  format_embl(format_embl const &) noexcept = default;
84  format_embl & operator=(format_embl const &) noexcept = default;
85  format_embl(format_embl &&) noexcept = default;
86  format_embl & operator=(format_embl &&) noexcept = default;
87  ~format_embl() noexcept = default;
88 
92  {
93  { "embl" },
94  };
95 
96 protected:
98  template <typename stream_type, // constraints checked by file
99  typename seq_legal_alph_type, bool seq_qual_combined,
100  typename seq_type, // other constraints checked inside function
101  typename id_type,
102  typename qual_type>
103  void read_sequence_record(stream_type & stream,
105  seq_type & sequence,
106  id_type & id,
107  qual_type & SEQAN3_DOXYGEN_ONLY(qualities))
108  {
109  auto stream_view = views::istreambuf(stream);
110  auto stream_it = std::ranges::begin(stream_view);
111 
112  std::string idbuffer;
113  std::ranges::copy(stream_view | views::take_until_or_throw(is_cntrl || is_blank),
114  std::ranges::back_inserter(idbuffer));
115  if (idbuffer != "ID")
116  throw parse_error{"An entry has to start with the code word ID."};
117 
118  if constexpr (!detail::decays_to_ignore_v<id_type>)
119  {
120  if (options.embl_genbank_complete_header)
121  {
122  std::ranges::copy(idbuffer | views::char_to<value_type_t<id_type>>, std::ranges::back_inserter(id));
123  do
124  {
125  std::ranges::copy(stream_view | views::take_until_or_throw(is_char<'S'>)
127  std::ranges::back_inserter(id));
128  id.push_back(*stream_it);
129  ++stream_it;
130  } while (*stream_it != 'Q');
131  id.pop_back(); // remove 'S' from id
132  idbuffer = "SQ";
133  }
134  else
135  {
136  // ID
137  detail::consume(stream_view | views::take_until(!is_blank));
138 
139  // read id
140  if (options.truncate_ids)
141  {
142  std::ranges::copy(stream_view | views::take_until_or_throw(is_blank || is_char<';'> || is_cntrl)
144  std::ranges::back_inserter(id));
145  }
146  else
147  {
148  std::ranges::copy(stream_view | views::take_until_or_throw(is_char<';'>)
150  std::ranges::back_inserter(id));
151  }
152  }
153  }
154 
155  // Jump to sequence
156  if (idbuffer !="SQ")
157  {
158  do
159  {
160  detail::consume(stream_view | views::take_until_or_throw(is_char<'S'>));
161  ++stream_it;
162  } while (*stream_it != 'Q');
163  }
164  detail::consume(stream_view | views::take_line_or_throw); //Consume line with infos to sequence
165 
166  // Sequence
167  auto constexpr is_end = is_char<'/'> ;
168  if constexpr (!detail::decays_to_ignore_v<seq_type>)
169  {
170  auto seq_view = stream_view | std::views::filter(!(is_space || is_digit)) // ignore whitespace and numbers
171  | views::take_until_or_throw(is_end); // until //
172 
173  auto constexpr is_legal_alph = is_in_alphabet<seq_legal_alph_type>;
174  std::ranges::copy(seq_view | std::views::transform([is_legal_alph] (char const c) // enforce legal alphabet
175  {
176  if (!is_legal_alph(c))
177  {
178  throw parse_error{std::string{"Encountered an unexpected letter: "} +
179  is_legal_alph.msg +
180  " evaluated to false on " +
181  detail::make_printable(c)};
182  }
183  return c;
184  })
185  | views::char_to<value_type_t<seq_type>>, // convert to actual target alphabet
186  std::ranges::back_inserter(sequence));
187  }
188  else
189  {
190  detail::consume(stream_view | views::take_until(is_end));
191  }
192  //Jump over // and cntrl
193  ++stream_it;
194  ++stream_it;
195  ++stream_it;
196  }
197 
199  template <typename stream_type, // constraints checked by file
200  typename seq_type, // other constraints checked inside function
201  typename id_type,
202  typename qual_type>
203  void write_sequence_record(stream_type & stream,
204  sequence_file_output_options const & options,
205  seq_type && sequence,
206  id_type && id,
207  qual_type && SEQAN3_DOXYGEN_ONLY(qualities))
208  {
209  seqan3::ostreambuf_iterator stream_it{stream};
210  [[maybe_unused]] size_t sequence_size = 0;
211  [[maybe_unused]] char buffer[50];
212  if constexpr (!detail::decays_to_ignore_v<seq_type>)
213  sequence_size = std::ranges::distance(sequence);
214 
215  // ID
216  if constexpr (detail::decays_to_ignore_v<id_type>)
217  {
218  throw std::logic_error{"The ID field may not be set to ignore when writing embl files."};
219  }
220  else
221  {
222  if (ranges::empty(id)) //[[unlikely]]
223  throw std::runtime_error{"The ID field may not be empty when writing embl files."};
224 
225  if (options.embl_genbank_complete_header)
226  {
227  std::ranges::copy(id, stream_it);
228  }
229  else
230  {
231  std::ranges::copy(std::string_view{"ID "}, stream_it);
232  std::ranges::copy(id, stream_it);
233  std::ranges::copy(std::string_view{"; "}, stream_it);
234  auto res = std::to_chars(&buffer[0], &buffer[0] + sizeof(buffer), sequence_size);
235  std::copy(&buffer[0], res.ptr, stream_it);
236  std::ranges::copy(std::string_view{" BP.\n"}, stream_it);
237  }
238 
239  }
240 
241  // Sequence
242  if constexpr (detail::decays_to_ignore_v<seq_type>) // sequence
243  {
244  throw std::logic_error{"The SEQ field may not be set to ignore when writing embl files."};
245  }
246  else
247  {
248  if (ranges::empty(sequence)) //[[unlikely]]
249  throw std::runtime_error{"The SEQ field may not be empty when writing embl files."};
250 
251  std::ranges::copy(std::string_view{"SQ Sequence "}, stream_it);
252  auto res = std::to_chars(&buffer[0], &buffer[0] + sizeof(buffer), sequence_size);
253  std::copy(&buffer[0], res.ptr, stream_it);
254  std::ranges::copy(std::string_view{" BP;\n"}, stream_it);
255  auto seqChunk = sequence | ranges::view::chunk(60);
256  unsigned int i = 0;
257  size_t bp = 0;
258  for (auto chunk : seqChunk)
259  {
260  std::ranges::copy(chunk | views::to_char
261  | ranges::view::chunk(10)
262  | views::join(' '), stream_it);
263  ++i;
264  stream_it = ' ';
265  bp = std::min(sequence_size, bp + 60);
266  uint8_t num_blanks = 60 * i - bp; // for sequence characters
267  num_blanks += num_blanks / 10; // additional chunk separators
268  std::ranges::copy(views::repeat_n(' ', num_blanks), stream_it);
269  std::ranges::copy(std::to_string(bp), stream_it);
270  stream_it = '\n';
271  }
272  std::ranges::copy(std::string_view{"//"}, stream_it);
273  stream_it = '\n';
274  }
275  }
276 };
277 
278 } // namespace seqan
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_embl::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_embl.hpp:103
std::string_view
seqan3::format_embl::operator=
format_embl & operator=(format_embl const &) noexcept=default
Defaulted.
charconv
Provides std::from_chars and std::to_chars if not defined in the stl <charconv> header.
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::embl_genbank_complete_header
bool embl_genbank_complete_header
Complete header given for embl or genbank.
Definition: output_options.hpp:42
seqan3::is_space
constexpr auto is_space
Checks whether c is a space character.
Definition: predicate.hpp:146
seqan3::sequence_file_input_options::embl_genbank_complete_header
bool embl_genbank_complete_header
Read the complete_header into the seqan3::field::id for embl or genbank format.
Definition: input_options.hpp:30
algorithm
Adaptations of algorithms from the Ranges TS.
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
repeat_n.hpp
Provides seqan3::views::repeat_n.
to.hpp
Provides seqan3::views::to.
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.
std::to_string
T to_string(T... args)
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
seqan3::format_embl::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_embl.hpp:203
seqan3::views::take_until
constexpr auto take_until
A view adaptor that returns elements from the underlying range until the functor evaluates to true (o...
Definition: take_until.hpp:585
std::copy
T copy(T... args)
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
seqan3::is_digit
constexpr auto is_digit
Checks whether c is a digital character.
Definition: predicate.hpp:287
std::logic_error
seqan3::format_embl::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_embl.hpp:92
output_options.hpp
Provides seqan3::sequence_file_output_options.
std::to_chars
T to_chars(T... args)
seqan3::is_cntrl
constexpr auto is_cntrl
Checks whether c is a control character.
Definition: predicate.hpp:110
std::min
T min(T... args)
join.hpp
Provides seqan3::views::join.
output_format_concept.hpp
Provides seqan3::SequenceFileFormatOut and auxiliary classes.
to_char.hpp
Provides seqan3::views::to_char.
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_embl
The EMBL format.
Definition: format_embl.hpp:76
predicate.hpp
Provides character predicates for tokenisation.
seqan3::format_embl::~format_embl
~format_embl() noexcept=default
input_options.hpp
Provides seqan3::sequence_file_input_options.
istreambuf.hpp
Provides seqan3::views::istreambuf.
seqan3::views::repeat_n
constexpr auto repeat_n
A view factory that repeats a given value n times.
Definition: repeat_n.hpp:94
seqan3::format_embl::format_embl
format_embl() noexcept=default
Defaulted.
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::is_char
constexpr auto is_char
Checks whether a given letter is the same as the template non-type argument.
Definition: predicate.hpp:83
seqan3::is_blank
constexpr auto is_blank
Checks whether c is a blank character.
Definition: predicate.hpp:163
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