Skip to content

Commit

Permalink
Fixed off by one error.
Browse files Browse the repository at this point in the history
  • Loading branch information
OndrejSladky committed Jul 6, 2024
1 parent 7f1df37 commit d425dc7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/fms_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ size_t rank(const fms_index& index, size_t i, byte c) {
} else { // A/C
auto c_position = index.ac_rank(i - gt_position);
if (c == 0) { // A
return i - gt_position - c_position - (i >= index.dollar_position);
// Ranks are offset by 1 compared to the indices.
return i - gt_position - c_position - (i >= index.dollar_position + 1);
} else { // C
return c_position;
}
Expand Down Expand Up @@ -151,6 +152,7 @@ fms_index construct(std::string ms) {
bwt[isa[i+1]] = char_to_int(ms[i]);
sa_transformed_mask[isa[i]] = is_upper(ms[i]);
}

index.dollar_position = isa[0];
delete[] isa;
index.sa_transformed_mask = sdsl::rrr_vector<>(sa_transformed_mask);
Expand Down
3 changes: 3 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ int ms_query(int argc, char *argv[]) {
} else if (fn.empty()) {
std::cerr << "Path to the fasta file is a required argument." << std::endl;
return usage_query();
} else if (k == 0) {
std::cerr << "k is a required argument." << std::endl;
return usage_query();
}

fms_index index = load_index(fn);
Expand Down
57 changes: 57 additions & 0 deletions tests/fms_index_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ namespace {
ret.gt_rank.set_vector(&ret.gt);
return ret;
}
fms_index get_dummy_index2() {
fms_index ret = { //GGTAAGA$, 11001000$
sdsl::bit_vector({0, 1, 1, 0, 0, 0, 1, 1, 1}),
sdsl::rank_support_v5<1>(),
sdsl::bit_vector({0, 0, 0, 0}),
sdsl::rank_support_v5<1>(),
sdsl::bit_vector({0,1,0,1,0}),
sdsl::rank_support_v5<1>(),
sdsl::rrr_vector<>({0,0,1,0,0,1,1,0,0}),
std::vector<size_t>({1, 4, 4, 7}),
5
};
ret.ac_gt_rank.set_vector(&ret.ac_gt);
ret.ac_rank.set_vector(&ret.ac);
ret.gt_rank.set_vector(&ret.gt);
return ret;
}

TEST(FMS_INDEX, RANK) {
auto index = get_dummy_index();
Expand All @@ -48,6 +65,26 @@ namespace {
}
}

TEST(FMS_INDEX, RANK2) {
auto index = get_dummy_index2();
struct test_case {
size_t i;
byte c;
size_t want_result;
};
std::vector<test_case> tests = {
{4, 0, 2},
{5, 0, 3},
{6, 0, 3}
};

for (auto t: tests) {
auto got_result = rank(index, t.i, t.c);

EXPECT_EQ(got_result, t.want_result);
}
}

TEST(FMS_INDEX, UPDATE_RANGE) {
auto index = get_dummy_index();
struct test_case {
Expand Down Expand Up @@ -100,6 +137,26 @@ namespace {
}
}

TEST(FMS_INDEX, QUERY2) {
auto index = get_dummy_index2();
struct test_case {
std::string query;
bool want_result;
};
std::vector<test_case> tests = {
{"AAGA", true},
{"AAGAA", false},
{"GGTTAAGA", true},
{"GTTAAGA", true},
};

for (auto t: tests) {
auto got_result = query(index, t.query, nullptr);

EXPECT_EQ(got_result, t.want_result);
}
}

TEST (FMS_INDEX, CONSTRUCT) {
std::string masked_superstring = "CaGGTag";
fms_index index = construct(masked_superstring);
Expand Down

0 comments on commit d425dc7

Please sign in to comment.