font-sizerdkitcheminformatics

RDKit: How to change the atom label fontsize?


When drawing structures with RDKit, the atom label font size and the ring size are not in a good proportion. The labels are either too small or too large or misaligned.

Unfortunately, the documentation about this is meager. I found this: https://rdkit.org/docs/source/rdkit.Chem.Draw.MolDrawing.html But I don't know whether this is related and how I would have to put it together. I'm missing simple practical code examples.

I tried also Draw.MolToQPixmap, but there I experienced that of the atom labels are misaligned and so far I learnt that the reason is the difficulty to make this cross-platform consistent and furthermore Draw.MolToPixmap uses old drawing code. I should use e.g. Draw.MolToImage instead. But there similar like with Draw.MolToFile the font size is simply too small. I'm not sure whether this is a cross-platform issue as well (I'm on Win10). So, the solution would be to simply set the fontsize, but how?

I know that there is a RDKit mailing list where I asked this question already without an answer so far. Here on SO, there is maybe a broader audience and I can attach images for illustration.

Code:

from rdkit import Chem
from rdkit.Chem import Draw

smiles = ' FC1OC2N3C4[Si]5=C6B7C(C=CC6=CC4=CC2=CC1)C=CC=C7C=C5C=C3'
mol = Chem.MolFromSmiles(smiles)
img = Draw.MolToFile(mol,"Test.png",size=(300,150))

Result: (using Draw.MolToFile, alignment ok, but too small atom labels)

enter image description here

Result: (using Draw.MolToQPixmap, misaligned and/or font too large for small pictures)

enter image description here

enter image description here

Edit: (with the suggestion of @Oliver Scott)

I get 3 times the same output with the same fontsize. I must be a stupid mistake or misunderstanding somewhere.

Code:

from rdkit.Chem.Draw import rdMolDraw2D
from rdkit import Chem

smiles = 'FC1OC2N3C4[Si]5=C6B7C(C=CC6=CC4=CC2=CC1)C=CC=C7C=C5C=C3'
mol = Chem.MolFromSmiles(smiles)
    
def drawMyMol(fname, myFontSize):
    d = rdMolDraw2D.MolDraw2DCairo(350, 300)
    d.SetFontSize(myFontSize)
    print(d.FontSize())
    d.DrawMolecule(mol)
    d.FinishDrawing()
    d.WriteDrawingText(fname)
    
drawMyMol("Test1.png", 6)
drawMyMol("Test2.png", 12)
drawMyMol("Test3.png", 24)

Result:

6.0
12.0
24.0

enter image description here


Solution

  • Thanks to the help of @Oliver Scott, I finally got what I was looking for: Apparently, the font size is relative (default 0.5), not absolute in points, at least in RDKit 2020.03, which I am using. Maybe this has changed in RDKit 2020.09?

    Code: (to get PNGs files)

    from rdkit.Chem.Draw import rdMolDraw2D
    from rdkit import Chem
    
    smiles = 'FC1OC2N3C4[Si]5=C6B7C(C=CC6=CC4=CC2=CC1)C=CC=C7C=C5C=C3'
    mol = Chem.MolFromSmiles(smiles)
    
    def myMolToPNG(fname, myFontSize):
        d = rdMolDraw2D.MolDraw2DCairo(350, 300)
        d.SetFontSize(myFontSize)
        d.DrawMolecule(mol)
        d.FinishDrawing()
        d.WriteDrawingText(fname)
    
    myMolToPNG("Test1.png", 0.5)
    myMolToPNG("Test2.png", 1.0)
    myMolToPNG("Test3.png", 1.5)
    

    Result:

    enter image description here

    Code: (to get a QPixmap, e.g for a PyQt QTableWidget)

    from rdkit.Chem.Draw import rdMolDraw2D
    from rdkit import Chem
    from PyQt5.QtGui import QPixmap
    
    smiles = 'FC1OC2N3C4[Si]5=C6B7C(C=CC6=CC4=CC2=CC1)C=CC=C7C=C5C=C3'
    mol = Chem.MolFromSmiles(smiles)
    
    def myMolToQPixmap(myFontSize):
        d = rdMolDraw2D.MolDraw2DCairo(350, 300)
        d.SetFontSize(myFontSize)
        d.DrawMolecule(mol)
        d.FinishDrawing()
        png = d.GetDrawingText()
        pixmap = QPixmap()
        pixmap.loadFromData(png)
        return pixmap