parser#

Entrypoint for parsing and preparing atomic-level structures with AtomWorks.

Use parse() for files and prepare_atom_array() for existing atoms. Both accept a config object or preset name. For unprepared file I/O, use atom_array_from_file() and atom_array_to_file().

class atomworks.io.parser.ParseConfig(add_missing_atoms: bool = True, add_bond_types_from_struct_conn: tuple[str, ...] = ('covale',), remove_ccds: tuple[str, ...] = ('SO4', 'GOL', 'EDO', 'PO4', 'ACT', 'PEG', 'DMS', 'TRS', 'PGE', 'PG4', 'FMT', 'EPE', 'MPD', 'MES', 'CD', 'IOD'), remove_waters: bool = True, fix_ligands_at_symmetry_centers: bool = True, fix_arginines: bool = True, long_bond_policy: Literal['keep', 'filter', 'warn', 'raise', 'filter_nonstandard_only', 'warn_nonstandard_only', 'raise_nonstandard_only'] = 'warn', struct_conn_distance_policy: Literal['keep', 'filter', 'warn', 'raise', 'filter_nonstandard_only', 'warn_nonstandard_only', 'raise_nonstandard_only'] = 'filter', add_id_and_entity_annotations: bool = True, convert_mse_to_met: bool = False, hydrogen_policy: Literal['keep', 'remove'] = 'keep', ccd_mirror_path: str | None = None, return_atom_array_plus: bool = False, model: int | None = None, build_assembly: Literal['first', 'all'] | list[str] | tuple[str] | None = 'all', extra_fields: list[str] | set[str] | frozenset[str] | dict[str, dict[str, Any] | None] | Literal['all'] | None = None, file_type: Literal['cif', 'pdb', 'bcif', 'mmjson'] | None = None, altloc: Literal['first', 'random_per_chain', 'random_clash_aware'] | str = 'first', altloc_seed: int | None = None, load_standard_annotations: bool = False, keep_cif_block: bool = False, cache_dir: str | None = None, save_to_cache: bool = False, load_from_cache: bool = False, cif_ccd_on_mismatch: Literal['ignore', 'error_heavy', 'error'] = 'error_heavy')[source]#

Bases: PrepareConfig

Configuration for parse() when loading from files.

Extends PrepareConfig with file/parse-specific fields. Use get_config() or from_preset() to construct from a named preset with optional overrides:

config = ParseConfig.from_preset("rcsb")
config = ParseConfig.from_preset("rcsb", model=0, build_assembly="first")
Parameters:
  • model – Model number to parse for files with multiple models (e.g., NMR). None loads all models. Defaults to None.

  • build_assembly – Which biological assembly to build. Options: None (asymmetric unit only), "first", "all", or a list/tuple of assembly IDs as strings. Defaults to "all".

  • extra_fields – Extra CIF fields to include in the AtomArrayStack. None includes no extra fields; "all" includes all fields. Only supported for CIF files. Defaults to None.

  • file_type – File type of the structure file. If None, inferred automatically from the file extension. Defaults to None.

  • altloc – Which alternate conformer to select. "first" selects the alphabetically-first altloc per chain, "random_per_chain" selects a random altloc per chain, "random_clash_aware" selects one altloc per residue while avoiding steric clashes between nearby residues, or a specific letter (e.g. "A"). Only supported for CIF files; PDB files always use "first". Defaults to "first".

  • altloc_seed – Seed for deterministic random altloc selection when altloc="random_per_chain" or altloc="random_clash_aware". If None (default), selection is non-deterministic.

  • load_standard_annotations – Whether to deserialize StandardAnnotations from the CIF block. Incompatible with add_missing_atoms=True and PDB files. Requires return_atom_array_plus=True. Defaults to False.

  • keep_cif_block – Whether to include the raw CIF block in the result dictionary. Defaults to False.

  • cache_dir – Cache directory; auto-defaults to <tempdir>/atomworks_parse_cache when caching is enabled. Defaults to None.

  • save_to_cache – Whether to save results as zstd-3 compressed pickles after parsing. Defaults to False.

  • load_from_cache – Whether to load complete parsed results from cache. Keys include parsing options, package versions, and the file path or buffer contents. Unseeded random altlocs and active external CCD overrides bypass caching. Defaults to False.

  • cif_ccd_on_mismatch – How authored chem_comp_atom templates treat atoms absent from the available CCD template: "error_heavy" (default) rejects unmatched heavy atoms; "error" rejects any unmatched atom; "ignore" keeps the component without CCD supplementation if heavy-atom names mismatch. Heavy atoms must consistently use standard or alternate CCD names.

