/
AutoCouple_Script_3_Buchwald-Hartwig.py
306 lines (299 loc) · 12.6 KB
/
AutoCouple_Script_3_Buchwald-Hartwig.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
## Python Scripts for AutoCouple
##
## Author: Laurent Batiste
##
## Affiliation: A. Caflisch' group at the Department of Biochemistry of the University of Zurich
##
## Date: October 31, 2017
##--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
##
##
import sys, os, rdkit, rdkit.Chem
from rdkit.Chem import rdchem
from rdkit.Chem import rdMolDescriptors
from rdkit.Chem import AllChem
from rdkit.Chem import rdmolfiles
from rdkit.Chem import rdmolops
from rdkit.Chem import EditableMol
#Class1 : Phosporus_ylids / Class2 : Phosporus_ylids_poly / Class3 : Boronic_acid_ester / Class4 : Boronic_acid_ester_poly / Class5 : I_II_amines / Class6 : I_II_amines_poly / Class7 : alcohols / Class8 : alcohols_poly / Class9 : C+III_carbonyls / Class10 : C+III_carbonyls_poly / Class11 : sulfonyl_chlorides / Class12 : sulfonyl_chlorides_poly / Class13 : isocyanates_thio / Class14 : isocyanates_thio_poly / Class15 : alkyl_halides / Class16 : aryl_vinyl_halides / Class17 : Grignard / Class18 : ketons_aldehydes / Class19 : ketons_aldehydes_poly / Class20 : epoxydes / Class21 : terminal_alkenes / Class22 : terminal_alkenes_poly
try:
Bromids_file = open(sys.argv[1],'r')
Nu_file = open(sys.argv[2],'r')
Title = sys.argv[3]
except:
print 'Usage \'python script.py bromid_sdf nucleophile_sdf output_name\''
exit()
NuBr_Mol_list = []
Bromids_string = Bromids_file.readlines()
Nu_string = Nu_file.readlines()
Bromids_file.close()
Nu_file.close()
count=0
linecount=0
sectioncount=0
outputstring = ''
Nu_mol_list = {}
Br_mol_list = {}
competitivecount=0
dictcompetitive = {'amine':[' 1 ',' 2 ',' 3 ',' 4 ',' 7 ',' 8 ',' 15 ',' 16 ',' 11 ',' 12 ',' 13 ',' 14 ',' 20 ',' 17 '],'halide':[' 1 ',' 2 ',' 3 ',' 4 ',' 5 ',' 6 ',' 7 ',' 8 ',' 11 ',' 12 ',' 13 ',' 14 ',' 20 ']}
"""
1 = 'Phosporus_ylids';class
2 = 'Phosporus_ylids_poly';class
3 = 'Boronic_acid_ester';class
4 = 'Boronic_acid_ester_poly';class
5 = 'I_II_amines';class
6 = 'I_II_amines_poly';class
7 = 'alcohols';class
8 = 'alcohols_poly';class
9 = 'C+III_carbonyls';class
10 = 'C+III_carbonyls_poly';class
11 = 'sulfonyl_chlorides';class
12 = 'sulfonyl_chlorides_poly';class
13 = 'isocyanates_thio';class
14 = 'isocyanates_thio_poly';class
15 = 'alkyl_halides';class
16 = 'aryl_vinyl_halides';class
17 = 'Grignard';class
18 = 'ketons_aldehydes';class
19 = 'ketons_aldehydes_poly';class
20 = 'epoxydes';class
21 = 'terminal_alkenes';class
22 = 'terminal_alkenes_poly';
"""
#######################################DICTIONARY_BROMID_REACTANTS############################################################
output=[]
atomoutput=[]
bondoutput=[]
atomidx=unknowCAS=0
atomsection=False
bondsection=False
CAS_nb=None
reactivecenter=''
competitive_Nu =False
outputlist=[]
nitril_flag=CIII_flag=False
br_count=0
br_allcount=0
flag_Mg=False
competitive_nucleophile=[]
flag_Mg = False
atomcount=0
activehalogen=activecarbon=atomidx=0
CIII_flag=False
nitril_flag=False
keton_aldh_flag = False
for n in range(0,len(Bromids_string)):
try:
outputlist.append(Bromids_string[n].split('\n')[0])
except:
print 'Line : \"%\" refuses command \"outputlist.append(Bromids_string[n].split(\'\\n\')[0])\"' %Bromids_string[n]
if 'REACTIVE_CENTERS' in Bromids_string[n]:
try:
reactivecenter=Bromids_string[n].split('<REACTIVE_CENTERS>')[1].split('[')[1].split(']')[0].split(',')
except:
print 'Line : \"%\" refuses command Bromids_string[n].split(<REACTIVE_CENTERS>)' %Bromids_string[n]
reactivecenter=int((Bromids_string[n].split('*REACTIVE_CENTERS*')[1]).split()[0])
elif 'CLASS' in Bromids_string[n]:
classes=Bromids_string[n].split('<CLASS>')[1]
for function in ['halide','sulfonyl_chlorides','C+III','keton']:
if function in sys.argv[1]:
competitive_nucleophile = dictcompetitive[function]
for classe in competitive_nucleophile:
if classe in classes:
competitive_Nu = True
elif 'CAS' in Bromids_string[n]:
CAS_nb = Bromids_string[n].split('<CAS>')[1]
elif '$$$$' in Bromids_string[n]:
activecarbon=int(reactivecenter[0])+1
activehalogen=int(reactivecenter[1])+1
newatomidx = {}
atomcount=bond=atomidx=None
atomidx=0
for m in range(0,len(outputlist)):
if '99 V2000' in outputlist[m]:
atomcount = int(outputlist[m].split()[0])
bond= int(outputlist[m].split()[1])-1
atomsection=True
continue
elif atomsection==True:
if 'Mg' in outputlist[m] and 'halide' in sys.argv[1]:
flag_Mg=True
atomidx+=1
if len(outputlist[m].split()) != len(outputlist[m+1].split()):
bondsection=True
atomsection=False
atomoutput.append(outputlist[m])
elif bondsection==True:
if len(outputlist[m].split()) != len(outputlist[m+1].split()) or 'M' in outputlist[m+1] or 'CHG' in outputlist[m+1]:
bondsection=False
if (activehalogen == int(outputlist[m].split()[0]) or activehalogen == int(outputlist[m].split()[1])) and (activecarbon == int(outputlist[m].split()[0]) or activecarbon == int(outputlist[m].split()[1])):
pass
else:
bondoutput.append(u"{0:>3s}{1:>3s}{2:>3s} 0\n".format(outputlist[m].split()[0],outputlist[m].split()[1],outputlist[m].split()[2]))
if CAS_nb == None or len(list(CAS_nb)) < 4:
unknowCAS +=1
CAS_nb = 'no_CAS_'+str(unknowCAS)
if competitive_Nu == False and flag_Mg==False:# and nitril_flag==False:
Br_mol_list[CAS_nb] = [atomoutput,bondoutput,atomcount,bond,activecarbon]
br_count+=1
if br_count == 100:
br_allcount +=br_count
br_count=0
else:
competitivecount+=1
reactivecenter=None
atomoutput=[]
bondoutput=[]
competitive_nucleophile=[]
CAS_nb=None
competitive_Nu = False
flag_Mg = False
atomsection=bonsection=False
nitril_flag=False
outputlist=[]
atomcount=0
activehalogen=activecarbon=atomidx=0
CIII_flag=False
nitril_flag=False
keton_aldh_flag = False
br_allcount +=br_count
print '-------------REACTANT 1--------------'
print 'Non-tolerated Building Blocks : '+str(competitivecount)
print 'Building Blocks kept for coupling : '+str(br_allcount)
#######################################DICTIONARY_NUCLEOPHILE_REACTANTS############################################################
mol_count=0
outputstring = []
CAS_nb=None
classes=CAS_nb=smarts=reactive_center=''
competitive_Nu=flag_halogen=False
nu_allcount=0
nu_count=0
flagmultiplectr=competitive_Nu=flag_halogen=False
mol_count=0
headoutput=''
outputstring = []
atomoutput=[]
bondoutput=[]
flag_halogen=False
CAS_nb=None
classes=CAS_nb=smarts=reactive_center=''
newatomidx={}
goodcount=0
competitivecount=0
for i in range(0,len(Nu_string)):
try:
outputstring.append(Nu_string[i].split('\n')[0])
except:
print 'Line : \"%\" refuses command \"outputlist.append(Nu_string[i].split(\'\\n\')[0])\"' %Nu_string[i]
if 'CAS' in Nu_string[i]:
CAS_nb = Nu_string[i].split('<CAS>')[1]
if 'FRAG>' in Nu_string[i]:
smarts = Nu_string[i].split('<FRAG>')[1]
if 'REACTIVE_CENTERS' in Nu_string[i]:
reactive_center = int(Nu_string[i].split('<REACTIVE_CENTERS>')[1])+1
if 'CLASS' in Nu_string[i]:
classes=Nu_string[i].split('<CLASS>')[1]
if "$$$$" in Nu_string[i]:
mol_count+=1
#if len(reactive_center.split()) > 1 or len(reactive_center.split()) == 0:
# flagmultiplectr=True
competitive_nucleophile=[]
competitive_nucleophile = dictcompetitive['amine']
for classe in competitive_nucleophile:
if classe in classes:
competitive_Nu = True
for h in range(0,len(outputstring)):
if '99 V2000' in outputstring[h]:
atomcount = int(outputstring[h].split()[0])
bond= int(outputstring[h].split()[1])
headoutput=' '+str(atomcount)+' '+str(bond)+' 0 0 0 0 0 0 0 0999 V2000'
atomsection=True
continue
elif atomsection==True:
if int(len(outputstring[h].split())) != int(len(outputstring[h+1].split())):
bondsection=True
atomsection=False
atomoutput.append(outputstring[h])
elif bondsection==True:
if len(outputstring[h].split()) != len(outputstring[h+1].split()) or 'M' in outputstring[h+1]:
bondsection=False
bondoutput.append("{0:>3s}{1:>3s}{2:>3s} 0".format(outputstring[h].split()[0],outputstring[h].split()[1],outputstring[h].split()[2]))
if competitive_Nu == False and flag_halogen==False:
goodcount+=1
if CAS_nb == None or len(list(CAS_nb)) < 4:
unknowCAS +=1
CAS_nb = 'no_CAS_'+str(unknowCAS)
Nu_mol_list[CAS_nb] = [headoutput,atomoutput,bondoutput,reactive_center,smarts]
nu_count += 1
if nu_count == 100:
nu_allcount +=nu_count
#print nu_allcount
nu_count=0
else:
competitivecount+=1
classes=CAS_nb=smarts=reactive_center=''
competitive_Nu=False
headoutput=''
atomsection=bondsection=False
atomoutput=[]
bondoutput=[]
outputstring=[]
reactive_center=None
CAS_nb=None
classes=CAS_nb=smarts=reactive_center=''
flagmultiplectr=competitive_Nu=False
competitive_Nu = False
atomsection=bonsection=False
headoutput=''
outputlist=[]
atomoutput=[]
bondoutput=[]
newatomidx = {}
atomcount=0
activehalogen=activecarbon=atomidx=0
print '-------------REACTANT 2--------------'
print 'Non-tolerated Building Blocks : '+str(competitivecount)
print 'Building Blocks kept for coupling : '+str(goodcount)
########################################REACTION_SECTION#################################################################
print '\n\nNo more than 10\'000 ligands will be generated - if you wish to change the number of generated ligands go to line 245 in the Script \"if threshold\"\n\n'
threshold=0
for CAS in (Nu_mol_list.keys()):
for Br_CAS in (Br_mol_list.keys()):
threshold+=1
if threshold==10000:
exit()
output=''
atomsection=False
bondsection=False
endbondsection=False
entry=False
output+=CAS+' '+Br_CAS+'\nNucleophile Coupling\n\n'
atomsNu=int(Nu_mol_list[CAS][0].split()[0])
atomcount = int(Nu_mol_list[CAS][0].split()[0])+Br_mol_list[Br_CAS][2]
bond= int(Nu_mol_list[CAS][0].split()[1])+Br_mol_list[Br_CAS][3]+1
newline=' '+str(atomcount)+' '+str(bond)+' 0 0 0 0 0 0 0 0999 V2000\n'
output+=newline
for n in range(0,len(Nu_mol_list[CAS][1])):
output+=Nu_mol_list[CAS][1][n]+'\n'
for t in range(0,len(Br_mol_list[Br_CAS][0])):
output+=Br_mol_list[Br_CAS][0][t]+'\n'
for f in range(0,len(Nu_mol_list[CAS][2])):
output+=Nu_mol_list[CAS][2][f]+'\n'
bondstoadd=Br_mol_list[Br_CAS][1]
for bondline in bondstoadd:
atom1=str(int(bondline.split()[0])+atomsNu)
atom2=str(int(bondline.split()[1])+atomsNu)
order=bondline.split()[2]
try:
output+= u"{0:>3s}{1:>3s}{2:>3s} 0\n".format(atom1,atom2,order)
except:
output+=' '+atom1+' '+atom2+' '+order+'\n'
#try:
output+= u"{0:>3s}{1:>3s}{2:>3s} 0\n".format(str(Nu_mol_list[CAS][3]),str(int(Br_mol_list[Br_CAS][4])+atomsNu),'1')
#except:
# output+=' '+str(int(Nu_mol_list[CAS][1].split()[0])+1)+' '+str(int(Br_mol_list[Br_CAS][4])+atomsNu+1)+' '+'1'
atomsection=bondsection=entry=False
#elif entry==False:
#output+=filelines[n]
output+='M END\n\n$$$$'
os.system(" echo \'"+output+"\' >> "+Title+".sdf")