-
Notifications
You must be signed in to change notification settings - Fork 2
/
rBRICS_public.py
1224 lines (1098 loc) · 37.6 KB
/
rBRICS_public.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# $Id$
#
# Copyright (c) 2009, Novartis Institutes for BioMedical Research Inc.
# All rights reserved.
# Copyright (c) 2022, IBM LLC, Leili Zhang, Vasu Rao, Wendy Cornell
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of Novartis Institutes for BioMedical Research Inc.
# nor the names of its contributors may be used to endorse or promote
# products derived from this software without specific prior written permission.
# * Neither the name of International Business Machine
# nor the names of its contributors may be used to endorse or promote
# products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Created by Greg Landrum, Nov 2008
# Updated by Leili Zhang, Vasu Rao, Jul 2022
""" Implementation of the BRICS algorithm from Degen et al. ChemMedChem *3* 1503-7 (2008)
*** Unpublished manuscript by Zhang et al. *** (to be updated)
"""
from rdkit import Chem
from rdkit.Chem import rdChemReactions as Reactions
import sys,re,random
# These are the definitions that will be applied to fragment molecules:
environs = {
'L1':'[C;D3]([#0,#6,#7,#8])(=O)',
#
# After some discussion, the L2 definitions ("N.pl3" in the original
# paper) have been removed and incorporated into a (almost) general
# purpose amine definition in L5 ("N.sp3" in the paper).
#
# The problem is one of consistency.
# Based on the original definitions you should get the following
# fragmentations:
# C1CCCCC1NC(=O)C -> C1CCCCC1N[2*].[1*]C(=O)C
# c1ccccc1NC(=O)C -> c1ccccc1[16*].[2*]N[2*].[1*]C(=O)C
# This difference just didn't make sense to us. By switching to
# the unified definition we end up with:
# C1CCCCC1NC(=O)C -> C1CCCCC1[15*].[5*]N[5*].[1*]C(=O)C
# c1ccccc1NC(=O)C -> c1ccccc1[16*].[5*]N[5*].[1*]C(=O)C
#
#'L2':'[N;!R;!D1;!$(N=*)]-;!@[#0,#6]',
# this one turned out to be too tricky to define above, so we set it off
# in its own definition:
#'L2a':'[N;D3;R;$(N(@[C;!$(C=*)])@[C;!$(C=*)])]',
'L3':'[O;D2]-;!@[#0,#6,#1]',
'L4':'[C;!D1;!$(C=*)]-;!@[#6]',
#'L5':'[N;!D1;!$(N*!-*);!$(N=*);!$(N-[!C;!#0])]-[#0,C]',
'L5':'[N;!D1;!$(N=*);!$(N-[!#6;!#16;!#0;!#1]);!$([N;R]@[C;R]=O)]',
'L51':'[N;!R;!D1;$(N(!@[N,O]))]', #Leili N-N, N-O, only breaks with carbon
'L6':'[C;D3;!R](=O)-;!@[#0,#6,#7,#8]',
'L7a':'[C;D2,D3]-[#6]',
'L7b':'[C;D2,D3]-[#6]',
'#L8':'[C;!R;!D1]-;!@[#6]', #Original L8
'##L8':'[C;!R;!D1;!$(C!-*)]',
'L8':'[C;!R;!D1;!$(C!-*);!$(C([H])([H])([H]))]', #Leili fixed a problem when Hs are explicit
'L81':'[C;!R;!D1;$(C(-[C,N,O,S])(=[N,S]))]', #Leili amidine group
#'L82':'[C;!R;!D1;$(C(-NH2)(=[O]))]', #Leili terminal COCH2, only for long chains
'L9':'[RN,n;+0;$([RN,n](@[RC,RN,RO,RS,c,n,o,s])@[RC,RN,RO,RS,c,n,o,s])]', #Leili generalized ring
'#L9':'[n;+0;$(n(:[c,n,o,s]):[c,n,o,s])]', #Original L9
'L10':'[N;R;$(N(@C(=O))@[C,N,O,S])]',
'L11':'[S;D2](-;!@[#0,#6])',
'L12':'[S;D4]([#6,#0])(=O)(=O)',
'L12b':'[S;D4;!R](!@O)(!@O)', #Leili, PDB-SMI conversion issue
'L13':'[C;$(C(-;@[C,N,O,S])-;@[N,O,S])]',
'L14':'[c;$(c(:[c,n,o,s]):[n,o,s])]',
'L14b':'[RC;$([RC](@[RC,RN,RO,RS])@[RN,RO,RS])]', #Leili generalized ring
'L15':'[C;$(C(-;@C)-;@C)]',
'L16':'[c;$(c(:c):c)]',
'L16b':'[RC;$([RC](@[RC])@[RC])]', #Leili generalized ring
# Our Proposed Environments, by Zhang and Rao
'L17':'[C](-C)(-C)(-C)', #Aliphatic 1 - isobutyl/isopropyl
'L18':'[R!#1;x3]', #Ring 1
'L19':'[R!#1;x2]', #Ring 2
'L182':'[R!#1;x3]', #Ring 1b double bond - for assembly
'L192':'[R!#1;x2]', #Ring 2b double bond - for assembly
'L20':'[CH2][CH2][CH2][CH3,RC,c,$(C(~[!#6]))]', #Aliphatic 2 - butyl, v2
'L21':'[CH2][CH2][CH2][CH2][CH3,RC,c,$(C(~[!#6]))]', #Aliphatic 3 - butyl-CH3 or butyl-ring C or butyl-C-X, v2
'L22':'[CH2][CH2][CH2][CH2][CH2][CH2][CH2][CH3,RC,c,$(C(~[!#6]))]', #Aliphatic 4 - octanyl, v2
'L23':'[CH2][CH2][CH2][CH2][CH2][CH2][CH2][CH2][CH3,RC,c,$(C(~[!#6]))]', #Aliphatic 5 - octanyl-CH3 or octanyl-ring C or octonyl-C-X, v2
'L30':'[C;D2]([#0,#6,#7,#8,#16])(#[N,C])' #C#N C#C
#'L51':'[N;!R;!D1;$(N(!@[N,O]))]', #duplication for record
#'L81':'[C;!R;!D1;$(C(-[C,N,O,S])(=[N,S]))]', #duplication for record
}
reactionDefs = (
# L1
[
('1','3','-'),
('1','5','-'),
('1','10','-'),
],
# L30
[
('30','30','-'),
('30','4','-'),
('30','5','-'),
('30','51','-'),
('30','6','-'),
('30','81','-'),
('30','9','-'),
('30','10','-'),
('30','11','-'),
('30','12','-'),
('30','12b','-'),
('30','13','-'),
('30','14','-'),
('30','14b','-'),
('30','15','-'),
('30','16','-'),
('30','16b','-'),
],
# L3
[
('3','4','-'),
('3','13','-'),
('3','14','-'),
('3','15','-'),
('3','16','-'),
],
# L4
[
('4','5','-'),
('4','11','-'),
],
# L5
[
('5','12','-'),
('5','14','-'),
('5','16','-'),
('5','13','-'),
('5','15','-'),
],
# L51 Leili
[
('51','1','-'),
('51','4','-'),
('51','12','-'),
('51','12b','-'),
('51','14','-'),
('51','16','-'),
('51','13','-'),
('51','15','-'),
],
# L6
[
('6','13','-'),
('6','14','-'),
('6','15','-'),
('6','16','-'),
],
# L7
[
('7a','7b','='),
],
# L8
[
('8','9','-'),
('8','10','-'),
('8','13','-'),
('8','14','-'),
('8','15','-'),
('8','16','-'),
],
# L81 Leili
[
('81','8','-'),
('81','9','-'),
('81','10','-'),
('81','13','-'),
('81','14','-'),
('81','15','-'),
('81','16','-'),
],
# L9
[
('9','13','-'),# not in Degen paper
('9','14','-'),# not in Degen paper
('9','15','-'),
('9','16','-'),
],
# L10
[
('10','13','-'),
('10','14','-'),
('10','15','-'),
('10','16','-'),
],
# L11
[
('11','13','-'),
('11','14','-'),
('11','15','-'),
('11','16','-'),
],
# L12
# none left
# L12b Leili
[
('12b','12b','-'),
('12b','5','-'),
('12b','4','-'),
('12b','13','-'),
('12b','14','-'),
('12b','15','-'),
('12b','16','-'),
],
# L13
[
('13','14','-;@,!@'),
('13','15','-;!@'),
('13','16','-;@,!@'),
],
# L14
[
('14','14','-;@,!@'),
('14','15','-;@,!@'),
('14','16','-;@,!@'),
],
# L14b
[
('3','14b','-'),
('5','14b','-'),
('51','14b','-'),
('6','14b','-'),
('8','14b','-'),
('81','14b','-'),
('9','14b','-'),
('10','14b','-'),
('11','14b','-'),
('12b','14b','-'),
('13','14b','-'),
('14','14b','-'),
('14b','14b','-'),
('14b','16','-'),
('14b','16b','-'),
('14b','15','-'),
('14b','17','-'),
],
# L15
[
('15','16','-;@,!@'),
],
# L16
[
('16','16','-;@,!@'), # not in Degen paper
],
# L16b
[
('3','16b','-'),
('5','16b','-'),
('51','16b','-'),
('6','16b','-'),
('8','16b','-'),
('81','16b','-'),
('9','16b','-'),
('10','16b','-'),
('11','16b','-'),
('12b','16b','-'),
('13','16b','-'),
('15','16b','-'),
('16','16b','-'),
('16b','16b','-'),
('17','16b','-'),
],
# L17 Vasu and Leili (17-21)
[
('17','17','-'),
('17','16','-'),
('17','15','-'),
('17','14','-'),
('17','13','-'),
('17','12b','-'),
('17','12','-'),
('17','11','-'),
('17','10','-'),
('17','9','-'),
('17','8','-'),
('17','81','-'),
('17','51','-'),
('17','5','-'),
],
#L18
[
('18','19','-;@'),
('182','192','=;@'),
],
#L20
[
('20','21','-')
],
#L22
[
('22','23','-')
]
)
reactionDefs_r = (
[
('20','21','-')
],
)
import copy
def init_reactions(environs,reactionDefs):
smartsGps=copy.deepcopy(reactionDefs)
for gp in smartsGps:
for j,defn in enumerate(gp):
g1,g2,bnd = defn
r1=environs['L'+g1]
r2=environs['L'+g2]
g1 = re.sub('[a-z,A-Z]','',g1)
g2 = re.sub('[a-z,A-Z]','',g2)
if "@" not in bnd: #Leili
#if 1 == 1:
sma = '[$(%s):1]%s;!@[$(%s):2]>>[%s*]-[*:1].[%s*]-[*:2]' % (r1, bnd, r2, g1, g2)
else:
sma = '[$(%s):1]%s[$(%s):2]>>[%s*]-[*:1].[%s*]-[*:2]' % (r1, bnd, r2, g1, g2)
gp[j] = sma
#sma='[$(%s):1]%s;!@[$(%s):2]>>[%s*]-[*:1].[%s*]-[*:2]'%(r1,bnd,r2,g1,g2) #original
#gp[j] =sma
for gp in smartsGps:
for defn in gp:
try:
t=Reactions.ReactionFromSmarts(defn)
t.Initialize()
except:
print(defn)
raise
environMatchers={}
for env,sma in environs.items():
environMatchers[env]=Chem.MolFromSmarts(sma)
bondMatchers=[]
for i,compats in enumerate(reactionDefs):
tmp=[]
for i1,i2,bType in compats:
e1 = environs['L%s'%i1]
e2 = environs['L%s'%i2]
if "@" in bType: #Leili
patt = '[$(%s)]%s[$(%s)]' % (e1, bType, e2)
else:
patt = '[$(%s)]%s;!@[$(%s)]' % (e1, bType, e2)
#patt = '[$(%s)]%s;!@[$(%s)]'%(e1,bType,e2) #original
patt = Chem.MolFromSmarts(patt)
tmp.append((i1,i2,bType,patt))
bondMatchers.append(tmp)
reactions = tuple([[Reactions.ReactionFromSmarts(y) for y in x] for x in smartsGps])
reverseReactions = []
for i,rxnSet in enumerate(smartsGps):
for j,sma in enumerate(rxnSet):
rs,ps = sma.split('>>')
sma = '%s>>%s'%(ps,rs)
rxn = Reactions.ReactionFromSmarts(sma)
labels = re.findall(r'\[([0-9]+?)\*\]',ps)
rxn._matchers=[Chem.MolFromSmiles('[%s*]'%x) for x in labels]
reverseReactions.append(rxn)
return gp, environMatchers, bondMatchers, reactions, reverseReactions
gp, environMatchers, bondMatchers, reactions, reverseReactions = init_reactions(environs,reactionDefs)
gp_r, environMatchers_r, bondMatchers_r, reactions_r, reverseReactions_r = init_reactions(environs,reactionDefs_r)
def FindrBRICSBonds(mol,randomizeOrder=False,silent=True):
""" returns the bonds in a molecule that BRICS would cleave
>>> from rdkit import Chem
>>> m = Chem.MolFromSmiles('CCCOCC')
>>> res = list(FindrBRICSBonds(m))
>>> res
[((3, 2), ('3', '4')), ((3, 4), ('3', '4'))]
a more complicated case:
>>> m = Chem.MolFromSmiles('CCCOCCC(=O)c1ccccc1')
>>> res = list(FindrBRICSBonds(m))
>>> res
[((3, 2), ('3', '4')), ((3, 4), ('3', '4')), ((6, 8), ('6', '16'))]
we can also randomize the order of the results:
>>> random.seed(23)
>>> res = list(FindrBRICSBonds(m,randomizeOrder=True))
>>> sorted(res)
[((3, 2), ('3', '4')), ((3, 4), ('3', '4')), ((6, 8), ('6', '16'))]
Note that this is a generator function :
>>> res = FindrBRICSBonds(m)
>>> res
<generator object ...>
>>> res.next()
((3, 2), ('3', '4'))
>>> m = Chem.MolFromSmiles('CC=CC')
>>> res = list(FindrBRICSBonds(m))
>>> sorted(res)
[((1, 2), ('7', '7'))]
make sure we don't match ring bonds:
>>> m = Chem.MolFromSmiles('O=C1NCCC1')
>>> list(FindrBRICSBonds(m))
[]
another nice one, make sure environment 8 doesn't match something connected
to a ring atom:
>>> m = Chem.MolFromSmiles('CC1(C)CCCCC1')
>>> list(FindrBRICSBonds(m))
[]
"""
letter = re.compile('[a-z,A-Z]')
indices = range(len(bondMatchers))
bondsDone=set()
if randomizeOrder: random.shuffle(indices)
envMatches={}
for env,patt in environMatchers.items(): #Leili
envMatches[env]=mol.HasSubstructMatch(patt)
for gpIdx in indices:
if randomizeOrder:
compats =bondMatchers[gpIdx][:]
random.shuffle(compats)
else:
compats = bondMatchers[gpIdx]
for i1,i2,bType,patt in compats:
if not envMatches['L'+i1] or not envMatches['L'+i2]: continue
matches = mol.GetSubstructMatches(patt)
i1 = letter.sub('',i1)
i2 = letter.sub('',i2)
for match in matches:
if match not in bondsDone and (match[1],match[0]) not in bondsDone:
bondsDone.add(match)
yield(((match[0],match[1]),(i1,i2)))
#Leili
def FindreBRICSBonds(mol,randomizeOrder=False,silent=True):
letter = re.compile('[a-z,A-Z]')
indices = range(len(bondMatchers_r))
bondsDone=set()
if randomizeOrder: random.shuffle(indices)
envMatches={}
for env,patt in environMatchers_r.items(): #Leili
envMatches[env]=mol.HasSubstructMatch(patt)
for gpIdx in indices:
if randomizeOrder:
compats =bondMatchers_r[gpIdx][:]
random.shuffle(compats)
else:
compats = bondMatchers_r[gpIdx]
for i1,i2,bType,patt in compats:
if not envMatches['L'+i1] or not envMatches['L'+i2]: continue
matches = mol.GetSubstructMatches(patt)
i1 = letter.sub('',i1)
i2 = letter.sub('',i2)
for match in matches:
if match not in bondsDone and (match[1],match[0]) not in bondsDone:
bondsDone.add(match)
yield(((match[0],match[1]),(i1,i2)))
def BreakrBRICSBonds(mol,bonds=None,sanitize=True,silent=True):
""" breaks the BRICS bonds in a molecule and returns the results
>>> from rdkit import Chem
>>> m = Chem.MolFromSmiles('CCCOCC')
>>> m2=BreakrBRICSBonds(m)
>>> Chem.MolToSmiles(m2,True)
'[3*]O[3*].[4*]CC.[4*]CCC'
a more complicated case:
>>> m = Chem.MolFromSmiles('CCCOCCC(=O)c1ccccc1')
>>> m2=BreakrBRICSBonds(m)
>>> Chem.MolToSmiles(m2,True)
'[3*]O[3*].[4*]CCC.[4*]CCC([6*])=O.[16*]c1ccccc1'
can also specify a limited set of bonds to work with:
>>> m = Chem.MolFromSmiles('CCCOCC')
>>> m2 = BreakrBRICSBonds(m,[((3, 2), ('3', '4'))])
>>> Chem.MolToSmiles(m2,True)
'[3*]OCC.[4*]CCC'
this can be used as an alternate approach for doing a BRICS decomposition by
following BreakrBRICSBonds with a call to Chem.GetMolFrags:
>>> m = Chem.MolFromSmiles('CCCOCC')
>>> m2=BreakrBRICSBonds(m)
>>> frags = Chem.GetMolFrags(m2,asMols=True)
>>> [Chem.MolToSmiles(x,True) for x in frags]
['[4*]CCC', '[3*]O[3*]', '[4*]CC']
"""
if not bonds:
#bonds = FindrBRICSBonds(mol)
res = Chem.FragmentOnBRICSBonds(mol)
if sanitize:
Chem.SanitizeMol(res)
return res
eMol = Chem.EditableMol(mol)
nAts = mol.GetNumAtoms()
dummyPositions=[]
for indices,dummyTypes in bonds:
ia,ib = indices
obond = mol.GetBondBetweenAtoms(ia,ib)
bondType=obond.GetBondType()
eMol.RemoveBond(ia,ib)
da,db = dummyTypes
atoma = Chem.Atom(0)
atoma.SetIsotope(int(da))
atoma.SetNoImplicit(True)
idxa = nAts
nAts+=1
eMol.AddAtom(atoma)
eMol.AddBond(ia,idxa,bondType)
atomb = Chem.Atom(0)
atomb.SetIsotope(int(db))
atomb.SetNoImplicit(True)
idxb = nAts
nAts+=1
eMol.AddAtom(atomb)
eMol.AddBond(ib,idxb,bondType)
if mol.GetNumConformers():
dummyPositions.append((idxa,ib))
dummyPositions.append((idxb,ia))
res = eMol.GetMol()
if sanitize:
Chem.SanitizeMol(res)
if mol.GetNumConformers():
for conf in mol.GetConformers():
resConf = res.GetConformer(conf.GetId())
for ia,pa in dummyPositions:
resConf.SetAtomPosition(ia,conf.GetAtomPosition(pa))
return res
def rBRICSDecompose(mol,allNodes=None,minFragmentSize=1,onlyUseReactions=None,
silent=True,keepNonLeafNodes=False,singlePass=False,returnMols=False):
""" returns the BRICS decomposition for a molecule
>>> from rdkit import Chem
>>> m = Chem.MolFromSmiles('CCCOCc1cc(c2ncccc2)ccc1')
>>> res = list(rBRICSDecompose(m))
>>> sorted(res)
['[14*]c1ccccn1', '[16*]c1cccc([16*])c1', '[3*]O[3*]', '[4*]CCC', '[4*]C[8*]']
>>> res = rBRICSDecompose(m,returnMols=True)
>>> res[0]
<rdkit.Chem.rdchem.Mol object ...>
>>> smis = [Chem.MolToSmiles(x,True) for x in res]
>>> sorted(smis)
['[14*]c1ccccn1', '[16*]c1cccc([16*])c1', '[3*]O[3*]', '[4*]CCC', '[4*]C[8*]']
nexavar, an example from the paper (corrected):
>>> m = Chem.MolFromSmiles('CNC(=O)C1=NC=CC(OC2=CC=C(NC(=O)NC3=CC(=C(Cl)C=C3)C(F)(F)F)C=C2)=C1')
>>> res = list(rBRICSDecompose(m))
>>> sorted(res)
['[1*]C([1*])=O', '[1*]C([6*])=O', '[14*]c1cc([16*])ccn1', '[16*]c1ccc(Cl)c([16*])c1', '[16*]c1ccc([16*])cc1', '[3*]O[3*]', '[5*]NC', '[5*]N[5*]', '[8*]C(F)(F)F']
it's also possible to keep pieces that haven't been fully decomposed:
>>> m = Chem.MolFromSmiles('CCCOCC')
>>> res = list(rBRICSDecompose(m,keepNonLeafNodes=True))
>>> sorted(res)
['CCCOCC', '[3*]OCC', '[3*]OCCC', '[3*]O[3*]', '[4*]CC', '[4*]CCC']
>>> m = Chem.MolFromSmiles('CCCOCc1cc(c2ncccc2)ccc1')
>>> res = list(rBRICSDecompose(m,keepNonLeafNodes=True))
>>> sorted(res)
['CCCOCc1cccc(-c2ccccn2)c1', '[14*]c1ccccn1', '[16*]c1cccc(-c2ccccn2)c1', '[16*]c1cccc(COCCC)c1', '[16*]c1cccc([16*])c1', '[3*]OCCC', '[3*]OC[8*]', '[3*]OCc1cccc(-c2ccccn2)c1', '[3*]OCc1cccc([16*])c1', '[3*]O[3*]', '[4*]CCC', '[4*]C[8*]', '[4*]Cc1cccc(-c2ccccn2)c1', '[4*]Cc1cccc([16*])c1', '[8*]COCCC']
or to only do a single pass of decomposition:
>>> m = Chem.MolFromSmiles('CCCOCc1cc(c2ncccc2)ccc1')
>>> res = list(rBRICSDecompose(m,singlePass=True))
>>> sorted(res)
['CCCOCc1cccc(-c2ccccn2)c1', '[14*]c1ccccn1', '[16*]c1cccc(-c2ccccn2)c1', '[16*]c1cccc(COCCC)c1', '[3*]OCCC', '[3*]OCc1cccc(-c2ccccn2)c1', '[4*]CCC', '[4*]Cc1cccc(-c2ccccn2)c1', '[8*]COCCC']
setting a minimum size for the fragments:
>>> m = Chem.MolFromSmiles('CCCOCC')
>>> res = list(rBRICSDecompose(m,keepNonLeafNodes=True,minFragmentSize=2))
>>> sorted(res)
['CCCOCC', '[3*]OCC', '[3*]OCCC', '[4*]CC', '[4*]CCC']
>>> m = Chem.MolFromSmiles('CCCOCC')
>>> res = list(rBRICSDecompose(m,keepNonLeafNodes=True,minFragmentSize=3))
>>> sorted(res)
['CCCOCC', '[3*]OCC', '[4*]CCC']
>>> res = list(rBRICSDecompose(m,minFragmentSize=2))
>>> sorted(res)
['[3*]OCC', '[3*]OCCC', '[4*]CC', '[4*]CCC']
"""
global reactions
mSmi = Chem.MolToSmiles(mol,1)
if allNodes is None:
allNodes=set()
if mSmi in allNodes:
return set()
activePool={mSmi:mol}
allNodes.add(mSmi)
foundMols={mSmi:mol}
for gpIdx,reactionGp in enumerate(reactions):
newPool = {}
while activePool:
matched=False
nSmi = list(activePool.keys())[0]
mol = activePool.pop(nSmi)
for rxnIdx,reaction in enumerate(reactionGp):
if onlyUseReactions and (gpIdx,rxnIdx) not in onlyUseReactions:
continue
if not silent:
print('--------')
print(smartsGps[gpIdx][rxnIdx])
ps = reaction.RunReactants((mol,))
if ps:
if not silent: print(nSmi,'->',len(ps),'products')
for prodSeq in ps:
seqOk=True
# we want to disqualify small fragments, so sort the product sequence by size
prodSeq = [(prod.GetNumAtoms(onlyExplicit=True),prod) for prod in prodSeq]
print(prodSeq)
prodSeq.sort(key=lambda y: y[0])
for nats,prod in prodSeq:
try:
Chem.SanitizeMol(prod)
except:
continue
pSmi = Chem.MolToSmiles(prod,1)
if minFragmentSize>0:
nDummies = pSmi.count('*')
if nats-nDummies<minFragmentSize:
seqOk=False
break
prod.pSmi = pSmi
if seqOk:
matched=True
for nats,prod in prodSeq:
pSmi = prod.pSmi
#print '\t',nats,pSmi
if pSmi not in allNodes:
if not singlePass:
activePool[pSmi] = prod
allNodes.add(pSmi)
foundMols[pSmi]=prod
if singlePass or keepNonLeafNodes or not matched:
newPool[nSmi]=mol
activePool = newPool
if not (singlePass or keepNonLeafNodes):
if not returnMols:
res = set(activePool.keys())
else:
res = activePool.values()
else:
if not returnMols:
res = allNodes
else:
res = foundMols.values()
return res
import random
dummyPattern=Chem.MolFromSmiles('[*]')
def BRICSBuild(fragments,onlyCompleteMols=True,seeds=None,uniquify=True,
scrambleReagents=True,maxDepth=3):
seen = set()
if not seeds:
seeds = list(fragments)
if scrambleReagents:
seeds = list(seeds)
random.shuffle(seeds)
if scrambleReagents:
tempReactions = list(reverseReactions)
random.shuffle(tempReactions)
else:
tempReactions=reverseReactions
for seed in seeds:
seedIsR1=False
seedIsR2=False
nextSteps=[]
for rxn in tempReactions:
if seed.HasSubstructMatch(rxn._matchers[0]):
seedIsR1=True
if seed.HasSubstructMatch(rxn._matchers[1]):
seedIsR2=True
for fragment in fragments:
ps = None
if fragment.HasSubstructMatch(rxn._matchers[0]):
if seedIsR2:
ps = rxn.RunReactants((fragment,seed))
if fragment.HasSubstructMatch(rxn._matchers[1]):
if seedIsR1:
ps = rxn.RunReactants((seed,fragment))
if ps:
for p in ps:
if uniquify:
pSmi =Chem.MolToSmiles(p[0],True)
if pSmi in seen:
continue
else:
seen.add(pSmi)
if p[0].HasSubstructMatch(dummyPattern):
nextSteps.append(p[0])
if not onlyCompleteMols:
yield p[0]
else:
yield p[0]
if nextSteps and maxDepth>0:
for p in BRICSBuild(fragments,onlyCompleteMols=onlyCompleteMols,
seeds=nextSteps,uniquify=uniquify,
maxDepth=maxDepth-1):
if uniquify:
pSmi =Chem.MolToSmiles(p,True)
if pSmi in seen:
continue
else:
seen.add(pSmi)
yield p
#
#Leili
#Iteratively breaks all aliphatic chains using linkage L20-L21, just in case chain is too long and L20-L23 aren't enough
#Not the most efficient code yet
def reBRICS(fragments):
oldfragments=fragments
breakable=[1]*len(oldfragments)
breakout=sum(breakable)
iteri=0
while breakout>0:
iii=0
newfrags=()
newbreakable=[]
for frag in oldfragments:
heavy=frag.GetNumHeavyAtoms()
if heavy > 5:
if len(frag.GetSubstructMatch(Chem.MolFromSmiles("CCCCCC"))) > 0:
secondbonds=FindreBRICSBonds(frag) #only breaks aliphatic chains
secondpieces=BreakrBRICSBonds(frag,secondbonds)
secondfrags=Chem.GetMolFrags(secondpieces,asMols=True)
if len(secondfrags) > 1:
newfrags=newfrags+secondfrags
newbreakable=newbreakable+[1]*len(secondfrags)
else:
newfrags=newfrags+secondfrags
newbreakable=newbreakable.append(0) #1 frag->1 frag, no longer breakable, criteria #1
else:
newfrags=newfrags+(frag,)
newbreakable.append(0) #frag doesn't contain CCCCCC chain, no longer breakable, criteria #2
else:
newfrags=newfrags+(frag,)
newbreakable.append(0) #frag contains less than 5 heavy atoms, no longer breakable, criteria #3
iii+=1
oldfragments=newfrags
breakable=newbreakable
breakout=sum(newbreakable)
iteri+=1
if iteri > 100: #more than 100 iterations, criteria #4
print("Too many iterations. Not trying to break a polymer, are we? :)")
breakout = 0
return newfrags
# ------- ------- ------- ------- ------- ------- ------- -------
# Begin testing code
#------------------------------------
#
# doctest boilerplate
#
def _test():
import doctest,sys
return doctest.testmod(sys.modules["__main__"],
optionflags=doctest.ELLIPSIS+doctest.NORMALIZE_WHITESPACE)
if __name__=='__main__':
import unittest
class TestCase(unittest.TestCase):
def test1(self):
m = Chem.MolFromSmiles('CC(=O)OC')
res = rBRICSDecompose(m)
self.failUnless(res)
self.failUnless(len(res)==2)
m = Chem.MolFromSmiles('CC(=O)N1CCC1=O')
res = rBRICSDecompose(m)
self.failUnless(res)
self.failUnless(len(res)==2,res)
m = Chem.MolFromSmiles('c1ccccc1N(C)C')
res = rBRICSDecompose(m)
self.failUnless(res)
self.failUnless(len(res)==2,res)
m = Chem.MolFromSmiles('c1cccnc1N(C)C')
res = rBRICSDecompose(m)
self.failUnless(res)
self.failUnless(len(res)==2,res)
m = Chem.MolFromSmiles('o1ccnc1N(C)C')
res = rBRICSDecompose(m)
self.failUnless(res)
self.failUnless(len(res)==2)
m = Chem.MolFromSmiles('c1ccccc1OC')
res = rBRICSDecompose(m)
self.failUnless(res)
self.failUnless(len(res)==2)
m = Chem.MolFromSmiles('o1ccnc1OC')
res = rBRICSDecompose(m)
self.failUnless(res)
self.failUnless(len(res)==2)
m = Chem.MolFromSmiles('O1CCNC1OC')
res = rBRICSDecompose(m)
self.failUnless(res)
self.failUnless(len(res)==2)
m = Chem.MolFromSmiles('CCCSCC')
res = rBRICSDecompose(m)
self.failUnless(res)
self.failUnless(len(res)==3,res)
self.failUnless('[11*]S[11*]' in res,res)
m = Chem.MolFromSmiles('CCNC(=O)C1CC1')
res = rBRICSDecompose(m)
self.failUnless(res)
self.failUnless(len(res)==4,res)
self.failUnless('[5*]N[5*]' in res,res)
def test2(self):
# example from the paper, nexavar:
m = Chem.MolFromSmiles('CNC(=O)C1=NC=CC(OC2=CC=C(NC(=O)NC3=CC(=C(Cl)C=C3)C(F)(F)F)C=C2)=C1')
res = rBRICSDecompose(m)
self.failUnless(res)
self.failUnless(len(res)==9,res)
def test3(self):
m = Chem.MolFromSmiles('FC(F)(F)C1=C(Cl)C=CC(NC(=O)NC2=CC=CC=C2)=C1')
res = rBRICSDecompose(m)
self.failUnless(res)
self.failUnless(len(res)==5,res)
self.failUnless('[5*]N[5*]' in res,res)
self.failUnless('[16*]c1ccccc1' in res,res)
self.failUnless('[8*]C(F)(F)F' in res,res)
def test4(self):
allNodes = set()
m = Chem.MolFromSmiles('c1ccccc1OCCC')
res = rBRICSDecompose(m,allNodes=allNodes)
self.failUnless(res)
leaves=res
self.failUnless(len(leaves)==3,leaves)
self.failUnless(len(allNodes)==6,allNodes)
res = rBRICSDecompose(m,allNodes=allNodes)
self.failIf(res)
self.failUnless(len(allNodes)==6,allNodes)
m = Chem.MolFromSmiles('c1ccccc1OCCCC')
res = rBRICSDecompose(m,allNodes=allNodes)
self.failUnless(res)
leaves.update(res)
self.failUnless(len(allNodes)==9,allNodes)
self.failUnless(len(leaves)==4,leaves)
m = Chem.MolFromSmiles('c1cc(C(=O)NCC)ccc1OCCC')
res = rBRICSDecompose(m,allNodes=allNodes)
self.failUnless(res)
leaves.update(res)
self.failUnless(len(leaves)==8,leaves)
self.failUnless(len(allNodes)==18,allNodes)
def test5(self):
allNodes = set()
frags = [
'[14*]c1ncncn1',
'[16*]c1ccccc1',
'[14*]c1ncccc1',
]
frags = [Chem.MolFromSmiles(x) for x in frags]
res = BRICSBuild(frags)
self.failUnless(res)
res= list(res)
self.failUnless(len(res)==6)
smis = [Chem.MolToSmiles(x,True) for x in res]
self.failUnless('c1ccc(-c2ccccc2)cc1' in smis)
self.failUnless('c1ccc(-c2ccccn2)cc1' in smis)
def test5a(self):
allNodes = set()
frags = [
'[3*]O[3*]',
'[16*]c1ccccc1',
]
frags = [Chem.MolFromSmiles(x) for x in frags]
res = BRICSBuild(frags)
self.failUnless(res)
res=list(res)
smis = [Chem.MolToSmiles(x,True) for x in res]
self.failUnless(len(smis)==2,smis)
self.failUnless('c1ccc(Oc2ccccc2)cc1' in smis)
self.failUnless('c1ccc(-c2ccccc2)cc1' in smis)
def test6(self):
allNodes = set()
frags = [
'[16*]c1ccccc1',
'[3*]OC',
'[9*]n1cccc1',
]
frags = [Chem.MolFromSmiles(x) for x in frags]
res = BRICSBuild(frags)
self.failUnless(res)
res= list(res)
self.failUnless(len(res)==3)
smis = [Chem.MolToSmiles(x,True) for x in res]
self.failUnless('c1ccc(-c2ccccc2)cc1' in smis)
self.failUnless('COc1ccccc1' in smis)
self.failUnless('c1ccn(-c2ccccc2)c1' in smis)
def test7(self):
allNodes = set()
frags = [
'[16*]c1ccccc1',
'[3*]OC',
'[3*]OCC(=O)[6*]',
]
frags = [Chem.MolFromSmiles(x) for x in frags]
res = BRICSBuild(frags)
self.failUnless(res)
res= list(res)
smis = [Chem.MolToSmiles(x,True) for x in res]
self.failUnless(len(res)==3)
self.failUnless('c1ccc(-c2ccccc2)cc1' in smis)
self.failUnless('COc1ccccc1' in smis)
self.failUnless('O=C(COc1ccccc1)c1ccccc1' in smis)
def test8(self):
random.seed(23)
base = Chem.MolFromSmiles("n1cncnc1OCC(C1CC1)OC1CNC1")
catalog = rBRICSDecompose(base)
self.failUnless(len(catalog)==5,catalog)
catalog = [Chem.MolFromSmiles(x) for x in catalog]
ms = [Chem.MolToSmiles(x) for x in BRICSBuild(catalog,maxDepth=4)]
self.failUnless(len(ms)==36,ms)