From e836084013ec68674e40e24f56335c69674b7a1d Mon Sep 17 00:00:00 2001 From: Enrico Seiler Date: Mon, 3 Jun 2024 11:50:57 +0200 Subject: [PATCH] [FIX] Make it throw --- src/util/display_layout/general.cpp | 10 +++--- test/cli/util_display_layout_test.cpp | 44 +++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/util/display_layout/general.cpp b/src/util/display_layout/general.cpp index 9761559a..50e98a23 100644 --- a/src/util/display_layout/general.cpp +++ b/src/util/display_layout/general.cpp @@ -285,9 +285,10 @@ int execute(config const & cfg) std::vector current_kmers{}; // How many user bins are stored in the current technical bin? Always 1 for split bins. size_t const ub_count{chunk.size()}; - // The current technical bin is a merged bin if it contains more than one user bin or if the (only) user bin - // has previous technical bins. - bool const is_merged{ub_count > 1u || hibf_layout.user_bins[chunk[0]].previous_TB_indices.size() != 0}; + bool const is_merged{ub_count > 1u}; + bool const has_lower_level = hibf_layout.user_bins[chunk[0]].previous_TB_indices.size() != 0; + if (!(is_merged ^ has_lower_level)) + throw std::logic_error{"Invalid Layout: There is a merged bin that only consists of a single user bin."}; for (size_t const ub_index : chunk) { @@ -328,8 +329,7 @@ int execute(config const & cfg) progress.report(); } - // Into how many techincal bins is the user bin split? Always 1 for merged bins. If it is a split bin, - // there is only one user bin. + // Into how many techincal bins is the user bin split? Always 1 for merged bins. size_t const split_count{is_merged ? 1u : hibf_layout.user_bins[chunk[0]].number_of_technical_bins}; size_t const avg_kmer_count = (current_kmer_set.size() + split_count - 1u) / split_count; size_t const sketch_estimate = (sketch.estimate() + split_count - 1u) / split_count; diff --git a/test/cli/util_display_layout_test.cpp b/test/cli/util_display_layout_test.cpp index 2e1a86dc..fe4b3edc 100644 --- a/test/cli/util_display_layout_test.cpp +++ b/test/cli/util_display_layout_test.cpp @@ -132,6 +132,50 @@ TEST_F(cli_test, display_layout_general) EXPECT_EQ(expected_general_file, actual_file); } +TEST_F(cli_test, display_layout_general_throw_invalid_layout) +{ + std::string const seq1_filename = data("seq1.fa"); + std::string const seq2_filename = data("seq2.fa"); + std::string const seq3_filename = data("seq3.fa"); + std::string const small_filename = data("small.fa"); + seqan3::test::tmp_directory tmp_dir{}; + std::filesystem::path const layout_filename{tmp_dir.path() / "small.layout"}; + std::filesystem::path const general_filename{tmp_dir.path() / "small.layout.general"}; + + { + std::ofstream fout{layout_filename}; + std::string content = get_layout_with_correct_filenames(seq1_filename, + seq2_filename, + seq3_filename, + small_filename, + layout_filename.string()); + size_t const last_header_line_start = content.rfind('#'); + size_t const last_header_line_end = content.find('\n', last_header_line_start); + ASSERT_GT(content.size(), last_header_line_end); + content.erase(last_header_line_end + 1u); + content += "0\t0\t1\n" + "1\t1;0\t1;64\n" + "2\t2\t1\n" + "3\t3\t2\n"; + fout << content; + } + + ASSERT_TRUE(std::filesystem::exists(layout_filename)); + + cli_test_result result = execute_app("display_layout", + "general", + "--input", + layout_filename.c_str(), + "--output", + general_filename.c_str()); + + ASSERT_NE(result.exit_code, 0) << "PWD: " << result.pwd << "\nCMD: " << result.command; + EXPECT_EQ(result.out, std::string{}); + EXPECT_TRUE(result.err.find("Invalid Layout: There is a merged bin that only consists of a single user bin.") + != std::string::npos) + << result.err; +} + TEST_F(cli_test, display_layout_general_with_shared_kmers) { std::string const seq1_filename = data("seq1.fa");