Loading...
Searching...
No Matches
phred_base.hpp
Go to the documentation of this file.
// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
#include <algorithm>
#include <seqan3/alphabet/alphabet_base.hpp>
#include <seqan3/alphabet/detail/convert.hpp>
#include <seqan3/alphabet/quality/concept.hpp>
namespace seqan3
{
template <typename derived_type, size_t size>
class phred_base : public alphabet_base<derived_type, size, char>
{
public:
using phred_type = int8_t;
private:
using base_t = alphabet_base<derived_type, size, char>;
friend base_t;
constexpr phred_base() noexcept = default;
constexpr phred_base(phred_base const &) noexcept = default;
constexpr phred_base(phred_base &&) noexcept = default;
constexpr phred_base & operator=(phred_base const &) noexcept = default;
constexpr phred_base & operator=(phred_base &&) noexcept = default;
~phred_base() noexcept = default;
friend derived_type;
public:
// Import from base type:
using base_t::alphabet_size;
using base_t::assign_rank;
using base_t::to_rank;
using typename base_t::char_type;
using typename base_t::rank_type;
// This constructor needs to be public, because constructor templates are not inherited otherwise
template <typename other_qual_type>
requires (!std::same_as<phred_base, other_qual_type>) && (!std::same_as<derived_type, other_qual_type>)
&& quality_alphabet<other_qual_type>
explicit constexpr phred_base(other_qual_type const & other) noexcept
{
assign_phred_to(seqan3::to_phred(other), static_cast<derived_type &>(*this));
}
constexpr phred_type to_phred() const noexcept
{
return rank_to_phred[to_rank()];
}
constexpr derived_type & assign_phred(phred_type const p) noexcept
{
return assign_rank(phred_to_rank[static_cast<rank_type>(p)]);
}
private:
static constexpr rank_type char_to_rank(char_type const chr)
{
int64_t difference = static_cast<int64_t>(chr) - static_cast<int64_t>(derived_type::offset_char);
return std::clamp<int64_t>(difference, 0, alphabet_size - 1);
}
static constexpr char_type rank_to_char(rank_type const rank)
{
return rank + derived_type::offset_char;
}
static constexpr std::array<rank_type, 256> phred_to_rank{
[]() constexpr
{
std::array<rank_type, 256> ret{};
for (int64_t i = std::numeric_limits<phred_type>::lowest(); i <= std::numeric_limits<phred_type>::max();
++i)
{
if (i < derived_type::offset_phred) // map too-small to smallest possible
ret[static_cast<rank_type>(i)] = 0;
else if (i >= derived_type::offset_phred + alphabet_size) // map too-large to highest possible
ret[static_cast<rank_type>(i)] = alphabet_size - 1;
else // map valid range to identity
ret[static_cast<rank_type>(i)] = i - derived_type::offset_phred;
}
return ret;
}()};
static constexpr std::array<phred_type, alphabet_size> rank_to_phred{
[]() constexpr
{
std::array<phred_type, alphabet_size> ret{};
for (size_t i = 0; i < alphabet_size; ++i)
ret[i] = i + derived_type::offset_phred;
return ret;
}()};
};
} // namespace seqan3
algorithm
concept.hpp
Quality alphabet concept.
alphabet_base.hpp
Provides seqan3::alphabet_base.
std::clamp
T clamp(T... args)
seqan3::alphabet_base< derived_type, size, char >::alphabet_base
constexpr alphabet_base() noexcept=default
seqan3::alphabet_base::rank_type
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:77
seqan3::alphabet_base::char_type
std::conditional_t< std::same_as< char_t, void >, char, char_t > char_type
The char representation; conditional needed to make semi alphabet definitions legal.
Definition alphabet_base.hpp:69
seqan3::phred_base::to_phred
constexpr phred_type to_phred() const noexcept
Return the alphabet's value in Phred score representation.
Definition phred_base.hpp:98
seqan3::phred_base< phred42, 42 >::alphabet_size
static constexpr detail::min_viable_uint_t< size > alphabet_size
seqan3::phred_base< phred42, 42 >::assign_rank
constexpr phred42 & assign_rank(rank_type const c) noexcept
seqan3::phred_base::assign_phred
constexpr derived_type & assign_phred(phred_type const p) noexcept
Assign from the numeric Phred score value.
Definition phred_base.hpp:123
seqan3::phred_base< phred42, 42 >::to_rank
constexpr rank_type to_rank() const noexcept
seqan3::phred_base< phred42, 42 >::phred_type
int8_t phred_type
Definition phred_base.hpp:39
seqan3::alphabet_size
constexpr auto alphabet_size
A type trait that holds the size of a (semi-)alphabet.
Definition alphabet/concept.hpp:834
quality_alphabet
A concept that indicates whether an alphabet represents quality scores.
std::numeric_limits::lowest
T lowest(T... args)
seqan3
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
seqan3::to_phred
constexpr auto to_phred
The public getter function for the Phred representation of a quality score.
Definition alphabet/quality/concept.hpp:97
seqan3::assign_phred_to
constexpr auto assign_phred_to
Assign a Phred score to a quality alphabet object.
Definition alphabet/quality/concept.hpp:225
std
SeqAn specific customisations in the standard namespace.
Hide me