BioC++ core-0.7.0
The Modern C++ libraries for Bioinformatics.
 
Loading...
Searching...
No Matches
aminoacid_base.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2022 deCODE Genetics
3// Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
4// Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik
5// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
6// shipped with this file and also available at: https://github.com/biocpp/biocpp-core/blob/main/LICENSE.md
7// -----------------------------------------------------------------------------------------------------
8
14#pragma once
15
17#include <bio/alphabet/base.hpp>
18#include <bio/alphabet/detail/convert.hpp>
19#include <bio/alphabet/detail/to_lower.hpp>
20
21namespace bio::alphabet
22{
23
29template <typename derived_type, auto size>
30class aminoacid_base : public aminoacid_empty_base, public base<derived_type, size, char>
31{
32private:
35
37 friend base_t;
38
42 constexpr aminoacid_base() noexcept = default;
43 constexpr aminoacid_base(aminoacid_base const &) noexcept = default;
44 constexpr aminoacid_base(aminoacid_base &&) noexcept = default;
45 constexpr aminoacid_base & operator=(aminoacid_base const &) noexcept = default;
46 constexpr aminoacid_base & operator=(aminoacid_base &&) noexcept = default;
47 ~aminoacid_base() noexcept = default;
49
51 friend derived_type;
52
53protected:
54 // Import from base:
55 using typename base_t::char_type;
56 using typename base_t::rank_type;
57
58public:
60 using base_t::to_rank;
61
65 // This constructor needs to be public, because constructor templates are not inherited otherwise
67 template <meta::different_from<derived_type> other_aa_type>
69 explicit constexpr aminoacid_base(other_aa_type const other) noexcept
70 {
72 detail::writable_constexpr_alphabet<other_aa_type>)
73 {
74 static_cast<derived_type &>(*this) =
75 detail::convert_through_char_representation<derived_type, other_aa_type>[bio::alphabet::to_rank(other)];
76 }
77 else
78 {
79 bio::alphabet::assign_char_to(bio::alphabet::to_char(other), static_cast<derived_type &>(*this));
80 }
81 }
83
101 static constexpr bool char_is_valid(char_type const c) noexcept
102 {
103 return valid_char_table[static_cast<uint8_t>(c)];
104 }
105
106private:
108 static constexpr std::array<bool, 256> valid_char_table = []() constexpr
109 {
110 static_assert(sizeof(char_type) == 1, "This table is unusable for char types larger than 1 byte.");
111
112 // init with false
114
115 // the original valid chars and their lower cases
116 for (char_type c : derived_type::rank_to_char)
117 {
118 ret[c] = true;
119 ret[detail::to_lower(c)] = true;
120 }
121
122 return ret;
123 }();
124};
125
126} // namespace bio::alphabet
Provides bio::alphabet::aminoacid.
Provides bio::alphabet::base.
A CRTP-base that refines bio::alphabet::base and is used by the amino acids.
Definition: aminoacid_base.hpp:31
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:101
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:69
A CRTP-base that makes defining a custom alphabet easier.
Definition: base.hpp:55
static constexpr size_t alphabet_size
The size of the alphabet, i.e. the number of different values it can take.
Definition: base.hpp:177
meta::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: base.hpp:65
std::conditional_t< std::same_as< char, void >, char, char > char_type
The char representation; conditional needed to make semi alphabet definitions legal.
Definition: base.hpp:63
constexpr rank_type to_rank() const noexcept
Return the letter's numeric value (rank in the alphabet).
Definition: base.hpp:124
A concept that indicates whether an alphabet represents amino acids.
Definition: concept.hpp:97
A type that is std::default_initializable<t> at compile-time.
Definition: core_language.hpp:143
constexpr auto to_char
Return the char representation of an alphabet object.
Definition: concept.hpp:192
constexpr auto assign_char_to
Assign a char to an alphabet object.
Definition: concept.hpp:260
constexpr auto to_rank
Return the rank representation of a (semi-)alphabet object.
Definition: concept.hpp:70
The alphabet module's namespace.
Definition: aa10li.hpp:23
This is an empty base class that can be inherited by types that shall model bio::alphabet::aminoacid.
Definition: concept.hpp:34