Hi Greg,

in the meantime,  I updated to RDKit2020.03 via Anaconda
and also tried Draw.MolToImage(), but I'm running into new problems.
I believe that I need a QPixmap for a PyQt5 QTableWideget, hence, I need to 
convert the PIL image into a QPixmap.

The lines:

    from PyQt5.QtGui import QPixmap
    from PIL.ImageQt import ImageQt
    from rdkit import Chem
    from rdkit.Chem import Draw
    smiles = 'c1ncncn1'
    mol = Chem.MolFromSmiles(smiles)
    im  = Draw.MolToImage(mol)
    qim = ImageQt(im)
    pix = QPixmap.fromImage(qim)

will crash (at least under Win10). Apparently, qim has the wrong type.
However, there is a workaround: https://stackoverflow.com/a/48705903

Good news with Draw.MolToImage(), the centering of the atom labels relative to 
the bonds look good :-)

However, apparently, Draw.MolToImage() works differently than 
Draw.MolToQPixmap()
Draw.MolToQPixmap(mol, highlightAtoms=matches[0]) was highlighting the bonds 
between the atoms.
Draw.MolToImagep(mol, highlightAtoms=matches[0]) is only highlighting the atoms 
(as it actually says).

Now, I still need to find out
1) how to highlight again the bonds of a substructure match
2) how to increase the font size of the atom labels, to get a better 
readability relative to the bonds/rings.
3) how to handle the issue with highlighting in combination with explicit 
hydrogen (I get the error: "list element larger than allowed value")
Since I'm not fluent in Python and RDKit, explicit examples would help a lot... 
which seems to be hard to find...

best,
Theo.


> Dear Greg and David,
>
> thank you for your responses. Sorry, for not being incomplete with 
> information.
> I'm using RDKit2019.03 under Windows using Anaconda3. I haven't found out yet 
> how to update to the latest RDKit (2020.09?) version.
>
> In a PyQt Application I want to list structures as images in a QTableWidget 
> and therefore don't want to save them as files first.
> That's why I'm using Draw.MolToQPixmap(mol)
> But maybe, as Greg suggests, there are even better ways with using 
> Draw.MolToImage() or using the Draw.MolDraw2DCairo() or Draw.MolDraw2DSVG()?
>
> I did some comparison.
> Apparently, it is a Draw.MolToQPixmap(mol) specific problem. In small images 
> a smaller font it would be better, but I haven't found yet to set the font 
> size. https://rdkit.org/docs/source/rdkit.Chem.Draw.MolDrawing.html should 
> tell me, but probably I'm not using it correctly.
>
> It is better with Draw.MolToFile.
> In the "fantasy" molecule
> smiles = ' FC1OC2N3C4[Si]5=C6B7C(C=CC6=CC4=CC2=CC1)C=CC=C7C=C5C=C3'
> only Si is slightly off horizontally. The other labels seem ok.
>
>
> from rdkit.Chem import Draw
> def smiles_to_png(smiles, ffname):
>     mol = Chem.MolFromSmiles(smiles)
>     try:
>         img = Draw.MolToFile(mol,ffname,size=(400,300))
>     except Exception as e:
>         print(e)
>
> smiles = ' FC1OC2N3C4[Si]5=C6B7C(C=CC6=CC4=CC2=CC1)C=CC=C7C=C5C=C3'
> smiles_to_png(smiles,"Test.png")
>
> I only get a warning, but I'm not sure whether this is connected to the offet 
> of the labels.
> Warning: unable to load font metrics from dir 
> C:\Users\Anaconda3\envs\my-rdkit-env\lib\site-packages\rdkit\sping\PIL\pilfonts
>
> Thanks,
> Theo.
>
> PS.
> I assume I cannot attach files to the mailing list. Nevertheless I will try.
> 1. Screenshot from PyQt application with Draw.MolToQPixmap
> 2. Result from above code with Draw.MolToFile
>
>
> Am 21.10.2020 um 05:51 schrieb Greg Landrum:
>> Hi Theo,
>>
>> The atom label placement in drawings turns out to be very tricky to do in a 
>> cross platform way.
>> You're using the RDKit's old drawing code which we haven't been maintaining 
>> for a while now, I'd recommend either switching to using one of the 
>> convenience functions likes Draw.MolToImage() or using the 
>> Draw.MolDraw2DCairo() or Draw.MolDraw2DSVG() classes directly.
>> I can maybe help with the decision which to use: once you've generated an 
>> image of the molecule, what do you want to do with it?
>>
>> -greg
>>
>> On Tue, Oct 20, 2020 at 1:32 PM theozh <the...@gmx.net 
>> <mailto:the...@gmx.net>> wrote:
>>
>>     Dear RDKit-ers,
>>
>>     this sounds like a simple problem, however, I haven't found a link to 
>> the corresponding documentation.
>>
>>     When I draw a structure with:
>>
>>     from.rdkit.Chem import Draw
>>
>>     Draw.MolToQPixmap(mol)
>>
>>     The non-Carbon atom labels are not centered to the bonds, but typically 
>> shifted downwards.
>>     How to center them also vertically?
>>
>>     https://rdkit.org/docs/source/rdkit.Chem.Draw.MolDrawing.html
>>     Here I don't see how to center atom labels vertically. Am I missing 
>> something?
>>
>>     Thanks for hints and the right link,
>>     Theo.
>>
>>
>>
>>
>>
>>
>>     _______________________________________________
>>     Rdkit-discuss mailing list
>>     Rdkit-discuss@lists.sourceforge.net 
>> <mailto:Rdkit-discuss@lists.sourceforge.net>
>>     https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
>>
>>
>>
>> _______________________________________________
>> Rdkit-discuss mailing list
>> Rdkit-discuss@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/rdkit-discuss


_______________________________________________
Rdkit-discuss mailing list
Rdkit-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss

Reply via email to