Loading...
Searching...
No Matches
io/structure_file/detail.hpp
Go to the documentation of this file.
// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
#include <map>
#include <ranges>
#include <stack>
#include <seqan3/alphabet/structure/concept.hpp>
#include <seqan3/io/exception.hpp>
namespace seqan3::detail
{
template <typename structure_alph_type, typename bpp_type, std::ranges::range structure_type>
inline void bpp_from_rna_structure(bpp_type & bpp, structure_type const & structure, double weight = 1.)
{
if constexpr (!rna_structure_alphabet<structure_alph_type>)
throw parse_error{"Cannot create base pair probabilities from a structure that is not RNA structure."};
bpp.clear();
if constexpr (std::ranges::sized_range<structure_type>)
bpp.reserve(size(structure));
std::stack<size_t> brackets[max_pseudoknot_depth<structure_alph_type>];
size_t pos = 0ul;
for (structure_alph_type symbol : structure)
{
bpp.push_back({});
uint8_t const id = pseudoknot_id(symbol).value_or(0);
if (symbol.is_pair_open())
{
brackets[id].push(pos);
}
else if (symbol.is_pair_close())
{
if (!brackets[id].empty())
{
bpp[pos].emplace(weight, brackets[id].top());
bpp[brackets[id].top()].emplace(weight, pos);
brackets[id].pop();
}
else
{
throw parse_error{std::string{"Invalid bracket notation: Unpaired closing bracket at position "}
+ std::to_string(pos) + "."};
};
}
// no actions for unpaired
++pos;
}
for (uint8_t id = 0u; id < max_pseudoknot_depth<structure_alph_type>; ++id)
{
if (!brackets[id].empty())
{
throw parse_error{std::string{"Invalid bracket notation: Unpaired opening bracket at position "}
+ std::to_string(brackets[id].top()) + "."};
}
}
}
} // namespace seqan3::detail
concept.hpp
Provides seqan3::rna_structure_alphabet.
std::empty
T empty(T... args)
seqan3::pseudoknot_id
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
seqan3::max_pseudoknot_depth
constexpr auto max_pseudoknot_depth
A type trait that holds the ability of the structure alphabet to represent pseudoknots,...
Definition alphabet/structure/concept.hpp:371
seqan3::field::structure
@ structure
Fixed interactions, usually a string of structure alphabet characters.
Definition record.hpp:70
seqan3::field::bpp
@ bpp
Base pair probability matrix of interactions, usually a matrix of float numbers.
Definition record.hpp:69
seqan3::field::id
@ id
The identifier, usually a string.
Definition record.hpp:63
seqan3::pack_traits::size
constexpr size_t size
The size of a type pack.
Definition type_pack/traits.hpp:143
exception.hpp
Provides exceptions used in the I/O module.
map
std::stack::pop
T pop(T... args)
std::stack::push
T push(T... args)
stack
std::to_string
T to_string(T... args)
std::stack::top
T top(T... args)
Hide me