SeqAn3
quality_base.hpp
Go to the documentation of this file.
1 // ============================================================================
2 // SeqAn - The Library for Sequence Analysis
3 // ============================================================================
4 //
5 // Copyright (c) 2006-2018, Knut Reinert & Freie Universitaet Berlin
6 // Copyright (c) 2016-2018, Knut Reinert & MPI Molekulare Genetik
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // * Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // * Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // * Neither the name of Knut Reinert or the FU Berlin nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 // ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
25 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 // DAMAGE.
32 //
33 // ============================================================================
34 
40 #pragma once
41 
43 #include <seqan3/alphabet/detail/convert.hpp>
45 
46 namespace seqan3
47 {
48 
54 template <typename derived_type, auto size>
55 class quality_base : public alphabet_base<derived_type, size, char>
56 {
57 public:
61  using phred_type = int8_t;
64 
65 private:
68 
72  constexpr quality_base() : base_t{} {}
73  constexpr quality_base(quality_base const &) = default;
74  constexpr quality_base(quality_base &&) = default;
75  constexpr quality_base & operator=(quality_base const &) = default;
76  constexpr quality_base & operator=(quality_base &&) = default;
77  ~quality_base() = default;
78 
80  constexpr quality_base(phred_type const p) noexcept
81  {
82  static_cast<derived_type *>(this)->assign_phred(p);
83  }
85 
87  friend derived_type;
88 
89 public:
90  // Import from base type:
91  using base_t::value_size;
92  using base_t::to_rank;
93  using base_t::assign_rank;
94  using typename base_t::char_type;
95  using typename base_t::rank_type;
96 
100  // This constructor needs to be public, because constructor templates are not inherited otherwise
102  template <typename other_qual_type>
108  explicit constexpr quality_base(other_qual_type const & other) noexcept
109  {
110  using seqan3::assign_phred;
111  using seqan3::to_phred;
112  assign_phred(static_cast<derived_type &>(*this), to_phred(other));
113  }
115 
119  constexpr char_type to_char() const noexcept
121  {
122  return static_cast<char_type>(to_rank()) + derived_type::offset_char;
123  }
124 
126  constexpr phred_type to_phred() const noexcept
127  {
128  return static_cast<phred_type>(to_rank()) + derived_type::offset_phred;
129  }
131 
146  constexpr derived_type & assign_phred(phred_type const p) noexcept
147  {
148  return assign_rank(phred_to_rank[static_cast<rank_type>(p)]);
149  }
151 
152 protected:
154  static std::array<rank_type, 256> constexpr phred_to_rank
155  {
156  [] () constexpr
157  {
158  std::array<rank_type, 256> ret{};
159 
160  for (int64_t i = std::numeric_limits<phred_type>::lowest(); i <= std::numeric_limits<phred_type>::max(); ++i)
161  {
162  if (i < derived_type::offset_phred) // map too-small to smallest possible
163  ret[static_cast<rank_type>(i)] = 0;
164  else if (i >= derived_type::offset_phred + value_size) // map too-large to highest possible
165  ret[static_cast<rank_type>(i)] = value_size - 1;
166  else // map valid range to identity
167  ret[static_cast<rank_type>(i)] = i - derived_type::offset_phred;
168  }
169  return ret;
170  }()
171  };
172 
174  static std::array<rank_type, 256> constexpr char_to_rank
175  {
176  [] () constexpr
177  {
178  std::array<rank_type, 256> ret{};
179 
180  for (int64_t i = std::numeric_limits<char_type>::lowest(); i <= std::numeric_limits<char_type>::max(); ++i)
181  {
182  if (i < derived_type::offset_char) // map too-small to smallest possible
183  ret[static_cast<rank_type>(i)] = 0;
184  else if (i >= derived_type::offset_char + value_size) // map too-large to highest possible
185  ret[static_cast<rank_type>(i)] = value_size - 1;
186  else // map valid range to identity
187  ret[static_cast<rank_type>(i)] = i - derived_type::offset_char;
188  }
189 
190  return ret;
191  }()
192  };
193 };
194 
195 } // namespace seqan3
static std::array< rank_type, 256 > constexpr phred_to_rank
Phred to rank conversion table.
Definition: quality_base.hpp:155
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:89
char char_type
The type of the alphabet when converted to char (e.g. via to_char()).
Definition: alphabet_base.hpp:87
constexpr char_type to_char() const noexcept
Return the letter as a character of char_type.
Definition: quality_base.hpp:120
constexpr derived_type & assign_phred(phred_type const p) noexcept
Assign from the numeric phred value.
Definition: quality_base.hpp:146
constexpr underlying_phred_t< alphabet_type > to_phred(alphabet_type const &chr)
The public getter function for the phred representation of a score.
Definition: concept.hpp:127
constexpr alphabet_type & assign_phred(alphabet_type &chr, char const in)
The public setter function of a phred score.
Definition: concept.hpp:102
int8_t phred_type
The integer representation of a quality score assignable with =operator.
Definition: quality_base.hpp:62
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:58
static std::array< rank_type, 256 > constexpr char_to_rank
Char to rank conversion table.
Definition: quality_base.hpp:175
A CRTP-base that refines seqan3::alphabet_base and is used by the quality alphabets.
Definition: quality_base.hpp:55
A concept that indicates whether an alphabet represents quality scores.In addition to the requirement...
constexpr phred_type to_phred() const noexcept
Return the alphabet&#39;s value in phred representation.
Definition: quality_base.hpp:126
constexpr derived_type & assign_rank(rank_type const c) noexcept
Assign from a numeric value.
Definition: alphabet_base.hpp:189
static detail::min_viable_uint_t< size > constexpr value_size
The size of the alphabet, i.e. the number of different values it can take.
Definition: alphabet_base.hpp:198
constexpr quality_base(other_qual_type const &other) noexcept
Allow explicit construction from any other quality type by means of the phred representation.
Definition: quality_base.hpp:108
Provides seqan3::alphabet_base.
The concept std::Same<T, U> is satisfied if and only if T and U denote the same type.
constexpr rank_type to_rank() const noexcept
Return the letter&#39;s numeric value (rank in the alphabet).
Definition: alphabet_base.hpp:142
A CRTP-base that makes defining a custom alphabet easier.
Definition: alphabet_base.hpp:77
Quality alphabet concept.