Adapter Module¶
The adapter module provides bidirectional conversion between MolPy structures and external molecular libraries, primarily RDKit.
Key Features¶
- Convert between RDKit
Molobjects and MolPyAtomisticstructures - Generate 3D coordinates using RDKit's ETKDG method
- Optimize geometries with MMFF/UFF force fields
- Generate 2D molecular drawings (SVG)
- Stable atom mapping for round-trip conversion
In [1]:
Copied!
from rdkit import Chem
from molpy.adapter import RDKitWrapper, atomistic_to_mol, mol_to_atomistic
# Create a molecule from SMILES
mol = Chem.MolFromSmiles("CCO")
# Convert to MolPy Atomistic
atomistic = mol_to_atomistic(mol)
print(f"Converted molecule with {len(atomistic.atoms)} atoms")
# Create wrapper for bidirectional conversion
wrapper = RDKitWrapper.from_mol(mol)
# Generate 3D coordinates
wrapper.generate_3d(optimize=True)
# Access the MolPy structure
atomistic_3d = wrapper.inner
print("Generated 3D structure with coordinates")
from rdkit import Chem
from molpy.adapter import RDKitWrapper, atomistic_to_mol, mol_to_atomistic
# Create a molecule from SMILES
mol = Chem.MolFromSmiles("CCO")
# Convert to MolPy Atomistic
atomistic = mol_to_atomistic(mol)
print(f"Converted molecule with {len(atomistic.atoms)} atoms")
# Create wrapper for bidirectional conversion
wrapper = RDKitWrapper.from_mol(mol)
# Generate 3D coordinates
wrapper.generate_3d(optimize=True)
# Access the MolPy structure
atomistic_3d = wrapper.inner
print("Generated 3D structure with coordinates")
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) Cell In[1], line 1 ----> 1 from rdkit import Chem 3 from molpy.adapter import RDKitWrapper, atomistic_to_mol, mol_to_atomistic 5 # Create a molecule from SMILES ModuleNotFoundError: No module named 'rdkit'
Converting from Atomistic to RDKit¶
You can also convert MolPy structures to RDKit:
In [2]:
Copied!
# Convert Atomistic back to RDKit
mol_from_atomistic = atomistic_to_mol(atomistic_3d)
print(f"RDKit molecule has {mol_from_atomistic.GetNumAtoms()} atoms")
# Generate 2D drawing
svg = wrapper.draw(show=False)
print("Generated 2D molecular drawing")
# Convert Atomistic back to RDKit
mol_from_atomistic = atomistic_to_mol(atomistic_3d)
print(f"RDKit molecule has {mol_from_atomistic.GetNumAtoms()} atoms")
# Generate 2D drawing
svg = wrapper.draw(show=False)
print("Generated 2D molecular drawing")
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[2], line 2 1 # Convert Atomistic back to RDKit ----> 2 mol_from_atomistic = atomistic_to_mol(atomistic_3d) 3 print(f"RDKit molecule has {mol_from_atomistic.GetNumAtoms()} atoms") 5 # Generate 2D drawing NameError: name 'atomistic_to_mol' is not defined