-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptimize_ligand.py
executable file
·77 lines (58 loc) · 1.79 KB
/
optimize_ligand.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/home/hamit/jupyter_env/venv/bin/python
###########
# Usage:
# python optimize_ligand.py (</path/to/ligand.mol> | </path/to/ligands/dir/>) (</path/to/output.pdb> | </path/to/output/dir/>)
###########
from rdkit import Chem
from rdkit.Chem import AllChem
def mol_with_atom_index(mol):
'''
Indexes the atoms of the structure obtained from the mol file.
Parameters
----------
mol : rdkit.Chem.rdchem.Mol
Chemical structure.
Returns
-------
mol : rdkit.Chem.rdchem.Mol
Indexed chemical structure.
'''
for atom in mol.GetAtoms():
atom.SetAtomMapNum(atom.GetIdx())
return mol
def optimize_ligand(ligand_path, output_path):
'''
Uses RDKit to add hydrogen atoms and 3d optimize ligand structures.
Parameters
----------
ligand_path : str
Path to ligand mol file.
output_path : str
Path to output optimized ligand.
Returns
-------
mol_H : rdkit.Chem.rdchem.Mol
Optimized structure.
'''
mol = Chem.MolFromMolFile(ligand_path)
mol_H = Chem.AddHs(mol)
while AllChem.EmbedMolecule(mol_H):
pass
while AllChem.MMFFOptimizeMolecule(mol_H):
pass
Chem.MolToPDBFile(mol_with_atom_index(mol_H), output_path)
return mol_H
if __name__ == '__main__':
import os
import sys
INPUT_PATH = sys.argv[1]
OUTPUT_PATH = sys.argv[2]
if INPUT_PATH.endswith('.mol'):
optimize_ligand(INPUT_PATH, OUTPUT_PATH)
print(f'Optimized: {os.path.basename(INPUT_PATH)}')
else:
for ligand_file in os.listdir(INPUT_PATH):
if not ligand_file.endswith('.mol'):
continue
optimize_ligand(f'{INPUT_PATH}/{ligand_file}', f'{OUTPUT_PATH}/{ligand_file[:-3]}pdb')
print(f'Optimized: {ligand_file}')