Skip to content

Adapter

Bidirectional sync between MolPy objects and external library representations.

Quick reference

Symbol Summary Preferred for
Adapter[I, E] ABC for internal ↔ external sync Custom adapter implementations
RDKitAdapter Sync Atomistic ↔ RDKit Mol 3D generation, substructure search
OpenBabelAdapter Sync Atomistic ↔ OpenBabel OBMol Format conversion

Canonical example

from molpy.adapter import RDKitAdapter
from rdkit.Chem import AllChem

adapter = RDKitAdapter(internal=mol)
rd_mol = adapter.get_external()
AllChem.EmbedMolecule(rd_mol)
adapter.set_external(rd_mol)
adapter.sync_to_internal()
optimized = adapter.get_internal()

Key behavior

  • get_external() auto-syncs internal → external if needed
  • get_internal() auto-syncs external → internal if needed
  • RDKit and OpenBabel are optional dependencies; adapters fail gracefully if not installed

Full API

Base

base

Base Adapter class for MolPy.

This module provides the abstract base class for adapters that maintain bidirectional synchronization between MolPy's internal data structures and external representations.

Adapters do NOT execute external tools or spawn subprocesses.

Adapter

Adapter(internal=None, external=None)

Bases: ABC, Generic[InternalT, ExternalT]

Abstract base class for representation adapters.

Adapters maintain bidirectional synchronization between MolPy's internal data structures (e.g., Atomistic, Frame) and external representations.

Adapters MUST NOT execute external tools or spawn subprocesses.

check
check()

Validate the adapter has enough state to do useful work.

This is intentionally lightweight and side-effect free. Concrete adapters may override with stronger validation.

sync_to_external
sync_to_external()

Sync from internal to external representation.

Subclasses should override _do_sync_to_external() to implement the actual synchronization logic.

sync_to_internal
sync_to_internal()

Sync from external to internal representation.

Subclasses should override _do_sync_to_internal() to implement the actual synchronization logic.

RDKit

rdkit

RDKit adapter for MolPy.

This module provides bidirectional synchronization between MolPy's Atomistic structures and RDKit's Chem.Mol objects.

RDKit is an optional dependency.

RDKitAdapter

RDKitAdapter(internal=None, external=None)

Bases: Adapter[Atomistic, Mol]

Bridge between MolPy's atomistic representation and rdkit.Chem.Mol.

copy
copy()

Return a new RDKitAdapter with copied internal and external state.

Both the Atomistic (internal) and Chem.Mol (external) are deep-copied so that the returned adapter is fully independent of the original.

Returns:

Type Description
'RDKitAdapter'

A new RDKitAdapter instance with copied state.

sync_to_internal
sync_to_internal(update_topology=True)

Sync from external to internal representation.

Parameters:

Name Type Description Default
update_topology bool

Whether to update topology when internal already exists.

True

OpenBabel

openbabel

OpenBabel adapter for MolPy.

This module provides bidirectional synchronization between MolPy's Atomistic structures and OpenBabel's OBMol objects.

OpenBabel is an optional dependency.

OpenBabelAdapter

OpenBabelAdapter(internal=None, external=None)

Bases: Adapter[Atomistic, 'ob.OBMol']

Bridge between MolPy's Atomistic and OpenBabel's OBMol.

OpenBabel provides built-in force field typing via its FFCalcTypes() function, which can be used to compare against our GAFF typifier implementation.

assign_gaff_types
assign_gaff_types()

Use OpenBabel's GAFF force field to assign atom types.

Returns:

Type Description
dict[int, str]

Dictionary mapping atom index (0-based) to GAFF atom type.

get_type_comparison
get_type_comparison()

Compare OpenBabel GAFF types against MolPy GAFF typing.

Requires that MolPy Atomistic has already been typed.

Returns:

Type Description
dict[str, Any]

Dictionary with comparison statistics and per-atom results.