def depict_smiles(smiles): """ OEChem and OEDepict image generation """ # Image to draw on image = OEImage(400, 400) # Process SMILES mol = OEGraphMol() parsed = OESmilesToMol(mol, str(unquote(smiles))) if parsed: # Create image of molecule newtitle = [] for c in mol.GetTitle(): newtitle.append(choice(ALL_EMOJI)) mol.SetTitle(("".join(newtitle)).encode("UTF-8")) OEPrepareDepiction(mol) disp = OE2DMolDisplay(mol) for adisp in disp.GetAtomDisplays(): adisp.SetLabel(choice(ALL_EMOJI).encode("UTF-8")) OERenderMolecule(image, disp) else: # Create error image font = OEFont(OEFontFamily_Helvetica, OEFontStyle_Default, 20, OEAlignment_Center, OERed) image.DrawText(OE2DPoint(image.GetWidth()/2.0, image.GetHeight()/2.0), 'Your SMILES is not valid', font) img_content = OEWriteImageToString('svg', image) return Response(img_content, mimetype='image/svg+xml')
def test_sanitizeSMILES(): """ Test SMILES sanitization. """ smiles_list = [ 'CC', 'CCC', '[H][C@]1(NC[C@@H](CC1CO[C@H]2CC[C@@H](CC2)O)N)[H]' ] sanitized_smiles_list = sanitizeSMILES(smiles_list, mode='drop') if len(sanitized_smiles_list) != 2: raise Exception( "Molecules with undefined stereochemistry are not being properly dropped (size=%d)." % len(sanitized_smiles_list)) sanitized_smiles_list = sanitizeSMILES(smiles_list, mode='expand') if len(sanitized_smiles_list) != 4: raise Exception( "Molecules with undefined stereochemistry are not being properly expanded (size=%d)." % len(sanitized_smiles_list)) # Check that all molecules can be round-tripped. from openeye.oechem import OEGraphMol, OESmilesToMol, OECreateIsoSmiString for smiles in sanitized_smiles_list: molecule = OEGraphMol() OESmilesToMol(molecule, smiles) isosmiles = OECreateIsoSmiString(molecule) if (smiles != isosmiles): raise Exception( "Molecule '%s' was not properly round-tripped (result was '%s')" % (smiles, isosmiles))
def test_success(self): mol = OEMol() OESmilesToMol(mol, str("c1(c(cccc1)OC(=O)C)C(=O)O")) self.cube.process(mol, self.cube.intake.name) self.assertEqual(self.runner.outputs["success"].qsize(), 1) self.assertEqual(self.runner.outputs["failure"].qsize(), 1) new_mol = self.runner.outputs["success"].get() self.assertTrue(OEExactGraphMatch(new_mol, mol))
def sanitizeSMILES(smiles_list, mode='drop', verbose=False): """ Sanitize set of SMILES strings by ensuring all are canonical isomeric SMILES. Duplicates are also removed. Parameters ---------- smiles_list : iterable of str The set of SMILES strings to sanitize. mode : str, optional, default='drop' When a SMILES string that does not correspond to canonical isomeric SMILES is found, select the action to be performed. 'exception' : raise an `Exception` 'drop' : drop the SMILES string 'expand' : expand all stereocenters into multiple molecules verbose : bool, optional, default=False If True, print verbose output. Returns ------- sanitized_smiles_list : list of str Sanitized list of canonical isomeric SMILES strings. Examples -------- Sanitize a simple list. >>> smiles_list = ['CC', 'CCC', '[H][C@]1(NC[C@@H](CC1CO[C@H]2CC[C@@H](CC2)O)N)[H]'] Throw an exception if undefined stereochemistry is present. >>> sanitized_smiles_list = sanitizeSMILES(smiles_list, mode='exception') Traceback (most recent call last): ... Exception: Molecule '[H][C@]1(NC[C@@H](CC1CO[C@H]2CC[C@@H](CC2)O)N)[H]' has undefined stereocenters Drop molecules iwth undefined stereochemistry. >>> sanitized_smiles_list = sanitizeSMILES(smiles_list, mode='drop') >>> len(sanitized_smiles_list) 2 Expand molecules iwth undefined stereochemistry. >>> sanitized_smiles_list = sanitizeSMILES(smiles_list, mode='expand') >>> len(sanitized_smiles_list) 4 """ from openeye import oechem from openeye.oechem import OEGraphMol, OESmilesToMol, OECreateIsoSmiString from perses.tests.utils import has_undefined_stereocenters, enumerate_undefined_stereocenters sanitized_smiles_set = set() OESMILES_OPTIONS = oechem.OESMILESFlag_DEFAULT | oechem.OESMILESFlag_ISOMERIC | oechem.OESMILESFlag_Hydrogens ## IVY for smiles in smiles_list: molecule = OEGraphMol() OESmilesToMol(molecule, smiles) oechem.OEAddExplicitHydrogens(molecule) if verbose: molecule.SetTitle(smiles) oechem.OETriposAtomNames(molecule) if has_undefined_stereocenters(molecule, verbose=verbose): if mode == 'drop': if verbose: print("Dropping '%s' due to undefined stereocenters." % smiles) continue elif mode == 'exception': raise Exception("Molecule '%s' has undefined stereocenters" % smiles) elif mode == 'expand': if verbose: print('Expanding stereochemistry:') print('original: %s', smiles) molecules = enumerate_undefined_stereocenters(molecule, verbose=verbose) for molecule in molecules: smiles_string = oechem.OECreateSmiString(molecule, OESMILES_OPTIONS) ## IVY sanitized_smiles_set.add(smiles_string) ## IVY if verbose: print('expanded: %s', smiles_string) else: # Convert to OpenEye's canonical isomeric SMILES. smiles_string = oechem.OECreateSmiString(molecule, OESMILES_OPTIONS) ## IVY sanitized_smiles_set.add(smiles_string) ## IVY sanitized_smiles_list = list(sanitized_smiles_set) return sanitized_smiles_list