SeqAn3
The Modern C++ library for sequence analysis.
detail.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2019, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2019, 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 
20 namespace seqan3::detail
21 {
34 template <typename structure_alph_type, typename bpp_type, std::ranges::Range structure_type>
35 inline
36 void bpp_from_rna_structure(bpp_type & bpp, structure_type const & structure, double weight = 1.)
37 {
38  if constexpr (!RnaStructureAlphabet<structure_alph_type>)
39  throw parse_error{"Cannot create base pair probabilities from a structure that is not RNA structure."};
40 
41  bpp.clear();
43  bpp.reserve(size(structure));
44 
45  std::stack<size_t> brackets[max_pseudoknot_depth<structure_alph_type>];
46  size_t pos = 0ul;
47  for (structure_alph_type symbol : structure)
48  {
49  bpp.push_back({});
50  uint8_t const id = pseudoknot_id(symbol).value_or(0);
51 
52  if (symbol.is_pair_open())
53  {
54  brackets[id].push(pos);
55  }
56  else if (symbol.is_pair_close())
57  {
58  if (!brackets[id].empty())
59  {
60  bpp[pos].emplace(weight, brackets[id].top());
61  bpp[brackets[id].top()].emplace(weight, pos);
62  brackets[id].pop();
63  }
64  else
65  {
66  throw parse_error{std::string{"Invalid bracket notation: Unpaired closing bracket at position "}
67  + std::to_string(pos) + "."};
68  };
69  }
70  // no actions for unpaired
71  ++pos;
72  }
73  for (uint8_t id = 0u; id < max_pseudoknot_depth<structure_alph_type>; ++id)
74  {
75  if (!brackets[id].empty())
76  {
77  throw parse_error{std::string{"Invalid bracket notation: Unpaired opening bracket at position "}
78  + std::to_string(brackets[id].top()) + "."};
79  }
80  }
81 }
82 
83 } // namespace seqan3::detail
T to_string(T... args)
T pop(T... args)
T top(T... args)
::ranges::size size
Alias for ranges::size. Obtains the size of a range whose size can be calculated in constant time...
Definition: ranges:189
T push(T... args)
Provides seqan3::RnaStructureAlphabet.
constexpr auto pseudoknot_id
Retrieve an id for the level of a pseudoknotted interaction (also known as &#39;page number&#39;).
Definition: concept.hpp:448
Definition: aligned_sequence_concept.hpp:35
::ranges::empty empty
Alias for ranges::empty. Checks whether a range is empty.
Definition: ranges:194
Specifies the requirements of a Range type that knows its size in constant time with the size functio...