SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
aminoacid_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 namespace seqan3
21 {
22 
30 template <typename derived_type, auto size>
31 class aminoacid_base : public alphabet_base<derived_type, size, char>, public aminoacid_empty_base
32 {
33 private:
36 
38  friend base_t;
39 
43  constexpr aminoacid_base() noexcept = default;
44  constexpr aminoacid_base(aminoacid_base const &) noexcept = default;
45  constexpr aminoacid_base(aminoacid_base &&) noexcept = default;
46  constexpr aminoacid_base & operator=(aminoacid_base const &) noexcept = default;
47  constexpr aminoacid_base & operator=(aminoacid_base &&) noexcept = default;
48  ~aminoacid_base() noexcept = default;
49 
51 
53  friend derived_type;
54 
55 protected:
56  // Import from base:
57  using typename base_t::char_type;
58  using typename base_t::rank_type;
59 
60 public:
62  using base_t::to_rank;
63 
67  // This constructor needs to be public, because constructor templates are not inherited otherwise
72  template <typename other_aa_type>
74  requires (!std::same_as<aminoacid_base, other_aa_type>) &&
75  (!std::same_as<derived_type, other_aa_type>) &&
78  explicit constexpr aminoacid_base(other_aa_type const other) noexcept
79  {
80  if constexpr (is_constexpr_default_constructible_v<other_aa_type> &&
83 #else
84  detail::writable_constexpr_alphabet<other_aa_type>
85 #endif // SEQAN3_WORKAROUND_GCC7_AND_8_CONCEPT_ISSUES
86  )
87  {
88  static_cast<derived_type &>(*this) =
89  detail::convert_through_char_representation<derived_type, other_aa_type>[seqan3::to_rank(other)];
90  }
91  else
92  {
93  seqan3::assign_char_to(seqan3::to_char(other), static_cast<derived_type &>(*this));
94  }
95  }
97 
117  static constexpr bool char_is_valid(char_type const c) noexcept
118  {
119  return valid_char_table[static_cast<uint8_t>(c)];
120  }
121 
122 private:
124  static constexpr std::array<bool, 256> valid_char_table
125  {
126  [] () constexpr
127  {
128  // init with false
129  std::array<bool, 256> ret{};
130 
131  // the original valid chars and their lower cases
132  for (size_t rank = 0u; rank < derived_type::alphabet_size; ++rank)
133  {
134  uint8_t c{};
135 #ifdef SEQAN3_DEPRECATED_310
136  if constexpr (detail::has_rank_to_char_table<derived_type>)
137  c = derived_type::rank_to_char[rank];
138  else
139  c = derived_type::rank_to_char(rank);
140 #else // ^^^ before 3.1.0 release / after 3.1.0 release vvv
141  c = derived_type::rank_to_char(rank);
142 #endif // SEQAN3_DEPRECATED_310
143  ret[c] = true;
144  ret[to_lower(c)] = true;
145  }
146 
147  return ret;
148  }()
149  };
150 };
151 
152 } // namespace seqan3
Provides seqan3::aminoacid_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 amino acids.
Definition: aminoacid_base.hpp:32
constexpr aminoacid_base(other_aa_type const other) noexcept
Allow explicit construction from any other aminoacid type and convert via the character representatio...
Definition: aminoacid_base.hpp:78
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: aminoacid_base.hpp:117
constexpr auto assign_char_to
Assign a character to an alphabet object.
Definition: concept.hpp:523
constexpr auto to_char
Return the char representation of an alphabet object.
Definition: concept.hpp:384
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 amino acids.
Refines seqan3::alphabet and adds assignability.
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_WORKAROUND_GCC7_AND_8_CONCEPT_ISSUES
Various concept problems only present in GCC7 and GCC8.
Definition: platform.hpp:293
This is an empty base class that can be inherited by types that shall model seqan3::aminoacid_alphabe...
Definition: concept.hpp:35
Provides utilities for modifying characters.