Constraint optimization in Python with Open Babel
This post is just a quick post to show, how you can optimize molecules with harmonic constraints in Python with Open Babel. This requires Open Babel to be compiled with Python SWIG-bindings.
There are three types of constraints, distances, angles and torsions. Here is a gist that might help get you started.
import openbabel as ob
# Standard openbabel molecule load
conv = ob.OBConversion()
conv.SetInAndOutFormats('xyz','xyz')
mol = obOBMol()
conv.ReadFile(mol,'my_mol.xyz')
# Define constraints
constraints = ob.OBFFConstraints()
constraints.AddDistanceConstraint(1, 10, 3.4) # Angstroms
constraints.AddAngleConstraint(1, 2, 3, 120.0) # Degrees
constraints.AddTorsionConstraint(1, 2, 3, 4, 180.0) # Degrees
# Setup the force field with the constraints
forcefield = ob.OBForceField.FindForceField("MMFF94")
forcefield.Setup(mol, constraints)
forcefield.SetConstraints(constraints)
# Do a 500 steps conjugate gradient minimiazation
# and save the coordinates to mol.
forcefield.ConjugateGradients(500)
forcefield.GetCoordinates(mol)
# Write the mol to a file
conv.WriteFile(mol,'my_mol.xyz')
Acknowledgements: Kasper Thofte pretty much wrote the above gist back in the days.