Search code examples
pythonpandasimagebioinformaticsrdkit

Using rdkit in a for loop to produce png file, but the same png file is produced each time


I have a dataframe that looks like this

np_id   SMILES  standard_inchi_key
0   NPC4665 OC(=O)Cc1ccc(c(c1)O)O   CFFZDZCDUFSOFZ-UHFFFAOYSA-N
2   NPC4668 OC(=O)C1=CCCNC1 QTDZOWFRBNTPQR-UHFFFAOYSA-N
32  NPC4962 CCCCCCCCC(=O)C  ZAJNGDIORYACQU-UHFFFAOYSA-N
36  NPC4986 CC1=CC[C@]23[C@H]1[C@H]1OC(=O)C(=C)[C@@H]1CC[C...   UVJYAKBJSGRTHA-CUZKYEQNSA-N
38  NPC5292 CC(=O)OC[C@]12CC[C@H]3[C@H]([C@]1(O)CC[C@@]2(O...   RGHQRULWHKEQHE-GRVQADPTSA-N

I am trying to produce a 2D representation of a molecule, using rdkit to process the SMILES. I've written the following code:

from rdkit import Chem
from rdkit.Chem import Draw
from rdkit.Chem import AllChem
import pandas as pd

image_path = '/DTA_training/short/'
list_of_paths = []
for inchi_key in df['standard_inchi_key']:
  image_name = str(inchi_key)
  full_path = image_path+image_name
  list_of_paths.append(full_path)

df['paths'] = list_of_paths

image_size = 500
for full_path in df['paths']:

    for smile in df["SMILES"]:
      mol = Chem.MolFromSmiles(str(smile))
      if mol is None:
        print(("Unable to read original SMILES"+full_path))
      else:
        _discard = AllChem.Compute2DCoords(mol)
        Draw.MolToFile(mol, full_path, size=(image_size,image_size), fitImage=False, imageType='png')

The same png is produced each time. I cannot workout what is wrong with my for loop. Can anyone advise?


Solution

  • In each loop of for full_path in df['paths']: you create an image of all SMILES in the dataframe one after the other and overwrite the previous one so that only the last one remains.

    Try this:

    df.reset_index(drop=True, inplace=True) # thanks to mnis
    
    for n in range(len(df["paths"])):
        full_path = df["paths"][n]
        mol = Chem.MolFromSmiles(df["SMILES"][n])
        if mol is None:
            print(("Unable to read original SMILES"+full_path))
        else:
            _discard = AllChem.Compute2DCoords(mol)
            Draw.MolToFile(mol, full_path, size=(image_size,image_size), fitImage=False, imageType='png')