Skip to content

Using mmcif-tools from Python

The tools also run from Python, in the same process, and give the same results as on the command line, byte for byte:

import mmciftools

# One tool, with the arguments of its command line: the output comes back as text
text = mmciftools.run('selchain', '-A', '4hhb.cif')
text = mmciftools.run('delentity', '-non-polymer,water', input=text)

# Several tools in a row, the last writing to a file
mmciftools.pipeline('selaltloc', 'delentity -non-polymer,water', 'reres -1',
                    path='4hhb.cif', output='clean.cif')

# Reports as dictionaries (the --json output of the tool)
info = mmciftools.report('info', '4hhb.cif')
print(info['blocks'][0]['resolution'])  # 1.74
check = mmciftools.report('validate', 'clean.cif')
print(check['valid'], check['warnings'])
  • run(tool, *args, input=None, output=None, cwd=None) returns the output as text, or writes it to output. The tool reads the file named in args, or else input: text, or bytes (gzip-compressed or not). Tools are named with or without mmcif_.
  • pipeline(*steps, path=None, input=None, output=None) runs steps such as 'selchain -A' (or ['selchain', '-A']) one after the other, each reading the output of the one before.
  • report(tool, *args, input=None) runs mmcif_info, mmcif_wc, mmcif_validate, mmcif_gap, mmcif_chkensemble or mmcif_blocks with --json and returns the report as a dictionary.
  • call(...) takes the arguments of run() and returns a Result (status, stdout, stderr) whatever the exit status; tools() lists the tools.
  • A failure raises mmciftools.ToolError, with the tool's message and exit status. What a tool prints as a warning comes as a mmciftools.ToolWarning (see Python's warnings module). The split tools write their files in cwd, the current directory by default.
  • The calls redirect the standard streams of the process, so they run one at a time (a lock keeps threads from overlapping). To run tools in parallel, run the commands in subprocesses.