def generate_mol_svg(smiles, name=False, path="fragments", ext="svg", size=(100, 50), substructure=False): molpath = os.path.join("static", "img", path) if not os.path.exists(molpath): os.mkdir(molpath) smiles_path = re.sub("/", "", smiles) smiles_path = re.sub("#", "hash", smiles_path) filepath = os.path.join(molpath, "{}.{}".format(name if name else smiles_path, ext)) try: mol = Chem.MolFromSmiles(smiles) if substructure: highlight_obj = Chem.MolFromSmiles(substructure) AllChem.Compute2DCoords(highlight_obj) AllChem.GenerateDepictionMatching2DStructure(mol, highlight_obj) matching = mol.GetSubstructMatch(highlight_obj) Draw.MolToFile(mol, filepath, size=size, highlightAtoms=matching) else: Draw.MolToFile(mol, filepath, size=size) except Exception as e: print(smiles, e)
def _toSVG(mol): if not ipython_useSVG: return None if hasattr(mol, '__sssAtoms'): highlightAtoms = mol.__sssAtoms else: highlightAtoms = [] try: mol.GetAtomWithIdx(0).GetExplicitValence() except RuntimeError: mol.UpdatePropertyCache(False) mc = copy.deepcopy(mol) sio = StringIO() try: Draw.MolToFile(mc, sio, size=molSize, imageType="svg", kekulize=kekulizeStructures, highlightAtoms=highlightAtoms) except ValueError: # <- can happen on a kekulization failure mc = copy.deepcopy(mol) Draw.MolToFile(mc, sio, size=molSize, kekulize=False, highlightAtoms=highlightAtoms, imageType="svg") # It seems that the svg renderer used doesn't quite hit the spec. # Here are some fixes to make it work in the notebook, although I think # the underlying issue needs to be resolved at the generation step svg_split = sio.getvalue().replace("svg:", "").splitlines() header = ('<svg version="1.1" xmlns="http://www.w3.org/2000/svg"' 'width="%dpx" height="%dpx">') % molSize svg = "\n".join(svg_split[:1] + [header] + svg_split[5:]) return svg
def generate_all_images(X_train, y_train, X_test, y_test, img_dir="../data/smile_images", target_size=(224, 224)): print("Generating all molecule images") X_test = X_test.reset_index(drop=True) y_test = y_test.reset_index(drop=True) X_train = X_train.reset_index(drop=True) y_train = y_train.reset_index(drop=True) try: os.makedirs(os.path.join(img_dir, "train", "0")) except: pass try: os.makedirs(os.path.join(img_dir, "train", "1")) except: pass try: os.makedirs(os.path.join(img_dir, "test", "0")) except: pass try: os.makedirs(os.path.join(img_dir, "test", "1")) except: pass for idx, smile_str in enumerate(X_train): if idx % 1000 == 0: print("Generating img {}".format(idx)) molsmile = MolFromSmiles(smile_str) prop_P1 = y_train[idx] Draw.MolToFile(molsmile, '{}/train/{}/{}.png'.format(img_dir, prop_P1, smile_str.replace("/", "-")), size = target_size) for idx, smile_str in enumerate(X_test): if idx % 1000 == 0: print(idx) molsmile = MolFromSmiles(smile_str) prop_P1 = y_test[idx] Draw.MolToFile(molsmile, '{}/test/{}/{}.png'.format(img_dir, prop_P1, smile_str.replace("/", "-")), size = target_size) print("Finished creating images")
def img_distance_eval(args, smis, transform): """ Edit distance between two images of generated molecules: """ scores = [] for reference_smi in smis: cur_scores = [] for candidate_smi in smis: if reference_smi != candidate_smi: try: m = Chem.MolFromSmiles(reference_smi) Draw.MolToFile(m, "tmp.png", size=(args.img_size, args.img_size)) reference_img = load_img(args, "tmp.png", transform) m = Chem.MolFromSmiles(candidate_smi) Draw.MolToFile(m, "tmp.png", size=(args.img_size, args.img_size)) candidate_img = load_img(args, "tmp.png", transform) cur_scores.append( math.log( torch.sum(torch.abs(reference_img - candidate_img)))) except: pass if len(cur_scores) == 0: cur_scores.append(0) scores.append(np.mean(cur_scores)) return round(np.mean(scores), 4)
def sample_subgraphs(smiles, num_samples=10, frac=0.5, vis_dir=None): mol = Chem.MolFromSmiles(smiles) clusters, atom_cls = find_clusters(mol) cluster_sizes = [len(cls) for cls in clusters] p = np.array(cluster_sizes).astype('float') p /= p.sum() selected_atoms_list = [] for n in range(num_samples): selected_clusters = np.random.choice(len(clusters), int(frac * len(clusters)), p=p, replace=False) selected_atoms = set() for i in selected_clusters: for j in clusters[i]: selected_atoms.add(j) minimum_smiles, _ = extract_subgraph(smiles, selected_atoms) selected_atoms_list.append(selected_atoms) if vis_dir is not None: png_f = f'subgraph_{n}.png' Draw.MolToFile(mol, filename=os.path.join(vis_dir, png_f), highlightAtoms=selected_atoms) png_f = f'subgraph_{n}_extracted.png' print(minimum_smiles) Draw.MolToFile(Chem.MolFromSmiles(minimum_smiles), filename=os.path.join(vis_dir, png_f)) return selected_atoms_list
def processUnit(iMol,start,i,batch_size,count,tracker,adj_len,full_size): size = (120, 120) tmp = rdkit.Chem.rdmolfiles.MolFragmentToSmiles(iMol, atomsToUse=start) tmp1=tmp j=0 full_size=full_size start=start while(rdkit.Chem.rdmolfiles.MolFromSmiles(tmp)==None and len(tmp)!=0): j=j+1 if(full_size>=3): full_size=full_size-j start = maxSum(tracker,adj_len,full_size) else: fig = Draw.MolToFile(iMol, "./amesfirstmodImg4bay/" + str(i * batch_size + count) + '.png', size=size, highlightAtoms=start) return max(tmp1.split('.'), key=len) if(len(start)>0): tmp = rdkit.Chem.rdmolfiles.MolFragmentToSmiles(iMol, atomsToUse=start) #print(tmp) else: fig = Draw.MolToFile(iMol, "./amesfirstmodImg4bay/" + str(i * batch_size + count) + '.png', size=size, highlightAtoms=start) return max(tmp1.split('.'), key=len) fig = Draw.MolToFile(iMol, "./amesfirstmodImg4bay/" + str(i * batch_size + count) + '.png', size=size, highlightAtoms=start) return max(tmp.split('.'), key=len)
def render_smarts(data): smarts_col_idx = [j for j in range(len(data[0])) if data[0][j]=="SMARTS"][0] smiles_col_idx_1 = [j for j in range(len(data[0])) if data[0][j]=="SMILES_1"][0] smiles_col_idx_2 = [j for j in range(len(data[0])) if data[0][j]=="SMILES_2"][0] for i, row in enumerate(data): if i==0: data[i].append("PRODUCT") data[i].append("SMILES_1 IMG") data[i].append("SMILES_2 IMG") data[i].append("PRODUCT IMG") continue try: smarts_str = data[i][smarts_col_idx] smiles_str_1 = data[i][smiles_col_idx_1] smiles_str_2 = data[i][smiles_col_idx_2] rxn = AllChem.ReactionFromSmarts(smarts_str) ps = rxn.RunReactants((Chem.MolFromSmiles(smiles_str_1), Chem.MolFromSmiles(smiles_str_2))) product = ps[0][0] product_str = Chem.MolToSmiles(product) data[i].append(product_str) AllChem.Compute2DCoords(product) product_fn = 'product_%d.png' % i product_img = os.path.join(UPLOAD_DIR, product_fn) Draw.MolToFile(product, product_img) smiles_1 = Chem.MolFromSmiles(smiles_str_1) AllChem.Compute2DCoords(smiles_1) smiles_1_fn = 'smiles_1_%d.png' % i smiles_1_img = os.path.join(UPLOAD_DIR, smiles_1_fn) Draw.MolToFile(smiles_1, smiles_1_img) smiles_2 = Chem.MolFromSmiles(smiles_str_2) AllChem.Compute2DCoords(smiles_2) smiles_2_fn = 'smiles_2_%d.png' % i smiles_2_img = os.path.join(UPLOAD_DIR, smiles_2_fn) Draw.MolToFile(smiles_2, smiles_2_img) data[i].append(url_for('static', filename='data/' + product_fn)) data[i].append(url_for('static', filename='data/' + smiles_1_fn)) data[i].append(url_for('static', filename='data/' + smiles_2_fn)) except Exception as e: print(e) data[i].append("Invalid") data[i].append("Invalid") data[i].append("Invalid") pass return data
def vis_partition(m: Mol, fnprefix='m'): cgs = RdFunc.get_conjugate_group_with_halogen(m) # cgs = RdFunc.get_conjugate_group(m) Draw.MolToFile(m, "{}.ps".format(fnprefix), kekulize=False) kk = 0 for cg in cgs: Chem.SanitizeMol(cg[0]) Draw.MolToFile( cg[0], "{}-{}.ps".format(fnprefix, kk), kekulize=False, ) kk += 1
def draw_2D_structures(): m = Chem.MolFromSmiles('C1=CC=CN=C1') m2 = Chem.AddHs(m) m4 = Chem.MolFromSmiles('OC1C2C1CC2') m5 = Chem.MolFromSmiles('c1nccc2n1ccc2') m_list = m, m2, m4, m5 # 2D; creates a png file with molecule in it Draw.MolToFile(m,'/home/stokm006/thesis/RDkit/images/molecule_m.png') Draw.MolToFile(m2,'/home/stokm006/thesis/RDkit/images/molecule_m2.png') Draw.MolToFile(m4,'/home/stokm006/thesis/RDkit/images/molecule_m4.png') Draw.MolToFile(m5,'/home/stokm006/thesis/RDkit/images/molecule_m5.png') # multiple molecules multiple_molecules = Draw.MolsToGridImage(m_list,molsPerRow=2,subImgSize=(100,100)) multiple_molecules.save('/home/stokm006/thesis/images/multiple_molecules.png')
def convertsmiles(): t_smiles.set('') t_sol.set('') t_lip.set('') t_sasc.set('') molecule = pcp.get_compounds(t_name.get(), 'name') print('molecule') print(molecule[0]) #print('canocical_smile', molecule[0].canonical_smiles) print('isomeric_smile', molecule[0].isomeric_smiles) mol_canonical_smiles = molecule[0].canonical_smiles mol_isomeric_smiles = molecule[0].isomeric_smiles t_smiles.set(mol_isomeric_smiles) mol_ = Chem.MolFromSmiles(mol_isomeric_smiles) Draw.MolToFile(mol_, 'tmp.png') global image_ image_open = Image.open('tmp.png') image_ = ImageTk.PhotoImage(image_open, master=frame1) canvas.create_image(150, 75, image=image_)
def write_image(self,path): # Convert to mol object, write, return success status. mol = Chem.MolFromSmiles(self.smiles) if mol is None: return False Draw.MolToFile(mol,path) return True
def mols_to_pngs(mols, basename="test"): filenames = [] for i, mol in enumerate(mols): filename = "%s%d.png" % (basename, i) Draw.MolToFile(mol, filename) filenames.append(filename) return filenames
def similar_mols(self): # Collecting text from a inputString molecule = self.inputString.toPlainText() # if Input String in InChI, change to SMILES if self.inputopt == 'InChI': mol_ = Chem.inchi.MolFromInchi(molecule) molecule = Chem.MolToSmiles(mol_) # Create similar molecules, fetch image and data self.sim_mols_inchi, self.sim_mols_smiles, self.similar_mols = create_similar_mols_( molecule, self.model_vae, self.charset, self.stdev) print(self.sim_mols_smiles) for index, mol in enumerate( self.similar_mols): # Creating images molecules Draw.MolToFile(mol, "temp.svg", size=(800, 800)) cairosvg.svg2png(url='./temp.svg', write_to='sim_mol' + str(index) + '.png') # Setting images for similar moleculew similar_mols = [ self.similarMol_1, self.similarMol_2, self.similarMol_3, self.similarMol_4, self.similarMol_5, self.similarMol_6, self.similarMol_7, self.similarMol_8, self.similarMol_9 ] for index, mol in enumerate(similar_mols): mol.setPixmap(QPixmap('sim_mol' + str(index) + '.png')) self.setImagesClick() print('similar_mols')
def generate_image(): #Retrieving the value of user's input and convert it to SMILES origmolName = textbox.get() nameToSmiles = SmilesFromName(textbox.get()) # Draw molecule and save the image try: m = Chem.MolFromSmiles(nameToSmiles) fig = Draw.MolToFile(m, 'images/' + origmolName + '.png') except ValueError: raise tkMessageBox.showinfo( "Invalid Input", "The molecule that you are searching is not yet available in the library" ) # Display the result result_title = tk.Label(root, text="Result", font=("Helvetica", 16)) result_title.grid(row=1, sticky="n", pady=45) load = Image.open("images/" + origmolName + ".png") render = ImageTk.PhotoImage(load) img = tk.Label(root, image=render) img.image = render img.place(x=150, y=170) molName = tk.Label(root, text=origmolName) molName.grid(pady=15)
def draw_rdmol(rdmol, name): print(name) # rdmol = AddHs(rdmol) AllChem.Compute2DCoords(rdmol) Draw.MolToFile(rdmol, '{}.png'.format(name)) AllChem.EmbedMolecule(rdmol) print(Chem.MolToMolBlock(rdmol), file=open('{}.mol'.format(name), 'w+'))
def get_pict(molec): mol = Chem.MolFromMolFile(molec) AllChem.Compute2DCoords(mol) DrawingOptions.atomLabelFontSize = 55 DrawingOptions.dotsPerAngstrom = 100 DrawingOptions.bondLineWidth = 3.0 Draw.MolToFile(mol, "molecule.png")
def draw_structure(smiles_str, image_path, image_size=500): """ Draw structure for the compound with the given SMILES string as a PNG file. Note that there are more flexible functions for drawing structures in the rdkit_easy module. This function is only retained for backward compatibility. Args: smiles_str (str): SMILES representation of compound. image_path (str): Filepath for image file to be generated. image_size (int): Width of square bounding box for image. Returns: None. """ mol = Chem.MolFromSmiles(smiles_str) if mol is None: print(("Unable to read original SMILES for %s" % cmpd_num)) else: _discard = AllChem.Compute2DCoords(mol) Draw.MolToFile(mol, image_path, size=(image_size, image_size), fitImage=False)
def gen_viz(edges, splines='none'): ''' Given a list of edges of a retrosynthetic tree, generates a graph visualization ''' unismi = set([]) img_map = {} for edge in edges: unismi.add(edge[0]) unismi.add(edge[1]) for i in unismi: mol = Chem.MolFromSmiles(i) j = i.replace('/', '%2F') Draw.MolToFile(mol, j + '.png') img_map[i] = j + '.png' if splines == 'ortho': graph = graphviz.Digraph(format='png', graph_attr={'splines': splines}) else: graph = graphviz.Digraph(format='png') parents = [] for edge in edges: graph.node(edge[0], label='', image=img_map[edge[0]], shape='rect', style='rounded') graph.node(edge[1], label='', image=img_map[edge[1]], shape='rect', style='rounded') graph.node(f'd{edge[0]}', label='', width='0', height='0') if edge[0] not in parents: parents.append(edge[0]) graph.edge(edge[0], f'd{edge[0]}', arrowhead='none') graph.edge(f'd{edge[0]}', edge[1]) return img_map, graph
def showMolecule(self, smile): m = Chem.MolFromSmiles(smile) Draw.MolToFile(m, fileName='im.png', size=(300, 300)) pixmap = QtGui.QPixmap('im.png') #scaledpixmap = pixmap.scaled(self.label.size()) self.label_4.setPixmap(pixmap)
def removeBond(mol): Chem.Kekulize(mol, clearAromaticFlags=True) bonds = mol.GetBonds() if (len(bonds) > 0): r_bond = bonds[randint(0, len(bonds) - 1)] editable = Chem.rdchem.EditableMol(mol) editable.RemoveBond(r_bond.GetBeginAtomIdx(), r_bond.GetEndAtomIdx()) newMol = editable.GetMol() newMol.UpdatePropertyCache() newMol = Chem.AddHs(newMol) AllChem.Compute2DCoords(newMol) Chem.Kekulize(newMol) frags = Chem.rdmolops.GetMolFrags(newMol, sanitizeFrags=True, asMols=True) largest_frag = frags[0] for mol_frag in frags: if (mol_frag.GetNumAtoms() >= largest_frag.GetNumAtoms()): largest_frag = mol_frag largest_frag.UpdatePropertyCache() AllChem.Compute2DCoords(largest_frag) Chem.Kekulize(largest_frag) AllChem.EmbedMolecule(largest_frag) Draw.MolToFile(largest_frag, 'test.png') return largest_frag return mol
def addBond(mol): bondType = randint(1, 3) possible_id = [] for atom in mol.GetAtoms(): if (bondType in atom_dict[atom.GetSymbol()] and atom.GetImplicitValence() >= bondType): possible_id.append(atom.GetIdx()) #print(possible_id) if len(possible_id) > 0: index = randint(0, len(possible_id) - 1) #print(bondType) if len(possible_id) > 1: id1 = possible_id[index] del possible_id[index] id2 = possible_id[randint(0, len(possible_id) - 1)] editable = Chem.rdchem.EditableMol(mol) if mol.GetBondBetweenAtoms(id1, id2) is not None: editable.RemoveBond(id1, id2) editable.AddBond(id1, id2, bond_type[bondType - 1]) newMol = editable.GetMol() newMol.UpdatePropertyCache() newMol = Chem.AddHs(newMol) AllChem.Compute2DCoords(newMol) AllChem.EmbedMolecule(newMol) Draw.MolToFile(newMol, 'test.png') return newMol return mol return mol
def mol_plotter(): if not os.path.isdir(base_dir): os.mkdir(base_dir) if not os.path.isdir(IMAGE_DIR): os.makedirs(IMAGE_DIR) if not os.path.join(MOL_DIR): os.makedirs(MOL_DIR) drugs = Drug.objects.exclude(mol=None) for drug in drugs: image_name = '%s.svg' % drug.pk image_file = os.path.join(IMAGE_DIR, image_name) print(image_file) if not os.path.exists(image_file): Draw.MolToFile(drug.mol, image_file) img = File(open(image_file)) drug.mol_image.save(image_name, img) mol_file_name = '%s.mol' % drug.pk mol_file = os.path.join(MOL_DIR, mol_file_name) print(mol_file) if not os.path.exists(mol_file): f = open(mol_file, 'w') f.write(drug.mol_block) f.close() mol_f = File(open(mol_file)) drug.mol_file.save(mol_file_name, mol_f) drug.save()
def main(tuple_tree): smiles_to_draw = set() node_structure1, smiles = convert_tuple_tree_to_js(tuple_tree) smiles_to_draw.update(smiles) with open( path.join( OP_path, f'plot_{time.strftime("%y-%m-%d_%H:%M:%S", time.gmtime())}.html' ), 'w') as fo: fo.write(template_.substitute(node_structure=node_structure1)) # Plot images os.makedirs(path.join(OP_path, 'imgs'), exist_ok=True) for smi in smiles_to_draw: mol = Chem.MolFromSmiles(smi) inchi_key = Chem.MolToInchiKey(mol) op_path = path.join(OP_path, 'imgs', f"{inchi_key}.svg") print(f"Saving {smi} to {op_path}") Draw.MolToFile(mol, op_path, size=(200, 200), imageType="svg", useBWAtomPalette=True) print("Done!")
def makeClusterPictures(self, result_df, strcture=None, multi=None): dir_list = result_df['cluster'].unique() if strcture is None: strctureDf = pd.read_csv( - 'G:\\マイドライブ\Data\\tox_predict\\all_data\\structure_result.csv', engine='python') extAllDataDf = strctureDf[[ 'CAS', 'canonical_smiles' ]][strctureDf['CAS'].isin(strctureDf['CAS'].tolist())] result_df = pd.merge(result_df, extAllDataDf) try: os.makedirs('pics') except: pass os.chdir('.\\pics') for dir in dir_list: dir = str(dir) try: os.makedirs(dir) except: pass extract = zip(result_df['CAS'], result_df['canonical_smiles'], result_df['cluster']) for CAS, smiles, cluster in extract: try: m = Chem.MolFromSmiles(smiles) AllChem.Compute2DCoords(m) name = '.\\' + str(cluster) + '\\' + str(CAS) + '.png' #if str(tox_median) == 'nan': #print(name) Draw.MolToFile(m, name) except: # print("pass1") pass
def mol_plotter(): if not os.path.isdir(BASE__DIR): os.mkdir(BASE__DIR) if not os.path.isdir(IMAGE_DIR): os.makedirs(IMAGE_DIR) if not os.path.isdir(MOL_DIR): os.makedirs(MOL_DIR) compounds = Compound.objects.exclude(mol=None) for compound in compounds: image_name = '%s.svg' % compound.pk image_file = os.path.join(IMAGE_DIR, image_name) print image_file if not os.path.exists(image_file): Draw.MolToFile(compound.mol, image_file, size=(300, 300)) img = File(open(image_file)) compound.mol_image.save(image_name, img) mol_file_name = '%s.mol' % compound.pk mol_file = os.path.join(MOL_DIR, mol_file_name) print mol_file if not os.path.exists(mol_file): f = open(mol_file, 'w') f.write(compound.mol_block) f.close() mol_f = File(open(mol_file)) compound.mol_file.save(mol_file_name, mol_f) compound.save()
def draw_smiles(smiles_string, file_path, size=(1000, 1000)): try: molecule = Chem.MolFromSmiles(smiles_string) Draw.MolToFile(molecule, file_path, size=size) trim_image(file_path) except ValueError: print "ERROR: Molecule could not be drawn to file."
def visualize(self, filename=None, **kwargs): """ This function visualizes the molecule. If both rdkit and pybel objects are avaialble, the rdkit object will be used for visualization. Parameters ---------- filename: str, optional (default = None) This is the path to the file that you want write the image in it. Tkinter and Python Imaging Library are required for writing the image. kwargs: any extra parameter that you want to pass to the rdkit or pybel draw tool. Additional information at: - https://www.rdkit.org/docs/source/rdkit.Chem.Draw.html - http://openbabel.org/docs/dev/UseTheLibrary/Python_PybelAPI.html#pybel.Molecule.draw Returns ------- object You will be able to display this object, e.g., inside the Jupyter Notebook. """ engine = self._check_original_molecule() if engine == 'rdkit': from rdkit.Chem import Draw if filename is not None: Draw.MolToFile(self.rdkit_molecule, filename, **kwargs) else: return Draw.MolToImage(self.rdkit_molecule, **kwargs) elif engine == 'pybel': if filename is not None: self.pybel_molecule.draw(show=False, filename=filename, **kwargs) else: return self.pybel_molecule # it seems that the object alone is displayable
def mols_to_pngs(mols, basename="test"): """Helper to write RDKit mols to png files.""" filenames = [] for i, mol in enumerate(mols): filename = "BACE_%s%d.png" % (basename, i) Draw.MolToFile(mol, filename) filenames.append(filename) return filenames
def _testMolToFile(self): _, fn = tempfile.mkstemp(suffix='.png') try: self.assertEqual(os.path.getsize(fn), 0) Draw.MolToFile(self.mol, fn) self.assertNotEqual(os.path.getsize(fn), 0) finally: os.remove(fn)
def plotMol(pdb,filename): pdbName = reformatPDB(pdb,1) mol = Chem.MolFromPDBFile(pdbName,removeHs=False,sanitize=False) os.remove(pdbName) # AllChem.GenerateDepictionMatching2DStructure(mol,refMol) opt = Draw.DrawingOptions() opt.bgColor=None Draw.MolToFile(mol,filename,kekulize=False,options=opt)