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-2020, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2020, 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 
19 
20 namespace seqan3
21 {
22 
39 template <typename derived_type, auto size>
40 class nucleotide_base : public alphabet_base<derived_type, size, char>
41 {
42 private:
45 
49  constexpr nucleotide_base() noexcept = default;
50  constexpr nucleotide_base(nucleotide_base const &) noexcept = default;
51  constexpr nucleotide_base(nucleotide_base &&) noexcept = default;
52  constexpr nucleotide_base & operator=(nucleotide_base const &) noexcept = default;
53  constexpr nucleotide_base & operator=(nucleotide_base &&) noexcept = default;
54  ~nucleotide_base() noexcept = default;
56 
58  friend derived_type;
59 
60 protected:
61  // Import from base:
62  using typename base_t::char_type;
63  using typename base_t::rank_type;
64 
65 public:
66  using base_t::alphabet_size;
67  using base_t::to_rank;
68 
72  // This constructor needs to be public, because constructor templates are not inherited otherwise
74  template <typename other_nucl_type>
76  requires (!std::same_as<nucleotide_base, other_nucl_type>) &&
77  (!std::same_as<derived_type, other_nucl_type>) &&
78  nucleotide_alphabet<other_nucl_type>
80  explicit constexpr nucleotide_base(other_nucl_type const & other) noexcept
81  {
82  static_cast<derived_type &>(*this) =
83  detail::convert_through_char_representation<derived_type, other_nucl_type>[seqan3::to_rank(other)];
84  }
86 
107  constexpr derived_type complement() const noexcept
108  {
109  return derived_type::complement_table[to_rank()];
110  }
112 
132  static constexpr bool char_is_valid(char_type const c) noexcept
133  {
134  return valid_char_table[static_cast<uint8_t>(c)];
135  }
136 
137 private:
140  {
141  [] () constexpr
142  {
143  // init with false
144  std::array<bool, 256> ret{};
145 
146  // the original valid chars and their lower cases
147  for (uint8_t c : derived_type::rank_to_char)
148  {
149  ret[ c ] = true;
150  ret[to_lower(c)] = true;
151  }
152 
153  // U and T shall be accepted for all
154  ret['U'] = true;
155  ret['T'] = true;
156  ret['u'] = true;
157  ret['t'] = true;
158 
159  return ret;
160  }()
161  };
162 };
163 
164 } // namespace seqan3
Provides seqan3::detail::convert_through_char_representation.
Provides seqan3::nucleotide_alphabet.
Provides seqan3::alphabet_base.
A CRTP-base that makes defining a custom alphabet easier.
Definition: alphabet_base.hpp:55
constexpr rank_type to_rank() const noexcept
Return the letter's numeric value (rank in the alphabet).
Definition: alphabet_base.hpp:132
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:197
A CRTP-base that refines seqan3::alphabet_base and is used by the nucleotides.
Definition: nucleotide_base.hpp:41
static constexpr std::array< bool, 256 > valid_char_table
Implementation of char_is_valid().
Definition: nucleotide_base.hpp:140
friend derived_type
Befriend the derived_type so it can instantiate.
Definition: nucleotide_base.hpp:58
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:132
constexpr nucleotide_base() noexcept=default
Defaulted.
constexpr derived_type complement() const noexcept
Return the complement of the letter.
Definition: nucleotide_base.hpp:107
constexpr auto to_rank
Return the rank representation of a (semi-)alphabet object.
Definition: concept.hpp:146
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
SeqAn specific customisations in the standard namespace.
Provides utilities for modifying characters.