110 template <
typename stream_type,
111 typename seq_legal_alph_type,
112 bool structured_seq_combined,
116 typename structure_type,
117 typename energy_type,
119 typename comment_type,
120 typename offset_type>
126 structure_type & structure,
127 energy_type & energy,
128 react_type & SEQAN3_DOXYGEN_ONLY(react),
129 react_type & SEQAN3_DOXYGEN_ONLY(react_err),
130 comment_type & SEQAN3_DOXYGEN_ONLY(comment),
131 offset_type & SEQAN3_DOXYGEN_ONLY(offset))
133 auto stream_view = detail::istreambuf(stream);
136 auto constexpr is_id = is_char<'>
'>;
137 if (is_id(*begin(stream_view)))
139 if constexpr (!detail::decays_to_ignore_v<id_type>)
141 if (options.truncate_ids)
143 std::ranges::copy(stream_view | std::views::drop_while(is_id || is_blank) // skip leading >
144 | detail::take_until_or_throw(is_cntrl || is_blank)
145 | views::char_to<std::ranges::range_value_t<id_type>>,
146 std::cpp20::back_inserter(id));
147 detail::consume(stream_view | detail::take_line_or_throw);
151 std::ranges::copy(stream_view | std::views::drop_while(is_id || is_blank) // skip leading >
152 | detail::take_line_or_throw
153 | views::char_to<std::ranges::range_value_t<id_type>>,
154 std::cpp20::back_inserter(id));
159 detail::consume(stream_view | detail::take_line_or_throw);
162 else if constexpr (!detail::decays_to_ignore_v<id_type>)
164 auto constexpr is_legal_seq = char_is_valid_for<seq_legal_alph_type>;
165 if (!is_legal_seq(*begin(stream_view))) // if neither id nor seq found: throw
167 throw parse_error{std::string{"Expected to be on beginning of ID or sequence, but "} +
168 is_id.msg + " and char_is_valid_for<" +
169 detail::type_name_as_string<seq_legal_alph_type> + ">" +
170 " evaluated to false on " + detail::make_printable(*begin(stream_view))};
175 if constexpr (!detail::decays_to_ignore_v<seq_type>)
177 auto constexpr is_legal_seq = char_is_valid_for<seq_legal_alph_type>;
178 std::ranges::copy(stream_view | detail::take_line_or_throw // until end of line
179 | std::views::filter(!(is_space || is_digit)) // ignore whitespace and numbers
180 | std::views::transform([is_legal_seq](char const c)
182 if (!is_legal_seq(c)) // enforce legal alphabet
184 throw parse_error{std::string{"Encountered an unexpected letter: "} +
185 "char_is_valid_for<" +
186 detail::type_name_as_string<seq_legal_alph_type> +
187 "> evaluated to false on " +
188 detail::make_printable(c)};
192 | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
193 std::cpp20::back_inserter(seq));
197 detail::consume(stream_view | detail::take_line_or_throw);
200 // READ STRUCTURE (if present)
201 [[maybe_unused]] int64_t structure_length{};
202 if constexpr (!detail::decays_to_ignore_v<structure_type>)
204 if constexpr (structured_seq_combined)
206 assert(std::addressof(seq) == std::addressof(structure));
207 using alph_type = typename std::ranges::range_value_t<structure_type>::structure_alphabet_type;
208 // We need the structure_length parameter to count the length of the structure while reading
209 // because we cannot infer it from the (already resized) structure_seq object.
210 auto res = std::ranges::copy(read_structure<alph_type>(stream_view), std::ranges::begin(structure));
211 structure_length = std::ranges::distance(std::ranges::begin(structure), res.out);
213 if constexpr (!detail::decays_to_ignore_v<bpp_type>)
214 detail::bpp_from_rna_structure<alph_type>(bpp, structure);
218 using alph_type = std::ranges::range_value_t<structure_type>;
219 std::ranges::copy(read_structure<alph_type>(stream_view), std::cpp20::back_inserter(structure));
220 structure_length = std::ranges::distance(structure);
222 if constexpr (!detail::decays_to_ignore_v<bpp_type>)
223 detail::bpp_from_rna_structure<alph_type>(bpp, structure);
226 else if constexpr (!detail::decays_to_ignore_v<bpp_type>)
228 detail::bpp_from_rna_structure<wuss51>(bpp, read_structure<wuss51>(stream_view));
229 structure_length = std::ranges::distance(bpp);
233 detail::consume(stream_view | detail::take_until(is_space)); // until whitespace
236 if constexpr (!detail::decays_to_ignore_v<seq_type> &&
237 !(detail::decays_to_ignore_v<structure_type> && detail::decays_to_ignore_v<bpp_type>))
239 if (std::ranges::distance(seq) != structure_length)
240 throw parse_error{"Found sequence and associated structure of different length."};
243 // READ ENERGY (if present)
244 if constexpr (!detail::decays_to_ignore_v<energy_type>)
246 std::string e_str = stream_view | detail::take_line
247 | std::views::filter(!(is_space || is_char<'(
'> || is_char<')
'>))
248 | views::to<std::string>;
251 size_t num_processed;
252 energy = std::stod(e_str, &num_processed);
253 if (num_processed != e_str.size()) // [[unlikely]]
255 throw parse_error{std::string{"Failed to parse energy value '"} + e_str + "'."};
261 detail::consume(stream_view | detail::take_line);
263 detail::consume(stream_view | detail::take_until(!is_space));
267 template <typename stream_type, // constraints checked by file
268 typename seq_type, // other constraints checked inside function
271 typename structure_type,
272 typename energy_type,
274 typename comment_type,
275 typename offset_type>
276 void write_structure_record(stream_type & stream,
277 structure_file_output_options const & options,
280 bpp_type && SEQAN3_DOXYGEN_ONLY(bpp),
281 structure_type && structure,
282 energy_type && energy,
283 react_type && SEQAN3_DOXYGEN_ONLY(react),
284 react_type && SEQAN3_DOXYGEN_ONLY(react_err),
285 comment_type && SEQAN3_DOXYGEN_ONLY(comment),
286 offset_type && SEQAN3_DOXYGEN_ONLY(offset))
288 std::cpp20::ostreambuf_iterator stream_it{stream};
290 // WRITE ID (optional)
291 if constexpr (!detail::decays_to_ignore_v<id_type>)
293 if (!std::ranges::empty(id))
297 std::ranges::copy(id, stream_it);
298 detail::write_eol(stream_it, options.add_carriage_return);
303 if constexpr (!detail::decays_to_ignore_v<seq_type>)
305 if (std::ranges::empty(seq)) //[[unlikely]]
306 throw std::runtime_error{"The SEQ field may not be empty when writing Vienna files."};
308 std::ranges::copy(seq | views::to_char, stream_it);
309 detail::write_eol(stream_it, options.add_carriage_return);
313 throw std::logic_error{"The SEQ and STRUCTURED_SEQ fields may not both be set to ignore "
314 "when writing Vienna files."};
317 // WRITE STRUCTURE (optional)
318 if constexpr (!detail::decays_to_ignore_v<structure_type>)
320 if (!std::ranges::empty(structure))
321 std::ranges::copy(structure | views::to_char, stream_it);
323 // WRITE ENERGY (optional)
324 if constexpr (!detail::decays_to_ignore_v<energy_type>)
328// TODO(joergi-w) enable the following when std::to_chars is implemented for float types
329// auto [endptr, ec] = std::to_chars(str.data(),
330// str.data() + str.size(),
332// std::chars_format::fixed,
333// options.precision);
334// if (ec == std::errc())
335// std::ranges::copy(str.data(), endptr, stream_it);
337// throw std::runtime_error{"The energy could not be transformed into a string."};
342 std::array<char, 100> str;
343 int len = std::snprintf(str.data(), 100, "%.*f", options.precision, energy);
344 if (len < 0 || len >= 100)
345 throw std::runtime_error{"The energy could not be transformed into a string."};
346 std::ranges::copy(str.data(), str.data() + len, stream_it);
351 detail::write_eol(stream_it, options.add_carriage_return);
353 else if constexpr (!detail::decays_to_ignore_v<energy_type>)
355 throw std::logic_error{"The ENERGY field cannot be written to a Vienna file without providing STRUCTURE."};
366 template <typename alph_type, typename stream_view_type>
367 auto read_structure(stream_view_type & stream_view)
369 auto constexpr is_legal_structure = char_is_valid_for<alph_type>;
370 return stream_view | detail::take_until(is_space) // until whitespace
371 | std::views::transform([is_legal_structure](char const c)
373 if (!is_legal_structure(c))
376 std::string{"Encountered an unexpected letter: char_is_valid_for<"} +
377 detail::type_name_as_string<alph_type> +
378 "> evaluated to false on " + detail::make_printable(c)};
381 }) // enforce legal alphabet
382 | views::char_to<alph_type>; // convert to actual target alphabet
386} // namespace seqan3::detail
The <algorithm> header from C++20's standard library.
Core alphabet concept and free function/type trait wrappers.
Provides alphabet adaptations for standard char types.
Provides seqan3::views::char_to.
Provides various utility functions.
Provides various transformation traits used by the range module.
Provides various utility functions.
Provides seqan3::detail::istreambuf.
The main SeqAn3 namespace.
Definition: cigar_operation_table.hpp:2
SeqAn specific customisations in the standard namespace.
Provides character predicates for tokenisation.
The <ranges> header from C++20's standard library.
Provides seqan3::structure_file_output_options.
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.
Provides seqan3::views::to_char.
Provides traits to inspect some information of a type, for example its name.
Provides the WUSS format for RNA structure.