SeqAn3  3.0.2
The Modern C++ library for sequence analysis.
wuss.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2020, 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 <cmath>
16 #include <vector>
17 
21 
22 // ------------------------------------------------------------------
23 // wuss
24 // ------------------------------------------------------------------
25 
26 namespace seqan3
27 {
28 
56 template <uint8_t SIZE = 51>
57 class wuss : public alphabet_base<wuss<SIZE>, SIZE>
58 {
59  static_assert(SIZE >= 15 && SIZE <= 67 && SIZE % 2 == 1,
60  "The wuss<> alphabet size must be an odd number in range 15..67.");
61 
62 private:
64  using base_t = alphabet_base<wuss<SIZE>, SIZE>;
65 
67  friend base_t;
68 
69 public:
71  using base_t::to_rank;
72  using base_t::to_char;
73  using typename base_t::rank_type;
74  using typename base_t::char_type;
75 
79  constexpr wuss() noexcept = default;
80  constexpr wuss(wuss const &) noexcept = default;
81  constexpr wuss(wuss &&) noexcept = default;
82  constexpr wuss & operator=(wuss const &) noexcept = default;
83  constexpr wuss & operator=(wuss &&) noexcept = default;
84  ~wuss() noexcept = default;
85 
93  constexpr bool is_pair_open() const noexcept
94  {
95  return interaction_tab[to_rank()] < 0;
96  }
97 
101  constexpr bool is_pair_close() const noexcept
102  {
103  return interaction_tab[to_rank()] > 0;
104  }
105 
109  constexpr bool is_unpaired() const noexcept
110  {
111  return interaction_tab[to_rank()] == 0;
112  }
113 
117  // formula: (alphabet size - 7 unpaired characters) / 2, as every bracket exists as opening/closing pair
118  static constexpr uint8_t max_pseudoknot_depth{static_cast<uint8_t>((alphabet_size - 7) / 2)};
119 
125  constexpr std::optional<uint8_t> pseudoknot_id() const noexcept
126  {
127  if (interaction_tab[to_rank()] != 0)
128  return std::abs(interaction_tab[to_rank()]) - 1;
129  else
130  return std::nullopt; // unpaired
131  }
133 
134 protected:
137  static constexpr std::array<char_type, alphabet_size> rank_to_char
138  {
139  [] () constexpr
140  {
142  {
143  '.', ':', ',', '-', '_', '~', ';', '<', '(', '[', '{', '>', ')', ']', '}'
144  };
145 
146  // pseudoknot letters
147  for (rank_type rnk = 15u; rnk + 1u < alphabet_size; rnk += 2u)
148  {
149  char_type const off = static_cast<char_type>((rnk - 15u) / 2u);
150  chars[rnk] = 'A' + off;
151  chars[rnk + 1u] = 'a' + off;
152  }
153 
154  return chars;
155  } ()
156  };
157 
159  static constexpr std::array<rank_type, 256> char_to_rank
160  {
161  [] () constexpr
162  {
163  std::array<rank_type, 256> rank_table{};
164 
165  // initialize with unpaired (std::array::fill unfortunately not constexpr)
166  for (rank_type & rnk : rank_table)
167  rnk = 6u;
168 
169  // set alphabet values
170  for (rank_type rnk = 0u; rnk < alphabet_size; ++rnk)
171  rank_table[rank_to_char[rnk]] = rnk;
172  return rank_table;
173  } ()
174  };
175 
179  static std::array<int8_t, SIZE> const interaction_tab;
180 };
181 
182 template <uint8_t SIZE>
183 constexpr std::array<int8_t, SIZE> wuss<SIZE>::interaction_tab = [] () constexpr
184 {
185  std::array<int8_t, alphabet_size> interaction_table{};
186  int cnt_open = 0;
187  int cnt_close = 0;
188 
189  for (rank_type rnk = 0u; rnk <= 6u; ++rnk)
190  {
191  interaction_table[rnk] = 0;
192  }
193 
194  for (rank_type rnk = 7u; rnk <= 10u; ++rnk)
195  {
196  interaction_table[rnk] = --cnt_open;
197  }
198 
199  for (rank_type rnk = 11u; rnk <= 14u; ++rnk)
200  {
201  interaction_table[rnk] = ++cnt_close;
202  }
203 
204  for (rank_type rnk = 15u; rnk + 1u < alphabet_size; rnk += 2u)
205  {
206  interaction_table[rnk] = --cnt_open;
207  interaction_table[rnk + 1u] = ++cnt_close;
208  }
209 
210  return interaction_table;
211 } ();
212 
215 using wuss51 = wuss<51>;
216 
230 inline std::vector<wuss51> operator""_wuss51(const char * str, std::size_t len)
231 {
233  vec.resize(len);
234 
235  for (size_t idx = 0ul; idx < len; ++idx)
236  vec[idx].assign_char(str[idx]);
237 
238  return vec;
239 }
240 
250 constexpr wuss51 operator""_wuss51(char const ch) noexcept
251 {
252  return wuss51{}.assign_char(ch);
253 }
254 
256 
257 } // namespace seqan3
seqan3::wuss::char_type
std::conditional_t< std::same_as< char_t, void >, char, char_t > char_type
The char representation; conditional needed to make semi alphabet definitions legal.
Definition: alphabet_base.hpp:62
std::vector::resize
T resize(T... args)
concept.hpp
Provides seqan3::rna_structure_alphabet.
alphabet_base.hpp
Provides seqan3::alphabet_base.
seqan3::alphabet_base::alphabet_size
static constexpr detail::min_viable_uint_t< size > alphabet_size
The size of the alphabet, i.e. the number of different values it can take.
Definition: alphabet_base.hpp:176
seqan3::wuss::rank_type
detail::min_viable_uint_t< size - 1 > rank_type
The type of the alphabet when represented as a number (e.g. via to_rank()).
Definition: alphabet_base.hpp:64
vector
seqan3::alphabet_base::to_char
constexpr char_type to_char() const noexcept
Return the letter as a character of char_type.
Definition: alphabet_base.hpp:96
seqan3::wuss::max_pseudoknot_depth
static constexpr uint8_t max_pseudoknot_depth
The ability of this alphabet to represent pseudoknots, i.e. crossing interactions,...
Definition: wuss.hpp:118
seqan3::alphabet_base
A CRTP-base that makes defining a custom alphabet easier.
Definition: alphabet_base.hpp:54
cmath
seqan3::alphabet_base::assign_char
constexpr derived_type & assign_char(char_type const c) noexcept
Assign from a character, implicitly converts invalid characters.
Definition: alphabet_base.hpp:142
seqan3::wuss::pseudoknot_id
constexpr std::optional< uint8_t > pseudoknot_id() const noexcept
Get an identifier for a pseudoknotted interaction, where opening and closing brackets of the same typ...
Definition: wuss.hpp:125
seqan3::wuss::is_pair_close
constexpr bool is_pair_close() const noexcept
Check whether the character represents a leftward interaction in an RNA structure.
Definition: wuss.hpp:101
seqan3::alphabet_base::rank_type
detail::min_viable_uint_t< size - 1 > rank_type
The type of the alphabet when represented as a number (e.g. via to_rank()).
Definition: alphabet_base.hpp:64
std::array< char_type, alphabet_size >
seqan3
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
seqan3::wuss::alphabet_size
static constexpr detail::min_viable_uint_t< size > alphabet_size
The size of the alphabet, i.e. the number of different values it can take.
Definition: alphabet_base.hpp:176
seqan3::alphabet_base::char_type
std::conditional_t< std::same_as< char_t, void >, char, char_t > char_type
The char representation; conditional needed to make semi alphabet definitions legal.
Definition: alphabet_base.hpp:62
seqan3::wuss::is_pair_open
constexpr bool is_pair_open() const noexcept
Check whether the character represents a rightward interaction in an RNA structure.
Definition: wuss.hpp:93
seqan3::wuss::is_unpaired
constexpr bool is_unpaired() const noexcept
Check whether the character represents an unpaired position in an RNA structure.
Definition: wuss.hpp:109
transform.hpp
Provides utilities for modifying characters.
seqan3::wuss::wuss
constexpr wuss() noexcept=default
Defaulted.
std::optional
std::size_t
seqan3::alphabet_base::to_rank
constexpr rank_type to_rank() const noexcept
Return the letter's numeric value (rank in the alphabet).
Definition: alphabet_base.hpp:118
seqan3::alphabet_size
constexpr auto alphabet_size
A type trait that holds the size of a (semi-)alphabet.
Definition: concept.hpp:707
seqan3::wuss::to_rank
constexpr rank_type to_rank() const noexcept
Return the letter's numeric value (rank in the alphabet).
Definition: alphabet_base.hpp:118
seqan3::wuss
The WUSS structure alphabet of the characters .<>:,-_~;()[]{}AaBbCcDd...
Definition: wuss.hpp:58