Parsers#

class pyinfernal.cm.CMFile#

A wrapper around a file storing serialized CMs.

Example

Load the first CM from a CM file located on the local filesystem:

>>> with CMFile("tests/data/cms/tRNA.c.cm") as cm_file:
...     cm = cm_file.read()
>>> cm.name
'tRNA'
>>> cm.accession
'RF00005'

Load all the CMs from a CM file into a list:

>>> with CMFile("tests/data/cms/5.c.cm") as cm_file:
...     cms = list(cm_file)
>>> len(cms)
5
>>> [cm.accession for cm in cms]
['RF00005', 'RF00006', 'RF01185', 'RF01855', 'RF01852']
__init__(file, db=True, *, alphabet=None)#

Create a CM reader from the given path or file.

Parameters:
  • file (str, bytes, os.PathLike or file-like object) – Either the path to a file containing the CMs 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.

  • alphabet (Alphabet, optional) – The alphabet of the CMs in the file. Supports auto-detection, but passing a non-None argument will facilitate MyPy type inference.

Raises:
  • TypeError – When file is not of the correct type, or when file is a file-like object open in text mode rather than binary mode.

  • RuntimeError – When the internal system function (fopencookie on Linux, funopen on BSD) fails to open the file.

close()#

Close the CM file and free resources.

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

>>> with CMFile("tests/data/cms/5.c.cm") as cm_file:
...     cm = cm_file.read()
>>> cm_file.closed
True
read()#

Read the next CM from the file.

Returns:

CM or None – The next CM in the file, or None if all CMs 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.

closed#

Whether the CMFile is closed or not.

Type:

bool

name#

The path to the CM file, if known.

Type:

str or None