SeqAn3  3.2.0
The Modern C++ library for sequence analysis.
dot_bracket3.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2022, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2022, 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 
136  static constexpr std::array<rank_type, 256> char_to_rank_table{
137  []() constexpr {std::array<rank_type, 256> rank_table{};
138 
139  // initialize with unpaired (std::array::fill unfortunately not constexpr)
140  for (rank_type & rnk : rank_table)
141  rnk = 0u;
142 
143  // canonical
144  rank_table['.'] = 0u;
145  rank_table['('] = 1u;
146  rank_table[')'] = 2u;
147 
148  return rank_table;
149 }()
150 }; // namespace seqan3
151 
153 static constexpr char_type rank_to_char(rank_type const rank)
154 {
155  return rank_to_char_table[rank];
156 }
157 
159 static constexpr rank_type char_to_rank(char_type const chr)
160 {
161  using index_t = std::make_unsigned_t<char_type>;
162  return char_to_rank_table[static_cast<index_t>(chr)];
163 }
164 }
165 ;
166 
167 inline namespace literals
168 {
169 
183 constexpr dot_bracket3 operator""_db3(char const ch) noexcept
184 {
185  return dot_bracket3{}.assign_char(ch);
186 }
187 
199 inline std::vector<dot_bracket3> operator""_db3(char const * str, std::size_t len)
200 {
202  vec.resize(len);
203 
204  for (size_t idx = 0ul; idx < len; ++idx)
205  vec[idx].assign_char(str[idx]);
206 
207  return vec;
208 }
210 
211 } // namespace literals
212 
213 } // 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:57
constexpr derived_type & assign_char(char_type const chr) noexcept requires(!std
Assign from a character, implicitly converts invalid characters.
Definition: alphabet_base.hpp:163
constexpr rank_type to_rank() const noexcept
Return the letter's numeric value (rank in the alphabet).
Definition: alphabet_base.hpp:137
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:80
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:199
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:72
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.