SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
dot_bracket3.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 <vector>
16 
20 
21 // ------------------------------------------------------------------
22 // dot_bracket3
23 // ------------------------------------------------------------------
24 
25 namespace seqan3
26 {
27 
53 class dot_bracket3 : public alphabet_base<dot_bracket3, 3>
54 {
55 private:
58 
60  friend base_t;
61 
62 public:
66  constexpr dot_bracket3() noexcept = default;
67  constexpr dot_bracket3(dot_bracket3 const &) noexcept = default;
68  constexpr dot_bracket3(dot_bracket3 &&) noexcept = default;
69  constexpr dot_bracket3 & operator=(dot_bracket3 const &) noexcept = default;
70  constexpr dot_bracket3 & operator=(dot_bracket3 &&) noexcept = default;
71  ~dot_bracket3() noexcept = default;
72 
74 
77 
83  constexpr bool is_pair_open() const noexcept
84  {
85  return to_rank() == 1u;
86  }
87 
93  constexpr bool is_pair_close() const noexcept
94  {
95  return to_rank() == 2u;
96  }
97 
103  constexpr bool is_unpaired() const noexcept
104  {
105  return to_rank() == 0u;
106  }
107 
114  static constexpr uint8_t max_pseudoknot_depth{1u};
115 
122  constexpr std::optional<uint8_t> pseudoknot_id() const noexcept
123  {
124  if (is_unpaired())
125  return std::nullopt;
126  else
127  return 0;
128  }
130 
131 private:
133  static constexpr char_type rank_to_char_table[alphabet_size]
134  {
135  '.',
136  '(',
137  ')'
138  };
139 
141  static constexpr std::array<rank_type, 256> char_to_rank_table
142  {
143  [] () constexpr
144  {
145  std::array<rank_type, 256> rank_table{};
146 
147  // initialize with unpaired (std::array::fill unfortunately not constexpr)
148  for (rank_type & rnk : rank_table)
149  rnk = 0u;
150 
151  // canonical
152  rank_table['.'] = 0u;
153  rank_table['('] = 1u;
154  rank_table[')'] = 2u;
155 
156  return rank_table;
157  } ()
158  };
159 
161  static constexpr char_type rank_to_char(rank_type const rank)
162  {
163  return rank_to_char_table[rank];
164  }
165 
167  static constexpr rank_type char_to_rank(char_type const chr)
168  {
169  using index_t = std::make_unsigned_t<char_type>;
170  return char_to_rank_table[static_cast<index_t>(chr)];
171  }
172 };
173 
174 inline namespace literals
175 {
176 
190 constexpr dot_bracket3 operator""_db3(char const ch) noexcept
191 {
192  return dot_bracket3{}.assign_char(ch);
193 }
194 
206 inline std::vector<dot_bracket3> operator""_db3(const char * str, std::size_t len)
207 {
209  vec.resize(len);
210 
211  for (size_t idx = 0ul; idx < len; ++idx)
212  vec[idx].assign_char(str[idx]);
213 
214  return vec;
215 }
217 
218 } // inline namespace literals
219 
220 } // namespace seqan3
Provides seqan3::rna_structure_alphabet.
Provides seqan3::alphabet_base.
A CRTP-base that makes defining a custom alphabet easier.
Definition: alphabet_base.hpp:81
constexpr rank_type to_rank() const noexcept
Return the letter's numeric value (rank in the alphabet).
Definition: alphabet_base.hpp:185
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:104
constexpr derived_type & assign_char(char_type const chr) noexcept
Assign from a character, implicitly converts invalid characters.
Definition: alphabet_base.hpp:211
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:276
std::conditional_t< std::same_as< char, void >, char, char > char_type
The char representation; conditional needed to make semi alphabet definitions legal.
Definition: alphabet_base.hpp:96
The three letter RNA structure alphabet of the characters ".()".
Definition: dot_bracket3.hpp:54
constexpr bool is_pair_close() const noexcept
Check whether the character represents a leftward interaction in an RNA structure.
Definition: dot_bracket3.hpp:93
static constexpr uint8_t max_pseudoknot_depth
The ability of this alphabet to represent pseudoknots, i.e. crossing interactions,...
Definition: dot_bracket3.hpp:114
constexpr bool is_unpaired() const noexcept
Check whether the character represents an unpaired position in an RNA structure.
Definition: dot_bracket3.hpp:103
constexpr dot_bracket3() noexcept=default
Defaulted.
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: dot_bracket3.hpp:122
constexpr bool is_pair_open() const noexcept
Check whether the character represents a rightward interaction in an RNA structure.
Definition: dot_bracket3.hpp:83
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
T resize(T... args)
Provides utilities for modifying characters.