21 #include <string_view>
108 template <
typename stream_type,
109 typename seq_legal_alph_type,
110 bool structured_seq_combined,
114 typename structure_type,
115 typename energy_type,
117 typename comment_type,
118 typename offset_type>
124 structure_type & structure,
125 energy_type & energy,
126 react_type & SEQAN3_DOXYGEN_ONLY(react),
127 react_type & SEQAN3_DOXYGEN_ONLY(react_err),
128 comment_type & SEQAN3_DOXYGEN_ONLY(comment),
129 offset_type & SEQAN3_DOXYGEN_ONLY(offset))
131 auto stream_view = detail::istreambuf(stream);
134 auto constexpr is_id = is_char<'>
'>;
135 if (is_id(*begin(stream_view)))
137 if constexpr (!detail::decays_to_ignore_v<id_type>)
139 if (options.truncate_ids)
141 std::ranges::copy(stream_view | std::views::drop_while(is_id || is_blank) // skip leading >
142 | detail::take_until_or_throw(is_cntrl || is_blank)
143 | views::char_to<std::ranges::range_value_t<id_type>>,
144 std::cpp20::back_inserter(id));
145 detail::consume(stream_view | detail::take_line_or_throw);
149 std::ranges::copy(stream_view | std::views::drop_while(is_id || is_blank) // skip leading >
150 | detail::take_line_or_throw
151 | views::char_to<std::ranges::range_value_t<id_type>>,
152 std::cpp20::back_inserter(id));
157 detail::consume(stream_view | detail::take_line_or_throw);
160 else if constexpr (!detail::decays_to_ignore_v<id_type>)
162 auto constexpr is_legal_seq = char_is_valid_for<seq_legal_alph_type>;
163 if (!is_legal_seq(*begin(stream_view))) // if neither id nor seq found: throw
165 throw parse_error{std::string{"Expected to be on beginning of ID or sequence, but "} +
166 is_id.msg + " and char_is_valid_for<" +
167 detail::type_name_as_string<seq_legal_alph_type> + ">" +
168 " evaluated to false on " + detail::make_printable(*begin(stream_view))};
173 if constexpr (!detail::decays_to_ignore_v<seq_type>)
175 auto constexpr is_legal_seq = char_is_valid_for<seq_legal_alph_type>;
176 std::ranges::copy(stream_view | detail::take_line_or_throw // until end of line
177 | std::views::filter(!(is_space || is_digit)) // ignore whitespace and numbers
178 | std::views::transform([is_legal_seq](char const c)
180 if (!is_legal_seq(c)) // enforce legal alphabet
182 throw parse_error{std::string{"Encountered an unexpected letter: "} +
183 "char_is_valid_for<" +
184 detail::type_name_as_string<seq_legal_alph_type> +
185 "> evaluated to false on " +
186 detail::make_printable(c)};
190 | views::char_to<std::ranges::range_value_t<seq_type>>, // convert to actual target alphabet
191 std::cpp20::back_inserter(seq));
195 detail::consume(stream_view | detail::take_line_or_throw);
198 // READ STRUCTURE (if present)
199 [[maybe_unused]] int64_t structure_length{};
200 if constexpr (!detail::decays_to_ignore_v<structure_type>)
202 if constexpr (structured_seq_combined)
204 assert(std::addressof(seq) == std::addressof(structure));
205 using alph_type = typename std::ranges::range_value_t<structure_type>::structure_alphabet_type;
206 // We need the structure_length parameter to count the length of the structure while reading
207 // because we cannot infer it from the (already resized) structure_seq object.
208 auto res = std::ranges::copy(read_structure<alph_type>(stream_view), std::ranges::begin(structure));
209 structure_length = std::ranges::distance(std::ranges::begin(structure), res.out);
211 if constexpr (!detail::decays_to_ignore_v<bpp_type>)
212 detail::bpp_from_rna_structure<alph_type>(bpp, structure);
216 using alph_type = std::ranges::range_value_t<structure_type>;
217 std::ranges::copy(read_structure<alph_type>(stream_view), std::cpp20::back_inserter(structure));
218 structure_length = std::ranges::distance(structure);
220 if constexpr (!detail::decays_to_ignore_v<bpp_type>)
221 detail::bpp_from_rna_structure<alph_type>(bpp, structure);
224 else if constexpr (!detail::decays_to_ignore_v<bpp_type>)
226 detail::bpp_from_rna_structure<wuss51>(bpp, read_structure<wuss51>(stream_view));
227 structure_length = std::ranges::distance(bpp);
231 detail::consume(stream_view | detail::take_until(is_space)); // until whitespace
234 if constexpr (!detail::decays_to_ignore_v<seq_type> &&
235 !(detail::decays_to_ignore_v<structure_type> && detail::decays_to_ignore_v<bpp_type>))
237 if (std::ranges::distance(seq) != structure_length)
238 throw parse_error{"Found sequence and associated structure of different length."};
241 // READ ENERGY (if present)
242 if constexpr (!detail::decays_to_ignore_v<energy_type>)
244 std::string e_str = stream_view | detail::take_line
245 | std::views::filter(!(is_space || is_char<'(
'> || is_char<')
'>))
246 | views::to<std::string>;
249 size_t num_processed;
250 energy = std::stod(e_str, &num_processed);
251 if (num_processed != e_str.size()) // [[unlikely]]
253 throw parse_error{std::string{"Failed to parse energy value '"} + e_str + "'."};
259 detail::consume(stream_view | detail::take_line);
261 detail::consume(stream_view | detail::take_until(!is_space));
265 template <typename stream_type, // constraints checked by file
266 typename seq_type, // other constraints checked inside function
269 typename structure_type,
270 typename energy_type,
272 typename comment_type,
273 typename offset_type>
274 void write_structure_record(stream_type & stream,
275 structure_file_output_options const & options,
278 bpp_type && SEQAN3_DOXYGEN_ONLY(bpp),
279 structure_type && structure,
280 energy_type && energy,
281 react_type && SEQAN3_DOXYGEN_ONLY(react),
282 react_type && SEQAN3_DOXYGEN_ONLY(react_err),
283 comment_type && SEQAN3_DOXYGEN_ONLY(comment),
284 offset_type && SEQAN3_DOXYGEN_ONLY(offset))
286 std::cpp20::ostreambuf_iterator stream_it{stream};
288 // WRITE ID (optional)
289 if constexpr (!detail::decays_to_ignore_v<id_type>)
291 if (!std::ranges::empty(id))
295 std::ranges::copy(id, stream_it);
296 detail::write_eol(stream_it, options.add_carriage_return);
301 if constexpr (!detail::decays_to_ignore_v<seq_type>)
303 if (std::ranges::empty(seq)) //[[unlikely]]
304 throw std::runtime_error{"The SEQ field may not be empty when writing Vienna files."};
306 std::ranges::copy(seq | views::to_char, stream_it);
307 detail::write_eol(stream_it, options.add_carriage_return);
311 throw std::logic_error{"The SEQ and STRUCTURED_SEQ fields may not both be set to ignore "
312 "when writing Vienna files."};
315 // WRITE STRUCTURE (optional)
316 if constexpr (!detail::decays_to_ignore_v<structure_type>)
318 if (!std::ranges::empty(structure))
319 std::ranges::copy(structure | views::to_char, stream_it);
321 // WRITE ENERGY (optional)
322 if constexpr (!detail::decays_to_ignore_v<energy_type>)
326 // TODO(joergi-w) enable the following when std::to_chars is implemented for float types
327 // auto [endptr, ec] = std::to_chars(str.data(),
328 // str.data() + str.size(),
330 // std::chars_format::fixed,
331 // options.precision);
332 // if (ec == std::errc())
333 // std::ranges::copy(str.data(), endptr, stream_it);
335 // throw std::runtime_error{"The energy could not be transformed into a string."};
340 std::array<char, 100> str;
341 int len = std::snprintf(str.data(), 100, "%.*f", options.precision, energy);
342 if (len < 0 || len >= 100)
343 throw std::runtime_error{"The energy could not be transformed into a string."};
344 std::ranges::copy(str.data(), str.data() + len, stream_it);
349 detail::write_eol(stream_it, options.add_carriage_return);
351 else if constexpr (!detail::decays_to_ignore_v<energy_type>)
353 throw std::logic_error{"The ENERGY field cannot be written to a Vienna file without providing STRUCTURE."};
364 template <typename alph_type, typename stream_view_type>
365 auto read_structure(stream_view_type & stream_view)
367 auto constexpr is_legal_structure = char_is_valid_for<alph_type>;
368 return stream_view | detail::take_until(is_space) // until whitespace
369 | std::views::transform([is_legal_structure](char const c)
371 if (!is_legal_structure(c))
374 std::string{"Encountered an unexpected letter: char_is_valid_for<"} +
375 detail::type_name_as_string<alph_type> +
376 "> evaluated to false on " + detail::make_printable(c)};
379 }) // enforce legal alphabet
380 | views::char_to<alph_type>; // convert to actual target alphabet
384 } // namespace seqan3::detail
Adaptations of algorithms from the Ranges TS.
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.
Provides C++20 additions to the <iterator> header.
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
SeqAn specific customisations in the standard namespace.
Definition: affine_cell_proxy.hpp:438
Provides character predicates for tokenisation.
Adaptations of concepts from the Ranges TS.
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 C++20 additions to the type_traits header.
Provides the WUSS format for RNA structure.