BioC++ core-0.7.0
The Modern C++ libraries for Bioinformatics.
 
Loading...
Searching...
No Matches
nucleotide_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
16#include <bio/alphabet/base.hpp>
17#include <bio/alphabet/detail/convert.hpp>
18#include <bio/alphabet/detail/to_lower.hpp>
20
21namespace bio::alphabet
22{
23
40template <typename derived_type, auto size>
41class nucleotide_base : public base<derived_type, size, char>
42{
43private:
46
50 constexpr nucleotide_base() noexcept = default;
51 constexpr nucleotide_base(nucleotide_base const &) noexcept = default;
52 constexpr nucleotide_base(nucleotide_base &&) noexcept = default;
53 constexpr nucleotide_base & operator=(nucleotide_base const &) noexcept = default;
54 constexpr nucleotide_base & operator=(nucleotide_base &&) noexcept = default;
55 ~nucleotide_base() noexcept = default;
57
59 friend derived_type;
60
61protected:
62 // Import from base:
63 using typename base_t::char_type;
64 using typename base_t::rank_type;
65
66public:
68 using base_t::to_rank;
69
73 // This constructor needs to be public, because constructor templates are not inherited otherwise
75 template <meta::different_from<derived_type> other_nucl_type>
77 explicit constexpr nucleotide_base(other_nucl_type const & other) noexcept
78 {
79 static_cast<derived_type &>(*this) =
80 detail::convert_through_char_representation<derived_type, other_nucl_type>[bio::alphabet::to_rank(other)];
81 }
83
104 constexpr derived_type complement() const noexcept
105 requires(requires(derived_type d) { d.complement_table; })
106 {
107 return derived_type::complement_table[to_rank()];
108 }
110
130 static constexpr bool char_is_valid(char_type const c) noexcept
131 {
132 return valid_char_table[static_cast<uint8_t>(c)];
133 }
134
135private:
137 static constexpr std::array<bool, 256> valid_char_table = []() constexpr
138 {
139 static_assert(sizeof(char_type) == 1, "This table is unusable for char types larger than 1 byte.");
140
141 // init with false
143
144 // the original valid chars and their lower cases
145 for (char_type c : derived_type::rank_to_char)
146 {
147 ret[c] = true;
148 ret[detail::to_lower(c)] = true;
149 }
150
151 // U and T shall be accepted for all
152 ret['U'] = true;
153 ret['T'] = true;
154 ret['u'] = true;
155 ret['t'] = true;
156
157 return ret;
158 }();
159
161 friend constexpr derived_type tag_invoke(custom::complement, derived_type const a) noexcept
162 {
163 return a.complement();
164 }
165};
166
167} // namespace bio::alphabet
Provides bio::alphabet::nucleotide.
Provides bio::alphabet::base.
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 CRTP-base that refines bio::alphabet::base and is used by the nucleotides.
Definition: nucleotide_base.hpp:42
friend constexpr derived_type tag_invoke(custom::complement, derived_type const a) noexcept
tag_invoke() wrapper around member.
Definition: nucleotide_base.hpp:161
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:130
constexpr derived_type complement() const noexcept
Return the complement of the letter.
Definition: nucleotide_base.hpp:104
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:77
A concept that indicates whether an alphabet represents nucleotides.
Definition: concept.hpp:113
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
Customisation tag for bio::alphabet::complement.
Definition: tag.hpp:49