-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
50 lines (40 loc) · 1.55 KB
/
utils.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
from rdkit.Chem import MolFromSmiles, SaltRemover, MolToSmiles, AllChem, MACCSkeys, RDKFingerprint
from rdkit.Chem.Descriptors import CalcMolDescriptors
class Molecule:
def __init__(self, smiles: str):
if not smiles :
print("Empty smiles are given")
sys.exit()
self.smiles = smiles
self.mol = MolFromSmiles(smiles)
self.clean_smiles = None
def remove_salt(self):
salt_remover = SaltRemover.SaltRemover()
self.mol = salt_remover.StripMol(self.mol, dontRemoveEverything=True, sanitize=True)
self.clean_smiles = MolToSmiles(self.mol)
def descriptor_generator(self):
return CalcMolDescriptors(self.mol)
def fingerprint_generator(self, fp_type: str, n_bits: int=2048):
fingerprint_type = {
'Morgan': AllChem.GetMorganFingerprintAsBitVect,
'RDKit': RDKFingerprint,
'Atom': AllChem.GetHashedAtomPairFingerprintAsBitVect,
'MACCS': MACCSkeys.GenMACCSKeys,
'Topological': AllChem.GetHashedTopologicalTorsionFingerprintAsBitVect
}
if fp_type not in fingerprint_type:
print("Unknown Fingerprint")
sys.exit()
if n_bits < 1:
print("Number of bits can't be less than 1")
sys.exit()
fingerprint_function = fingerprint_type[fp_type]
if fp_type == 'Morgan':
fingerprint= fingerprint_function(self.mol, 2, nBits=n_bits)
elif fp_type == 'MACCS':
fingerprint = fingerprint_function(self.mol)
else:
fingerprint = fingerprint_function(self.mol, nBits=n_bits)
fingerprint_list = fingerprint.ToList()
fingerprint_dict = {f'{fingerprint_type}_{i}': int(bit) for i, bit in enumerate(fingerprint_list)}
return fingerprint_dict