altloc: Literal['first', 'random_per_chain', 'random_clash_aware'] | str = 'first'#
altloc_seed: int | None = None#
build_assembly: Literal['first', 'all'] | list[str] | tuple[str] | None = 'all'#
cache_dir: str | None = None#
cif_ccd_on_mismatch: Literal['ignore', 'error_heavy', 'error'] = 'error_heavy'#
extra_fields: list[str] | set[str] | frozenset[str] | dict[str, dict[str, Any] | None] | Literal['all'] | None = None#
file_type: Literal['cif', 'pdb', 'bcif', 'mmjson'] | None = None#
keep_cif_block: bool = False#
load_from_cache: bool = False#
load_standard_annotations: bool = False#
model: int | None = None#
save_to_cache: bool = False#
atomworks.io.parser.get_config(preset: str = 'default', *, cls: type[~atomworks.io.config.T] = <class 'atomworks.io.config.PrepareConfig'>, **overrides) → T[source]#

Build a config from a named preset with optional overrides.

Parameters:
  • preset – Preset name. Available: "default", "rcsb", "lightweight", "annotations_only", "minimal".

  • cls – Config class to instantiate. Defaults to PrepareConfig.

  • **overrides – Fields to override on the preset.

atomworks.io.parser.parse(source: PathLike | StringIO | BytesIO | Callable[[], StringIO | BytesIO] | None = None, *, config: str | ParseConfig | None = None, filename: PathLike | StringIO | BytesIO | None = None, cache_key: str | None = None, **kwargs) → dict[str, Any][source]#

Read and prepare a structure file, returning atoms, assemblies and metadata.

Processing behaviour is controlled by ParseConfig. Legacy bare keyword arguments (e.g. add_missing_atoms=True) are still accepted but will emit a DeprecationWarning; prefer passing a config object.

Parameters:
  • source – Path or buffer to the structure file. May be any format of atomic-level structure (e.g. .cif, .bcif, .pdb), optionally compressed with gzip (.gz/.gzip) or Zstandard (.zst), although .cif files are strongly recommended. A callable returning a fresh buffer is evaluated only on cache misses and requires cache_key and config.file_type.

  • config – Processing configuration. Pass a preset name ("default", "rcsb", "lightweight", "minimal"), a ParseConfig instance, or None for defaults.

  • filename – Deprecated alias for source. Cannot be used together with source.

  • cache_key – Immutable source identity for deferred reads, replacing content hashing. Must change whenever source contents change; parser settings and package versions are included automatically. Cached results still honor the config’s cache flags.

Returns:

A result dictionary, or a list for models with different atom sets. Each dictionary contains:

  • chain_info: chain sequences, types, entities, and related information.

  • ligand_info: information about ligands of interest.

  • asym_unit: an AtomArrayStack representing the asymmetric unit.

  • assemblies: a mapping of assembly IDs to AtomArrayStacks.

  • metadata: structure metadata, such as resolution and deposition date.

  • extra_info: compatibility and caching information for internal use.

atomworks.io.parser.parse_atom_array(source: AtomArray | AtomArrayStack | AtomArrayPlus | AtomArrayPlusStack, *, config: str | PrepareConfig | None = None, **kwargs) → dict[str, Any][source]#

Prepare existing atoms and return the parser’s result-dictionary schema.

This compatibility entry point shares preparation with prepare_atom_array(). Prefer that function when only processed atoms are needed. Source-file metadata and biological assembly definitions are unavailable for an array input. Preparation may modify the input; pass source.copy() to preserve it.

Parameters:
  • source – The AtomArray or AtomArrayStack to process.

  • config – Preset name ("default", "rcsb", "lightweight"), PrepareConfig, or None for defaults.

atomworks.io.parser.prepare_atom_array(source: AtomArray | AtomArrayStack | AtomArrayPlus | AtomArrayPlusStack, *, config: str | PrepareConfig | None = None, cif_block: Any | None = None, extra_fields: list[str] | set[str] | frozenset[str] | dict[str, dict[str, Any] | None] | Literal['all'] | None = None) → AtomArray | AtomArrayStack[source]#

Perform standard AtomWorks preparation of an AtomArray, returning the processed atoms directly.

Runs the same core processing as parse() (standardize, add missing atoms, infer bonds, annotate) but returns the processed atoms instead of a full result dictionary. Single-model results are squeezed to AtomArray for conciseness.

This does not read a file, build biological assemblies or return source metadata. Preparation may modify the input; pass source.copy() to preserve it.

Parameters:
  • source – The structure to process.

  • config – Preset name ("default", "rcsb", "lightweight"), PrepareConfig, or None for defaults.

  • cif_block – Optional CIF block for richer processing (struct_conn bonds, entity categories, custom bonds).

  • extra_fields – Extra CIF fields to preserve through processing.