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 _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 _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 _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 _isBondInRing(data, index, size, sanitize=True, removeHs=True, strictParsing=True): mols = _parseMolData(data, sanitize=sanitize, removeHs=removeHs, strictParsing=strictParsing) return _call(_call(mols, 'GetRingInfo'), 'IsBondInRingOfSize', index, size)
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 _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 _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 _numBondRings(data, sanitize=True, removeHs=True, strictParsing=True): mols = _parseMolData(data, sanitize=sanitize, removeHs=removeHs, strictParsing=strictParsing) ring_infos = _call(mols, 'GetRingInfo') return [[ring_info.NumBondRings(bond.GetIdx()) for bond in mol.GetBonds()] for (mol, ring_info) in zip(mols, ring_infos)]
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 _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 _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 _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 _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 _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 _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 _getMatches(mols, smarts, force=False): _call(mols, 'UpdatePropertyCache', strict=False) return _apply(mols, _getSubstructMatch, _molFromSmarts(smarts), force)
def _numRings(data, sanitize=True, removeHs=True, strictParsing=True): mols = _parseMolData(data, sanitize=sanitize, removeHs=removeHs, strictParsing=strictParsing) return _call(_call(mols, 'GetRingInfo'), 'NumRings')
def _getNumBonds(data, sanitize=True, removeHs=True, strictParsing=True): return _call(_parseMolData(data, sanitize=sanitize, removeHs=removeHs, strictParsing=strictParsing), "GetNumBonds")
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 _getNumAtoms(data): return _call(_parseMolData(data), "GetNumAtoms")
def _getMatches(mols, smarts, force=False): _call(mols, 'UpdatePropertyCache', strict=False) return _apply(mols, _getSubstructMatch, _molFromSmarts(smarts), force)
def _getNumAtoms(data): return _call(_parseMolData(data), "GetNumAtoms")
def _numBondRings(data, sanitize=True, removeHs=True, strictParsing=True): mols = _parseMolData(data, sanitize=sanitize, removeHs=removeHs, strictParsing=strictParsing) ring_infos = _call(mols, 'GetRingInfo') return [[ring_info.NumBondRings(bond.GetIdx()) for bond in mol.GetBonds()] for (mol, ring_info) in zip(mols, ring_infos)]
def _getNumBonds(data, sanitize=True, removeHs=True, strictParsing=True): return _call( _parseMolData(data, sanitize=sanitize, removeHs=removeHs, strictParsing=strictParsing), "GetNumBonds")
def _numRings(data, sanitize=True, removeHs=True, strictParsing=True): mols = _parseMolData(data, sanitize=sanitize, removeHs=removeHs, strictParsing=strictParsing) return _call(_call(mols, 'GetRingInfo'), 'NumRings')
def _isBondInRing(data, index, size, sanitize=True, removeHs=True, strictParsing=True): mols = _parseMolData(data, sanitize=sanitize, removeHs=removeHs, strictParsing=strictParsing) return _call(_call(mols, 'GetRingInfo'), 'IsBondInRingOfSize', index, size)
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 NEW_RENDER_ENGINE: 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() 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): highlight = [] if highlightAtomLists: highlight = highlightAtomLists[i] or [] tx = size * (i % molsPerRow) ty = size * (i // molsPerRow) ctx.translate(tx, ty) canv = cairoCanvas.Canvas(ctx=ctx, size=(size, size), imageType='svg') draw.MolToImage(mol, size=(size, size), legend=legends[i], canvas=canv, kekulize=kekulize, wedgeBonds=wedgeBonds, fitImage=fitImage, highlightAtoms=highlight) canv.flush() ctx.translate(-tx, -ty) surf.finish() return imageData.getvalue()