Advanced usage and customization¶
pidibble is driven by a declarative description of the PDB and mmCIF formats, and several constructor arguments let you extend or override it.
Custom field mappers¶
Every field in the format specification names a type, and each type maps to a
callable that turns the raw column text into a Python value. The built-in types
are String, Integer, Float and HxInteger (the hexadecimal-aware
atom-serial type), plus a family of list parsers.
You can add or override these with the mappers argument — a dict from type
name to a one-argument callable. This is the hook for a custom type used by a
custom format, or for changing how an existing type is coerced:
def tenths(text):
return round(float(text), 1)
p = PDBParser(source_db='rcsb', source_id='4zmj',
mappers={'Float': tenths}).parse()
Your mappers are merged over the defaults, so you only supply what you want to change.
Overriding the format specification¶
The PDB and mmCIF record definitions are shipped as YAML resources
(pdb_format.yaml and mmcif_format.yaml). To parse a nonstandard dialect
or add a record type, point the parser at your own files:
p = PDBParser(filepath='custom.pdb',
pdb_format_file='my_pdb_format.yaml').parse()
c = PDBParser(filepath='custom.cif', input_format='mmCIF',
mmcif_format_file='my_mmcif_format.yaml').parse()
The format-specification schema¶
Each record type in pdb_format.yaml is described by a small set of keys:
typeThe record’s cardinality/shape: one-time-one-line, one-time-multiple-lines, multiple-times-one-line, multiple-times-multiple-lines, grouping, or other.
fieldsA mapping of field name to a
[type, [start, end]]pair giving the field’s data type and its (1-based, inclusive) column range.continuesThe fields that a continuation record appends to or extends.
token_formatsHow a field’s text is broken into named tokens (with optional
determinantsthat group token/value pairs) — this is what produces the token groups ofCOMPNDandSOURCE.concatenateNew fields built by list-concatenating other fields.
allowedPer-field lists of allowed values, for validation.
determinantsThe fields whose values decide whether a line starts a new record instance or continues the current one.
subrecordsHow to parse variant sub-formats, selected by the value of a
branchonfield.tablesHow embedded tables (such as the
BIOMTmatrices) are parsed out.
Reusable sub-formats (for example the Residue variants and the Biomt row)
live under custom_formats and are referenced by name from field definitions.
Comment characters¶
By default, lines beginning with
#are treated as comments and skipped. Passcomment_charsto change that set — for example, to also ignore!lines: