SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
io/structure_file/detail.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 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{
33template <typename structure_alph_type, typename bpp_type, std::ranges::range structure_type>
34inline void bpp_from_rna_structure(bpp_type & bpp, structure_type const & structure, double weight = 1.)
35{
37 throw parse_error{"Cannot create base pair probabilities from a structure that is not RNA structure."};
38
39 bpp.clear();
40 if constexpr (std::ranges::sized_range<structure_type>)
41 bpp.reserve(size(structure));
42
43 std::stack<size_t> brackets[max_pseudoknot_depth<structure_alph_type>];
44 size_t pos = 0ul;
45 for (structure_alph_type symbol : structure)
46 {
47 bpp.push_back({});
48 uint8_t const id = pseudoknot_id(symbol).value_or(0);
49
50 if (symbol.is_pair_open())
51 {
52 brackets[id].push(pos);
53 }
54 else if (symbol.is_pair_close())
55 {
56 if (!brackets[id].empty())
57 {
58 bpp[pos].emplace(weight, brackets[id].top());
59 bpp[brackets[id].top()].emplace(weight, pos);
60 brackets[id].pop();
61 }
62 else
63 {
64 throw parse_error{std::string{"Invalid bracket notation: Unpaired closing bracket at position "}
65 + std::to_string(pos) + "."};
66 };
67 }
68 // no actions for unpaired
69 ++pos;
70 }
71 for (uint8_t id = 0u; id < max_pseudoknot_depth<structure_alph_type>; ++id)
72 {
73 if (!brackets[id].empty())
74 {
75 throw parse_error{std::string{"Invalid bracket notation: Unpaired opening bracket at position "}
76 + std::to_string(brackets[id].top()) + "."};
77 }
78 }
79}
80
81} // 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:456
@ 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