def MolToMPL(mol, size=(300, 300), kekulize=True, wedgeBonds=True, imageType=None, fitImage=False, options=None, **kwargs): if not mol: raise ValueError('Null molecule provided') from rdkit.Chem.Draw.mplCanvas import Canvas canvas = Canvas(size) if options is None: options = DrawingOptions() options.bgColor = None if fitImage: options.dotsPerAngstrom = int(min(size) / 10) options.atomLabelFontSize = 7 options.bondLineWidth = 2 options.coordScale = 1 options.wedgeDashedBonds = wedgeBonds drawer = MolDrawing(canvas=canvas, drawingOptions=options) omol = mol if kekulize: from rdkit import Chem mol = Chem.Mol(mol.ToBinary()) Chem.Kekulize(mol) if not mol.GetNumConformers(): from rdkit.Chem import AllChem AllChem.Compute2DCoords(mol) drawer.AddMol(mol, **kwargs) omol._atomPs = drawer.atomPs[mol] for k, v in omol._atomPs.items(): omol._atomPs[k] = canvas.rescalePt(v) canvas._figure.set_size_inches(float(size[0]) / 100, float(size[1]) / 100) return canvas._figure
def MolToJSON(mol, size=(300, 300), kekulize=True, wedgeBonds=True, fitImage=False, options=None, **kwargs): if not mol: raise ValueError, 'Null molecule provided' canvas = Canvas(size=size) if options is None: options = DrawingOptions() if fitImage: options.dotsPerAngstrom = int(min(size) / 10) options.wedgeDashedBonds = wedgeBonds try: drawer = MolDrawing(canvas=canvas, drawingOptions=options) except TypeError: drawer = MolDrawing(canvas=canvas) if kekulize: mol = Chem.Mol(mol.ToBinary()) Chem.Kekulize(mol) if not mol.GetNumConformers(): Compute2DCoords(mol) drawer.AddMol(mol, **kwargs) try: drawer.AddLegend(kwargs.get('legend', '')) except AttributeError: pass canvas.flush() return canvas.json
def compute_mol_attributes(graph, labels_dict, actions_history_smi_pop, actions_history_smi_removed, actions_history_scores_pop, actions_history_scores_removed, legend_scores_keys_strat=None): images_attributes = {} scores_attributes = {} draw_opt = DrawingOptions() draw_opt.coordScale = 0.9 draw_opt.dotsPerAngstrom = 30 for action_history_k in labels_dict.keys(): if action_history_k in actions_history_smi_pop: smi = actions_history_smi_pop[action_history_k] img = MolToImage(MolFromSmiles(smi), size=(800, 800), options=draw_opt) images_attributes[action_history_k] = crop_image_with_transparency( img) legend, _ = compute_mol_legend(action_history_k, smi, actions_history_scores_pop, legend_scores_keys_strat) scores_attributes[action_history_k] = legend else: smi = actions_history_smi_removed[action_history_k] img = MolToImage(MolFromSmiles(smi), size=(800, 800), options=draw_opt) images_attributes[action_history_k] = crop_image_with_transparency( img) legend, _ = compute_mol_legend(action_history_k, smi, actions_history_scores_removed, legend_scores_keys_strat) scores_attributes[action_history_k] = legend nx.set_node_attributes(graph, images_attributes, "image") nx.set_node_attributes(graph, scores_attributes, "score_label")