Rdkit: Question on how to display images of the molecules

Created on 28 Feb 2020  路  5Comments  路  Source: rdkit/rdkit

I should preempt my question by stating I am very new to using rdkit.

I was wondering how to make the images of the files to show after a run. To be clear, I can execute the following code to output the image to an external file.

"""""""""""""""""""""""""""""""""
from rdkit import Chem
from rdkit.Chem import Draw

glycine = Chem.MolFromSmiles('C(C(=O)O)N')
Draw.MolToFile(glycine, 'glycine.png')
"""""""""""""""""""""""""""""""""

However, when I try to execute the following code I do not observe an image.

"""""""""""""""""""""""""""""""""
smiles_list = ['NC@@HC(O)=O', 'C(C(=O)O)N', 'O=C(C@HN)O', 'C(C@@HN)S']
mol_list = []
for smiles in smiles_list:
mol = Chem.MolFromSmiles(smiles)
mol_list.append(mol)

Draw.MolsToGridImage(mol_list)
"""""""""""""""""""""""""""""""""

To summarize my questions, am I suppose to have an image output after executing the prior command? If so why do you think I do not have one, and if not how do I view the image?

question

All 5 comments

Draw.MolsToGridImage() returns the image data. By default this is a PIL Image object which you can then save. Something like this should work:

img = Draw.MolsToGridImage(mol_list)
img.save('output.png')

You are correct the output is a PIL image, and I was able to save the file as an output to observe what is inside of it by using the code you provided.

I have another question now, are all Draw objects suppose to be a PIL image? I am now trying to visualize the morgan fingerprints but the object is a string type rather than an image. Do you know how I can visualize these fingerprints? See the code below.

"""""""""""""""""""""
from rdkit import Chem
from rdkit.Chem import Draw
from rdkit.Chem import Descriptors
from rdkit.Chem import AllChem
from rdkit import DataStructs
import numpy as np

glycine = Chem.MolFromSmiles('C(C(=O)O)N')
Draw.MolToFile(glycine, 'glycine.png')

dic = {}
fp = AllChem.GetMorganFingerprintAsBitVect(glycine, 2, nBits=1024, bitInfo=dic)
fp_arr = np.zeros(1,)
DataStructs.ConvertToNumpyArray(fp, fp_arr)
fp_arr = np.nonzero(fp_arr)
prints = [(glycine, x, dic) for x in fp.GetOnBits()]
img = Draw.DrawMorganBits(prints, molsPerRow=4, legends=[str(x) for x in fp.GetOnBits()])
print(type(img))

I fixed the issue.

I didn't write down which lines of code I changed but there was an issue with some python files in rdkit calling decorator. What I ended up doing to fix the issue was changing the calls to decorator from being "decorator" to "decorator.decorator".

Draw.MolsToGridImage() returns the image data. By default this is a PIL Image object which you can then save. Something like this should work:

img = Draw.MolsToGridImage(mol_list)
img.save('output.png')

Hi, I am also new to rdkit and am also trying to save an image.
For some reason, the default format is not PIL for me. Any idea why?

Below is my code:


from rdkit import Chem
from rdkit.Chem import Draw
from PIL import Image

unique_smiles_freq_non = dict()
suppl_non = Chem.SDMolSupplier('cid2sids-uracil.sdf')

for mol in suppl_non:
smiles_non = Chem.MolToSmiles(mol,isomericSmiles=True, canonical=False)
if smiles_non != "":
unique_smiles_freq_non[ smiles_non ] = unique_smiles_freq_non.get(smiles_non,0) + 1

unique_mols_non = [Chem.MolFromSmiles(smile) for smile in unique_smiles_freq_non.keys()]
all_non = Draw.MolsToGridImage(unique_mols_non, molsPerRow=8, subImgSize=(150, 150))

print(type(all_non))
all_non.save("all_non .png")


This is what I get when I run the code:


AttributeError Traceback (most recent call last)
in
15
16 print(type(all_non))
---> 17 all_non.save("all_non .png")
18
19

AttributeError: 'Image' object has no attribute 'save'


To summarise, I have created a list of molecules (unique_mols_non) and I used the module Draw.MolsToGridImage to create one PIL image with all the structures. However, the format isn't PIL and hence I can't save it as PNG.
Thank you:)

@Maron-ChingFungChan you're seeing that behavior because MolsToGridImage() behaves differently when you use it in a Jupyter notebook. Here's a gist demonstrating how to get the raw PNG data and save it to a file:
https://gist.github.com/greglandrum/56a80e84676fc3a24250b821d0faac13

Was this page helpful?
0 / 5 - 0 ratings

Related issues

BillLawrence111 picture BillLawrence111  路  3Comments

mc-robinson picture mc-robinson  路  3Comments

thegodone picture thegodone  路  3Comments

IgnacioJPickering picture IgnacioJPickering  路  3Comments

crisfbazz picture crisfbazz  路  4Comments