SeqAn3
format_vienna.hpp
Go to the documentation of this file.
1 // ============================================================================
2 // SeqAn - The Library for Sequence Analysis
3 // ============================================================================
4 //
5 // Copyright (c) 2006-2018, Knut Reinert & Freie Universitaet Berlin
6 // Copyright (c) 2016-2018, Knut Reinert & MPI Molekulare Genetik
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // * Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // * Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // * Neither the name of Knut Reinert or the FU Berlin nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 // ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
25 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 // DAMAGE.
32 //
33 // ============================================================================
34 
40 #pragma once
41 
42 #include <cstdio>
43 #include <iterator>
44 #include <stack>
45 #include <string>
46 #include <string_view>
47 #include <type_traits>
48 #include <vector>
49 
50 #include <range/v3/algorithm/copy.hpp>
51 #include <range/v3/utility/iterator.hpp>
52 #include <range/v3/view/chunk.hpp>
53 #include <range/v3/view/drop_while.hpp>
54 #include <range/v3/view/join.hpp>
55 #include <range/v3/view/remove_if.hpp>
56 #include <range/v3/view/transform.hpp>
57 
71 #include <seqan3/std/ranges>
74 
75 namespace seqan3
76 {
114 {
115 public:
120  structure_file_format_vienna() = default;
122  structure_file_format_vienna & operator=(structure_file_format_vienna const &) = delete;
126 
128  static inline std::vector<std::string> file_extensions
129  {
130  { "dbn" },
131  { "fasta" },
132  { "fa" }
133  };
134 
136  template <typename stream_type, // constraints checked by file
137  typename seq_legal_alph_type,
138  bool structured_seq_combined,
139  typename seq_type, // other constraints checked inside function
140  typename id_type,
141  typename bpp_type,
142  typename structure_type,
143  typename energy_type,
144  typename react_type,
145  typename comment_type,
146  typename offset_type>
147  void read(stream_type & stream,
149  seq_type & seq,
150  id_type & id,
151  bpp_type & bpp,
152  structure_type & structure,
153  energy_type & energy,
154  react_type & SEQAN3_DOXYGEN_ONLY(react),
155  react_type & SEQAN3_DOXYGEN_ONLY(react_err),
156  comment_type & SEQAN3_DOXYGEN_ONLY(comment),
157  offset_type & SEQAN3_DOXYGEN_ONLY(offset))
158  {
159  auto stream_view = view::subrange<decltype(std::istreambuf_iterator<char>{stream}),
160  decltype(std::istreambuf_iterator<char>{})>
161  { std::istreambuf_iterator<char>{stream}, std::istreambuf_iterator<char>{} };
162 
163  // READ ID (if present)
164  auto constexpr is_id = is_char<'>'>;
165  if (is_id(*begin(stream_view)))
166  {
167  if constexpr (!detail::decays_to_ignore_v<id_type>)
168  {
169  if (options.truncate_ids)
170  {
171  std::ranges::copy(stream_view | ranges::view::drop_while(is_id || is_blank) // skip leading >
172  | view::take_until_or_throw(is_cntrl || is_blank)
173  | view::char_to<value_type_t<id_type>>,
174  std::back_inserter(id));
175  detail::consume(stream_view | view::take_line_or_throw);
176  }
177  else
178  {
179  std::ranges::copy(stream_view | ranges::view::drop_while(is_id || is_blank) // skip leading >
180  | view::take_line_or_throw
181  | view::char_to<value_type_t<id_type>>,
182  std::back_inserter(id));
183  }
184  }
185  else
186  {
187  detail::consume(stream_view | view::take_line_or_throw);
188  }
189  }
190  else if constexpr (!detail::decays_to_ignore_v<id_type>)
191  {
192  auto constexpr is_legal_seq = is_in_alphabet<seq_legal_alph_type>;
193  if (!is_legal_seq(*begin(stream_view))) // if neither id nor seq found: throw
194  {
195  throw parse_error{std::string{"Expected to be on beginning of ID or sequence, but "} +
196  is_id.msg.string() + " and " + is_legal_seq.msg.string() +
197  " evaluated to false on " + detail::make_printable(*begin(stream_view))};
198  }
199  }
200 
201  // READ SEQUENCE
202  if constexpr (!detail::decays_to_ignore_v<seq_type>)
203  {
204  auto constexpr is_legal_seq = is_in_alphabet<seq_legal_alph_type>;
205  std::ranges::copy(stream_view | view::take_line_or_throw // until end of line
206  | ranges::view::remove_if(is_space || is_digit) // ignore whitespace and numbers
207  | ranges::view::transform([is_legal_seq](char const c)
208  {
209  if (!is_legal_seq(c)) // enforce legal alphabet
210  {
211  throw parse_error{std::string{"Encountered an unexpected letter: "} +
212  is_legal_seq.msg.string() +
213  " evaluated to false on " +
214  detail::make_printable(c)};
215  }
216  return c;
217  })
218  | view::char_to<value_type_t<seq_type>>, // convert to actual target alphabet
219  std::back_inserter(seq));
220  }
221  else
222  {
223  detail::consume(stream_view | view::take_line_or_throw);
224  }
225 
226  // READ STRUCTURE (if present)
227  if constexpr (!detail::decays_to_ignore_v<structure_type>)
228  {
229  if constexpr (structured_seq_combined)
230  {
231  assert(std::addressof(seq) == std::addressof(structure));
232  using alph_type = typename value_type_t<structure_type>::structure_alphabet_type;
233  std::ranges::copy(read_structure<alph_type>(stream_view), begin(structure));
234 
235  if constexpr (!detail::decays_to_ignore_v<bpp_type>)
236  detail::bpp_from_rna_structure<alph_type>(bpp, structure);
237  }
238  else
239  {
240  using alph_type = value_type_t<structure_type>;
241  std::ranges::copy(read_structure<alph_type>(stream_view), std::back_inserter(structure));
242 
243  if constexpr (!detail::decays_to_ignore_v<bpp_type>)
244  detail::bpp_from_rna_structure<alph_type>(bpp, structure);
245  }
246  if constexpr (!detail::decays_to_ignore_v<seq_type>)
247  if (size(seq) != size(structure))
248  throw parse_error{"Found sequence and associated structure of different length."};
249  }
250  else if constexpr (!detail::decays_to_ignore_v<bpp_type>)
251  {
252  detail::bpp_from_rna_structure<wuss51>(bpp, read_structure<wuss51>(stream_view));
253 
254  if constexpr (!detail::decays_to_ignore_v<seq_type>)
255  if (size(seq) != size(bpp))
256  throw parse_error{"Found sequence and associated structure of different length."};
257  }
258  else
259  {
260  detail::consume(stream_view | view::take_until(is_space)); // until whitespace
261  }
262 
263  // READ ENERGY (if present)
264  if constexpr (!detail::decays_to_ignore_v<energy_type>)
265  {
266  std::string e_str = stream_view | view::take_line
267  | ranges::view::remove_if(is_space || is_char<'('> || is_char<')'>);
268  if (!e_str.empty())
269  {
270  size_t num_processed;
271  energy = std::stod(e_str, &num_processed);
272  if (num_processed != e_str.size()) // [[unlikely]]
273  {
274  throw parse_error{std::string{"Failed to parse energy value '"} + e_str + "'."};
275  }
276  }
277  }
278  else
279  {
280  detail::consume(stream_view | view::take_line);
281  }
282  detail::consume(stream_view | view::take_until(!is_space));
283 
284  // make sure "buffer at end" implies "stream at end"
285  if ((std::istreambuf_iterator<char>{stream} == std::istreambuf_iterator<char>{}) && (!stream.eof()))
286  {
287  stream.get(); // triggers error in stream and sets eof
288  }
289  }
290 
292  template <typename stream_type, // constraints checked by file
293  typename seq_type, // other constraints checked inside function
294  typename id_type,
295  typename bpp_type,
296  typename structure_type,
297  typename energy_type,
298  typename react_type,
299  typename comment_type,
300  typename offset_type>
301  void write(stream_type & stream,
302  structure_file_output_options const & options,
303  seq_type && seq,
304  id_type && id,
305  bpp_type && SEQAN3_DOXYGEN_ONLY(bpp),
306  structure_type && structure,
307  energy_type && energy,
308  react_type && SEQAN3_DOXYGEN_ONLY(react),
309  react_type && SEQAN3_DOXYGEN_ONLY(react_err),
310  comment_type && SEQAN3_DOXYGEN_ONLY(comment),
311  offset_type && SEQAN3_DOXYGEN_ONLY(offset))
312  {
313  std::ranges::ostreambuf_iterator stream_it{stream};
314 
315  // WRITE ID (optional)
316  if constexpr (!detail::decays_to_ignore_v<id_type>)
317  {
318  if (!empty(id))
319  {
320  stream_it = '>';
321  stream_it = ' ';
322  std::ranges::copy(id, stream_it);
323  detail::write_eol(stream_it, options.add_carriage_return);
324  }
325  }
326 
327  // WRITE SEQUENCE
328  if constexpr (!detail::decays_to_ignore_v<seq_type>)
329  {
330  if (empty(seq)) //[[unlikely]]
331  throw std::runtime_error{"The SEQ field may not be empty when writing Vienna files."};
332 
333  std::ranges::copy(seq | view::to_char, stream_it);
334  detail::write_eol(stream_it, options.add_carriage_return);
335  }
336  else
337  {
338  throw std::logic_error{"The SEQ and STRUCTURED_SEQ fields may not both be set to ignore "
339  "when writing Vienna files."};
340  }
341 
342  // WRITE STRUCTURE (optional)
343  if constexpr (!detail::decays_to_ignore_v<structure_type>)
344  {
345  if (!empty(structure))
346  std::ranges::copy(structure | view::to_char, stream_it);
347 
348  // WRITE ENERGY (optional)
349  if constexpr (!detail::decays_to_ignore_v<energy_type>)
350  {
351  if (energy)
352  {
353 // TODO(joergi-w) enable the following when std::to_chars is implemented for float types
354 // auto [endptr, ec] = std::to_chars(str.data(),
355 // str.data() + str.size(),
356 // energy,
357 // std::chars_format::fixed,
358 // options.precision);
359 // if (ec == std::errc())
360 // std::ranges::copy(str.data(), endptr, stream_it);
361 // else
362 // throw std::runtime_error{"The energy could not be transformed into a string."};
363 
364  stream_it = ' ';
365  stream_it = '(';
366 
367  std::array<char, 100> str;
368  int len = std::snprintf(str.data(), 100, "%.*f", options.precision, energy);
369  if (len < 0 || len >= 100)
370  throw std::runtime_error{"The energy could not be transformed into a string."};
371  std::ranges::copy(str.data(), str.data() + len, stream_it);
372 
373  stream_it = ')';
374  }
375  }
376  detail::write_eol(stream_it, options.add_carriage_return);
377  }
378  else if constexpr (!detail::decays_to_ignore_v<energy_type>)
379  {
380  throw std::logic_error{"The ENERGY field cannot be written to a Vienna file without providing STRUCTURE."};
381  }
382  }
383 
384 private:
392  template <typename alph_type, typename stream_view_type>
393  auto read_structure(stream_view_type & stream_view)
394  {
395  auto constexpr is_legal_structure = is_in_alphabet<alph_type>;
396  return stream_view | view::take_until(is_space) // until whitespace
397  | ranges::view::transform([is_legal_structure](char const c)
398  {
399  if (!is_legal_structure(c))
400  {
401  throw parse_error{
402  std::string{"Encountered an unexpected letter: "} +
403  is_legal_structure.msg.string() +
404  " evaluated to false on " + detail::make_printable(c)};
405  }
406  return c;
407  }) // enforce legal alphabet
408  | view::char_to<alph_type>; // convert to actual target alphabet
409  }
410 };
411 
412 } // namespace seqan3
Provides C++20 additions to the <iterator> header.
Contains various shortcuts for common std::ranges functions.
void read(stream_type &stream, structure_file_input_options< seq_legal_alph_type, structured_seq_combined > const &options, seq_type &seq, id_type &id, bpp_type &bpp, structure_type &structure, energy_type &energy, react_type &react, react_type &react_err, comment_type &comment, offset_type &offset)
Read from the specified stream and back-insert into the given field buffers.
Definition: format_vienna.hpp:147
Provides seqan3::structure_file_input_options.
Provides seqan3::view::take.
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:58
Provides seqan3::view::take_line and seqan3::view::take_line_or_throw.
Helper functions (e.g. conversions) for the structure IO submodule.
Provides seqan3::view::subrange.
Provides seqan3::structure_file_output_options.
Provides seqan3::view::take_until and seqan3::view::take_until_or_throw.
Provides various utility functions.
Provides various utility functions.
Provides seqan3::view::char_to.
The Vienna format (dot bracket notation) for RNA sequences with secondary structure.
Definition: format_vienna.hpp:113
Adaptations of concepts from the Ranges TS.
std::ranges::iterator_range< it_t, sen_t > subrange
Create a view from a pair of iterator and sentinel.
Definition: subrange.hpp:96
Provides seqan3::view::transform.
Provides C++20 additions to the type_traits header.
Provides seqan3::view::to_char.
Provides parse conditions for tokenization.
Provides various metafunctions used by the range module.
static std::vector< std::string > file_extensions
The valid file extensions for this format; note that you can modify this value.
Definition: format_vienna.hpp:129
The options type defines various option members that influence the behaviour of all or some formats...
Definition: input_options.hpp:54