def render(self, mode="human"): """ Generates a 3D rendering of the current molecule in the environment. Parameters ------- mode: string Just a way of indicating whether the rendering is mainly for humans vs machines in OpenAI """ if self.check_valency() and self.check_chemical_validity(): if not self.pymol_window_flag: self.start_pymol() molecule = self.mol self.mol.UpdatePropertyCache( strict=False) # Update valence information FastFindRings( self.mol ) # Quick for finding out if an atom is in a ring, use Chem.GetSymmSSR() if more reliability is desired Chem.AddHs( molecule) # Add explicit hydrogen atoms for rendering purposes # remove stereochemistry information rdmolops.RemoveStereochemistry(molecule) # Generate 3D structure AllChem.EmbedMolecule(molecule) AllChem.MMFFOptimizeMolecule(molecule) v = MolViewer() v.ShowMol(molecule) v.GetPNG(h=400) # Save the rendering in pse format pymol.cmd.save("./pymol_renderings/" + Chem.MolToSmiles(molecule) + ".pse", format="pse") Chem.RemoveHs(molecule) # Remove explicit hydrogen else: print( "The molecule is not chemically valid, and rendering has been terminated." )
parser.add_option('--verbose', default=False, action='store_true', help='be verbose') if __name__ == '__main__': from rdkit import Chem from rdkit.Chem import AllChem from rdkit.Chem.PyMol import MolViewer options, args = parser.parse_args() if len(args) < 1: parser.error('please provide either at least one sd or mol file') try: v = MolViewer() except Exception: logger.error( 'Unable to connect to PyMol server.\nPlease run ~landrgr1/extern/PyMol/launch.sh to start it.' ) sys.exit(1) if options.clearAll: v.DeleteAll() try: fdef = open(options.fdef, 'r').read() except IOError: logger.error('ERROR: Could not open fdef file %s' % options.fdef) sys.exit(1) factory = AllChem.BuildFeatureFactoryFromString(fdef)
print 'm1:' s1 = b.GenerateSubshapeShape(m1) cPickle.dump(s1,file('test_data/square1.shp.pkl','wb+')) print 'm2:' s2 = b.GenerateSubshapeShape(m2) cPickle.dump(s2,file('test_data/square2.shp.pkl','wb+')) ns1 = b.CombineSubshapes(s1,s2) b.GenerateSubshapeSkeleton(ns1) cPickle.dump(ns1,file('test_data/combined.shp.pkl','wb+')) else: s1 = cPickle.load(file('test_data/square1.shp.pkl','rb')) s2 = cPickle.load(file('test_data/square2.shp.pkl','rb')) #ns1 = cPickle.load(file('test_data/combined.shp.pkl','rb')) ns1=cPickle.load(file('test_data/combined.shp.pkl','rb')) v = MolViewer() SubshapeObjects.DisplaySubshape(v,s1,'shape1') SubshapeObjects.DisplaySubshape(v,ns1,'ns1') #SubshapeObjects.DisplaySubshape(v,s2,'shape2') a = SubshapeAligner.SubshapeAligner() pruneStats={} algs =a.GetSubshapeAlignments(None,ns1,m1,s1,b,pruneStats=pruneStats) print len(algs) print pruneStats import os,tempfile from rdkit import Geometry fName = tempfile.mktemp('.grd') Geometry.WriteGridToFile(ns1.coarseGrid.grid,fName) v.server.loadSurface(fName,'coarse','',2.5)
from rdkit import Chem from rdkit.Chem.PyMol import MolViewer from rdkit.Chem import AllChem import gzip try: v = MolViewer() v.DeleteAll() except Exception: v = None outf = open('chembl_20_chiral.problems.smi', 'w+') noconff = open('chembl_20_chiral.noconfs.smi', 'w+') for i, line in enumerate(gzip.open('../Data/chembl_20_chiral.smi.gz')): line = line.strip().decode().split(' ') mol = Chem.MolFromSmiles(line[0]) if not mol: continue cents = Chem.FindMolChiralCenters(mol, includeUnassigned=True) if len([y for x, y in cents if y == '?']): continue nm = line[1] csmi = Chem.MolToSmiles(mol, True) for j in range(100): mh = Chem.AddHs(mol) ok = AllChem.EmbedMolecule(mh, randomSeed=j + 1) if ok >= 0: Chem.AssignAtomChiralTagsFromStructure(mh) newm = Chem.RemoveHs(mh) smi = Chem.MolToSmiles(newm, True) if smi != csmi:
from rdkit.Chem.PyMol import MolViewer #cmpd = Chem.MolFromSmiles('CCCc1cc(C(=O)O)ccc1') #cmpd = Chem.AddHs(cmpd) if 1: cmpd = Chem.MolFromSmiles('C1=CC=C1C#CC1=CC=C1') cmpd = Chem.AddHs(cmpd) AllChem.EmbedMolecule(cmpd) AllChem.UFFOptimizeMolecule(cmpd) AllChem.CanonicalizeMol(cmpd) print >> file('testmol.mol', 'w+'), Chem.MolToMolBlock(cmpd) else: cmpd = Chem.MolFromMolFile('testmol.mol') builder = SubshapeBuilder() if 1: shape = builder.GenerateSubshapeShape(cmpd) v = MolViewer() if 1: import tempfile tmpFile = tempfile.mktemp('.grd') v.server.deleteAll() Geometry.WriteGridToFile(shape.grid, tmpFile) time.sleep(1) v.ShowMol(cmpd, name='testMol', showOnly=True) v.server.loadSurface(tmpFile, 'testGrid', '', 2.5) v.server.resetCGO('*') cPickle.dump(shape, file('subshape.pkl', 'w+')) for i, pt in enumerate(shape.skelPts): v.server.sphere(tuple(pt.location), .5, (1, 0, 1), 'Pt-%d' % i) if not hasattr(pt, 'shapeDirs'): continue momBeg = pt.location - pt.shapeDirs[0]
targetConf=tgtConf, queryConf=queryConf, pruneStats=pruneStats): continue # if we made it this far, it's a good alignment yield alignment if __name__ == '__main__': from rdkit.six.moves import cPickle tgtMol, tgtShape = cPickle.load(file('target.pkl', 'rb')) queryMol, queryShape = cPickle.load(file('query.pkl', 'rb')) builder = cPickle.load(file('builder.pkl', 'rb')) aligner = SubshapeAligner() algs = aligner.GetSubshapeAlignments(tgtMol, tgtShape, queryMol, queryShape, builder) print(len(algs)) from rdkit.Chem.PyMol import MolViewer v = MolViewer() v.ShowMol(tgtMol, name='Target', showOnly=True) v.ShowMol(queryMol, name='Query', showOnly=False) SubshapeObjects.DisplaySubshape(v, tgtShape, 'target_shape', color=(.8, .2, .2)) SubshapeObjects.DisplaySubshape(v, queryShape, 'query_shape', color=(.2, .2, .8))