-
Notifications
You must be signed in to change notification settings - Fork 33
/
uffminimize.py
executable file
·51 lines (41 loc) · 1.61 KB
/
uffminimize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/python
import sys,string
from rdkit.Chem import AllChem as Chem
from optparse import OptionParser
#minimize sdf structures with the UFF forcefiled and rdkit
parser = OptionParser(usage="Usage: %prog [options] <input>.sdf <output>.sdf")
parser.add_option("-v","--verbose", dest="verbose",action="store_true",default=False,
help="verbose output")
parser.add_option("-a","--add_hydrogens", dest="addh",action="store_true",default=False,
help="add hydrogens")
(options, args) = parser.parse_args()
input = args[0]
output = args[1]
inmols = Chem.SDMolSupplier(input)
if inmols is None:
print("Could not open ".input)
sys.exit(-1)
sdwriter = Chem.SDWriter(output)
if sdwriter is None:
print("Could not open ".output)
sys.exit(-1)
for mol in inmols:
if mol is not None:
try:
if options.addh:
mol = Chem.AddHs(mol,addCoords=True)
orig = Chem.UFFGetMoleculeForceField(mol).CalcEnergy()
a=Chem.UFFOptimizeMolecule(mol)
#sometimes the UFF Optimization does not converge (returns a 1 instead of 0)
if a==1:
a=Chem.UFFOptimizeMolecule(mol, maxIters=1000)
if options.verbose:
e = Chem.UFFGetMoleculeForceField(mol).CalcEnergy()
print(mol.GetProp('_Name'),orig,"->",e)
sdwriter.write(mol)
except (KeyboardInterrupt, SystemExit):
raise
except:
print("Exception occurred",mol.GetProp('_Name'))
else:
print("ERROR")