SeqAn3  3.2.0-rc.1
The Modern C++ library for sequence analysis.
io/structure_file/detail.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2022, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2022, Knut Reinert & MPI für molekulare Genetik
4 // This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5 // shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6 // -----------------------------------------------------------------------------------------------------
7 
13 #pragma once
14 
15 #include <map>
16 #include <ranges>
17 #include <stack>
18 
20 #include <seqan3/io/exception.hpp>
21 
22 namespace seqan3::detail
23 {
36 template <typename structure_alph_type, typename bpp_type, std::ranges::range structure_type>
37 inline void bpp_from_rna_structure(bpp_type & bpp, structure_type const & structure, double weight = 1.)
38 {
40  throw parse_error{"Cannot create base pair probabilities from a structure that is not RNA structure."};
41 
42  bpp.clear();
43  if constexpr (std::ranges::sized_range<structure_type>)
44  bpp.reserve(size(structure));
45 
46  std::stack<size_t> brackets[max_pseudoknot_depth<structure_alph_type>];
47  size_t pos = 0ul;
48  for (structure_alph_type symbol : structure)
49  {
50  bpp.push_back({});
51  uint8_t const id = pseudoknot_id(symbol).value_or(0);
52 
53  if (symbol.is_pair_open())
54  {
55  brackets[id].push(pos);
56  }
57  else if (symbol.is_pair_close())
58  {
59  if (!brackets[id].empty())
60  {
61  bpp[pos].emplace(weight, brackets[id].top());
62  bpp[brackets[id].top()].emplace(weight, pos);
63  brackets[id].pop();
64  }
65  else
66  {
67  throw parse_error{std::string{"Invalid bracket notation: Unpaired closing bracket at position "}
68  + std::to_string(pos) + "."};
69  };
70  }
71  // no actions for unpaired
72  ++pos;
73  }
74  for (uint8_t id = 0u; id < max_pseudoknot_depth<structure_alph_type>; ++id)
75  {
76  if (!brackets[id].empty())
77  {
78  throw parse_error{std::string{"Invalid bracket notation: Unpaired opening bracket at position "}
79  + std::to_string(brackets[id].top()) + "."};
80  }
81  }
82 }
83 
84 } // 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:459
@ 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:146
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)
The <ranges> header from C++20's standard library.
T to_string(T... args)
T top(T... args)