cbouy / mols2grid

Interactive molecule viewer for 2D structures

Home Page:https://mols2grid.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AttributeError: 'MolDraw2DSVG' object has no attribute 'SetDrawOptions'

LiuCMU opened this issue · comments

import pandas as pd
import mols2grid

smiles = ["CCO", "c1ccccc1", "N", "CO", "O=S(=O)(-O)(-O)", "CCC", "CCC=O"]
df = pd.DataFrame({"smi": smiles,
                   "id": range(1, len(smiles) + 1)})
mg = mols2grid.MolGrid(df, smiles_col="smi", size=(110, 90))
mg.display(subset=["id", "img"], n_cols=7)

I was trying to visualize a list of SMILES, as shown in your colab. But I got the following error message.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-26-cbf1ce952db4> in <module>
      7                    "id": range(1, len(smiles) + 1)})
      8 # setup the grid
----> 9 mg = mols2grid.MolGrid(df, smiles_col="smi", size=(110, 90))
     10 mg.display(subset=["id", "img"], n_cols=7)

~/anaconda3/lib/python3.7/site-packages/mols2grid/molgrid.py in __init__(self, df, smiles_col, mol_col, coordGen, useSVG, mapping, **kwargs)
     74         dataframe.dropna(axis=0, subset=[mol_col], inplace=True)
     75         # generate drawings
---> 76         dataframe["img"] = dataframe[mol_col].apply(self.mol_to_img, **kwargs)
     77         self.dataframe = dataframe
     78         self.mol_col = mol_col

~/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
   4133             else:
   4134                 values = self.astype(object)._values
-> 4135                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   4136 
   4137         if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

~/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in f(x)
   4118 
   4119             def f(x):
-> 4120                 return func(x, *args, **kwds)
   4121 
   4122         else:

~/anaconda3/lib/python3.7/site-packages/mols2grid/molgrid.py in mol_to_img(self, mol, **kwargs)
    170         """Convert an RDKit mol to an HTML img tag containing a drawing of the
    171         molecule"""
--> 172         img = self.draw_mol(mol, **kwargs)
    173         if self.useSVG:
    174             return img

~/anaconda3/lib/python3.7/site-packages/mols2grid/molgrid.py in draw_mol(self, mol, size, use_coords, MolDrawOptions, **kwargs)
    158         for key, value in kwargs.items():
    159             setattr(MolDrawOptions, key, value)
--> 160         d2d.SetDrawOptions(MolDrawOptions)
    161         if not use_coords:
    162             mol = deepcopy(mol)

AttributeError: 'MolDraw2DSVG' object has no attribute 'SetDrawOptions'

could you please take a look maybe? Thanks!

Hi @LiuCMU,

Which version of RDKit are you using ? You can check that with:

import rdkit
print(rdkit.__version__)

Hi, thanks for the quick response:))

my RDKit version is 2019.03.2

Try updating RDKit to version 2019.09.1 or above: conda install -c conda-forge 'rdkit>=2019.09.1' and tell me if that works

Hi, I've updated to RDKit 2019.09.3. This gives me the Value Error:

# example dataframe
import pandas as pd

smiles = ["CCO", "c1ccccc1", "N", "CO", "O=S(=O)(-O)(-O)", "CCC", "CCC=O"]
df = pd.DataFrame({"smi": smiles,
                   "id": range(1, len(smiles) + 1)})
# setup the grid
mg = mols2grid.MolGrid(df, smiles_col="smi", size=(110, 90))
mg.display(subset=["id", "img"], n_cols=7)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-327fbcd76707> in <module>
      6                    "id": range(1, len(smiles) + 1)})
      7 # setup the grid
----> 8 mg = mols2grid.MolGrid(df, smiles_col="smi", size=(110, 90))
      9 mg.display(subset=["id", "img"], n_cols=7)

~/anaconda3/lib/python3.7/site-packages/mols2grid/molgrid.py in __init__(self, df, smiles_col, mol_col, coordGen, useSVG, mapping, **kwargs)
     74         dataframe.dropna(axis=0, subset=[mol_col], inplace=True)
     75         # generate drawings
---> 76         dataframe["img"] = dataframe[mol_col].apply(self.mol_to_img, **kwargs)
     77         self.dataframe = dataframe
     78         self.mol_col = mol_col

~/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
   4133             else:
   4134                 values = self.astype(object)._values
-> 4135                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   4136 
   4137         if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

~/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in f(x)
   4118 
   4119             def f(x):
-> 4120                 return func(x, *args, **kwds)
   4121 
   4122         else:

~/anaconda3/lib/python3.7/site-packages/mols2grid/molgrid.py in mol_to_img(self, mol, **kwargs)
    170         """Convert an RDKit mol to an HTML img tag containing a drawing of the
    171         molecule"""
--> 172         img = self.draw_mol(mol, **kwargs)
    173         if self.useSVG:
    174             return img

~/anaconda3/lib/python3.7/site-packages/mols2grid/molgrid.py in draw_mol(self, mol, size, use_coords, MolDrawOptions, **kwargs)
    163             mol.RemoveAllConformers()
    164         hl_atoms = getattr(mol, "__sssAtoms", [])
--> 165         d2d.DrawMolecule(mol, highlightAtoms=hl_atoms)
    166         d2d.FinishDrawing()
    167         return d2d.GetDrawingText()

ValueError: Bad Conformer Id

Hmmm it appears I've messed up with the minimal RDKit version! I could reproduce the bug with 2019.09.3, but 2020.03.1 works fine so try with this version instead

The error comes from the drawing code that expects atomic coordinates to already exist in v2019 of RDKit and crashes otherwise, while v2020 generates them if needed.
So alternatively, you could try the following code and keep v2019:

from rdkit import Chem
from rdkit.Chem import AllChem

# generate molecules
smiles = ["CCO", "c1ccccc1", "N", "CO", "O=S(=O)(-O)(-O)", "CCC", "CCC=O"]
mols = [Chem.MolFromSmiles(s) for s in smiles]

for i, mol in enumerate(mols):
    # set properties
    mol.SetIntProp("id", i)
    # generate 2D coordinates
    AllChem.Compute2DCoords(mol)

# setup the grid
mg = mols2grid.MolGrid.from_mols(mols, size=(110, 90))
mg.display(subset=["id", "img"], n_cols=7)

Hi, thank you very much! After updating RDKit to 2020.09.1.0, the script works well for me (sorry for the sluggish feedback, I don't know why but my desktop takes a very long time to uninstall old RDKit vai conda:))

I've updated the installation instructions on the readme file with the correct rdkit version.
If you install mols2grid from conda the correct rdkit version will now automatically be installed, which closes this issue.