Recipes

This page references recipes for common operations for the PyHMMER API, similarly to the itertools recipes from the Python documentation.

Loading multiple HMMs

An adapter for loading several HMMFile objects into a single object to pass to .

Credits

The original implementation proposed by Zachary Kurtz in #24, which was failing when too many file-descriptors where open on some OS (#48), was updated to keep at most a single file open at a time.

[1]:
import contextlib
import itertools
import os
import typing

from pyhmmer.plan7 import HMMFile, HMM

class HMMFiles(typing.Iterable[HMM]):
    def __init__(self, *files: typing.Union[str, bytes, os.PathLike]):
        self.files = files

    def __iter__(self):
        for file in self.files:
            with HMMFile(file) as hmm_file:
                yield from hmm_file

To use it with hmmsearch, simply create a HMMFiles object with the paths for the different HMM files to concatenate. They will be read in the order given as argument.

[2]:
import pyhmmer
from pyhmmer.easel import SequenceFile

with SequenceFile("data/seqs/938293.PRJEB85.HG003687.faa", digital=True) as sequences:
    targets = sequences.read_block()

hmm_files = HMMFiles("data/hmms/txt/PKSI-AT.hmm", "data/hmms/txt/LuxC.hmm")
all_hits = list(pyhmmer.hmmsearch(hmm_files, targets))

print("HMMs searched:", len(all_hits))
print("Hits found:   ", sum(len(hits) for hits in all_hits))
HMMs searched: 2
Hits found:    2

If your filenames are stored in a list, just use the splat operator to unpack it:

[3]:
filenames = ["data/hmms/txt/PKSI-AT.hmm", "data/hmms/txt/LuxC.hmm"]
hmm_files = HMMFiles(*filenames)