-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathadd_h.py
More file actions
executable file
·36 lines (25 loc) · 1.14 KB
/
add_h.py
File metadata and controls
executable file
·36 lines (25 loc) · 1.14 KB
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
#!/usr/bin/env python3
__author__ = 'Pavel Polishchuk'
import argparse
import glob
from rdkit import Chem
def main():
parser = argparse.ArgumentParser(description='Add hydrogens to input files.')
parser.add_argument('-i', '--input', metavar='FILENAME', nargs='+', required=True,
help='input MOL/SDF files. Multiple files can be supplied. MOL files are modified in place. '
'SDF files result in new SDF files with suffix _H in file name.')
args = parser.parse_args()
for path in args.input:
for fname in glob.glob(path):
if fname.endswith('.mol'):
mol = Chem.MolFromMolFile(fname, removeHs=False)
mol = Chem.AddHs(mol, addCoords=True)
Chem.MolToMolFile(mol, fname)
if fname.endswith('.sdf'):
mols = [Chem.AddHs(mol, addCoords=True) for mol in Chem.SDMolSupplier(fname, removeHs=False) if mol is not None]
w = Chem.SDWriter(fname[:-4] + '_H.sdf')
for mol in mols:
w.write(mol)
w.close()
if __name__ == '__main__':
main()