Skip to content

Commit

Permalink
[FEATURE] Make hibf constructible from a layout file.
Browse files Browse the repository at this point in the history
  • Loading branch information
smehringer committed Aug 23, 2023
1 parent 991c413 commit 96456d7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/hibf/hierarchical_interleaved_bloom_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class hierarchical_interleaved_bloom_filter
~hierarchical_interleaved_bloom_filter() = default; //!< Defaulted.

hierarchical_interleaved_bloom_filter(config const & configuration);
hierarchical_interleaved_bloom_filter(std::function<void(size_t const, insert_iterator &&)> input_fn,
std::filesystem::path const & layout_filename);
//!\}

//!\brief The individual interleaved Bloom filters.
Expand Down
16 changes: 16 additions & 0 deletions src/hierarchical_interleaved_bloom_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,20 @@ hierarchical_interleaved_bloom_filter::hierarchical_interleaved_bloom_filter(con
build_index(*this, configuration, layout);
}

hierarchical_interleaved_bloom_filter::hierarchical_interleaved_bloom_filter(
std::function<void(size_t const, insert_iterator &&)> input_fn,
std::filesystem::path const & layout_filename)
{
// read config and layout from file
std::ifstream layout_file{layout_filename};
config configuration;
layout::layout hibf_layout;
configuration.read_from(layout_file);
hibf_layout.read_from(layout_file);

configuration.input_fn = input_fn; // set input as it cannot be serialized.

build_index(*this, configuration, hibf_layout);
}

} // namespace hibf
54 changes: 54 additions & 0 deletions test/unit/hibf/hierarchical_interleaved_bloom_filter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@

#include <cstddef> // for size_t
#include <functional> // for function
#include <iostream>
#include <vector> // for vector, allocator

#include <hibf/config.hpp> // for insert_iterator, config
#include <hibf/hierarchical_interleaved_bloom_filter.hpp> // for hierarchical_interleaved_bloom_filter
#include <hibf/test/expect_range_eq.hpp> // for expect_range_eq, EXPECT_RANGE_EQ
#include <hibf/test/tmp_directory.hpp>

TEST(hibf_test, test_specific_hash_values)
{
Expand Down Expand Up @@ -41,6 +43,58 @@ TEST(hibf_test, test_specific_hash_values)
}
}

TEST(hibf_test, build_from_layout)
{
// range of range of sequences
std::vector<std::vector<size_t>> hashes{{1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u, 10u}, {1u, 2u, 3u, 4u, 5u}};

auto input_fn = [&](size_t const num, hibf::insert_iterator it)
{
for (auto const hash : hashes[num])
it = hash;
};

hibf::test::tmp_directory tmp{};
std::filesystem::path layout_filename{tmp.path()/"dummy.layout"};

{
std::ofstream stream{layout_filename};
stream << "##CONFIG:\n";
stream << "##{\n";
stream << "## \"config\": {\n";
stream << "## \"version\": 1,\n";
stream << "## \"number_of_user_bins\": 2,\n";
stream << "## \"number_of_hash_functions\": 2,\n";
stream << "## \"maximum_false_positive_rate\": 0.05,\n";
stream << "## \"threads\": 1,\n";
stream << "## \"sketch_bits\": 12,\n";
stream << "## \"tmax\": 64,\n";
stream << "## \"alpha\": 1.2,\n";
stream << "## \"max_rearrangement_ratio\": 0.5,\n";
stream << "## \"disable_estimate_union\": false,\n";
stream << "## \"disable_rearrangement\": true,\n";
stream << "## \"disable_cutoffs\": false\n";
stream << "## }\n";
stream << "##}\n";
stream << "##ENDCONFIG\n";
stream << "#HIGH_LEVEL_IBF max_bin_id:0\n";
stream << "#USER_BIN_IDX\tTECHNICAL_BIN_INDICES\tNUMBER_OF_TECHNICAL_BINS\n";
stream << "1\t0\t34\n";
stream << "0\t34\t30\n";
}

hibf::hierarchical_interleaved_bloom_filter hibf{input_fn, layout_filename};

{
std::vector<size_t> query{1, 2, 3, 4, 5};

auto agent = hibf.membership_agent();
auto result = agent.bulk_contains(query, 2);

EXPECT_RANGE_EQ(result, (std::vector<size_t>{0u, 1u}));
}
}

// #ifdef HIBF_HAS_SEQAN3

// #include <seqan3/alphabet/nucleotide/dna4.hpp>
Expand Down

0 comments on commit 96456d7

Please sign in to comment.