Skip to content

Instantly share code, notes, and snippets.

@ghutchis
Created August 7, 2019 01:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghutchis/0766478cc9bc1c00f623ce560954b8c1 to your computer and use it in GitHub Desktop.
Save ghutchis/0766478cc9bc1c00f623ce560954b8c1 to your computer and use it in GitHub Desktop.
Convert non-3D molecules to 3D using Open Babel
#!/usr/bin/env python
import sys, os
import pybel
ob = pybel.ob
# read through multiple files on command-line
for argument in sys.argv[1:]:
filename, extension = os.path.splitext(argument)
# read the molecule from the supplied file
for mol in pybel.readfile(extension[1:], argument):
if mol.OBMol.GetDimension() == 3:
continue
pybel._builder.Build(mol.OBMol)
mol.addh()
ff = pybel._forcefields["mmff94"]
success = ff.Setup(mol.OBMol)
if not success:
ff = pybel._forcefields["uff"]
success = ff.Setup(mol.OBMol)
if not success:
sys.exit("Cannot set up forcefield")
ff.ConjugateGradients(100, 1.0e-3)
ff.WeightedRotorSearch(100, 25)
ff.ConjugateGradients(250, 1.0e-4)
ff.GetCoordinates(mol.OBMol)
mol.write("sdf", "test.sdf", overwrite=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment