-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReactor.py
128 lines (88 loc) · 3.27 KB
/
Reactor.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from itertools import product
from rdkit import Chem
from rdkit.Chem import AllChem
import utils.io as io
def run_reactor(reactants, reaction, add_hs=True):
"""Perform reaction with reactants
Args:
reactant (list): List of reactants
reaction (str): SMARTS reaction
Returns:
set: List of products of reaction
"""
rxn = AllChem.ReactionFromSmarts(reaction)
if rxn is None:
raise Exception("Cannot read {0}".format(reaction))
for i, elem in enumerate(reactants):
pattern = reaction.split(">>")[0].split(".")[i]
pattern = Chem.MolFromSmarts(pattern)
if elem.HasSubstructMatch(pattern) is False:
print(Chem.MolToSmiles(elem), Chem.MolToSmiles(pattern))
raise Exception("Reactant {0} doesn't match to template".format(i+1))
product = rxn.RunReactants(reactants)
prods = []
for i in range(len(product)):
mol = product[i][0]
Chem.SanitizeMol(mol, sanitizeOps=Chem.SanitizeFlags.SANITIZE_ALL ^ Chem.SanitizeFlags.SANITIZE_SETAROMATICITY)
if add_hs:
mol = io.add_hydrogens(mol)
prods.append(Chem.MolToSmiles(mol))
return list(set(prods))
def reactor(reactants, reaction, prefix="R1"):
bbs = []
for i, file in enumerate(reactants):
with open(file, "r") as f:
f = f.read().split("\n")
f = [elem for elem in f if elem != ""]
bbs.append(f)
# parse reaction
with open(reaction, "r") as f:
reaction = f.read().split("\n")[0]
bbs = list(product(*bbs))
products = []
# perform reaction
for i, line in enumerate(bbs):
smi_mols = []
smi_names = []
for i, smi in enumerate(line):
if "\t" in smi:
smi_mol, smi_name = smi.split("\t")
elif "," in smi:
smi_mol, smi_name = smi.split(",")
elif " " in smi:
smi_mol, smi_name = smi.split(" ")
else:
smi_mol = smi
smi_name = "Mol_" + str(i+1)
smi_mols.append(smi_mol)
smi_names.append(smi_name)
smi_mols_new = []
try:
for smi in smi_mols:
smi_mols_new.append(io.read_smi(smi, add_hs=False))
except:
continue
smi_mols = smi_mols_new[:]
try:
prods = run_reactor(smi_mols, reaction, add_hs=False)
for j, prod in enumerate(prods):
products.append([prod, prefix + "_" + "_".join(smi_names) + "v" + str(j)])
except Exception as e:
print(e)
# print(io.write_smi(smi_mols[0]))
return products
if __name__ == "__main__":
def args():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-reactants", help="", nargs="+", type=str, required=True)
parser.add_argument("-reaction", help="", type=str, required=True)
parser.add_argument("-out", help="", type=str, required=True)
args = parser.parse_args()
return args
args = args()
products = reactor(args.reactants, args.reaction)
products = [" ".join(row) for row in products]
with open(args.out, "w") as f:
f.write("\n".join(products))
f.close()