SeqAn3
detail.hpp
Go to the documentation of this file.
1 // ============================================================================
2 // SeqAn - The Library for Sequence Analysis
3 // ============================================================================
4 //
5 // Copyright (c) 2006-2018, Knut Reinert & Freie Universitaet Berlin
6 // Copyright (c) 2016-2018, Knut Reinert & MPI Molekulare Genetik
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // * Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // * Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // * Neither the name of Knut Reinert or the FU Berlin nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 // ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
25 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 // DAMAGE.
32 //
33 // ============================================================================
34 
40 #pragma once
41 
42 #include <map>
43 #include <stack>
44 
46 
47 namespace seqan3::detail
48 {
61 template <typename structure_alph_type, typename bpp_type, std::ranges::Range structure_type>
62 inline
63 void bpp_from_rna_structure(bpp_type & bpp, structure_type const & structure, double weight = 1.)
64 {
65  if constexpr (!rna_structure_concept<structure_alph_type>)
66  throw parse_error{"Cannot create base pair probabilities from a structure that is not RNA structure."};
67 
68  bpp.clear();
70  bpp.reserve(size(structure));
71 
72  std::stack<size_t> brackets[max_pseudoknot_depth_v<structure_alph_type>];
73  size_t pos = 0ul;
74  for (structure_alph_type symbol : structure)
75  {
76  bpp.push_back({});
77  uint8_t const id = pseudoknot_id(symbol).value_or(0);
78 
79  if (symbol.is_pair_open())
80  {
81  brackets[id].push(pos);
82  }
83  else if (symbol.is_pair_close())
84  {
85  if (!brackets[id].empty())
86  {
87  bpp[pos].emplace(weight, brackets[id].top());
88  bpp[brackets[id].top()].emplace(weight, pos);
89  brackets[id].pop();
90  }
91  else
92  {
93  throw parse_error{std::string{"Invalid bracket notation: Unpaired closing bracket at position "}
94  + std::to_string(pos) + "."};
95  };
96  }
97  // no actions for unpaired
98  ++pos;
99  }
100  for (uint8_t id = 0u; id < max_pseudoknot_depth_v<structure_alph_type>; ++id)
101  {
102  if (!brackets[id].empty())
103  {
104  throw parse_error{std::string{"Invalid bracket notation: Unpaired opening bracket at position "}
105  + std::to_string(brackets[id].top()) + "."};
106  }
107  }
108 }
109 
110 } // namespace seqan3::detail
Provides seqan3::rna_structure_concept.
::ranges::size size
Alias for ranges::size. Obtains the size of a range whose size can be calculated in constant time...
Definition: ranges:195
Definition: aligned_sequence_concept.hpp:288
::ranges::empty empty
Alias for ranges::empty. Checks whether a range is empty.
Definition: ranges:205
Specifies the requirements of a Range type that knows its size in constant time with the size functio...