From d4ffe559c1f099851f5792d383f149ed4d721735 Mon Sep 17 00:00:00 2001 From: Harun Mustafa Date: Sat, 5 Feb 2022 12:17:16 +0100 Subject: [PATCH] Fix asserts when aligning an empty sequence (#383) --- .../graph/alignment/aligner_extender_methods.cpp | 5 +++-- .../src/graph/alignment/aligner_seeder_methods.cpp | 2 +- metagraph/tests/graph/test_aligner.cpp | 13 +++++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/metagraph/src/graph/alignment/aligner_extender_methods.cpp b/metagraph/src/graph/alignment/aligner_extender_methods.cpp index dfaf07f26e..91526368b6 100644 --- a/metagraph/src/graph/alignment/aligner_extender_methods.cpp +++ b/metagraph/src/graph/alignment/aligner_extender_methods.cpp @@ -32,8 +32,9 @@ DefaultColumnExtender::DefaultColumnExtender(const DeBruijnGraph &graph, [&](char c) { return config_.get_row(c)[c]; }); std::partial_sum(partial_sums_.rbegin(), partial_sums_.rend(), partial_sums_.rbegin()); - assert(config_.match_score(query_) == partial_sums_.front()); - assert(config_.get_row(query_.back())[query_.back()] == partial_sums_.back()); + assert(query_.empty() || config_.match_score(query_) == partial_sums_.front()); + assert(query_.empty() || config_.get_row(query_.back())[query_.back()] == partial_sums_.back()); + partial_sums_.push_back(0); // precompute profiles to store match/mismatch scores and Cigar::Operators diff --git a/metagraph/src/graph/alignment/aligner_seeder_methods.cpp b/metagraph/src/graph/alignment/aligner_seeder_methods.cpp index b754aa76de..9775170c91 100644 --- a/metagraph/src/graph/alignment/aligner_seeder_methods.cpp +++ b/metagraph/src/graph/alignment/aligner_seeder_methods.cpp @@ -30,7 +30,7 @@ ExactSeeder::ExactSeeder(const DeBruijnGraph &graph, std::partial_sum(partial_sum_.begin(), partial_sum_.end(), partial_sum_.begin()); assert(config_.match_score(query_) == partial_sum_.back()); - assert(config_.get_row(query_.front())[query_.front()] == partial_sum_[1]); + assert(query_.empty() || config_.get_row(query_.front())[query_.front()] == partial_sum_[1]); assert(!partial_sum_.front()); } diff --git a/metagraph/tests/graph/test_aligner.cpp b/metagraph/tests/graph/test_aligner.cpp index 4f23d02f0f..0f98f57552 100644 --- a/metagraph/tests/graph/test_aligner.cpp +++ b/metagraph/tests/graph/test_aligner.cpp @@ -96,6 +96,19 @@ TYPED_TEST(DBGAlignerTest, bad_min_cell_score) { ASSERT_THROW(DBGAligner<>(*graph, config), std::runtime_error); } +TYPED_TEST(DBGAlignerTest, align_empty) { + size_t k = 4; + std::string reference = "CATTT"; + std::string query; + + auto graph = build_graph_batch(k, { reference }); + DBGAlignerConfig config(DBGAlignerConfig::dna_scoring_matrix(2, -1, -2)); + DBGAligner<> aligner(*graph, config); + auto paths = aligner.align(query); + + EXPECT_EQ(0ull, paths.size()); +} + TYPED_TEST(DBGAlignerTest, align_sequence_much_too_short) { size_t k = 4; std::string reference = "CATTT";