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.PathLikeor 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 toFalseto force the parser to ignore the pressed HMM database if it finds one. Defaults toTrue.alphabet (
Alphabet, optional) – The alphabet of the CMs in the file. Supports auto-detection, but passing a non-Noneargument will facilitate MyPy type inference.
- Raises:
TypeError – When
fileis not of the correct type, or whenfileis a file-like object open in text mode rather than binary mode.RuntimeError – When the internal system function (
fopencookieon Linux,funopenon 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
CMFilewas 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:
CMorNone– The next CM in the file, orNoneif 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.