def test_PatternHolder(self): fname = os.path.join(os.environ["RDBASE"], "Data", "NCI", "first_5K.smi") suppl = Chem.SmilesMolSupplier(fname, delimiter="\t", titleLine=False) mols1 = rdSubstructLibrary.CachedTrustedSmilesMolHolder() fps1 = rdSubstructLibrary.PatternHolder(2048) ssslib1 = rdSubstructLibrary.SubstructLibrary(mols1, fps1) mols2 = rdSubstructLibrary.CachedTrustedSmilesMolHolder() fps2 = rdSubstructLibrary.PatternHolder() ssslib2 = rdSubstructLibrary.SubstructLibrary(mols2, fps2) RDLogger.DisableLog('rdApp.error') for i in range(0, 1000, 10): try: mol = suppl[i] except Exception: continue if (not mol): continue mols1.AddSmiles(Chem.MolToSmiles(mol)) fps1.AddFingerprint(fps1.MakeFingerprint(mol)) ssslib2.AddMol(mol) RDLogger.EnableLog('rdApp.error') query = Chem.MolFromSmarts("N") self.assertIsNotNone(query) matches1 = sorted(ssslib1.GetMatches(query)) matches2 = sorted(ssslib2.GetMatches(query)) self.assertEqual(len(matches1), len(matches2)) self.assertTrue(all([m1 == matches2[i] for i, m1 in enumerate(matches1)]))
def test_addpatterns(self): pdb_ligands = [ "CCS(=O)(=O)c1ccc(OC)c(Nc2ncc(-c3cccc(-c4ccccn4)c3)o2)c1", "COc1ccc(S(=O)(=O)NCC2CC2)cc1Nc1ncc(-c2cccc(-c3cccnc3)c2)o1", "COc1ccc(-c2oc3ncnc(N)c3c2-c2ccc(NC(=O)Nc3cc(C(F)(F)F)ccc3F)cc2)cc1", "COC(=O)Nc1nc2ccc(Oc3ccc(NC(=O)Nc4cc(C(F)(F)F)ccc4F)cc3)cc2[nH]1", "COc1cc(Nc2ncnc(-c3cccnc3Nc3ccccc3)n2)cc(OC)c1OC", "O=C(Nc1ccc(Oc2ccccc2)cc1)c1cccnc1NCc1ccncc1", "O=C(Nc1ccc(Oc2ccccc2)cc1)c1cccnc1NCc1ccncc1", "CNC(=O)c1cc(Oc2ccc3[nH]c(Nc4ccc(Cl)c(C(F)(F)F)c4)nc3c2)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", "O=C(Nc1cncnc1)c1c(Cl)ccc2c(Nc3cccc(C(F)(F)F)c3)noc12", "CC1(C)CNc2cc(NC(=O)c3cccnc3NCc3ccncc3)ccc21", "CC1(C)CNc2cc(NC(=O)c3cccnc3NCc3ccncc3)ccc21" ] for patterns in [ rdSubstructLibrary.PatternHolder(), rdSubstructLibrary.TautomerPatternHolder() ]: mols = [Chem.MolFromSmiles(smi) for smi in pdb_ligands] holder = rdSubstructLibrary.CachedMolHolder() slib_with_patterns = rdSubstructLibrary.SubstructLibrary( holder, patterns) for mol in mols: slib_with_patterns.AddMol(mol) for nthreads in [1, 2, 0]: slib_without_patterns = rdSubstructLibrary.SubstructLibrary( holder, None) rdSubstructLibrary.AddPatterns(slib_without_patterns, nthreads) # check for seg fault # were the fingerprints really created slib_without_patterns.GetFpHolder().GetFingerprint(0) for mol in mols: l1 = slib_with_patterns.CountMatches(mol) l2 = slib_without_patterns.CountMatches(mol) self.assertTrue(l1) self.assertEqual(l1, l2)
def test_basic_addpatterns(self): # add mols pdb_ligands = [ "CCS(=O)(=O)c1ccc(OC)c(Nc2ncc(-c3cccc(-c4ccccn4)c3)o2)c1", "COc1ccc(S(=O)(=O)NCC2CC2)cc1Nc1ncc(-c2cccc(-c3cccnc3)c2)o1", "COc1ccc(-c2oc3ncnc(N)c3c2-c2ccc(NC(=O)Nc3cc(C(F)(F)F)ccc3F)cc2)cc1", "COC(=O)Nc1nc2ccc(Oc3ccc(NC(=O)Nc4cc(C(F)(F)F)ccc4F)cc3)cc2[nH]1", "COc1cc(Nc2ncnc(-c3cccnc3Nc3ccccc3)n2)cc(OC)c1OC", "O=C(Nc1ccc(Oc2ccccc2)cc1)c1cccnc1NCc1ccncc1", "O=C(Nc1ccc(Oc2ccccc2)cc1)c1cccnc1NCc1ccncc1", "CNC(=O)c1cc(Oc2ccc3[nH]c(Nc4ccc(Cl)c(C(F)(F)F)c4)nc3c2)ccn1", "CNC(=O)c1cc(Oc2ccc3oc(Nc4ccc(Cl)c(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F)F)c3)noc12", "CC1(C)CNc2cc(NC(=O)c3cccnc3NCc3ccncc3)ccc21", "CC1(C)CNc2cc(NC(=O)c3cccnc3NCc3ccncc3)ccc21" ] for holder in [ rdSubstructLibrary.CachedSmilesMolHolder(), rdSubstructLibrary.CachedTrustedSmilesMolHolder() ]: for smi in pdb_ligands: holder.AddSmiles(smi) for patttern in [ None, rdSubstructLibrary.PatternHolder(), rdSubstructLibrary.TautomerPatternHolder() ]: lib = rdSubstructLibrary.SubstructLibrary(holder) rdSubstructLibrary.AddPatterns(lib, numThreads=-1) self.assertEqual(len(lib.GetMolHolder()), len(lib.GetFpHolder())) for smi in pdb_ligands: self.assertTrue(lib.CountMatches(Chem.MolFromSmiles(smi)))
def main(directory: str, chebml_version: str): """Download the ChEBML data.""" os.makedirs(directory, exist_ok=True) bradley_path = os.path.join(directory, 'jm020472j_s2.xls') if not os.path.exists(bradley_path): try: wget.download(bradley_url, out=directory) except: click.echo('There goes ACS stopping science') chembl_url = ( f'ftp://ftp.ebi.ac.uk/pub/databases/chembl/ChEMBLdb/releases/' f'chembl_{chebml_version}/chembl_{chebml_version}.sdf.gz') sdf_path = os.path.join(directory, f'chembl_{chebml_version}.sdf.gz') if not os.path.exists(sdf_path): wget.download(chembl_url, out=directory) sss_path = os.path.join(directory, f'chembl{chebml_version}_sssdata.pkl') if not os.path.exists(sss_path): click.echo(f'RDKit Version: {rdBase.rdkitVersion}') data = [] with gzip.GzipFile(sdf_path) as gz: suppl = Chem.ForwardSDMolSupplier(gz) for mol in tqdm(suppl, desc=f'Processing ChEBML {chebml_version}', unit_scale=True): if mol is None or mol.GetNumAtoms() > 50: continue fp = Chem.PatternFingerprint(mol) smi = Chem.MolToSmiles(mol) data.append((smi, fp)) click.echo(f'Outputting to {sss_path}') with open(sss_path, 'wb') as file: mols = rdSubstructLibrary.CachedTrustedSmilesMolHolder() fps = rdSubstructLibrary.PatternHolder() for smi, fp in data: mols.AddSmiles(smi) fps.AddFingerprint(fp) library = rdSubstructLibrary.SubstructLibrary(mols, fps) pickle.dump(library, file, protocol=pickle.HIGHEST_PROTOCOL) click.echo('Done ;)')
def read_in_lib(input_smi): t1 = time.time() mols = rdSubstructLibrary.CachedTrustedSmilesMolHolder() fps = rdSubstructLibrary.PatternHolder() with open(input_smi, 'r') as inf: ls = [x.split() for x in inf] ls.pop(0) with open(input_smi.replace(".smi", ".pkl"), 'rb') as pklf: for l in tqdm.tqdm(ls): smi = l[1] mols.AddSmiles(smi) fp = pickle.load(pklf) fps.AddFingerprint(fp) library = rdSubstructLibrary.SubstructLibrary(mols, fps) t2 = time.time() print("That took %.2f seconds. The library has %d molecules." % (t2 - t1, len(library))) return library
def __init__(self, proj, datapath, dbpath, chembldb, flimit=1e-3, MinClusterSize=20, clustering='UPGMA', calcDists=True, calcScores=False): self.proj=proj self.datapath=datapath self.dbpath=dbpath self.chembldb=chembldb self.flimit=flimit self.MinClusterSize=MinClusterSize self.clustering=clustering self.calcScores=calcScores self.calcDists=calcDists # load data self.moldata_proj, self.distdata_proj=utilsDataPrep.PrepareData(self.proj,self.datapath,distMeasure='Tanimoto',FP='Morgan2', calcDists=self.calcDists) if arthor is not None: if not os.path.isdir(dbpath): os.mkdir(dbpath) # set up project database for arthor substructure matching df=self.moldata_proj[['Structure','ID']] df.to_csv('./arthor/{0}.smi'.format(self.proj), header=None, index=None, sep=' ') os.system('smi2atdb -j 0 -t {0}{1}.smi {0}{1}.atdb'.format(self.dbpath,self.proj)) os.system('atdb2fp -j 0 {0}{1}.atdb'.format(self.dbpath,self.proj)) self.proj_db=arthor.SubDb('{0}{1}.atdb'.format(self.dbpath,self.proj)) else: if type(dbpath)==rdSubstructLibrary.SubstructLibrary: self.proj_db = dbpath self.db_size = len(self.proj_db) else: if not os.path.exists(dbpath): print("creating database") mols = rdSubstructLibrary.CachedTrustedSmilesMolHolder() fps = rdSubstructLibrary.PatternHolder() for smi in self.moldata_proj['Structure']: m = Chem.MolFromSmiles(smi) mols.AddSmiles(Chem.MolToSmiles(m)) fps.AddFingerprint(Chem.PatternFingerprint(m)) self.proj_db = rdSubstructLibrary.SubstructLibrary(mols,fps) self.db_size = len(mols) pickle.dump(self.proj_db,open(dbpath,'wb+')) else: self.proj_db = pickle.load(open(dbpath,'rb')) self.db_size = len(self.proj_db)