Skip to content

Commit

Permalink
Make RandstrobeIterator/-Generator yield the same results
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelm committed Oct 3, 2024
1 parent 701c472 commit 99b5030
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
9 changes: 3 additions & 6 deletions src/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,15 @@ static const uint32_t STI_FILE_FORMAT_VERSION = 3;
namespace {

uint64_t count_randstrobes(const std::string& seq, const IndexParameters& parameters) {
// To reduce runtime, this function actually counts *syncmers* and
// assumes that one randstrobe is generated per syncmer
uint64_t n_syncmers = 0;
SyncmerIterator syncmer_iterator(seq, parameters.syncmer);
Syncmer syncmer;
while (!(syncmer = syncmer_iterator.next()).is_end()) {
n_syncmers++;
}
// The last w_min syncmers do not result in a randstrobe
if (n_syncmers < parameters.randstrobe.w_min) {
return 0;
} else {
return n_syncmers - parameters.randstrobe.w_min;
}
return n_syncmers;
}

std::vector<uint64_t> count_all_randstrobes(const References& references, const IndexParameters& parameters, size_t n_threads) {
Expand Down
13 changes: 12 additions & 1 deletion src/randstrobes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,21 @@ Randstrobe RandstrobeGenerator::next() {
}
syncmers.push_back(syncmer);
}
if (syncmers.size() <= w_min) {
if (syncmers.empty()) {
return RandstrobeGenerator::end();
}
auto strobe1 = syncmers[0];

if (syncmers.size() < w_min) {
auto randstrobe = Randstrobe{
randstrobe_hash(strobe1.hash, 0, aux_len),
static_cast<uint32_t>(strobe1.position),
static_cast<uint32_t>(strobe1.position),
true
};
syncmers.pop_front();
return randstrobe;
}
auto max_position = strobe1.position + max_dist;
uint64_t min_val = std::numeric_limits<uint64_t>::max();
Syncmer strobe2 = strobe1; // Default if no nearby syncmer
Expand Down
4 changes: 2 additions & 2 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ TEST_CASE("both randstrobes iterator implementations give same results") {
}

// This tests an assumption that we need to hold for count_randstrobes()
TEST_CASE("syncmer and randstrobe iterators return (nearly) same no. of items") {
TEST_CASE("syncmer and randstrobe iterators return same no. of items") {
auto references = References::from_fasta("tests/phix.fasta");
auto& seq = references.sequences[0];
auto parameters = IndexParameters::from_read_length(100);
Expand All @@ -157,7 +157,7 @@ TEST_CASE("syncmer and randstrobe iterators return (nearly) same no. of items")
syncmer_count++;
}

CHECK(randstrobe_count + parameters.randstrobe.w_min == syncmer_count);
CHECK(randstrobe_count == syncmer_count);
}

TEST_CASE("reverse complement") {
Expand Down

0 comments on commit 99b5030

Please sign in to comment.