Skip to content

Commit

Permalink
Fix/issue_3264 (#3268)
Browse files Browse the repository at this point in the history
* Fixes #3267 by adding proper constraints to the char conversion table.

* Fixes Question regarding the usage of `seqan3::fm_index_cursor` using `seqan3::bitpacked_sequence`. #3264

Further constraints the conversion constructors of the nucleotide and aminoacid base alphabet types to only consider conversion if all requirements are fulfilled. This is now represented by the specific concept `convertable_to_through_char_representation`.
Through this proxy types will not be converted through their char representation but will be implicitly converted to their underling base alphabet type, e.g. dna4, and then copied/moved using the copy/move constructor of the underlying base alphabet type.

* [MISC] automatic linting

* Adds missing copyright text in test file

* Removes superfluous vector include in test file.

---------

Co-authored-by: seqan-actions[bot] <[email protected]>
  • Loading branch information
rrahn and seqan-actions authored Jul 16, 2024
1 parent 4cc708d commit 69284e8
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 3 deletions.
2 changes: 2 additions & 0 deletions include/seqan3/alphabet/aminoacid/aminoacid_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <seqan3/alphabet/alphabet_base.hpp>
#include <seqan3/alphabet/aminoacid/concept.hpp>
#include <seqan3/alphabet/detail/concept.hpp>
#include <seqan3/alphabet/detail/convert.hpp>
#include <seqan3/utility/char_operations/transform.hpp>

Expand Down Expand Up @@ -69,6 +70,7 @@ class aminoacid_base : public aminoacid_empty_base, public alphabet_base<derived
template <typename other_aa_type>
requires (!std::same_as<aminoacid_base, other_aa_type>)
&& (!std::same_as<derived_type, other_aa_type>) && aminoacid_alphabet<other_aa_type>
&& detail::convertable_to_through_char_representation<other_aa_type, derived_type>
explicit constexpr aminoacid_base(other_aa_type const other) noexcept
{
if constexpr (is_constexpr_default_constructible_v<other_aa_type>
Expand Down
15 changes: 15 additions & 0 deletions include/seqan3/alphabet/detail/concept.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <concepts>

#include <seqan3/alphabet/concept.hpp>
#include <seqan3/core/platform.hpp>

namespace seqan3::detail
Expand Down Expand Up @@ -54,4 +55,18 @@ concept weakly_ordered_with =
};
//!\endcond

/*!\interface seqan3::detail::convertable_to_through_char_representation <>
* \ingroup alphabet
* \tparam from_t The type to convert from.
* \tparam to_t The type to convert to.
* \brief Checks whether `from_t` can be converted through `to_t` using their char representation.
*
* Requires that both `from_t` and `to_t` are alphabets and additionally, that `from_t` is default constructible.
*/
//!\cond
template <typename from_t, typename to_t>
concept convertable_to_through_char_representation =
alphabet<from_t> && alphabet<to_t> && std::default_initializable<from_t>;
//!\endcond

} // namespace seqan3::detail
7 changes: 4 additions & 3 deletions include/seqan3/alphabet/detail/convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#include <array>

#include <seqan3/alphabet/concept.hpp>
#include <seqan3/alphabet/detail/concept.hpp>

// ============================================================================
// conversion to/from char/rank types
Expand All @@ -25,11 +25,12 @@ namespace seqan3::detail
// clang-format off
/*!\brief A precomputed conversion table for two alphabets based on their char representations.
* \ingroup alphabet
* \tparam in_t The type of the input, must satisfy seqan3::alphabet.
* \tparam in_t The type of the input, must satisfy seqan3::alphabet and must be default initializable.
* \tparam out_t The type of the output, must satisfy seqan3::alphabet.
* \hideinitializer
*/
template <alphabet in_t, alphabet out_t>
template <typename in_t, typename out_t>
requires convertable_to_through_char_representation<in_t, out_t>
constexpr std::array<out_t, alphabet_size<in_t>> convert_through_char_representation
{
[]() constexpr {
Expand Down
2 changes: 2 additions & 0 deletions include/seqan3/alphabet/nucleotide/nucleotide_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#pragma once

#include <seqan3/alphabet/alphabet_base.hpp>
#include <seqan3/alphabet/detail/concept.hpp>
#include <seqan3/alphabet/detail/convert.hpp>
#include <seqan3/alphabet/nucleotide/concept.hpp>
#include <seqan3/utility/char_operations/transform.hpp>
Expand Down Expand Up @@ -77,6 +78,7 @@ class nucleotide_base : public alphabet_base<derived_type, size, char>
template <typename other_nucl_type>
requires (!std::same_as<nucleotide_base, other_nucl_type>)
&& (!std::same_as<derived_type, other_nucl_type>) && nucleotide_alphabet<other_nucl_type>
&& detail::convertable_to_through_char_representation<other_nucl_type, derived_type>
explicit constexpr nucleotide_base(other_nucl_type const & other) noexcept
{
static_cast<derived_type &>(*this) =
Expand Down
11 changes: 11 additions & 0 deletions test/unit/alphabet/container/bitpacked_sequence_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,14 @@ TEST(bitpacked_sequence_test, issue371)
auto end = source.end();
[[maybe_unused]] bool result = it != end; // This line causes error.
}

// https://github.com/seqan/seqan3/issues/3264
TEST(bitpacked_sequence_test, issue3264)
{
using namespace seqan3::literals;

seqan3::bitpacked_sequence<seqan3::dna4> source{"ACGGTCAGGTTC"_dna4};
auto it = source.begin();
seqan3::dna4 val = static_cast<seqan3::dna4>(*it); // this line caused the compiler error
EXPECT_EQ(val, 'A'_dna4);
}
1 change: 1 addition & 0 deletions test/unit/search/fm_index_cursor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ seqan3_test (fm_index_cursor_test.cpp)
seqan3_test (fm_index_cursor_collection_test.cpp)
seqan3_test (bi_fm_index_cursor_test.cpp)
seqan3_test (bi_fm_index_cursor_collection_test.cpp)
seqan3_test (extend_bitpacked_query_sequence_test.cpp)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: BSD-3-Clause

#include <gtest/gtest.h>

#include <vector>

#include <seqan3/alphabet/container/bitpacked_sequence.hpp>
#include <seqan3/alphabet/nucleotide/dna4.hpp>
#include <seqan3/search/fm_index/fm_index.hpp>
#include <seqan3/utility/views/slice.hpp>

#include "../helper.hpp"

TEST(fm_index_cursor_test, extend_right_with_bitpacked_sequence)
{
using namespace seqan3::literals;
using result_t = std::vector<std::pair<uint64_t, uint64_t>>;

seqan3::bitpacked_sequence<seqan3::dna4> seq{"ACGGTCAGGTTC"_dna4};
seqan3::fm_index index{seq};

auto bitpacked_query = seqan3::views::slice(seq, 1, 4);

auto it = index.cursor();
it.extend_right(bitpacked_query); // this line caused the compile time error
EXPECT_EQ(seqan3::uniquify(it.locate()), (result_t{{0, 1}}));
}

0 comments on commit 69284e8

Please sign in to comment.