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
HMMobjects.Example
Load the first HMM from a text HMM file located on the local filesystem:
>>> with HMMFile("tests/data/hmms/txt/PF02826.hmm") as hmm_file: ... hmm = hmm_file.read() >>> hmm.name '2-Hacid_dh_C' >>> hmm.accession 'PF02826.20'
Load all the HMMs from a binary HMM file into a
list:>>> with HMMFile("tests/data/hmms/bin/RREFam.h3m") as hmm_file: ... hmms = list(hmm_file) >>> len(hmms) 10 >>> hmms[0].accession 'RREFam002.1'
- __init__(file, db=True, *, alphabet=None)#
Create a new HMM reader from the given path or file.
- Parameters:
file (
str,bytes,os.PathLikeor 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 toFalseto force the parser to ignore the pressed HMM database if it finds one. Defaults toTrue.alphabet (
Alphabet, optional) – The alphabet of the HMMs in the file. Supports auto-detection, but passing a non-Noneargument will facilitate MyPy type inference.
- 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
HMMFilewas used in a context:>>> with HMMFile("tests/data/hmms/bin/Thioesterase.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
HMMto anOptimizedProfile.Example
>>> HMMFile("tests/data/hmms/bin/Thioesterase.h3m").is_pressed() False >>> HMMFile("tests/data/hmms/db/Thioesterase.hmm").is_pressed() True
Added in version 0.4.11.
- optimized_profiles()#
Get an iterator over the
OptimizedProfilein 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, orNoneif 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.
- class pyhmmer.plan7.HMMPressedFile#
An iterator over each
OptimizedProfilein a pressed HMM database.This class cannot be instantiated: use the
HMMFile.optimized_profilesto obtain an instance from anHMMFilethat 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,
hmmsearchwill receive an iterator ofOptimizedProfileinstead of an iterator ofHMMishfilewas passed directly; this letshmmsearchskip the conversion step before running the search pipeline.Added in version 0.4.11.
Changed in version 0.7.0: Allow instantiating an
HMMPressedFilefrom a filename.- __init__(file, *, alphabet=None)#
Create a new pressed file from the given filename.
- Parameters:
file (
str,bytesoros.PathLike) – The path to the pressed HMM file containing the optimized profiles to read.alphabet (
Alphabet, optional) – The alphabet of the profiles in the file. Supports auto-detection, but passing a non-Noneargument will facilitate MyPy type inference.
- 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
HMMFilewas used in a context:>>> with HMMPressedFile("tests/data/hmms/db/Thioesterase.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, orNoneif 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
OptimizedProfilecould 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
HMMPressedFileis closed or not.Added in version 0.7.0.
- Type:
- name#
The path to the HMM file.
Note
Unlike
HMMFile.namethis attribute can never beNone, since aHMMPressedFileobject can only be created from a filename, and not from a file-like object.Added in version 0.7.0.
- Type: