def testPNGMetadata(self): m = Chem.MolFromMolBlock(''' Mrv2014 08172015242D 0 0 0 0 0 999 V3000 M V30 BEGIN CTAB M V30 COUNTS 3 2 0 0 0 M V30 BEGIN ATOM M V30 1 C 2.31 -1.3337 0 0 M V30 2 C 3.6437 -2.1037 0 0 M V30 3 O 4.9774 -1.3337 0 0 M V30 END ATOM M V30 BEGIN BOND M V30 1 1 1 2 M V30 2 1 2 3 M V30 END BOND M V30 END CTAB M END''') d = Draw.MolDraw2DCairo(200, 200) d.DrawMolecule(m) txt = d.GetDrawingText() nm = Chem.MolFromPNGString(txt) self.assertEqual(Chem.MolToSmiles(m), Chem.MolToSmiles(nm))
def _smiles_to_im(smiles: str) -> np.array: """ Generate a molecule figure from its SMILES representation. Parameters ---------- smiles : str The SMILES representation of the molecule to be converted to a figure. Returns ------- np.array The molecule figure as an [N, M, 3] NumPy RGB array. N and M are defined by `_mol_size` with empty lines cropped. """ # Draw the molecule. # https://sourceforge.net/p/rdkit/mailman/message/36735785/ d2d = Draw.MolDraw2DCairo(_mol_size, _mol_size) d2d.SetFontSize(_mol_font_size) d2d.drawOptions().bondLineWidth = _mol_line_width Draw.PrepareAndDrawMolecule(d2d, Chem.MolFromSmiles(smiles)) d2d.FinishDrawing() im = plt.imread(io.BytesIO(d2d.GetDrawingText())) # Make white pixels transparent. im = np.dstack((im, (~im.all(2)).astype(np.float32))) # Crop the image by removing empty (white) lines. bottom, top, left, right = 0, im.shape[0] - 1, 0, im.shape[1] - 1 while bottom < im.shape[0] and np.isclose(im[bottom, :, 3].sum(), 0): bottom += 1 while top > 0 and np.isclose(im[top, :, 3].sum(), 0): top -= 1 while left < im.shape[1] and np.isclose(im[:, left, 3].sum(), 0): left += 1 while right > 0 and np.isclose(im[:, right, 3].sum(), 0): right -= 1 return im[bottom:top + 1:, left:right + 1, :]
def draw(reaction, save_reaction_path): d2d = Draw.MolDraw2DCairo(6000, 2000) d2d.DrawReaction(reaction) png = d2d.GetDrawingText() # 'D:/test/image/output.png' open(save_reaction_path, 'wb+').write(png)
def draw_mol_with_property(mol, property, **kwargs): """ http://rdkit.blogspot.com/2015/02/new-drawing-code.html Parameters --------- property : dict key atom idx, val the property (need to be stringfiable) """ import copy from rdkit.Chem import Draw from rdkit.Chem import AllChem def run_from_ipython(): try: __IPYTHON__ return True except NameError: return False AllChem.Compute2DCoords(mol) mol = copy.deepcopy(mol) #FIXME do I really need deepcopy? for idx in property: # opts.atomLabels[idx] = mol.GetAtomWithIdx(idx).SetProp('molAtomMapNumber', "({})".format(str(property[idx]))) mol = Draw.PrepareMolForDrawing(mol, kekulize=False) #enable adding stereochem if run_from_ipython(): from IPython.display import SVG, display if "width" in kwargs and type( kwargs["width"]) is int and "height" in kwargs and type( kwargs["height"]) is int: drawer = Draw.MolDraw2DSVG(kwargs["width"], kwargs["height"]) else: drawer = Draw.MolDraw2DSVG(500, 250) drawer.DrawMolecule(mol) drawer.FinishDrawing() display(SVG(drawer.GetDrawingText().replace("svg:", ""))) else: if "width" in kwargs and type( kwargs["width"]) is int and "height" in kwargs and type( kwargs["height"]) is int: drawer = Draw.MolDraw2DCairo(kwargs["width"], kwargs["height"]) else: drawer = Draw.MolDraw2DCairo(500, 250) #cairo requires anaconda rdkit # opts = drawer.drawOptions() drawer.DrawMolecule(mol) drawer.FinishDrawing() # # with open("/home/shuwang/sandbox/tmp.png","wb") as f: # f.write(drawer.GetDrawingText()) import io import matplotlib.pyplot as plt import matplotlib.image as mpimg buff = io.BytesIO() buff.write(drawer.GetDrawingText()) buff.seek(0) plt.figure() i = mpimg.imread(buff) plt.imshow(i) plt.show()
# open('reaction3.png', 'wb+').write(png) # rxn = AllChem.ReactionFromSmarts('C[O:1][c:2]1[cH:3][c:4]([c:5]([cH:6][c:7]1[Cl:8])[CH:9]=[O:10])[Cl:11]>CN(C)C=O.Cl[Li]>[O:10]=[CH:9][c:5]1[cH:6][c:7]([c:2]([cH:3][c:4]1[Cl:11])[OH:1])[Cl:8]') # d2d = Draw.MolDraw2DCairo(800, 300) # d2d.DrawReaction(rxn, highlightByReactant=True) # png = d2d.GetDrawingText() # open('reaction3.png', 'wb+').write(png) rxn = AllChem.ReactionFromSmarts( '[CH3:1][CH2:2][O:3][C:4](=[O:5])[c:6]1[cH:7][c:8]([cH:9][c:10]([cH:11]1)[C:12](=[O:13])O)-[c:14]1[cH:15][cH:16][c:17]([cH:18][c:19]1[C:20]#[N:21])[CH3:22].[OH:23][CH:24]1[CH2:25][NH:26][CH2:27]1.Cl>CCN=C=NCCCN(C)C.On1nnc2ccccc21.CC(C)N(CC)C(C)C.ClCCl.O.Cl>[CH3:1][CH2:2][O:3][C:4](=[O:5])[c:6]1[cH:7][c:8]([cH:9][c:10]([cH:11]1)[C:12](=[O:13])[N:26]1[CH2:27][CH:24]([CH2:25]1)[OH:23])-[c:14]1[cH:15][cH:16][c:17]([cH:18][c:19]1[C:20]#[N:21])[CH3:22]' ) # d2d = Draw.MolDraw2DCairo(800, 300) # d2d.DrawReaction(rxn, highlightByReactant=True) # png = d2d.GetDrawingText() # open('reaction4.png', 'wb+').write(png)') d2d = Draw.MolDraw2DCairo(800, 300) d2d.DrawReaction(rxn, highlightByReactant=True) png = d2d.GetDrawingText() open('reaction6.png', 'wb+').write(png) rxn = AllChem.ReactionFromSmarts( '([C:13]-[N;H0;D3;+0:14](-[C:15])-[C;H0;D3;+0:1](=[O;D1;H0:2])-[c:3]1:[c:4]:[c:5](-[C:6](=[O;D1;H0:7])-[#8:8]-[C:9]):[c:10]:[c:11]:[c:12]:1)>>(O-[C;H0;D3;+0:1](=[O;D1;H0:2])-[c:3]1:[c:4]:[c:5](-[C:6](=[O;D1;H0:7])-[#8:8]-[C:9]):[c:10]:[c:11]:[c:12]:1).([C:13]-[NH;D2;+0:14]-[C:15])' ) # d2d = Draw.MolDraw2DCairo(800, 300) # d2d.DrawReaction(rxn, highlightByReactant=True) # png = d2d.GetDrawingText() # open('reaction4.png', 'wb+').write(png)') d2d = Draw.MolDraw2DCairo(800, 300) d2d.DrawReaction(rxn, highlightByReactant=True) png = d2d.GetDrawingText() open('reaction7.png', 'wb+').write(png)