SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
quality_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 
18 
19 namespace seqan3
20 {
21 
29 template <typename derived_type, auto size>
30 class quality_base : public alphabet_base<derived_type, size, char>
31 {
32 public:
40  using phred_type = int8_t;
42 
43 private:
46 
48  friend base_t;
49 
53  constexpr quality_base() noexcept = default;
54  constexpr quality_base(quality_base const &) noexcept = default;
55  constexpr quality_base(quality_base &&) noexcept = default;
56  constexpr quality_base & operator=(quality_base const &) noexcept = default;
57  constexpr quality_base & operator=(quality_base &&) noexcept = default;
58  ~quality_base() noexcept = default;
59 
61  constexpr quality_base(phred_type const p) noexcept
62  {
63  static_cast<derived_type *>(this)->assign_phred(p);
64  }
66 
68  friend derived_type;
69 
70 public:
71  // Import from base type:
73  using base_t::to_rank;
74  using base_t::assign_rank;
75  using typename base_t::char_type;
76  using typename base_t::rank_type;
77 
81  // This constructor needs to be public, because constructor templates are not inherited otherwise
86  template <typename other_qual_type>
88  requires (!std::same_as<quality_base, other_qual_type>) &&
89  (!std::same_as<derived_type, other_qual_type>) &&
92  explicit constexpr quality_base(other_qual_type const & other) noexcept
93  {
94  assign_phred_to(seqan3::to_phred(other), static_cast<derived_type &>(*this));
95  }
97 
107  constexpr phred_type to_phred() const noexcept
108  {
109  return rank_to_phred[to_rank()];
110  }
112 
132  constexpr derived_type & assign_phred(phred_type const p) noexcept
133  {
134  return assign_rank(phred_to_rank[static_cast<rank_type>(p)]);
135  }
137 
138 private:
141  {
142  [] () constexpr
143  {
145 
146  for (int64_t i = std::numeric_limits<phred_type>::lowest(); i <= std::numeric_limits<phred_type>::max(); ++i)
147  {
148  if (i < derived_type::offset_phred) // map too-small to smallest possible
149  ret[static_cast<rank_type>(i)] = 0;
150  else if (i >= derived_type::offset_phred + alphabet_size) // map too-large to highest possible
151  ret[static_cast<rank_type>(i)] = alphabet_size - 1;
152  else // map valid range to identity
153  ret[static_cast<rank_type>(i)] = i - derived_type::offset_phred;
154  }
155  return ret;
156  }()
157  };
158 
161  {
162  [] () constexpr
163  {
165 
166  for (size_t i = 0; i < alphabet_size; ++i)
167  ret[i] = i + derived_type::offset_phred;
168 
169  return ret;
170  }()
171  };
172 
174  static constexpr rank_type char_to_rank(char_type const chr)
175  {
176  int64_t difference = static_cast<int64_t>(chr) - static_cast<int64_t>(derived_type::offset_char);
177  return std::clamp<int64_t>(difference, 0, alphabet_size - 1);
178  }
179 
181  static constexpr char_type rank_to_char(rank_type const rank)
182  {
183  return rank + derived_type::offset_char;
184  }
185 };
186 
187 } // namespace seqan3
Provides seqan3::detail::convert_through_char_representation.
Quality alphabet concept.
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
constexpr derived_type & assign_rank(rank_type const c) noexcept
Assign from a numeric value.
Definition: alphabet_base.hpp:264
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
rank_type rank
The value of the alphabet letter is stored as the rank.
Definition: alphabet_base.hpp:338
A CRTP-base that refines seqan3::alphabet_base and is used by the quality alphabets.
Definition: quality_base.hpp:31
static constexpr rank_type char_to_rank(char_type const chr)
Returns the rank representation of character.
Definition: quality_base.hpp:174
static constexpr std::array< phred_type, alphabet_size > rank_to_phred
Rank to phred conversion table.
Definition: quality_base.hpp:161
constexpr quality_base(other_qual_type const &other) noexcept
Allow explicit construction from any other quality type by means of the Phred score representation.
Definition: quality_base.hpp:92
static constexpr std::array< rank_type, 256 > phred_to_rank
Phred to rank conversion table.
Definition: quality_base.hpp:141
constexpr derived_type & assign_phred(phred_type const p) noexcept
Assign from the numeric Phred score value.
Definition: quality_base.hpp:132
constexpr phred_type to_phred() const noexcept
Return the alphabet's value in Phred score representation.
Definition: quality_base.hpp:107
int8_t phred_type
The integer representation of the quality score.
Definition: quality_base.hpp:40
friend derived_type
Befriend the derived_type so it can instantiate.
Definition: quality_base.hpp:68
constexpr quality_base() noexcept=default
Defaulted.
friend base_t
Befriend the base type so it can access char_to_rank and rank_to_char.
Definition: quality_base.hpp:48
static constexpr char_type rank_to_char(rank_type const rank)
Returns the character representation of rank.
Definition: quality_base.hpp:181
constexpr auto to_phred
The public getter function for the Phred representation of a quality score.
Definition: concept.hpp:100
constexpr auto assign_phred_to
Assign a Phred score to a quality alphabet object.
Definition: concept.hpp:231
A concept that indicates whether an alphabet represents quality scores.
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29