Skip to content

Commit 79cdd96

Browse files
authored
Merge pull request #400 from ksahlin/fix-bindings
Fix StrobemerIndex.find() in Python bindings
2 parents d420610 + 4141d21 commit 79cdd96

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

pyproject.toml

+3
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ requires = [
88
"ninja; platform_system!='Windows'",
99
"nanobind>=0.2.0",
1010
]
11+
12+
[tool.pytest.ini_options]
13+
testpaths = ["tests"]

src/python/strobealign.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,9 @@ NB_MODULE(strobealign_extension, m_) {
108108
.def_ro("syncmer", &IndexParameters::syncmer)
109109
.def_ro("randstrobe", &IndexParameters::randstrobe)
110110
;
111-
nb::class_<RefRandstrobe>(m, "RefRandstrobeWithHash", "Randstrobe on a reference")
111+
nb::class_<RefRandstrobe>(m, "RefRandstrobe", "Randstrobe on a reference")
112112
.def_ro("position", &RefRandstrobe::position)
113+
.def_ro("hash", &RefRandstrobe::hash)
113114
.def_prop_ro("reference_index", &RefRandstrobe::reference_index)
114115
.def_prop_ro("strobe2_offset", &RefRandstrobe::strobe2_offset)
115116
;
@@ -119,11 +120,9 @@ NB_MODULE(strobealign_extension, m_) {
119120
.def("find", [](const StrobemerIndex& index, uint64_t key) -> std::vector<RefRandstrobe> {
120121
std::vector<RefRandstrobe> v;
121122
auto position = index.find(key);
122-
if (position != index.end()) {
123-
/*while (index.randstrobes[position].hash == key) {
124-
v.push_back(index.randstrobes[position]);
125-
position++;
126-
}*/
123+
while (position != index.end() && index.get_hash(position) == key) {
124+
v.push_back(index.get_randstrobe(position));
125+
position++;
127126
}
128127
return v;
129128
})

tests/test_bindings.py

+21
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,24 @@ def test_indexing_and_nams_finding():
4141
ref_aligned = ref[nam.ref_start:nam.ref_end]
4242
query_aligned = query[nam.query_start:nam.query_end]
4343
score = nam.score
44+
45+
46+
def test_index_find():
47+
refs = strobealign.References.from_fasta("tests/phix.fasta")
48+
index_parameters = strobealign.IndexParameters.from_read_length(100)
49+
index = strobealign.StrobemerIndex(refs, index_parameters)
50+
index.populate()
51+
52+
query = "TGCGTTTATGGTACGCTGGACTTTGTGGGATACCCTCGCTTTCCTGCTCCTGTTGAGTTTATTGCTGCCG"
53+
query_randstrobes = strobealign.randstrobes_query(query, index_parameters)
54+
assert query_randstrobes
55+
# First randstrobe must be found
56+
assert index.find(query_randstrobes[0].hash)
57+
58+
n = 0
59+
for qr in query_randstrobes:
60+
for rs in index.find(qr.hash):
61+
n += 1
62+
assert rs.hash == qr.hash
63+
# Ensure the for loop did test something
64+
assert n > 1

0 commit comments

Comments
 (0)