def _similarityMap(ms, width=500, height=500, radius=2, fingerprint='morgan', format='png'): if matplotlib is None: raise ValueError('matplotlib not useable') _call(ms, 'UpdatePropertyCache', strict=False) _apply(ms, ct._sssr) fn = None if fingerprint == 'morgan': fn = lambda x, i: SimilarityMaps.GetMorganFingerprint( x, i, radius=radius) elif fingerprint == 'tt': fn = SimilarityMaps.GetAPFingerprint elif fingerprint == 'ap': fn = SimilarityMaps.GetTTFingerprint SimilarityMaps.GetSimilarityMapForFingerprint(ms[0], ms[1], fn, size=(width, height)) sio = StringIO.StringIO() pyplot.savefig(sio, format=format, bbox_inches='tight', dpi=100) return sio.getvalue()
def _inchi2svg(inchis,size,legend, kekulize=True, wedgeBonds=True, fitImage=True, atomMapNumber=False, computeCoords=False): mols = _apply(inchis.split(), Chem.MolFromInchi) _apply(mols, _computeCoords) return _mols2svg(mols, size, legend, kekulize=kekulize, wedgeBonds=wedgeBonds, fitImage=fitImage, atomMapNumber=atomMapNumber, computeCoords=computeCoords) #-----------------------------------------------------------------------------------------------------------------------
def _mols2imageString(mols,size,legend, format): if not mols: return '' _apply(mols, _computeCoords) imageData = StringIO.StringIO() _mols2imageStream(mols, imageData, format, size, legend) return imageData.getvalue()
def _mols2imageStream(mols, f, format, size, legend, highlightAtomLists=None, kekulize=True): labels = [x for x in islice(cycle(legend), len(mols))] if isinstance(legend, (list, tuple)) else \ [x for x in islice(cycle([legend]), len(mols))] legends = [x.GetProp("_Name") if (x.HasProp("_Name") and x.GetProp("_Name")) else labels[idx] for idx, x in enumerate(mols)] molsPerRow = min(len(mols), 4) nRows = len(mols) // molsPerRow if len(mols) % molsPerRow: nRows += 1 _call(mols, 'UpdatePropertyCache', strict=False) if kekulize: _apply(mols, _kekulize) highlightBondLists = [] if highlightAtomLists: for mol, highlightAtomList in zip(mols, highlightAtomLists): highlightBondList = [] for bnd in mol.GetBonds(): if bnd.GetBeginAtomIdx() in highlightAtomList and bnd.GetEndAtomIdx() in highlightAtomList: highlightBondList.append(bnd.GetIdx()) highlightBondLists.append(highlightBondList) if not highlightBondLists: highlightBondLists = None panelx = size panely = size canvasx = panelx * molsPerRow canvasy = panely * nRows drawer = rdMolDraw2D.MolDraw2DCairo(canvasx, canvasy, panelx, panely) drawer.DrawMolecules(mols, highlightAtoms=highlightAtomLists, highlightBonds=highlightBondLists, legends=legends) drawer.FinishDrawing() f.write(drawer.GetDrawingText())
def _mols2svg(mols, size, legend, kekulize=True, wedgeBonds=True, fitImage=True, atomMapNumber=False, computeCoords=False): if computeCoords: _apply(mols, _computeCoords, True) if atomMapNumber: _apply(mols, _atomMapNumber) molsPerRow=min(len(mols),4) totalWidth=molsPerRow*size totalHeight=molsPerRow*size if cffi and cairocffi.version <= (1,10,0) : imageData = io.BytesIO() else: imageData = StringIO.StringIO() surf = cairo.SVGSurface(imageData,totalWidth,totalHeight) ctx = cairo.Context(surf) for i, mol in enumerate(mols): tx = size*(i%molsPerRow) ty = size*(i//molsPerRow) ctx.translate(tx, ty) canv = cairoCanvas.Canvas(ctx=ctx, size=(size,size), imageType='svg') leg = mol.GetProp("_Name") if (mol.HasProp("_Name") and mol.GetProp("_Name")) else legend draw.MolToImage(mol, size=(size,size), legend=leg, canvas=canv, kekulize=kekulize, wedgeBonds=wedgeBonds, fitImage=fitImage) canv.flush() ctx.translate(-tx, -ty) surf.finish() return imageData.getvalue()
def _hydrogenize(block, hydro): mol = Chem.MolFromMolBlock(block, sanitize=False) _call([mol], 'UpdatePropertyCache', strict=False) _apply([mol], ct._sssr) res = Chem.AddHs(mol, addCoords=True) if hydro else Chem.RemoveHs( mol, sanitize=False) return MolToMarvin(Chem.MolToMolBlock(res))
def _kekulize(data, sanitize=True, removeHs=True, strictParsing=True): mols = _parseMolData(data, sanitize=sanitize, removeHs=removeHs, strictParsing=strictParsing) _apply(mols, Chem.Kekulize) return _getSDFString(mols)
def _mols2imageString(mols, size, legend, format): if not mols: return '' _apply(mols, _computeCoords) imageData = StringIO.StringIO() _mols2imageStream(mols, imageData, format, size, legend) return imageData.getvalue()
def _autoDetect(structure): mol = None if Chem.MolFromSmiles(structure.strip(), sanitize=False): mol = Chem.MolFromSmiles(structure.strip(), sanitize=False) elif Chem.MolFromMolBlock(structure, sanitize=False): return Chem.MolFromMolBlock(structure, sanitize=False) elif Chem.INCHI_AVAILABLE and Chem.inchi.MolFromInchi(structure.strip(), sanitize=True, removeHs=True): mol = Chem.inchi.MolFromInchi(structure.strip(), sanitize=True, removeHs=True) elif hasattr(Chem, 'MolFromSmarts') and Chem.MolFromSmarts(structure): mol = Chem.MolFromSmarts(structure) elif hasattr(Chem, 'MolFromMol2Block') and Chem.MolFromMol2Block(structure): mol = Chem.MolFromMol2Block(structure) elif hasattr(Chem, 'MolFromPDBBlock') and Chem.MolFromPDBBlock(structure): mol = Chem.MolFromPDBBlock(structure) elif hasattr(Chem, 'MolFromTPLBlock') and Chem.MolFromTPLBlock(structure): mol = Chem.MolFromTPLBlock(structure) else: try: mol = Chem.MolFromMolBlock(MarvinToMol(structure)) except: pass _call([mol], 'UpdatePropertyCache', strict=False) _apply([mol], ct._sssr) return mol
def _sanitize(data, sanitizeOps=SANITIZE_ALL): mols = _parseMolData(data, sanitize=False, removeHs=False, strictParsing=False) try: _apply(mols, Chem.SanitizeMol, sanitizeOps=sanitizeOps) except: pass return _getSDFString(mols)
def _parseSMILESData(data, computeCoords=False): suppl = SmilesMolSupplier() suppl.SetData(data) mols = [x for x in suppl if x] if not mols: mols = [MolFromSmiles(data)] if computeCoords: _apply(mols, _computeCoords, True) return mols
def _mols2imageString(mols, size, legend, format, atomMapNumber=False, computeCoords=False): if not mols: return '' if computeCoords: _apply(mols, _computeCoords, True) if atomMapNumber: _apply(mols, _atomMapNumber) imageData = StringIO.StringIO() _mols2imageStream(mols, imageData, format, size, legend) return imageData.getvalue()
def _mols2imageString(mols, size, legend, format, atomMapNumber=False, computeCoords=False, highlightAtomLists=None, kekulize=True): if not mols: return '' if computeCoords: _apply(mols, _computeCoords, True) if atomMapNumber: _apply(mols, _atomMapNumber) image_data = StringIO.StringIO() _mols2imageStream(mols, image_data, format, size, legend, highlightAtomLists, kekulize) return image_data.getvalue()
def _clean(mrv, dim=2): block = MarvinToMol(mrv) mol = Chem.MolFromMolBlock(block, sanitize=False) _call([mol], 'UpdatePropertyCache', strict=False) _apply([mol], ct._sssr) if not mol: print "No mol for block:\n %s" % block return mrv AllChem.Compute2DCoords(mol, bondLength=0.8) if dim == 3: mol = _2D23D(mol, True) mol = Chem.RemoveHs(mol) return MolToMarvin(Chem.MolToMolBlock(mol))
def _smarts2ctab(data, computeCoords=True, delimiter=' ', sanitize=True): mols = [] for line in data.splitlines(): if not line: continue for chunk in line.strip().split(delimiter): if not chunk: continue mols.append(_molFromSmarts(chunk)) if computeCoords: _apply(mols, _computeCoords, True) if sanitize: _apply(mols, _sanitize) return _getSDFString(mols)
def _parseSMILESData(data, computeCoords=False, delimiter=' ', smilesColumn=0, nameColumn=1, titleLine=True, sanitize=True): fd, fpath = tempfile.mkstemp(text=True) os.write(fd, data) os.close(fd) suppl = SmilesMolSupplier(fpath, delimiter=delimiter, smilesColumn=smilesColumn, nameColumn=nameColumn, titleLine=titleLine, sanitize=sanitize) mols = [x for x in suppl if x] # if not mols: # mols = [MolFromSmiles(data, sanitize=sanitize)] if computeCoords: _apply(mols, _computeCoords, True) _call(mols, 'SetProp', '_Name', '') os.remove(fpath) return mols
def _ctab2inchi(data, sanitize=True, removeHs=True, strictParsing=True): return '\n'.join( _apply( _parseMolData(data, sanitize=sanitize, removeHs=removeHs, strictParsing=strictParsing), Chem.MolToInchi))
def _removeHs(data, implicitOnly=False): mols = _parseMolData(data, sanitize=False, removeHs=False, strictParsing=True) mols = _apply(mols, Chem.RemoveHs, implicitOnly=implicitOnly) return _getSDFString(mols)
def _standardise(data): mols = _parseMolData(data) res = _apply(mols, standardise) return _getSDFString(res) #-----------------------------------------------------------------------------------------------------------------------
def _unsalt(data, sanitize=True, removeHs=True, strictParsing=True): mols = _parseMolData(data, sanitize=sanitize, removeHs=removeHs, strictParsing=strictParsing) res = _apply(mols, unsalt) return _getSDFString(res)
def _standardise(data, sanitize=True, removeHs=True, strictParsing=True): mols = _parseMolData(data, sanitize=sanitize, removeHs=removeHs, strictParsing=strictParsing) res = _apply(mols, standardise) return _getSDFString(res)
def _break_bonds(data, sanitize=True, removeHs=True, strictParsing=True): mols = _parseMolData(data, sanitize=sanitize, removeHs=removeHs, strictParsing=strictParsing) res = _apply(mols, break_bonds) return _getSDFString(res)
def _molExport(structure, **kwargs): input_f = kwargs.get('input', None) output_f = kwargs.get('output', None) if not input_f: mol = _autoDetect(str(structure)) elif input_f == 'mrv': mol = Chem.MolFromMolBlock(MarvinToMol(structure), sanitize=False) elif input_f == 'smiles': mol = Chem.MolFromSmiles(str(structure), sanitize=False) elif input_f == 'inchi': mol = Chem.MolFromInchi(str(structure)) else: mol = Chem.MolFromMolBlock(structure, sanitize=False) _call([mol], 'UpdatePropertyCache', strict=False) _apply([mol], ct._sssr) if not mol.GetNumConformers() or mol.GetConformer().Is3D(): AllChem.Compute2DCoords(mol, bondLength=0.8) if output_f == 'smiles': out_structure = Chem.MolToSmiles(mol) elif output_f == 'inchi': out_structure = Chem.MolToInchi(mol) elif output_f == 'inchikey': out_structure = Chem.InchiToInchiKey(Chem.MolToInchi(mol)) elif output_f == 'mol': out_structure = Chem.MolToMolBlock(mol) elif output_f == 'sdf': out_structure = Chem.MolToMolBlock(mol) + '\n$$$$\n' elif output_f == 'mrv': out_structure = MolToMarvin(Chem.MolToMolBlock(mol)) return { "structure": out_structure, "format": output_f, "contentUrl": "", "contentBaseUrl": "" }
def _symmsssr(data, sanitize=True, removeHs=True, strictParsing=True): mols = _parseMolData(data, sanitize=sanitize, removeHs=removeHs, strictParsing=strictParsing) return [[list(xx) for xx in x] for x in _apply(mols, Chem.GetSymmSSSR)] #-----------------------------------------------------------------------------------------------------------------------
def _inchi2svg(inchis, size, legend, kekulize=True, wedgeBonds=True, fitImage=True, atomMapNumber=False, computeCoords=False): mols = _apply(inchis.split(), Chem.MolFromInchi) _apply(mols, _computeCoords) return _mols2svg(mols, size, legend, kekulize=kekulize, wedgeBonds=wedgeBonds, fitImage=fitImage, atomMapNumber=atomMapNumber, computeCoords=computeCoords)
def _addHs(data, explicitOnly=False, addCoords=False): mols = _parseMolData(data, sanitize=True, removeHs=False, strictParsing=True) mols = _apply(mols, Chem.AddHs, explicitOnly=explicitOnly, addCoords=addCoords) return _getSDFString(mols)
def _mols2svg(mols, size, legend, kekulize=True, wedgeBonds=True, fitImage=True, atomMapNumber=False, computeCoords=False): if computeCoords: _apply(mols, _computeCoords, True) if atomMapNumber: _apply(mols, _atomMapNumber) molsPerRow = min(len(mols), 4) totalWidth = molsPerRow * size totalHeight = molsPerRow * size if cffi and cairocffi.version <= (1, 10, 0): imageData = io.BytesIO() else: imageData = StringIO.StringIO() surf = cairo.SVGSurface(imageData, totalWidth, totalHeight) ctx = cairo.Context(surf) for i, mol in enumerate(mols): tx = size * (i % molsPerRow) ty = size * (i // molsPerRow) ctx.translate(tx, ty) canv = cairoCanvas.Canvas(ctx=ctx, size=(size, size), imageType='svg') leg = mol.GetProp("_Name") if (mol.HasProp("_Name") and mol.GetProp("_Name")) else legend draw.MolToImage(mol, size=(size, size), legend=leg, canvas=canv, kekulize=kekulize, wedgeBonds=wedgeBonds, fitImage=fitImage) canv.flush() ctx.translate(-tx, -ty) surf.finish() return imageData.getvalue()
def _neutralise(data, balance, sanitize=True, removeHs=True, strictParsing=True): mols = _parseMolData(data, sanitize=sanitize, removeHs=removeHs, strictParsing=strictParsing) res = _apply(mols, neutralise, balance) return _getSDFString(res)
def _similarityMap(ms, width=500, height=500, radius=2, fingerprint='morgan', format='png'): if matplotlib is None: raise ValueError('matplotlib not useable') _call(ms, 'UpdatePropertyCache', strict=False) _apply(ms, ct._sssr) fn = None if fingerprint == 'morgan': fn = lambda x, i: SimilarityMaps.GetMorganFingerprint(x, i, radius=radius) elif fingerprint == 'tt': fn = SimilarityMaps.GetAPFingerprint elif fingerprint == 'ap': fn = SimilarityMaps.GetTTFingerprint SimilarityMaps.GetSimilarityMapForFingerprint(ms[0], ms[1], fn, size=(width, height)) sio = StringIO.StringIO() pyplot.savefig(sio, format=format, bbox_inches='tight', dpi=100) return sio.getvalue()
def _getDescriptors(data, ds, sanitize=True, removeHs=True, strictParsing=True): if ds: ds = ds.split(',') return _apply( _parseMolData(data, sanitize=sanitize, removeHs=removeHs, strictParsing=strictParsing), _desc_list, ds)
def _ctab23D(data, multi, mmff, sanitize=True, removeHs=True, strictParsing=True): mols = _parseMolData(data, sanitize=sanitize, removeHs=removeHs, strictParsing=strictParsing) optimisedMols = _apply(mols, _2D23D, multi, mmff) return _getSDFString(optimisedMols)
def _molExport(structure, **kwargs): input_f = kwargs.get('input', None) output_f = kwargs.get('output', None) if not input_f: mol = _autoDetect(str(structure)) elif input_f == 'mrv': mol = Chem.MolFromMolBlock(MarvinToMol(structure), sanitize=False) elif input_f == 'smiles': mol = Chem.MolFromSmiles(str(structure), sanitize=False) elif input_f == 'inchi': mol = Chem.MolFromInchi(str(structure)) else: mol = Chem.MolFromMolBlock(structure, sanitize=False) _call([mol], 'UpdatePropertyCache', strict=False) _apply([mol], ct._sssr) if not mol.GetNumConformers() or mol.GetConformer().Is3D(): AllChem.Compute2DCoords(mol, bondLength=0.8) if output_f == 'smiles': out_structure = Chem.MolToSmiles(mol) elif output_f == 'inchi': out_structure = Chem.MolToInchi(mol) elif output_f == 'inchikey': out_structure = Chem.InchiToInchiKey(Chem.MolToInchi(mol)) elif output_f == 'mol': out_structure = Chem.MolToMolBlock(mol) elif output_f == 'sdf': out_structure = Chem.MolToMolBlock(mol) + '\n$$$$\n' elif output_f == 'mrv': out_structure = MolToMarvin(Chem.MolToMolBlock(mol)) return {"structure": out_structure, "format": output_f, "contentUrl": "", "contentBaseUrl": ""}
def _autoDetect(structure): mol = None if Chem.MolFromSmiles(structure.strip(), sanitize=False): mol = Chem.MolFromSmiles(structure.strip(), sanitize=False) elif Chem.MolFromMolBlock(structure, sanitize=False): return Chem.MolFromMolBlock(structure, sanitize=False) elif Chem.INCHI_AVAILABLE and Chem.inchi.MolFromInchi( structure.strip(), sanitize=True, removeHs=True): mol = Chem.inchi.MolFromInchi(structure.strip(), sanitize=True, removeHs=True) elif hasattr(Chem, 'MolFromSmarts') and Chem.MolFromSmarts(structure): mol = Chem.MolFromSmarts(structure) elif hasattr(Chem, 'MolFromMol2Block') and Chem.MolFromMol2Block(structure): mol = Chem.MolFromMol2Block(structure) elif hasattr(Chem, 'MolFromPDBBlock') and Chem.MolFromPDBBlock(structure): mol = Chem.MolFromPDBBlock(structure) elif hasattr(Chem, 'MolFromTPLBlock') and Chem.MolFromTPLBlock(structure): mol = Chem.MolFromTPLBlock(structure) else: try: mol = Chem.MolFromMolBlock(MarvinToMol(structure)) except: pass _call([mol], 'UpdatePropertyCache', strict=False) _apply([mol], ct._sssr) return mol
def _mols2svg(mols, size, legend): _apply(mols, _computeCoords) molsPerRow = min(len(mols), 4) totalWidth = molsPerRow * size totalHeight = molsPerRow * size if cffi and cairocffi.version <= (1, 10, 0): imageData = io.BytesIO() else: imageData = StringIO.StringIO() surf = cairo.SVGSurface(imageData, totalWidth, totalHeight) ctx = cairo.Context(surf) for i, mol in enumerate(mols): tx = size * (i % molsPerRow) ty = size * (i // molsPerRow) ctx.translate(tx, ty) canv = cairoCanvas.Canvas(ctx=ctx, size=(size, size), imageType='svg') leg = mol.GetProp("_Name") if mol.HasProp("_Name") else legend draw.MolToImage(mol, size=(size, size), legend=leg, canvas=canv) canv.flush() ctx.translate(-tx, -ty) surf.finish() return imageData.getvalue()
def _mols2svg(mols,size,legend): _apply(mols, _computeCoords) molsPerRow=min(len(mols),4) totalWidth=molsPerRow*size totalHeight=molsPerRow*size if cffi and cairocffi.version <= (1,10,0) : imageData = io.BytesIO() else: imageData = StringIO.StringIO() surf = cairo.SVGSurface(imageData,totalWidth,totalHeight) ctx = cairo.Context(surf) for i, mol in enumerate(mols): tx = size*(i%molsPerRow) ty = size*(i//molsPerRow) ctx.translate(tx, ty) canv = cairoCanvas.Canvas(ctx=ctx, size=(size,size), imageType='svg') leg = mol.GetProp("_Name") if mol.HasProp("_Name") else legend draw.MolToImage(mol, size=(size,size), legend=leg, canvas=canv) canv.flush() ctx.translate(-tx, -ty) surf.finish() return imageData.getvalue()
def _smiles2inchi(data, computeCoords=False, delimiter=' ', smilesColumn=0, nameColumn=1, titleLine=True, sanitize=True): return '\n'.join( _apply( _parseSMILESData(data, computeCoords=computeCoords, delimiter=delimiter, smilesColumn=smilesColumn, nameColumn=nameColumn, titleLine=titleLine, sanitize=sanitize), Chem.MolToInchi))
def _smiles23D(data, multi, mmff, computeCoords=False, delimiter=' ', smilesColumn=0, nameColumn=1, titleLine=True, sanitize=True): mols = _parseSMILESData(data, computeCoords=computeCoords, delimiter=delimiter, smilesColumn=smilesColumn, nameColumn=nameColumn, titleLine=titleLine, sanitize=sanitize) optimisedMols = _apply(mols, _2D23D, multi, mmff) return _getSDFString(optimisedMols)
def _mols2svg(mols, size, legend, kekulize=True, wedgeBonds=True, fitImage=True, atomMapNumber=False, computeCoords=False, highlightAtomLists=None): if not mols: return '' _call(mols, 'UpdatePropertyCache', strict=False) if computeCoords: _apply(mols, _computeCoords, True) if atomMapNumber: _apply(mols, _atomMapNumber) labels = [x for x in islice(cycle(legend), len(mols))] if isinstance(legend, (list, tuple)) else \ [x for x in islice(cycle([legend]), len(mols))] legends = [x.GetProp("_Name") if (x.HasProp("_Name") and x.GetProp("_Name")) else labels[idx] for idx, x in enumerate(mols)] molsPerRow = min(len(mols), 4) nRows = len(mols) // molsPerRow if len(mols) % molsPerRow: nRows += 1 if kekulize: _apply(mols, _kekulize) highlightBondLists = [] if highlightAtomLists: for mol, highlightAtomList in zip(mols, highlightAtomLists): highlightBondList = [] for bnd in mol.GetBonds(): if bnd.GetBeginAtomIdx() in highlightAtomList and bnd.GetEndAtomIdx() in highlightAtomList: highlightBondList.append(bnd.GetIdx()) highlightBondLists.append(highlightBondList) if not highlightBondLists: highlightBondLists = None panelx = size panely = size canvasx = panelx * molsPerRow canvasy = panely * nRows drawer = rdMolDraw2D.MolDraw2DSVG(canvasx, canvasy, panelx, panely) drawer.DrawMolecules(mols, highlightAtoms=highlightAtomLists, highlightBonds=highlightBondLists, legends=legends) drawer.FinishDrawing() return drawer.GetDrawingText()
def _align(mols, pattern, force=False): if all(mol.HasSubstructMatch(pattern) for mol in mols): _apply(mols, GenerateDepictionMatching2DStructure, pattern) return mols if not force: return pattern = _adjustQuery(pattern) matches = _call(mols, 'HasSubstructMatch', pattern) if all(matches): _apply(mols, GenerateDepictionMatching2DStructure, pattern) return mols if any(matches): _apply(mols, GenerateDepictionMatching2DStructure, pattern, acceptFailure=True) return compress(mols, matches)
def _rules(data): mols = _parseMolData(data) res = _apply(mols, rules) return _getSDFString(res)
def _neutralise(data, balance): mols = _parseMolData(data) res = _apply(mols, neutralise, balance) return _getSDFString(res)
def _ctab23D(data, multi, mmff): mols = _parseMolData(data) optimisedMols = _apply(mols, _2D23D, multi, mmff) return _getSDFString(optimisedMols)
def _smiles23D(data, multi, mmff): mols = _parseSMILESData(data) optimisedMols = _apply(mols, _2D23D, multi, mmff) return _getSDFString(optimisedMols)
def _unsalt(data): mols = _parseMolData(data) res = _apply(mols, unsalt) return _getSDFString(res)
def _smiles2inchi(data, computeCoords=False, delimiter=' ', smilesColumn=0, nameColumn=1, titleLine=True, sanitize=True): return '\n'.join(_apply(_parseSMILESData(data, computeCoords=computeCoords, delimiter=delimiter, smilesColumn=smilesColumn, nameColumn=nameColumn, titleLine=titleLine, sanitize=sanitize), Chem.MolToInchi))
def _ctab2inchi(data, sanitize=True, removeHs=True, strictParsing=True): return '\n'.join(_apply(_parseMolData(data, sanitize=sanitize, removeHs=removeHs, strictParsing=strictParsing), Chem.MolToInchi))
def _inchi2ctab(inchis): mols = _apply(inchis.split(),Chem.MolFromInchi) _apply(mols, _computeCoords) return _getSDFString(mols)