-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKRFingerprints.py
167 lines (121 loc) · 6.22 KB
/
KRFingerprints.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
import json
import pandas as pd
from rdkit import Chem, DataStructs, RDLogger
from rdkit.Chem import MolStandardize
from rdkit.Chem import Draw
from rdkit.Chem.Fingerprints import FingerprintMols
import time
import numbers
import numpy as np
class KRFingerprints:
with open('krfp.json') as json_file:
KRFPDictSmarts = json.load(json_file)
KRFPDictSmarts = KRFPDictSmarts
KRFPKeys = list(KRFPDictSmarts.keys())
def SmartsToMol(KRFPDictSmarts, KRFPKeys):
return {x: Chem.MolFromSmarts(KRFPDictSmarts[x]) for x in KRFPKeys}
KRFPDictMol = SmartsToMol(KRFPDictSmarts, KRFPKeys)
def GenerateKRFingerprints(structures, count=False, output_type='list', verbose=True):
#string to list
if isinstance(structures, str) or isinstance(structures, Chem.rdchem.Mol):
structures = [structures]
krfp_ligands = []
RDLogger.DisableLog('rdApp.info')
for i, ligand in enumerate(structures):
start = time.time()
if isinstance(ligand,str):
ligand = Chem.MolFromSmiles(ligand)
elif isinstance(ligand,Chem.rdchem.Mol):
ligand = ligand
ligand = MolStandardize.rdMolStandardize.Cleanup(ligand)
ligand = MolStandardize.rdMolStandardize.FragmentParent(ligand)
uncharger = MolStandardize.rdMolStandardize.Uncharger() # annoying, but necessary as no convenience method exists
ligand = uncharger.uncharge(ligand)
if count is False:
krfp_ligand = [1 if ligand.HasSubstructMatch(KRFingerprints.KRFPDictMol[fp]) else 0 for fp in KRFingerprints.KRFPDictMol]
elif count:
krfp_ligand = [len(ligand.GetSubstructMatches(KRFingerprints.KRFPDictMol[fp])) for fp in KRFingerprints.KRFPDictMol]
krfp_ligands.append(krfp_ligand)
end = time.time()
#Interface
if verbose:
estimated_time = (end-start)*(len(structures)-i+1)
estimated_time_str = time.strftime("%H:%M:%S", time.gmtime(estimated_time))
print(str(i+1)+"/"+str(len(structures))+" structures, time remaining: " + str(estimated_time_str))
percentage=(i+1)/len(structures)
for hashes in range(int(percentage*20)): print('#',end='')
for spaces in range(20-int(percentage*20)): print(' ',end='')
print(' '+str(int(percentage*100))+'%')
from IPython.display import clear_output
clear_output(wait=True)
if output_type=='list':
return krfp_ligands
elif output_type=='dictionary':
krfp_ligands = KRFingerprints.ListToDictionary(krfp_ligands)
elif output_type=='dataframe':
krfp_ligands = pd.DataFrame(KRFingerprints.ListToDictionary(krfp_ligands))
return krfp_ligands
def GenerateKRFingerprintsToDataFrame(data, structures_column, count=False, verbose=True):
krfp_ligands = KRFingerprints.GenerateKRFingerprints(data[structures_column], count, 'dictionary', verbose=verbose)
data = pd.concat([data,pd.DataFrame(krfp_ligands,index=data.index, columns=list(KRFingerprints.KRFPDictSmarts.keys()))],axis=1)
return data
def ListToDictionary(krfp_list):
descriptors={}
for i,ligand_krfp in enumerate(krfp_list):
for j,fp in enumerate(KRFingerprints.KRFPDictMol):
if i==0:
descriptors[fp] = [ligand_krfp[j]]
else:
descriptors[fp][i:] = [ligand_krfp[j]]
return descriptors
def DrawFingerprint(krfp):
fp = KRFingerprints.KRFPDictMol[krfp]
img = Draw.MolToImage(fp, legend = krfp)
return img
def DrawFingerprints(krfps, molsPerRow=3):
if isinstance(krfps, str):
krfps = [krfps]
imgs = [KRFingerprints.DrawFingerprint(x) for x in krfps]
structs = [KRFingerprints.KRFPDictMol[x] for x in krfps]
img = Draw.MolsToGridImage(structs, molsPerRow=3, legends=krfps,subImgSize=(300, 300))
return img
def FindKRFP(find):
find = Chem.MolFromSmiles(find)
find_fps = FingerprintMols.FingerprintMol(find)
krfp_fps = {x: FingerprintMols.FingerprintMol(KRFingerprints.KRFPDictMol[x]) for x in KRFingerprints.KRFPKeys}
tanimotos = {x: DataStructs.TanimotoSimilarity(find_fps, krfp_fps[x]) for x in KRFingerprints.KRFPKeys}
tanimotos = dict(sorted(tanimotos.items(), key=lambda item:item[1], reverse=True))
tanimotos_return={}
for i in tanimotos:
if tanimotos[i]==1.0:
tanimotos_return[i] = tanimotos[i]
if len(tanimotos_return)==0:
tanimotos_return[list(tanimotos.items())[0][0]] = list(tanimotos.items())[0][1]
return tanimotos_return
def HighlightKRFP(structures, krfp, names=None):
#imgs = [KRFingerprints.HighlightKRFP(x, krfp, False) for x in mols]
if isinstance(structures, Chem.rdchem.Mol):
mols = [structures]
if isinstance(names, str):
names = [names]
for i, name in enumerate(names):
if isinstance(name, numbers.Number):
names[i] = str(name)
fp = KRFingerprints.KRFPDictMol[krfp]
mols_atoms=[]
mols_bonds=[]
for i, mol in enumerate(structures):
if isinstance(mol,str):
mol = Chem.MolFromSmiles(mol)
structures[i]=Chem.MolFromSmiles(mol)
atoms = sum(mol.GetSubstructMatches(fp),())
mols_atoms.append(atoms)
bonds = []
for a1 in atoms:
for a2 in atoms:
s =mol.GetBondBetweenAtoms(a1,a2)
if s is not None:
bonds.append(s.GetIdx())
mols_bonds.append(bonds)
img = Draw.MolsToGridImage(structures, molsPerRow=3, legends=names, subImgSize=(300, 300), highlightAtomLists=mols_atoms, highlightBondLists=mols_bonds)
return img