SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
detail.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2021, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2021, 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 <stack>
17 
19 #include <seqan3/io/exception.hpp>
20 #include <seqan3/std/ranges>
21 
22 namespace seqan3::detail
23 {
36 template <typename structure_alph_type, typename bpp_type, std::ranges::range structure_type>
37 inline
38 void bpp_from_rna_structure(bpp_type & bpp, structure_type const & structure, double weight = 1.)
39 {
41  throw parse_error{"Cannot create base pair probabilities from a structure that is not RNA structure."};
42 
43  bpp.clear();
44  if constexpr (std::ranges::sized_range<structure_type>)
45  bpp.reserve(size(structure));
46 
47  std::stack<size_t> brackets[max_pseudoknot_depth<structure_alph_type>];
48  size_t pos = 0ul;
49  for (structure_alph_type symbol : structure)
50  {
51  bpp.push_back({});
52  uint8_t const id = pseudoknot_id(symbol).value_or(0);
53 
54  if (symbol.is_pair_open())
55  {
56  brackets[id].push(pos);
57  }
58  else if (symbol.is_pair_close())
59  {
60  if (!brackets[id].empty())
61  {
62  bpp[pos].emplace(weight, brackets[id].top());
63  bpp[brackets[id].top()].emplace(weight, pos);
64  brackets[id].pop();
65  }
66  else
67  {
68  throw parse_error{std::string{"Invalid bracket notation: Unpaired closing bracket at position "}
69  + std::to_string(pos) + "."};
70  };
71  }
72  // no actions for unpaired
73  ++pos;
74  }
75  for (uint8_t id = 0u; id < max_pseudoknot_depth<structure_alph_type>; ++id)
76  {
77  if (!brackets[id].empty())
78  {
79  throw parse_error{std::string{"Invalid bracket notation: Unpaired opening bracket at position "}
80  + std::to_string(brackets[id].top()) + "."};
81  }
82  }
83 }
84 
85 } // namespace seqan3::detail
Provides seqan3::rna_structure_alphabet.
T empty(T... args)
@ bpp
Base pair probability matrix of interactions, usually a matrix of float numbers.
@ id
The identifier, usually a string.
constexpr auto pseudoknot_id
Retrieve an id for the level of a pseudoknotted interaction (also known as 'page number').
Definition: concept.hpp:500
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:151
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)
Adaptations of concepts from the Ranges TS.
T to_string(T... args)
T top(T... args)