Parsers#

class pyhmmer.plan7.HMMFile#

A wrapper around a file (or database), storing serialized HMMs.

You can use this class to iterate on the HMMs inside a file, loading them into HMM objects.

Example

Load the first HMM from an HMM file located on the local filesystem:

>>> with HMMFile("tests/data/hmms/txt/PF02826.hmm") as hmm_file:
...     hmm = hmm_file.read()
>>> hmm.name
b'2-Hacid_dh_C'
>>> hmm.accession
b'PF02826.20'

Load all the HMMs from an HMM file into a list:

>>> with HMMFile("tests/data/hmms/txt/RREFam.hmm") as hmm_file:
...     hmms = list(hmm_file)
>>> len(hmms)
28
>>> hmms[0].accession
b'RREFam008.1'
__init__(file, db=True)#

Create a new HMM reader from the given file.

Parameters:
  • file (str, bytes, os.PathLike or file-like object) – Either the path to a file containing the HMMs to read, or a file-like object in binary mode.

  • db (bool) – Set to False to force the parser to ignore the pressed HMM database if it finds one. Defaults to True.

close()#

Close the HMM file and free resources.

This method has no effect if the file is already closed. It is called automatically if the HMMFile was used in a context:

>>> with HMMFile("tests/data/hmms/bin/PKSI-AT.h3m") as hmm_file:
...     hmm = hmm_file.read()
is_pressed()#

Check whether the HMM file is a pressed HMM database.

A pressed database is an HMMER format to store optimized profiles in several files on the disk. It can be used to reduce the time needed to process sequences by cutting down the time needed to convert from an HMM to an OptimizedProfile.

Example

>>> HMMFile("tests/data/hmms/txt/PKSI-AT.hmm").is_pressed()
False
>>> HMMFile("tests/data/hmms/bin/PKSI-AT.h3m").is_pressed()
False
>>> HMMFile("tests/data/hmms/db/PKSI-AT.hmm").is_pressed()
True

Added in version 0.4.11.

optimized_profiles()#

Get an iterator over the OptimizedProfile in the HMM database.

Returns:

HMMPressedFile – An iterator over the optimized profiles in a pressed HMM database.

Added in version 0.4.11.

read()#

Read the next HMM from the file.

Returns:

HMM – The next HMM in the file, or None if all HMMs were read from the file already.

Raises:
  • ValueError – When attempting to read a HMM from a closed file, or when the file could not be parsed.

  • AllocationError – When memory for the HMM could not be allocated successfully.

  • AlphabetMismatch – When the file contains HMMs in different alphabets, or in an alphabet that is different from the alphabet used to initialize the HMMFile.

Added in version 0.4.11.

rewind()#

Rewind the file back to the beginning.

closed#

Whether the HMMFile is closed or not.

Type:

bool

name#

The path to the HMM file, if known.

Added in version 0.7.0.

Type:

str or None

class pyhmmer.plan7.HMMPressedFile#

An iterator over each OptimizedProfile in a pressed HMM database.

This class cannot be instantiated: use the HMMFile.optimized_profiles to obtain an instance from an HMMFile that wraps a pressed HMM database.

Example

Use a pressed HMM database to run a search pipeline using optimized profiles directly, instead of converting them from the text HMMs:

>>> with HMMFile("tests/data/hmms/db/Thioesterase.hmm") as hfile:
...     models = hfile.optimized_profiles()
...     hits = next(pyhmmer.hmmsearch(models, proteins))

In this example, hmmsearch will receive an iterator of OptimizedProfile instead of an iterator of HMM is hfile was passed directly; this lets hmmsearch skip the conversion step before running the search pipeline.

Added in version 0.4.11.

Changed in version 0.7.0: Allow instantiating an HMMPressedFile from a filename.

__init__(file)#

Create a new pressed file from the given filename.

Parameters:

file (str, bytes or os.PathLike) – The path to the pressed HMM file containing the optimized profiles to read.

close()#

Close the pressed file and free resources.

This method has no effect if the file is already closed. It is called automatically if the HMMFile was used in a context:

>>> with HMMPressedFile("tests/data/hmms/db/PKSI-AT.hmm") as hmm_db:
...     optimized_profile = hmm_db.read()
read()#

Read the next optimized profile from the file.

Returns:

OptimizedProfile – The next optimized profile in the file, or None if all profiles were read from the file already.

Raises:
  • ValueError – When attempting to read an optimized profile from a closed file, or when the file could not be parsed.

  • AllocationError – When memory for the OptimizedProfile could not be allocated successfully.

  • AlphabetMismatch – When the file contains optimized profiles in different alphabets, or in an alphabet that is different from the alphabet used to initialize the HMMFile.

Added in version 0.4.11.

rewind()#

Rewind the file back to the beginning.

closed#

Whether the HMMPressedFile is closed or not.

Added in version 0.7.0.

Type:

bool

name#

The path to the HMM file.

Note

Unlike HMMFile.name this attribute can never be None, since a HMMPressedFile object can only be created from a filename, and not from a file-like object.

Added in version 0.7.0.

Type:

str