SeqAn3  3.2.0-rc.1
The Modern C++ library for sequence analysis.
aminoacid_base.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 
17 #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>
73  requires (!std::same_as<aminoacid_base, other_aa_type>)
74  && (!std::same_as<derived_type, other_aa_type>) && aminoacid_alphabet<other_aa_type>
75  explicit constexpr aminoacid_base(other_aa_type const other) noexcept
76  {
77  if constexpr (is_constexpr_default_constructible_v<other_aa_type>
78  && detail::writable_constexpr_alphabet<other_aa_type>)
79  {
80  static_cast<derived_type &>(*this) =
81  detail::convert_through_char_representation<other_aa_type, derived_type>[seqan3::to_rank(other)];
82  }
83  else
84  {
85  seqan3::assign_char_to(seqan3::to_char(other), static_cast<derived_type &>(*this));
86  }
87  }
89 
109  static constexpr bool char_is_valid(char_type const c) noexcept
110  {
111  return valid_char_table[static_cast<uint8_t>(c)];
112  }
113 
114 private:
116  static constexpr std::array<bool, 256> valid_char_table{[]() constexpr {// init with false
117  std::array<bool, 256> ret{};
118 
119  // the original valid chars and their lower cases
120  for (size_t rank = 0u; rank < derived_type::alphabet_size; ++rank)
121  {
122  uint8_t c = derived_type::rank_to_char(rank);
123  ret[c] = true;
124  ret[to_lower(c)] = true;
125  }
126 
127  return ret;
128 }()
129 }; // namespace seqan3
130 }
131 ;
132 
133 } // namespace seqan3
Provides seqan3::aminoacid_alphabet.
Provides seqan3::alphabet_base.
A CRTP-base that makes defining a custom alphabet easier.
Definition: alphabet_base.hpp:57
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
A CRTP-base that refines seqan3::alphabet_base and is used by the amino acids.
Definition: aminoacid_base.hpp:32
constexpr auto assign_char_to
Assign a character to an alphabet object.
Definition: alphabet/concept.hpp:524
constexpr auto to_char
Return the char representation of an alphabet object.
Definition: alphabet/concept.hpp:386
requires requires
The rank_type of the semi-alphabet; defined as the return type of seqan3::to_rank....
Definition: alphabet/concept.hpp:164
constexpr auto to_rank
Return the rank representation of a (semi-)alphabet object.
Definition: alphabet/concept.hpp:155
A concept that indicates whether an alphabet represents amino acids.
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:83
This is an empty base class that can be inherited by types that shall model seqan3::aminoacid_alphabe...
Definition: alphabet/aminoacid/concept.hpp:35
Provides utilities for modifying characters.