-
Notifications
You must be signed in to change notification settings - Fork 1
/
protonate_molecules.py
75 lines (51 loc) · 1.59 KB
/
protonate_molecules.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
#!/usr/bin/env python
import copy
import numpy as np
import pandas as pd
import scipy
import sklearn
import rdkit.Chem as Chem
def protonate_molobj(molobj, atom):
"""Find all atom and protonate, return to protonated states
:molobj: TODO
:atom: TODO
:returns: TODO
"""
atoms = molobj.GetAtoms()
atoms = [x.GetAtomicNum() for x in atoms]
atoms = np.asarray(atoms, dtype=int)
idxs, = np.where(atoms == atom)
# If no 'atom' in 'atoms', then return none
if len(idxs) == 0: return
# Protonate atom with idx
for idx in idxs:
idx = int(idx)
molobj_prime = copy.deepcopy(molobj)
molobj_prime.GetAtomWithIdx(idx).SetFormalCharge(1)
yield molobj_prime
return
def main(args=None):
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--version', action='version', version="1.0")
parser.add_argument('--smiles', action='store', help='', metavar='FILE')
if args is None:
args = parser.parse_args()
else:
args = parser.parse_args(args)
# Parse GDB file
sep = "\t"
df = pd.read_csv(args.smiles, sep=sep, header=None)
molecules = df.iloc[:,0].to_list()
target_atom = 7
for molsmi in molecules:
molobj = Chem.MolFromSmiles(molsmi)
molobjs = protonate_molobj(molobj, target_atom)
molobjs = list(molobjs)
if len(molobjs) == 0: continue
protonated = [Chem.MolToSmiles(x) for x in molobjs]
rtn = [molsmi] + protonated
print(", ".join(rtn))
return
if __name__ == '__main__':
main()