#!/usr/bin/env python
# coding: utf-8
# # Thresholds for "random" in fingerprints the RDKit supports
#
# This is an updated version of a post. The original version of the notebook can be [found in github](https://github.com/greglandrum/rdkit_blog/blob/936b35b77ddca3e843991d1c4b1e3532e754b734/notebooks/Fingerprint%20Thresholds.ipynb).
#
# A frequent question that comes up when considering fingerprint similarity is: "What threshold should I use to determine what a neighbor is?" The answer is poorly defined. Of course it depends heavily on the details of the fingerprint, but there's also a very subjective component: you want to pick a low enough threshold that you're sure you won't miss anything, but you don't want to pick up too much noise.
#
# The goal here is to systematically come up with some guidelines that can be used for fingerprints supported within the RDKit. We will do that by looking a similarities between random "drug-like" (MW<600) molecules picked from ChEMBL.
#
# For the analysis, the 25K similarity values are sorted and the values at particular threshold are examined.
#
# There's a fair amount of code and results below, so here's the summary table. To help interpret this: 22500 of the 25000 pairs (90%) have a MACCS keys similarity value less than 0.528.
#
#
#
# Fingerprint | Metric | 70% level | 80% level | 90% level | 95% level | 99% level |
# MACCS | Tanimoto |
# 0.431 |
# 0.471 |
# 0.528 |
# 0.575 |
# 0.655 |
#
Morgan0 (counts) | Tanimoto |
# 0.429 |
# 0.471 |
# 0.525 |
# 0.568 |
# 0.651 |
#
Morgan1 (counts) | Tanimoto |
# 0.265 |
# 0.293 |
# 0.333 |
# 0.364 |
# 0.429 |
#
Morgan2 (counts) | Tanimoto |
# 0.181 |
# 0.201 |
# 0.229 |
# 0.252 |
# 0.305 |
#
Morgan3 (counts) | Tanimoto |
# 0.141 |
# 0.156 |
# 0.178 |
# 0.196 |
# 0.238 |
#
Morgan0 (bits) | Tanimoto |
# 0.435 |
# 0.475 |
# 0.529 |
# 0.571 |
# 0.656 |
#
Morgan1 (bits) | Tanimoto |
# 0.273 |
# 0.301 |
# 0.341 |
# 0.371 |
# 0.434 |
#
Morgan2 (bits) | Tanimoto |
# 0.197 |
# 0.217 |
# 0.246 |
# 0.269 |
# 0.322 |
#
Morgan3 (bits) | Tanimoto |
# 0.165 |
# 0.181 |
# 0.203 |
# 0.222 |
# 0.264 |
#
FeatMorgan0 (counts) | Tanimoto |
# 0.583 |
# 0.630 |
# 0.690 |
# 0.737 |
# 0.818 |
#
FeatMorgan1 (counts) | Tanimoto |
# 0.390 |
# 0.425 |
# 0.474 |
# 0.511 |
# 0.581 |
#
FeatMorgan2 (counts) | Tanimoto |
# 0.272 |
# 0.298 |
# 0.333 |
# 0.364 |
# 0.424 |
#
FeatMorgan3 (counts) | Tanimoto |
# 0.209 |
# 0.228 |
# 0.256 |
# 0.279 |
# 0.328 |
#
FeatMorgan0 (bits) | Tanimoto |
# 0.583 |
# 0.630 |
# 0.690 |
# 0.737 |
# 0.818 |
#
FeatMorgan1 (bits) | Tanimoto |
# 0.395 |
# 0.429 |
# 0.477 |
# 0.514 |
# 0.585 |
#
FeatMorgan2 (bits) | Tanimoto |
# 0.284 |
# 0.310 |
# 0.347 |
# 0.376 |
# 0.434 |
#
FeatMorgan3 (bits) | Tanimoto |
# 0.228 |
# 0.248 |
# 0.276 |
# 0.299 |
# 0.349 |
#
RDKit 4 (bits) | Tanimoto |
# 0.209 |
# 0.239 |
# 0.285 |
# 0.325 |
# 0.426 |
#
RDKit 5 (bits) | Tanimoto |
# 0.197 |
# 0.219 |
# 0.253 |
# 0.287 |
# 0.368 |
#
RDKit 6 (bits) | Tanimoto |
# 0.230 |
# 0.250 |
# 0.280 |
# 0.308 |
# 0.369 |
#
RDKit 7 (bits) | Tanimoto |
# 0.313 |
# 0.346 |
# 0.389 |
# 0.429 |
# 0.507 |
#
linear RDKit 4 (bits) | Tanimoto |
# 0.225 |
# 0.258 |
# 0.309 |
# 0.354 |
# 0.462 |
#
linear RDKit 5 (bits) | Tanimoto |
# 0.198 |
# 0.225 |
# 0.269 |
# 0.309 |
# 0.404 |
#
linear RDKit 6 (bits) | Tanimoto |
# 0.187 |
# 0.210 |
# 0.246 |
# 0.282 |
# 0.365 |
#
linear RDKit 7 (bits) | Tanimoto |
# 0.182 |
# 0.203 |
# 0.234 |
# 0.264 |
# 0.337 |
#
Atom Pairs (counts) | Tanimoto |
# 0.180 |
# 0.204 |
# 0.237 |
# 0.265 |
# 0.325 |
#
Torsions (counts) | Tanimoto |
# 0.107 |
# 0.130 |
# 0.165 |
# 0.194 |
# 0.266 |
#
Atom Pairs (bits) | Tanimoto |
# 0.275 |
# 0.301 |
# 0.335 |
# 0.363 |
# 0.415 |
#
Torsions (bits) | Tanimoto |
# 0.133 |
# 0.155 |
# 0.188 |
# 0.219 |
# 0.288 |
#
Avalon 512 (bits) | Tanimoto |
# 0.369 |
# 0.407 |
# 0.461 |
# 0.505 |
# 0.575 |
#
Avalon 1024 (bits) | Tanimoto |
# 0.269 |
# 0.297 |
# 0.340 |
# 0.375 |
# 0.449 |
#
Avalon 512 (counts) | Tanimoto |
# 0.300 |
# 0.333 |
# 0.379 |
# 0.418 |
# 0.491 |
#
Avalon 1024 (counts) | Tanimoto |
# 0.267 |
# 0.299 |
# 0.344 |
# 0.384 |
# 0.462 |
#
#
#
# In[1]:
from rdkit import Chem
from rdkit.Chem import rdMolDescriptors
from rdkit.Avalon import pyAvalonTools
from rdkit.Chem import Draw
from rdkit.Chem.Draw import IPythonConsole
from rdkit import rdBase
from rdkit import DataStructs
from collections import defaultdict
import pickle,random,gzip
print(rdBase.rdkitVersion)
import time
print(time.asctime())
get_ipython().run_line_magic('pylab', 'inline')
# # Read in the data
#
# We're using the set of 25K reference pairs generated in an earlier post: http://rdkit.blogspot.ch/2013/10/building-similarity-comparison-set-goal.html
#
# As a quick reminder: these are pairs of molecules taken from ChEMBL with MW<600 and a count-based MFP0 similarity of at least 0.7 to each other.
# In[2]:
ind = [x.split() for x in gzip.open('../data/chembl16_25K.pairs.txt.gz')]
ms1 = []
ms2 = []
for i,row in enumerate(ind):
m1 = Chem.MolFromSmiles(row[1])
ms1.append((row[0],m1))
m2 = Chem.MolFromSmiles(row[3])
ms2.append((row[2],m2))
# Those pairs are related to each other, but we want random pairs, so shuffle the second list:
# In[3]:
random.seed(23)
random.shuffle(ms2)
# In[4]:
try:
import ipyparallel as ipp
rc = ipp.Client()
dview = rc[:]
dview.execute('from rdkit import Chem')
dview.execute('from rdkit import Descriptors')
dview.execute('from rdkit.Chem import rdMolDescriptors')
dview.execute('from rdkit.Avalon import pyAvalonTools')
except:
print("could not use ipyparallel")
dview = None
results_accum = dict()
def compareFPs(ms1,ms2,fpfn,fpName):
if dview is not None:
fps = dview.map_sync(lambda x:fpfn(x[1]),ms1)
fp2s = dview.map_sync(lambda x:fpfn(x[1]),ms2)
else:
fps = [fpfn(x[1]) for x in ms1]
fp2s = [fpfn(x[1]) for x in ms2]
sims = [DataStructs.TanimotoSimilarity(x,y) for x,y in zip(fps,fp2s)]
sl = sorted(sims)
np = len(sl)
with open('fp_results.txt','a+') as outf:
outf.write(f'{fpName} | Tanimoto | \n')
accum = {}
for bin in (.7,.8,.9,.95,.99):
simv = sl[int(bin*np)]
print( bin,simv)
outf.write(f' {simv:.3f} | \n')
accum[bin] = simv
outf.write('
')
results_accum[fpName] = accum
hist(sims,bins=20)
xlabel(fpName)
# # MACCS
# In[5]:
compareFPs(ms1,ms2,lambda x:rdMolDescriptors.GetMACCSKeysFingerprint(x),"MACCS")
# # Morgan FPs
# ## count based
# In[6]:
compareFPs(ms1,ms2,lambda x:rdMolDescriptors.GetMorganFingerprint(x,0),"Morgan0 (counts)")
# In[7]:
compareFPs(ms1,ms2,lambda x:rdMolDescriptors.GetMorganFingerprint(x,1),"Morgan1 (counts)")
# In[8]:
compareFPs(ms1,ms2,lambda x:rdMolDescriptors.GetMorganFingerprint(x,2),"Morgan2 (counts)")
# In[9]:
compareFPs(ms1,ms2,lambda x:rdMolDescriptors.GetMorganFingerprint(x,3),"Morgan3 (counts)")
# ## bit-vector based
# In[10]:
compareFPs(ms1,ms2,lambda x:rdMolDescriptors.GetHashedMorganFingerprint(x,0,1024),"Morgan0 (bits)")
# In[11]:
compareFPs(ms1,ms2,lambda x:rdMolDescriptors.GetHashedMorganFingerprint(x,1,1024),"Morgan1 (bits)")
# In[12]:
compareFPs(ms1,ms2,lambda x:rdMolDescriptors.GetHashedMorganFingerprint(x,2,1024),"Morgan2 (bits)")
# In[13]:
compareFPs(ms1,ms2,lambda x:rdMolDescriptors.GetHashedMorganFingerprint(x,3,1024),"Morgan3 (bits)")
# # FeatMorgan
# ## count based
# In[14]:
compareFPs(ms1,ms2,lambda x:rdMolDescriptors.GetMorganFingerprint(x,0,useFeatures=True),"FeatMorgan0 (counts)")
# In[15]:
compareFPs(ms1,ms2,lambda x:rdMolDescriptors.GetMorganFingerprint(x,1,useFeatures=True),"FeatMorgan1 (counts)")
# In[16]:
compareFPs(ms1,ms2,lambda x:rdMolDescriptors.GetMorganFingerprint(x,2,useFeatures=True),"FeatMorgan2 (counts)")
# In[17]:
compareFPs(ms1,ms2,lambda x:rdMolDescriptors.GetMorganFingerprint(x,3,useFeatures=True),"FeatMorgan3 (counts)")
# ## bit vectors
# In[18]:
compareFPs(ms1,ms2,lambda x:rdMolDescriptors.GetHashedMorganFingerprint(x,0,1024,useFeatures=True),"FeatMorgan0 (bits)")
# In[19]:
compareFPs(ms1,ms2,lambda x:rdMolDescriptors.GetHashedMorganFingerprint(x,1,1024,useFeatures=True),"FeatMorgan1 (bits)")
# In[20]:
compareFPs(ms1,ms2,lambda x:rdMolDescriptors.GetHashedMorganFingerprint(x,2,1024,useFeatures=True),"FeatMorgan2 (bits)")
# In[21]:
compareFPs(ms1,ms2,lambda x:rdMolDescriptors.GetHashedMorganFingerprint(x,3,1024,useFeatures=True),"FeatMorgan3 (bits)")
# # RDKit
# ## Branched (default)
# In[22]:
compareFPs(ms1,ms2,lambda x:Chem.RDKFingerprint(x,maxPath=4),"RDKit 4 (bits)")
# In[23]:
compareFPs(ms1,ms2,lambda x:Chem.RDKFingerprint(x,maxPath=5),"RDKit 5 (bits)")
# In[24]:
compareFPs(ms1,ms2,lambda x:Chem.RDKFingerprint(x,maxPath=6),"RDKit 6 (bits)")
# In[25]:
compareFPs(ms1,ms2,lambda x:Chem.RDKFingerprint(x,maxPath=7),"RDKit 7 (bits)")
# ## linear
# In[26]:
compareFPs(ms1,ms2,lambda x:Chem.RDKFingerprint(x,maxPath=4,branchedPaths=False),"linear RDKit 4 (bits)")
# In[27]:
compareFPs(ms1,ms2,lambda x:Chem.RDKFingerprint(x,maxPath=5,branchedPaths=False),"linear RDKit 5 (bits)")
# In[28]:
compareFPs(ms1,ms2,lambda x:Chem.RDKFingerprint(x,maxPath=6,branchedPaths=False),"linear RDKit 6 (bits)")
# In[29]:
compareFPs(ms1,ms2,lambda x:Chem.RDKFingerprint(x,maxPath=7,branchedPaths=False),"linear RDKit 7 (bits)")
# # Atom pairs and torsions
# ## count-based
# In[30]:
compareFPs(ms1,ms2,lambda x:rdMolDescriptors.GetAtomPairFingerprint(x),"Atom Pairs (counts)")
# In[31]:
compareFPs(ms1,ms2,lambda x:rdMolDescriptors.GetTopologicalTorsionFingerprint(x),"Topological Torsions (counts)")
# ## bit vectors
# In[32]:
compareFPs(ms1,ms2,lambda x:rdMolDescriptors.GetHashedAtomPairFingerprintAsBitVect(x),"Atom Pairs (bits)")
# In[33]:
compareFPs(ms1,ms2,lambda x:rdMolDescriptors.GetHashedTopologicalTorsionFingerprintAsBitVect(x),"Topological Torsions (bits)")
# # Avalon
# In[34]:
compareFPs(ms1,ms2,lambda x:pyAvalonTools.GetAvalonFP(x,512),"Avalon 512 (bits)")
# In[35]:
compareFPs(ms1,ms2,lambda x:pyAvalonTools.GetAvalonFP(x,1024),"Avalon 1024 (bits)")
# # Avalon Counts
# In[36]:
compareFPs(ms1,ms2,lambda x:pyAvalonTools.GetAvalonCountFP(x,512),"Avalon 512 (counts)")
# In[37]:
compareFPs(ms1,ms2,lambda x:pyAvalonTools.GetAvalonCountFP(x,1024),"Avalon 1024 (counts)")
# In[38]:
pickle.dump(results_accum,open('./results/fp_thresholds_random_accum.pkl','wb+'))