You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用CDPL Chem读取RDKit生成的SDF文件时参数不匹配错误求助

问题:CDPL Chem读取RDKit生成的SDF文件时参数不匹配错误

错误详情

运行代码时触发以下Boost.Python参数错误:

mol = CDPL_readSDF(temp_file)
  File "~/csv2sdf.py", line 462, in CDPL_readSDF
    reader = CDPLChem.MoleculeReader(input_sd_file)
Boost.Python.ArgumentError: Python argument types in
    MoleculeReader.__init__(MoleculeReader, str)
did not match C++ signature:
    __init__(_object* self, std::istream {lvalue} is, CDPL::Base::DataFormat fmt)
    __init__(_object* self, std::istream {lvalue} is, std::string fmt)
    __init__(_object* self, std::string file_name, CDPL::Base::DataFormat fmt, std::_Ios_Openmode mode=CDPL.Base._base.OpenMode(12))
    __init__(_object* self, std::string file_name, std::string fmt, std::_Ios_Openmode mode=CDPL.Base._base.OpenMode(12))
    __init__(_object* self, std::string file_name, std::_Ios_Openmode mode=CDPL.Base._base.OpenMode(12))

导入语句

import CDPL.Chem as CDPLChem
import CDPL.ConfGen as CCfGen
from rdkit import Chem
from rdkit.Chem import AllChem

RDKit生成SDF文件的代码

def prepare_mol_for_SDPL(smiles:str, columns: list, row: pd.Series, temp_file: str):
    """
    为输入SMILES添加氢原子并生成SDF文件
    """
    mol = Chem.MolFromSmiles(smiles)
    mol = Chem.AddHs(mol)
    AllChem.Compute2DCoords(mol)
    for col in columns:
        mol.SetProp(col, str(row[col]))
    mol.SetProp("_InputInChIKey", str(Chem.inchi.InchiToInchiKey(Chem.inchi.MolToInchi(Chem.MolFromSmiles(smiles)))))
    mol.SetProp("_SMILES", str(Chem.MolToSmiles(mol)))
    mol.SetProp("_InChIKey", str(Chem.inchi.InchiToInchiKey(Chem.inchi.MolToInchi(mol))))
    writer = Chem.SDWriter(temp_file)
    writer.write(mol)
    writer.close()

CDPL读取SDF的函数

def CDPL_readSDF(input_sd_file: str) -> None:
    """
    读取SD文件中的分子结构数据并返回
    参数:
    - input_sd_file (str): 输入SD文件路径
    """
    # 原注释尝试用FileSDFMoleculeReader但被注释了
    reader = CDPLChem.MoleculeReader(input_sd_file)

    mol = CDPLChem.BasicMolecule()

    try:
        while reader.read(mol):
            return mol
    except Exception as e:
        raise Exception('读取分子失败: ' + str(e))

主函数相关代码片段

for ind in df.index:
        smiles = df.loc[ind, args.smi_column]
        mol = None
        try:
            try:
                os.remove(temp_file)
            except:
                print()
            temp_file = output_file.replace('.sdf', '_temp.sdf')
            prepare_mol_for_SDPL(smiles, df.columns, df.loc[ind], temp_file)
            mol = CDPL_readSDF(temp_file)
            os.remove(temp_file)

参考过的示例代码

  • smiles-to-3d项目中的smi3d模块
  • smiles-to-3d项目中的gen_3d_structs工具模块
  • CDPL官方教程中的读取SDF示例

解决方案

根据错误信息,CDPLChem.MoleculeReader的构造函数不支持仅传入文件路径的调用方式,必须指定文件格式或者使用专门的SDF读取器:

方案1:使用专门的SDF文件读取器

取消注释代码中已有的FileSDFMoleculeReader调用,这是CDPL为SDF格式提供的专用读取器,无需额外指定格式:

def CDPL_readSDF(input_sd_file: str):
    # 使用专用的SDF读取器
    reader = CDPLChem.FileSDFMoleculeReader(input_sd_file)
    mol = CDPLChem.BasicMolecule()
    try:
        while reader.read(mol):
            return mol
    except Exception as e:
        raise Exception('读取分子失败: ' + str(e))

方案2:给MoleculeReader指定格式参数

如果坚持使用MoleculeReader,需要明确传入SDF格式标识,可以用字符串或者CDPL的枚举值:

# 方式一:使用字符串指定格式
reader = CDPLChem.MoleculeReader(input_sd_file, "SDF")

# 方式二:使用CDPL的DataFormat枚举
import CDPL.Base as CDPLBase
reader = CDPLChem.MoleculeReader(input_sd_file, CDPLBase.DataFormat.SDF)

这两种方式都能匹配MoleculeReader的构造函数签名,解决参数不匹配的问题。


内容的提问来源于stack exchange,提问作者M.Vu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 05:22:09