ArgumentError Draw.MolsToGridImage rdkit.Chem.Draw

224 Views Asked by At

I am working with the rdkit package and I am using the rdkit.Chem.Draw.MolsToGridImage function. I am trying to find a solution to my "ArgumentError" error:

---------------------------------------------------------------------------
ArgumentError                             Traceback (most recent call last)
Cell In [72], line 7
      5 subs = [x[2] for x in reaction_.Substrate()]
      6 prods =  [x[2] for x in reaction_.Product()]
----> 7 display(Draw.MolsToGridImage([validation_metabolites_list[x].structure for x in subs], legends=subs, subImgSize=(1000/len(subs),200), molsPerRow=len(subs)))
      8 display(Draw.MolsToGridImage([validation_metabolites_list[x].structure for x in prods], legends=prods, subImgSize=(1000/len(prods),200), molsPerRow=len(prods)))

File ~/opt/anaconda3/lib/python3.9/site-packages/rdkit/Chem/Draw/IPythonConsole.py:271, in ShowMols(mols, maxMols, **kwargs)
    268 if "drawOptions" not in kwargs:
    269   kwargs["drawOptions"] = drawOptions
--> 271 res = fn(mols, **kwargs)
    272 if InteractiveRenderer.isEnabled():
    273   return HTML(res)

File ~/opt/anaconda3/lib/python3.9/site-packages/rdkit/Chem/Draw/__init__.py:620, in MolsToGridImage(mols, molsPerRow, subImgSize, legends, highlightAtomLists, highlightBondLists, useSVG, returnPNG, **kwargs)
    616   return _MolsToGridSVG(mols, molsPerRow=molsPerRow, subImgSize=subImgSize, legends=legends,
    617                         highlightAtomLists=highlightAtomLists,
    618                         highlightBondLists=highlightBondLists, **kwargs)
    619 else:
--> 620   return _MolsToGridImage(mols, molsPerRow=molsPerRow, subImgSize=subImgSize, legends=legends,
    621                           highlightAtomLists=highlightAtomLists,
    622                           highlightBondLists=highlightBondLists, returnPNG=returnPNG, **kwargs)

File ~/opt/anaconda3/lib/python3.9/site-packages/rdkit/Chem/Draw/__init__.py:554, in _MolsToGridImage(mols, molsPerRow, subImgSize, legends, highlightAtomLists, highlightBondLists, drawOptions, returnPNG, **kwargs)
    552 else:
    553   fullSize = (molsPerRow * subImgSize[0], nRows * subImgSize[1])
--> 554   d2d = rdMolDraw2D.MolDraw2DCairo(fullSize[0], fullSize[1], subImgSize[0], subImgSize[1])
    555   if drawOptions is not None:
    556     d2d.SetDrawOptions(drawOptions)

ArgumentError: Python argument types in
    MolDraw2DCairo.__init__(MolDraw2DCairo, float, int, float, int)
did not match C++ signature:
    __init__(_object*, int width, int height, int panelWidth=-1, int panelHeight=-1, bool noFreetype=False)

my code looks like this :

for reaction_ID in under_predicted_reactions:
    print(reaction_ID)
    print(under_predicted_reactions[reaction_ID])
    reaction_ = recon_reactions[reaction_ID]
    subs = [x[2] for x in reaction_.Substrate()]
    prods =  [x[2] for x in reaction_.Product()]
    display(Draw.MolsToGridImage([validation_metabolites_list[x].structure for x in subs], legends=subs, subImgSize=(1000/len(subs),200), molsPerRow=len(subs)))
    display(Draw.MolsToGridImage([validation_metabolites_list[x].structure for x in prods], legends=prods, subImgSize=(1000/len(prods),200), molsPerRow=len(prods)))

can someone help me?

1

There are 1 best solutions below

0
Oliver Scott On

Looks like the subImgSize requires (int, int) and you have (float, int) try swapping / for // or cast to an integer.

Draw.MolsToGridImage([validation_metabolites_list[x].structure for x in subs], legends=subs, subImgSize=(1000//len(subs),200), molsPerRow=len(subs)))

# or 

Draw.MolsToGridImage([validation_metabolites_list[x].structure for x in subs], legends=subs, subImgSize=(int(1000/len(subs)),200), molsPerRow=len(subs)))

That should do the trick.