【问题标题】:Using rdkit in a for loop to produce png file, but the same png file is produced each time在for循环中使用rdkit生成png文件,但每次生成的都是同一个png文件
【发布时间】:2021-07-13 00:25:16
【问题描述】:

我有一个看起来像这样的数据框

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

我正在尝试生成分子的 2D 表示,使用 rdkit 来处理 SMILES。我写了以下代码:

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')

每次生成相同的 png。我无法锻炼我的 for 循环有什么问题。谁能给点建议?

【问题讨论】:

    标签: python pandas image bioinformatics rdkit


    【解决方案1】:

    for full_path in df['paths']: 的每个循环中,您会在数据框中创建所有 SMILES 的图像 一个接一个地覆盖前一个,这样就只剩下最后一个了。

    试试这个:

    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')
    

    【讨论】:

    • 用那个代码我得到一个KeyError: 1,我想是因为这条线:---> 20 full_path = df["paths"][n]
    • for 循环之前重置数据帧的索引。添加df.reset_index(drop=True, inplace=True)
    • @mnis 感谢您的帮助。我在发送之前没有验证我的代码。
    • @ChemBot 随着 minis 的 sn-p,KeyError 消失了
    猜你喜欢
    • 2015-07-08
    • 2011-06-02
    • 2013-05-26
    • 2017-02-05
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 2019-08-21
    • 2014-03-08
    相关资源
    最近更新 更多