Skip to content

Commit

Permalink
Implement __len__ for HMMPressedFile using the SSI index
Browse files Browse the repository at this point in the history
  • Loading branch information
althonos committed Oct 9, 2024
1 parent 480b7fb commit c7cc98b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions pyhmmer/plan7.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ cdef class HMMPressedFile:
cdef P7_HMMFILE* _hfp
cdef Alphabet _alphabet
cdef HMMFile _hmmfile
cdef size_t _position

cpdef void close(self) except *
cpdef void rewind(self) except *
Expand Down
1 change: 1 addition & 0 deletions pyhmmer/plan7.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ class HMMPressedFile(typing.Iterator[OptimizedProfile]):
) -> bool: ...
def __iter__(self) -> HMMPressedFile: ...
def __next__(self) -> OptimizedProfile: ...
def __len__(self) -> int: ...
@property
def closed(self) -> bool: ...
@property
Expand Down
7 changes: 7 additions & 0 deletions pyhmmer/plan7.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3750,6 +3750,7 @@ cdef class HMMPressedFile:
self._alphabet = None
self._hmmfile = None
self._hfp = NULL
self._position = 0

def __init__(self, object file):
"""__init__(self, file)\n--\n
Expand Down Expand Up @@ -3790,6 +3791,10 @@ cdef class HMMPressedFile:
def __exit__(self, exc_type, exc_value, traceback):
self.close()

def __len__(self):
assert self._hfp.ssi != NULL
return self._hfp.ssi.nprimary - self._position

# --- Properties ---------------------------------------------------------

@property
Expand Down Expand Up @@ -3822,6 +3827,7 @@ cdef class HMMPressedFile:
"""Rewind the file back to the beginning.
"""
self._hmmfile.rewind()
self._position = 0

cpdef OptimizedProfile read(self):
"""Read the next optimized profile from the file.
Expand Down Expand Up @@ -3856,6 +3862,7 @@ cdef class HMMPressedFile:

if status == libeasel.eslOK:
om.alphabet = self._alphabet
self._position += 1
return om
elif status == libeasel.eslEOF:
return None
Expand Down
10 changes: 10 additions & 0 deletions pyhmmer/tests/test_plan7/test_hmmfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ def test_read_optimized_profiles(self):
with self.open_hmm(path) as f:
self.check_hmmfile(f.optimized_profiles())

def test_optimized_profiles_length(self):
path = os.path.join(self.hmms_folder, "db", "{}.hmm".format(self.ID))
with self.open_hmm(path) as f:
profiles = f.optimized_profiles()
self.assertEqual(len(profiles), len(self.NAMES))
profiles.read()
self.assertEqual(len(profiles), len(self.NAMES) - 1)
profiles.rewind()
self.assertEqual(len(profiles), len(self.NAMES))

def test_rewind(self):
path = os.path.join(self.hmms_folder, "txt", "{}.hmm".format(self.ID))
with self.open_hmm(path) as f:
Expand Down

0 comments on commit c7cc98b

Please sign in to comment.