SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
nucleotide_base.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 
16 #include <seqan3/alphabet/detail/convert.hpp>
19 
20 #ifdef SEQAN3_DEPRECATED_310
21 namespace seqan3::detail
22 {
24 // helper concept to deprecate old char_to_rank lookup tables
25 template <typename alphabet_t>
26 SEQAN3_CONCEPT has_complement_table = requires()
27 {
28  { alphabet_t::complement_table[0] };
29 };
31 } // namespace seqan3::detail
32 #endif // SEQAN3_DEPRECATED_310
33 
34 namespace seqan3
35 {
36 
55 template <typename derived_type, auto size>
56 class nucleotide_base : public alphabet_base<derived_type, size, char>
57 {
58 private:
61 
65  constexpr nucleotide_base() noexcept = default;
66  constexpr nucleotide_base(nucleotide_base const &) noexcept = default;
67  constexpr nucleotide_base(nucleotide_base &&) noexcept = default;
68  constexpr nucleotide_base & operator=(nucleotide_base const &) noexcept = default;
69  constexpr nucleotide_base & operator=(nucleotide_base &&) noexcept = default;
70  ~nucleotide_base() noexcept = default;
71 
73 
75  friend derived_type;
76 
77 protected:
78  // Import from base:
79  using typename base_t::char_type;
80  using typename base_t::rank_type;
81 
82 public:
84  using base_t::to_rank;
85 
89  // This constructor needs to be public, because constructor templates are not inherited otherwise
94  template <typename other_nucl_type>
96  requires (!std::same_as<nucleotide_base, other_nucl_type>) &&
97  (!std::same_as<derived_type, other_nucl_type>) &&
100  explicit constexpr nucleotide_base(other_nucl_type const & other) noexcept
101  {
102  static_cast<derived_type &>(*this) =
103  detail::convert_through_char_representation<derived_type, other_nucl_type>[seqan3::to_rank(other)];
104  }
106 
129  constexpr derived_type complement() const noexcept
130  {
131 #ifdef SEQAN3_DEPRECATED_310
132  if constexpr (detail::has_complement_table<derived_type>)
133  return complement_table_deprecated(to_rank());
134  else
135  return derived_type{}.assign_rank(derived_type{}.rank_complement(to_rank()));
136 #else // ^^^ before 3.1.0 release / after 3.1.0 release vvv
137  return derived_type{}.assign_rank(derived_type{}.rank_complement(to_rank()));
138 #endif // SEQAN3_DEPRECATED_310
139  }
140 
141 #ifdef SEQAN3_DEPRECATED_310
142 private:
143 
148  SEQAN3_DEPRECATED_310 static constexpr derived_type complement_table_deprecated(rank_type const rank) noexcept
149  {
150  return derived_type::complement_table[rank];
151  }
152 
153 public:
154 #endif // SEQAN3_DEPRECATED_310
156 
178  static constexpr bool char_is_valid(char_type const c) noexcept
179  {
180  return valid_char_table[static_cast<uint8_t>(c)];
181  }
182 
183 private:
185  static constexpr std::array<bool, 256> valid_char_table
186  {
187  [] () constexpr
188  {
189  // init with false
190  std::array<bool, 256> ret{};
191 
192  // the original valid chars and their lower cases
193  for (size_t rank = 0u; rank < derived_type::alphabet_size; ++rank)
194  {
195  uint8_t c{};
196 #ifdef SEQAN3_DEPRECATED_310
197  if constexpr (detail::has_rank_to_char_table<derived_type>)
198  c = derived_type::rank_to_char[rank];
199  else
200  c = derived_type::rank_to_char(rank);
201 #else // ^^^ before 3.1.0 release / after 3.1.0 release vvv
202  c = derived_type::rank_to_char(rank);
203 #endif // SEQAN3_DEPRECATED_310
204  ret[c] = true;
205  ret[to_lower(c)] = true;
206  }
207 
208  // U and T shall be accepted for all
209  ret['U'] = true;
210  ret['T'] = true;
211  ret['u'] = true;
212  ret['t'] = true;
213 
214  return ret;
215  }()
216  };
217 };
218 
219 } // namespace seqan3
Provides seqan3::nucleotide_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
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
A CRTP-base that refines seqan3::alphabet_base and is used by the nucleotides.
Definition: nucleotide_base.hpp:57
static constexpr bool char_is_valid(char_type const c) noexcept
Validate whether a character value has a one-to-one mapping to an alphabet value.
Definition: nucleotide_base.hpp:178
constexpr derived_type complement() const noexcept
Return the complement of the letter.
Definition: nucleotide_base.hpp:129
constexpr nucleotide_base(other_nucl_type const &other) noexcept
Allow explicit construction from any other nucleotide type and convert via the character representati...
Definition: nucleotide_base.hpp:100
constexpr auto alphabet_size
A type trait that holds the size of a (semi-)alphabet.
Definition: concept.hpp:858
constexpr auto to_rank
Return the rank representation of a (semi-)alphabet object.
Definition: concept.hpp:155
A concept that indicates whether an alphabet represents nucleotides.
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
constexpr char_type to_lower(char_type const c) noexcept
Converts 'A'-'Z' to 'a'-'z' respectively; other characters are returned as is.
Definition: transform.hpp:81
#define SEQAN3_DEPRECATED_310
Deprecation message for SeqAn 3.1.0 release.
Definition: platform.hpp:203
Provides utilities for modifying characters.