Search code examples
python-3.xrdkitopenbabel

How can I generate a molecular image from the molecular structure provided in MBL molfile format?


from rdkit import Chem
from rdkit.Chem import AllChem, Draw
import matplotlib.pyplot as plt  

molfile_string = """
1 0 0 0 0 0             999 V2000
0.0000  0.0000  0.0000 O  0  -1  0  0
1.0000  0.8144  0.0000 H  0  1  0  0
0.0000 -0.8144  0.0000 H  0  1  0  0
M END
"""
mol = Chem.MolFromMolBlock(molfile_string)

gives me below error:

[18:47:21] Cannot convert '1.0' to unsigned int on line 4

I want to know what's wrong in the format and what should be the correct format so that I can view the image with following lines:

AllChem.Compute2DCoords(mol)
image = Draw.MolToImage(mol, size=(300, 300), kekulize=False)
plt.imshow(image, interpolation='nearest')
plt.axis('off')  # Turn off axis labels
plt.show()

Solution

  • Your are missing some parts of the molfile.

    The MDL Molfileformat starts with three lines (first and third can be blank) followed by a counts line, an atom and a bond block.

    See: Wikipedia

    from rdkit import Chem
    
    molfile_string = """                                                  # first line
         RDKit          2D                                                # second line
                                                                          # third line
      3  2  0  0  0  0  0  0  0  0999 V2000                               # counts line: 3 atoms 2 bonds
        0.0000    0.0000    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0 # atom block
        1.2990    0.7500    0.0000 H   0  0  0  0  0  0  0  0  0  0  0  0
       -1.2990    0.7500    0.0000 H   0  0  0  0  0  0  0  0  0  0  0  0
      1  2  1  0                                                          # bond block
      1  3  1  0
    M  END
    """
    mol = Chem.MolFromMolBlock(molfile_string, removeHs=False)
    mol
    

    enter image description here