SeqAn3 3.4.0-rc.4
The Modern C++ library for sequence analysis.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
io/structure_file/detail.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <map>
13#include <ranges>
14#include <stack>
15
18
19namespace seqan3::detail
20{
32template <typename structure_alph_type, typename bpp_type, std::ranges::range structure_type>
33inline void bpp_from_rna_structure(bpp_type & bpp, structure_type const & structure, double weight = 1.)
34{
36 throw parse_error{"Cannot create base pair probabilities from a structure that is not RNA structure."};
37
38 bpp.clear();
39 if constexpr (std::ranges::sized_range<structure_type>)
40 bpp.reserve(size(structure));
41
42 std::stack<size_t> brackets[max_pseudoknot_depth<structure_alph_type>];
43 size_t pos = 0ul;
44 for (structure_alph_type symbol : structure)
45 {
46 bpp.push_back({});
47 uint8_t const id = pseudoknot_id(symbol).value_or(0);
48
49 if (symbol.is_pair_open())
50 {
51 brackets[id].push(pos);
52 }
53 else if (symbol.is_pair_close())
54 {
55 if (!brackets[id].empty())
56 {
57 bpp[pos].emplace(weight, brackets[id].top());
58 bpp[brackets[id].top()].emplace(weight, pos);
59 brackets[id].pop();
60 }
61 else
62 {
63 throw parse_error{std::string{"Invalid bracket notation: Unpaired closing bracket at position "}
64 + std::to_string(pos) + "."};
65 };
66 }
67 // no actions for unpaired
68 ++pos;
69 }
70 for (uint8_t id = 0u; id < max_pseudoknot_depth<structure_alph_type>; ++id)
71 {
72 if (!brackets[id].empty())
73 {
74 throw parse_error{std::string{"Invalid bracket notation: Unpaired opening bracket at position "}
75 + std::to_string(brackets[id].top()) + "."};
76 }
77 }
78}
79
80} // namespace seqan3::detail
Provides seqan3::rna_structure_alphabet.
T empty(T... args)
constexpr auto pseudoknot_id
Retrieve an id for the level of a pseudoknotted interaction (also known as 'page number').
Definition alphabet/structure/concept.hpp:454
@ structure
Fixed interactions, usually a string of structure alphabet characters.
@ bpp
Base pair probability matrix of interactions, usually a matrix of float numbers.
@ id
The identifier, usually a string.
constexpr size_t size
The size of a type pack.
Definition type_pack/traits.hpp:143
A concept that indicates whether an alphabet represents RNA structure.
Provides exceptions used in the I/O module.
T pop(T... args)
T push(T... args)
T to_string(T... args)
T top(T... args)
Hide me