Skip to content

Commit 0dfedee

Browse files
committed
Make HMMFile._open_fileobj pretend to stream gzip to prevent HMMER from seeking when possible
1 parent 9b01bfc commit 0dfedee

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/pyhmmer/plan7.pyx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3445,7 +3445,7 @@ cdef class HMMFile:
34453445

34463446
# store options
34473447
hfp.f = fopen_obj(fh_, "r")
3448-
hfp.do_gzip = False
3448+
hfp.do_gzip = True
34493449
hfp.do_stdin = False
34503450
hfp.newly_opened = True
34513451
hfp.is_pressed = False
@@ -3464,6 +3464,8 @@ cdef class HMMFile:
34643464
filename = fh.name.encode()
34653465
hfp.fname = strdup(filename)
34663466
if hfp.fname == NULL:
3467+
fclose(hfp.f)
3468+
libhmmer.p7_hmmfile.p7_hmmfile_Close(hfp)
34673469
raise AllocationError("char", sizeof(char), strlen(filename))
34683470

34693471
# check if the parser is in binary format,
@@ -3480,22 +3482,28 @@ cdef class HMMFile:
34803482
# create and configure the file parser
34813483
hfp.efp = libeasel.fileparser.esl_fileparser_Create(hfp.f)
34823484
if hfp.efp == NULL:
3485+
fclose(hfp.f)
34833486
libhmmer.p7_hmmfile.p7_hmmfile_Close(hfp)
34843487
raise AllocationError("ESL_FILEPARSER", sizeof(ESL_FILEPARSER))
34853488
status = libeasel.fileparser.esl_fileparser_SetCommentChar(hfp.efp, b"#")
34863489
if status != libeasel.eslOK:
3490+
fclose(hfp.f)
34873491
libhmmer.p7_hmmfile.p7_hmmfile_Close(hfp)
34883492
raise UnexpectedError(status, "esl_fileparser_SetCommentChar")
34893493

34903494
# get the magic string at the beginning
34913495
status = libeasel.fileparser.esl_fileparser_NextLine(hfp.efp)
34923496
if status == libeasel.eslEOF:
3497+
fclose(hfp.f)
3498+
libhmmer.p7_hmmfile.p7_hmmfile_Close(hfp)
34933499
raise EOFError("HMM file is empty")
34943500
elif status != libeasel.eslOK:
3501+
fclose(hfp.f)
34953502
libhmmer.p7_hmmfile.p7_hmmfile_Close(hfp)
34963503
raise UnexpectedError(status, "esl_fileparser_NextLine");
34973504
status = libeasel.fileparser.esl_fileparser_GetToken(hfp.efp, &token, &token_len)
34983505
if status != libeasel.eslOK:
3506+
fclose(hfp.f)
34993507
libhmmer.p7_hmmfile.p7_hmmfile_Close(hfp)
35003508
raise UnexpectedError(status, "esl_fileparser_GetToken");
35013509

@@ -3514,6 +3522,7 @@ cdef class HMMFile:
35143522
# check the format tag was recognized
35153523
if hfp.parser == NULL:
35163524
text = token.decode("utf-8", "replace")
3525+
fclose(hfp.f)
35173526
libhmmer.p7_hmmfile.p7_hmmfile_Close(hfp)
35183527
raise ValueError("Unrecognized format tag in HMM file: {!r}".format(text))
35193528

@@ -3692,6 +3701,10 @@ cdef class HMMFile:
36923701
36933702
"""
36943703
if self._hfp:
3704+
# if we read from a file-like object, `p7_hmmfile_Close` won't
3705+
# close self._hfp.f but we actually still need to free it.
3706+
if self._hfp.do_gzip:
3707+
fclose(self._hfp.f)
36953708
libhmmer.p7_hmmfile.p7_hmmfile_Close(self._hfp)
36963709
self._hfp = NULL
36973710

0 commit comments

Comments
 (0)