-
Notifications
You must be signed in to change notification settings - Fork 1
/
featurizer.py
1046 lines (935 loc) · 31.9 KB
/
featurizer.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
#! /usr/bin/env python
# -*- coding: utf-8
"""Molecule featurization classes.
Adapted from: https://github.com/BenevolentAI/MolBERT/blob/main/molbert/utils/featurizer/molfeaturizer.py
"""
import logging
import os
from abc import ABC, abstractmethod
from typing import Any, Dict, List, Optional, Sequence, Tuple, Union
import numpy as np
import scipy.stats as st
import torch
from rdkit import Chem
from rdkit.Chem import Descriptors
from rdkit.Chem.rdMolDescriptors import GetMorganFingerprint
from torch_geometric.loader import DataLoader
from dataset import AttFPDataset, PropertyScaler
from model import AttentiveFP, AttentiveFP2
from utils import get_input_dims, read_config_ini
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
logger = logging.getLogger(__name__)
class MolFeaturizer(ABC):
"""
Interface for the featurization of molecules, given as SMILES strings, to some vectorized representation.
"""
def __call__(self, molecules: Sequence[str]) -> Tuple[np.ndarray, np.ndarray]:
return self.transform(molecules)
def transform(self, molecules: Sequence[str]) -> Tuple[np.ndarray, np.ndarray]:
"""
Featurizes a sequence of molecules.
Args:
molecules: molecules, given as a sequence of SMILES strings
Returns:
Tuple: 2D array for the feature vectors, 1D array for the validity masks
"""
single_results = [self.transform_single(m) for m in molecules]
features_list, mask_list = zip(*single_results)
return np.vstack(features_list), np.hstack(mask_list)
@abstractmethod
def transform_single(self, molecule: str) -> Tuple[np.ndarray, bool]:
"""
Featurizes one molecule.
Args:
molecule: molecule, given as a SMILES string
Returns:
Tuple: feature vector (1D array), boolean for successful featurization
"""
def invalid_mol_features(self) -> np.ndarray:
"""
Features to return for invalid molecules.
"""
return np.zeros(self.output_size)
@property
@abstractmethod
def output_size(self) -> int:
"""
Get the dimension after featurization
"""
def is_valid(self, molecules: Sequence[str]) -> Sequence[bool]:
return np.array([self.is_valid_single(mol) for mol in molecules])
def is_valid_single(self, molecule: str) -> bool:
mol = Chem.MolFromSmiles(molecule, True, {})
if mol is None or len(molecule) == 0:
return False
return True
class RDKitFeaturizer(MolFeaturizer, ABC):
"""
Base class for MolFeaturizers relying on RDKit.Mols during featurization
"""
def __init__(self, sanitize: bool = True, replacements: Optional[dict] = None):
"""
Args:
sanitize: toggles sanitization of the molecule.
replacements: a dictionary of replacement strings. Defaults to {}
(@see http://www.rdkit.org/Python_Docs/rdkit.Chem.rdmolfiles-module.html#MolFromSmiles)
"""
if replacements is None:
replacements = {}
self.sanitize = sanitize
self.replacements = replacements
def transform_single(self, molecule: str) -> Tuple[np.ndarray, bool]:
mol = Chem.MolFromSmiles(molecule, self.sanitize, self.replacements)
if mol is None or len(molecule) == 0:
return self.invalid_mol_features(), False
return self.transform_mol(mol)
@abstractmethod
def transform_mol(self, molecule: Chem.rdchem.Mol) -> Tuple[np.ndarray, bool]:
"""
Featurizes one molecule given as a RDKit.Mol
"""
class PhysChemFeaturizer(RDKitFeaturizer):
"""
MolFeaturizer that featurizes a molecule with an array of phys-chem properties.
@see http://www.rdkit.org/Python_Docs/rdkit.ML.Descriptors.MoleculeDescriptors-module.html
For available descriptors @see http://rdkit.org/docs/source/rdkit.ML.Descriptors.MoleculeDescriptors.html
"""
def __init__(
self,
descriptors: List[str] = [],
named_descriptor_set: str = "all",
fingerprint_extra_args: Optional[dict] = None,
normalise: bool = False,
subset_size: int = 200,
):
"""
Args:
descriptors: list of descriptor names -
the subset given is validated to make sure they exist and will be used.
named_descriptor_set: 'all' or 'simple' to use preset subsets
fingerprint_extra_args: optional kwargs for `MolecularDescriptorCalculator`
subset_size: number of descriptors to return (or the size of the subset if that's smaller)
"""
super().__init__()
if fingerprint_extra_args is None:
fingerprint_extra_args = {}
self.descriptors = self._get_descriptor_list(
named_descriptor_set=named_descriptor_set, descriptor_list=descriptors, subset_size=subset_size
)
self.fingerprint_extra_args = fingerprint_extra_args
self.scaler = PropertyScaler(descriptors=self.descriptors, do_scale=normalise)
@staticmethod
def get_descriptor_subset(subset: str, subset_size: int) -> List[str]:
if subset == "all":
return PhysChemFeaturizer.get_all_descriptor_names()[:subset_size]
elif subset == "simple":
return PhysChemFeaturizer.get_simple_descriptor_subset()[:subset_size]
elif subset == "uncorrelated":
return PhysChemFeaturizer.get_uncorrelated_descriptor_subset(subset_size)
elif subset == "fragment":
return PhysChemFeaturizer.get_fragment_descriptor_subset()[:subset_size]
elif subset == "graph":
return PhysChemFeaturizer.get_graph_descriptor_subset()[:subset_size]
elif subset == "surface":
return PhysChemFeaturizer.get_surface_descriptor_subset()[:subset_size]
elif subset == "druglikeness":
return PhysChemFeaturizer.get_druglikeness_descriptor_subset()[:subset_size]
elif subset == "logp":
return PhysChemFeaturizer.get_logp_descriptor_subset()[:subset_size]
elif subset == "refractivity":
return PhysChemFeaturizer.get_refractivity_descriptor_subset()[:subset_size]
elif subset == "estate":
return PhysChemFeaturizer.get_estate_descriptor_subset()[:subset_size]
elif subset == "charge":
return PhysChemFeaturizer.get_charge_descriptor_subset()[:subset_size]
elif subset == "general":
return PhysChemFeaturizer.get_general_descriptor_subset()[:subset_size]
else:
raise ValueError(
f'Unrecognised descriptor subset: {subset} (should be "all", "simple",'
f'"uncorrelated", "fragment", "graph", "logp", "refractivity",'
f'"estate", "druglikeness", "surface", "charge", "general").'
)
@property
def output_size(self):
return len(self.descriptors)
def transform(self, molecules: Sequence[str]) -> Tuple[np.ndarray, np.ndarray]:
features, valids = super().transform(molecules)
return features, valids
def transform_single(self, molecule: str) -> Tuple[np.ndarray, bool]:
features, valid = super().transform_single(molecule)
return features, valid
def transform_mol(self, molecule: Chem.rdchem.Mol) -> Tuple[np.ndarray, bool]:
fp = self.scaler.transform(molecule)
return fp, True
def is_valid_single(self, molecule: str) -> bool:
_, valid = self.transform_single(molecule)
return valid
# control pickling / unpickling
def __getstate__(self):
return {
"descriptors": self.descriptors,
"fingerprint_extra_args": self.fingerprint_extra_args,
"normalise": self.normalise,
}
def __setstate__(self, saved_dict):
# ignore mypy check: calling __init__ directly as a form of reflection during unpickling
self.__init__( # type: ignore
descriptors=saved_dict["descriptors"],
fingerprint_extra_args=saved_dict["fingerprint_extra_args"],
normalise=saved_dict["normalise"],
)
@staticmethod
def get_all_descriptor_names() -> List[str]:
"""
Get available descriptor names for RDKit physchem features. Custom subset can be used as list of descriptors.
"""
return sorted([x[0] for x in Descriptors._descList])
@staticmethod
def get_simple_descriptor_subset() -> List[str]:
return [
"FpDensityMorgan2",
"FractionCSP3",
"MolLogP",
"MolWt",
"NumHAcceptors",
"NumHDonors",
"NumRotatableBonds",
"TPSA",
]
@staticmethod
def get_refractivity_descriptor_subset() -> List[str]:
return [
"MolMR",
"SMR_VSA1",
"SMR_VSA10",
"SMR_VSA2",
"SMR_VSA3",
"SMR_VSA4",
"SMR_VSA5",
"SMR_VSA6",
"SMR_VSA7",
"SMR_VSA8",
"SMR_VSA9",
]
@staticmethod
def get_logp_descriptor_subset() -> List[str]:
"""LogP descriptors and VSA/LogP descriptors
SlogP_VSA: VSA of atoms contributing to a specified bin of SlogP
"""
return [
"MolLogP",
"SlogP_VSA1",
"SlogP_VSA10",
"SlogP_VSA11",
"SlogP_VSA12",
"SlogP_VSA2",
"SlogP_VSA3",
"SlogP_VSA4",
"SlogP_VSA5",
"SlogP_VSA6",
"SlogP_VSA7",
"SlogP_VSA8",
"SlogP_VSA9",
]
@staticmethod
def get_graph_descriptor_subset() -> List[str]:
"""Graph descriptors (https://www.rdkit.org/docs/source/rdkit.Chem.GraphDescriptors.html)"""
return [
"BalabanJ",
"BertzCT",
"Chi0",
"Chi0n",
"Chi0v",
"Chi1",
"Chi1n",
"Chi1v",
"Chi2n",
"Chi2v",
"Chi3n",
"Chi3v",
"Chi4n",
"Chi4v",
"HallKierAlpha",
"Ipc",
"Kappa1",
"Kappa2",
"Kappa3",
]
@staticmethod
def get_surface_descriptor_subset() -> List[str]:
"""MOE-like surface descriptors
EState_VSA: VSA (van der Waals surface area) of atoms contributing to a specified bin of e-state
SlogP_VSA: VSA of atoms contributing to a specified bin of SlogP
SMR_VSA: VSA of atoms contributing to a specified bin of molar refractivity
PEOE_VSA: VSA of atoms contributing to a specified bin of partial charge (Gasteiger)
LabuteASA: Labute's approximate surface area descriptor
"""
return [
"SlogP_VSA1",
"SlogP_VSA10",
"SlogP_VSA11",
"SlogP_VSA12",
"SlogP_VSA2",
"SlogP_VSA3",
"SlogP_VSA4",
"SlogP_VSA5",
"SlogP_VSA6",
"SlogP_VSA7",
"SlogP_VSA8",
"SlogP_VSA9",
"SMR_VSA1",
"SMR_VSA10",
"SMR_VSA2",
"SMR_VSA3",
"SMR_VSA4",
"SMR_VSA5",
"SMR_VSA6",
"SMR_VSA7",
"SMR_VSA8",
"SMR_VSA9",
"EState_VSA1",
"EState_VSA10",
"EState_VSA11",
"EState_VSA2",
"EState_VSA3",
"EState_VSA4",
"EState_VSA5",
"EState_VSA6",
"EState_VSA7",
"EState_VSA8",
"EState_VSA9",
"LabuteASA",
"PEOE_VSA1",
"PEOE_VSA10",
"PEOE_VSA11",
"PEOE_VSA12",
"PEOE_VSA13",
"PEOE_VSA14",
"PEOE_VSA2",
"PEOE_VSA3",
"PEOE_VSA4",
"PEOE_VSA5",
"PEOE_VSA6",
"PEOE_VSA7",
"PEOE_VSA8",
"PEOE_VSA9",
"TPSA",
]
@staticmethod
def get_druglikeness_descriptor_subset() -> List[str]:
"""Descriptors commonly used to assess druglikeness"""
return [
"TPSA",
"MolLogP",
"MolMR",
"ExactMolWt",
"FractionCSP3",
"HeavyAtomCount",
"MolWt",
"NHOHCount",
"NOCount",
"NumAliphaticCarbocycles",
"NumAliphaticHeterocycles",
"NumAliphaticRings",
"NumAromaticCarbocycles",
"NumAromaticHeterocycles",
"NumAromaticRings",
"NumHAcceptors",
"NumHDonors",
"NumHeteroatoms",
"NumRotatableBonds",
"NumSaturatedCarbocycles",
"NumSaturatedHeterocycles",
"NumSaturatedRings",
"RingCount",
"qed",
]
@staticmethod
def get_fragment_descriptor_subset() -> List[str]:
return [
"NHOHCount",
"NOCount",
"NumAliphaticCarbocycles",
"NumAliphaticHeterocycles",
"NumAliphaticRings",
"NumAromaticCarbocycles",
"NumAromaticHeterocycles",
"NumAromaticRings",
"NumHAcceptors",
"NumHDonors",
"NumHeteroatoms",
"NumRotatableBonds",
"NumSaturatedCarbocycles",
"NumSaturatedHeterocycles",
"NumSaturatedRings",
"RingCount",
"fr_Al_COO",
"fr_Al_OH",
"fr_Al_OH_noTert",
"fr_ArN",
"fr_Ar_COO",
"fr_Ar_N",
"fr_Ar_NH",
"fr_Ar_OH",
"fr_COO",
"fr_COO2",
"fr_C_O",
"fr_C_O_noCOO",
"fr_C_S",
"fr_HOCCN",
"fr_Imine",
"fr_NH0",
"fr_NH1",
"fr_NH2",
"fr_N_O",
"fr_Ndealkylation1",
"fr_Ndealkylation2",
"fr_Nhpyrrole",
"fr_SH",
"fr_aldehyde",
"fr_alkyl_carbamate",
"fr_alkyl_halide",
"fr_allylic_oxid",
"fr_amide",
"fr_amidine",
"fr_aniline",
"fr_aryl_methyl",
"fr_azide",
"fr_azo",
"fr_barbitur",
"fr_benzene",
"fr_benzodiazepine",
"fr_bicyclic",
"fr_diazo",
"fr_dihydropyridine",
"fr_epoxide",
"fr_ester",
"fr_ether",
"fr_furan",
"fr_guanido",
"fr_halogen",
"fr_hdrzine",
"fr_hdrzone",
"fr_imidazole",
"fr_imide",
"fr_isocyan",
"fr_isothiocyan",
"fr_ketone",
"fr_ketone_Topliss",
"fr_lactam",
"fr_lactone",
"fr_methoxy",
"fr_morpholine",
"fr_nitrile",
"fr_nitro",
"fr_nitro_arom",
"fr_nitro_arom_nonortho",
"fr_nitroso",
"fr_oxazole",
"fr_oxime",
"fr_para_hydroxylation",
"fr_phenol",
"fr_phenol_noOrthoHbond",
"fr_phos_acid",
"fr_phos_ester",
"fr_piperdine",
"fr_piperzine",
"fr_priamide",
"fr_prisulfonamd",
"fr_pyridine",
"fr_quatN",
"fr_sulfide",
"fr_sulfonamd",
"fr_sulfone",
"fr_term_acetylene",
"fr_tetrazole",
"fr_thiazole",
"fr_thiocyan",
"fr_thiophene",
"fr_unbrch_alkane",
"fr_urea",
]
@staticmethod
def get_estate_descriptor_subset() -> List[str]:
"""Electrotopological state (e-state) and VSA/e-state descriptors
EState_VSA: VSA (van der Waals surface area) of atoms contributing to a specified bin of e-state
VSA_EState: e-state values of atoms contributing to a specific bin of VSA
"""
return [
"EState_VSA1",
"EState_VSA10",
"EState_VSA11",
"EState_VSA2",
"EState_VSA3",
"EState_VSA4",
"EState_VSA5",
"EState_VSA6",
"EState_VSA7",
"EState_VSA8",
"EState_VSA9",
"VSA_EState1",
"VSA_EState10",
"VSA_EState2",
"VSA_EState3",
"VSA_EState4",
"VSA_EState5",
"VSA_EState6",
"VSA_EState7",
"VSA_EState8",
"VSA_EState9",
"MaxAbsEStateIndex",
"MaxEStateIndex",
"MinAbsEStateIndex",
"MinEStateIndex",
]
@staticmethod
def get_charge_descriptor_subset() -> List[str]:
"""
Partial charge and VSA/charge descriptors
PEOE: Partial Equalization of Orbital Electronegativities (Gasteiger partial atomic charges)
PEOE_VSA: VSA of atoms contributing to a specific bin of partial charge
"""
return [
"PEOE_VSA1",
"PEOE_VSA10",
"PEOE_VSA11",
"PEOE_VSA12",
"PEOE_VSA13",
"PEOE_VSA14",
"PEOE_VSA2",
"PEOE_VSA3",
"PEOE_VSA4",
"PEOE_VSA5",
"PEOE_VSA6",
"PEOE_VSA7",
"PEOE_VSA8",
"PEOE_VSA9",
"MaxAbsPartialCharge",
"MaxPartialCharge",
"MinAbsPartialCharge",
"MinPartialCharge",
]
@staticmethod
def get_general_descriptor_subset() -> List[str]:
"""Descriptors from https://www.rdkit.org/docs/source/rdkit.Chem.Descriptors.html"""
return [
"MaxAbsPartialCharge",
"MaxPartialCharge",
"MinAbsPartialCharge",
"MinPartialCharge",
"ExactMolWt",
"MolWt",
"FpDensityMorgan1",
"FpDensityMorgan2",
"FpDensityMorgan3",
"HeavyAtomMolWt",
"NumRadicalElectrons",
"NumValenceElectrons",
]
@staticmethod
def get_uncorrelated_descriptor_subset(subset_size: int) -> List[str]:
"""
Column names are sorted starting with the non-informative descriptors, then the rest are ordered
from most correlated to least correlated. This will return the n least correlated descriptors.
Args:
subset_size: how many to return
Returns:
List of descriptors
"""
columns_sorted_by_correlation = [
"fr_sulfone",
"MinPartialCharge",
"fr_C_O_noCOO",
"fr_hdrzine",
"fr_Ndealkylation2",
"NumAromaticHeterocycles",
"fr_N_O",
"fr_piperdine",
"fr_HOCCN",
"fr_Nhpyrrole",
"NumHAcceptors",
"NumHeteroatoms",
"fr_C_O",
"VSA_EState5",
"fr_Al_OH",
"SlogP_VSA9",
"fr_benzodiazepine",
"VSA_EState6",
"fr_Ar_N",
"VSA_EState7",
"fr_COO2",
"VSA_EState3",
"fr_Imine",
"fr_sulfide",
"FractionCSP3",
"fr_imidazole",
"fr_azo",
"NumHDonors",
"fr_COO",
"fr_ether",
"fr_nitro",
"NumSaturatedHeterocycles",
"fr_lactam",
"fr_aniline",
"NumAliphaticCarbocycles",
"fr_para_hydroxylation",
"SMR_VSA2",
"MaxAbsPartialCharge",
"fr_thiocyan",
"NHOHCount",
"fr_ester",
"fr_aldehyde",
"SMR_VSA8",
"fr_halogen",
"fr_NH0",
"fr_furan",
"fr_tetrazole",
"HeavyAtomCount",
"NumRotatableBonds",
"NumSaturatedCarbocycles",
"fr_SH",
"fr_Ar_NH",
"SlogP_VSA7",
"fr_ketone",
"fr_alkyl_halide",
"fr_NH1",
"NumRadicalElectrons",
"MaxPartialCharge",
"fr_ArN",
"fr_imide",
"fr_priamide",
"fr_hdrzone",
"fr_azide",
"NumAromaticCarbocycles",
"NOCount",
"fr_isocyan",
"RingCount",
"fr_nitroso",
"EState_VSA11",
"MinAbsPartialCharge",
"fr_Ar_COO",
"fr_prisulfonamd",
"fr_sulfonamd",
"VSA_EState4",
"fr_quatN",
"fr_NH2",
"fr_epoxide",
"fr_allylic_oxid",
"fr_piperzine",
"VSA_EState1",
"NumAliphaticHeterocycles",
"fr_Ndealkylation1",
"fr_Al_OH_noTert",
"fr_aryl_methyl",
"NumAromaticRings",
"fr_bicyclic",
"fr_methoxy",
"fr_oxazole",
"fr_barbitur",
"NumAliphaticRings",
"fr_Ar_OH",
"fr_phos_ester",
"fr_thiophene",
"fr_nitrile",
"fr_dihydropyridine",
"VSA_EState2",
"fr_nitro_arom",
"SlogP_VSA11",
"fr_thiazole",
"fr_ketone_Topliss",
"fr_term_acetylene",
"fr_isothiocyan",
"fr_urea",
"fr_nitro_arom_nonortho",
"fr_lactone",
"fr_diazo",
"fr_amide",
"fr_alkyl_carbamate",
"fr_Al_COO",
"fr_amidine",
"fr_phos_acid",
"fr_oxime",
"fr_guanido",
"fr_C_S",
"NumSaturatedRings",
"fr_benzene",
"fr_phenol",
"fr_unbrch_alkane",
"fr_phenol_noOrthoHbond",
"fr_pyridine",
"fr_morpholine",
"MaxAbsEStateIndex",
"ExactMolWt",
"MolWt",
"Chi0",
"LabuteASA",
"Chi0n",
"NumValenceElectrons",
"Chi3n",
"Chi0v",
"Chi3v",
"Chi1",
"Chi1n",
"Chi1v",
"FpDensityMorgan2",
"HeavyAtomMolWt",
"Kappa1",
"SMR_VSA7",
"Chi2n",
"Chi2v",
"Kappa2",
"Chi4n",
"SMR_VSA5",
"MolMR",
"EState_VSA10",
"BertzCT",
"MinEStateIndex",
"SMR_VSA1",
"FpDensityMorgan1",
"VSA_EState10",
"SlogP_VSA2",
"SMR_VSA10",
"HallKierAlpha",
"VSA_EState9",
"TPSA",
"MaxEStateIndex",
"Chi4v",
"SMR_VSA4",
"MolLogP",
"qed",
"VSA_EState8",
"EState_VSA2",
"SMR_VSA6",
"PEOE_VSA1",
"EState_VSA1",
"SlogP_VSA8",
"SlogP_VSA6",
"SlogP_VSA5",
"SlogP_VSA10",
"BalabanJ",
"Kappa3",
"EState_VSA4",
"PEOE_VSA6",
"EState_VSA9",
"PEOE_VSA2",
"PEOE_VSA5",
"SMR_VSA3",
"SlogP_VSA3",
"EState_VSA7",
"EState_VSA3",
"PEOE_VSA7",
"SlogP_VSA1",
"SMR_VSA9",
"EState_VSA8",
"EState_VSA6",
"PEOE_VSA3",
"MinAbsEStateIndex",
"PEOE_VSA14",
"FpDensityMorgan3",
"PEOE_VSA12",
"SlogP_VSA4",
"PEOE_VSA9",
"PEOE_VSA13",
"PEOE_VSA10",
"PEOE_VSA8",
"EState_VSA5",
"SlogP_VSA12",
"PEOE_VSA4",
"Ipc",
"PEOE_VSA11",
]
return columns_sorted_by_correlation[-subset_size:]
@staticmethod
def _get_descriptor_list(
named_descriptor_set: str = "all", descriptor_list: List[str] = [], subset_size: int = 200
):
if len(descriptor_list) == 0:
descriptor_list = PhysChemFeaturizer.get_descriptor_subset(named_descriptor_set, subset_size)
else: # else use the named_descriptor_set given by the user
assert isinstance(descriptor_list, list)
all_descriptors = set(PhysChemFeaturizer.get_all_descriptor_names())
assert set(descriptor_list).issubset(all_descriptors)
descriptor_list.sort()
return descriptor_list
MetricDictType = Dict[str, Tuple[str, Sequence[float], float, float, float, float]]
class PhyschemScaler:
def __init__(self, descriptor_list: List[str], dists: MetricDictType):
self.descriptor_list = descriptor_list
self.dists = dists
self.cdfs = self.prepare_cdfs()
def prepare_cdfs(self):
cdfs = {}
dist_subset = dict(filter(lambda elem: elem[0] in self.descriptor_list, self.dists.items()))
for descriptor_name, (dist, params, minV, maxV, avg, std) in dist_subset.items():
arg = params[:-2] # type: ignore
loc = params[-2] # type: ignore
scale = params[-1] # type: ignore
dist = getattr(st, dist)
# make the cdf with the parameters
def cdf(v, dist=dist, arg=arg, loc=loc, scale=scale, minV=minV, maxV=maxV):
v = dist.cdf(np.clip(v, minV, maxV), loc=loc, scale=scale, *arg)
return np.clip(v, 0.0, 1.0)
cdfs[descriptor_name] = cdf
return cdfs
def transform(self, X):
# transform each column with the corresponding descriptor
transformed_list = [
self.cdfs[descriptor](X[:, idx])[..., np.newaxis] for idx, descriptor in enumerate(self.descriptor_list)
]
transformed = np.concatenate(transformed_list, axis=1)
# make sure the shape is intact
assert X.shape == transformed.shape
return transformed
def transform_single(self, X):
assert len(X.shape) == 1, "When using transform_single, input should have a 1-dimensional shape (e.g. (200,))"
X = X[np.newaxis, :]
transformed = self.transform(X)
transformed = transformed.squeeze(axis=0)
return transformed
class MorganFPFeaturizer(RDKitFeaturizer):
"""
MolFeaturizer generating the Morgan fingerprints.
@see http://rdkit.org/docs/source/rdkit.Chem.rdMolDescriptors.html#rdkit.Chem.rdMolDescriptors.GetMorganFingerprint
"""
def __init__(
self,
fp_size: int = 2048,
radius: int = 2,
use_counts: bool = False,
use_features: bool = False,
use_chirality=False,
fingerprint_extra_args: Optional[dict] = None,
):
"""
Args:
fp_size: fingerprint length to generate.
radius: fingerprint radius to generate.
use_counts: use counts in fingerprint.
use_features: use features in fingerprint.
fingerprint_extra_args: kwargs for `GetMorganFingerprint`
"""
super().__init__()
if fingerprint_extra_args is None:
fingerprint_extra_args = {}
self.fp_size = fp_size
self.radius = radius
self.use_features = use_features
self.use_counts = use_counts
self.use_chirality = use_chirality
self.fingerprint_extra_args = fingerprint_extra_args
def transform_mol(self, molecule: Chem.rdchem.Mol) -> Tuple[np.ndarray, bool]:
use_chirality = self.__dict__.get("use_chirality", False)
fp = GetMorganFingerprint(
molecule,
radius=self.radius,
useFeatures=self.use_features,
useCounts=self.use_counts,
useChirality=use_chirality,
**self.fingerprint_extra_args,
)
fp = rdkit_sparse_array_to_np(
fp.GetNonzeroElements().items(), use_counts=self.use_counts, fp_size=self.fp_size
)
return fp, True
@property
def output_size(self) -> int:
return self.fp_size
def rdkit_dense_array_to_np(dense_fp, dtype=np.int32):
"""
Converts RDKit ExplicitBitVect to 1D numpy array with specified dtype.
Args:
dense_fp (ExplicitBitVect or np.ndarray): fingerprint
dtype: dtype of the returned array
Returns:
Numpy matrix with shape (fp_len,)
"""
dense_fp = np.array(dense_fp, dtype=dtype)
if len(dense_fp.shape) == 1:
pass
elif len(dense_fp.shape) == 2 and dense_fp.shape[0] == 1:
dense_fp = np.squeeze(dense_fp, axis=0)
else:
raise ValueError("Input matrix should either have shape of (fp_size, ) or (1, fp_size).")
return np.array(dense_fp)
def rdkit_sparse_array_to_np(sparse_fp, use_counts, fp_size):
"""
Converts an rdkit int hashed fingerprint into a 1D numpy array.
Args:
sparse_fp (dict: int->float): sparse dict of values set
use_counts (bool): when folding up the hash, should it sum or not
fp_size (int): length of fingerprint
Returns:
Numpy array of fingerprint
"""
fp = np.zeros((fp_size,), np.int32)
for idx, v in sparse_fp:
if use_counts:
fp[idx % fp_size] += int(v)
else:
fp[idx % fp_size] = 1
return fp
class GiraffeFeaturizer:
"""
This featurizer takes a giraffe model and transforms the input data and
returns the representation of the last global attentiveFP graph layer.
"""
def __init__(
self,
checkpoint_path: str,
n_jobs: int = 4,
device: str = None,
) -> None:
"""
Args:
checkpoint_path: path or S3 location of trained model checkpoint
device: device for torch
"""
super().__init__()
self.checkpoint_path = checkpoint_path
self.model_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.dirname(checkpoint_path)))
self.device = device or "cuda" if torch.cuda.is_available() else "cpu"
self.dim_atom, self.dim_bond = get_input_dims()
self.n_jobs = n_jobs
# read config file
conf = read_config_ini(self.model_dir)