import argparse import numpy as np from openeye import oechem def clear_stereochemistry(mol): clear_atom_stereochemistry(mol) clear_bond_sterochemistry(mol) oechem.OESuppressHydrogens(mol, False, False, False) def clear_atom_stereochemistry(mol): for atom in mol.GetAtoms(): chiral = atom.IsChiral() stereo = oechem.OEAtomStereo_Undefined v = [] for nbr in atom.GetAtoms(): v.append(nbr) if atom.HasStereoSpecified(oechem.OEAtomStereo_Tetrahedral): stereo = atom.GetStereo(v, oechem.OEAtomStereo_Tetrahedral) if chiral or stereo != oechem.OEAtomStereo_Undefined: atom.SetStereo(v, oechem.OEAtomStereo_Tetrahedral, oechem.OEAtomStereo_Undefined) def clear_bond_sterochemistry(mol): for bond in mol.GetBonds(): if bond.HasStereoSpecified(oechem.OEBondStereo_CisTrans): for atomB in bond.GetBgn().GetAtoms(): if atomB == bond.GetEnd(): continue for atomE in bond.GetEnd().GetAtoms(): if atomE == bond.GetBgn(): continue v = [] v.append(atomB) v.append(atomE) stereo = bond.SetStereo(v, oechem.OEBondStereo_CisTrans, oechem.OEBondStereo_Undefined) def abs_smi(x): mol = oechem.OEGraphMol() if oechem.OESmilesToMol(mol, x): clear_stereochemistry(mol) return oechem.OEMolToSmiles(mol) else: return np.nan if __name__ == '__main__': parser = argparse.ArgumentParser(description="Remove stereochemistry from the input data set.") parser.add_argument("--in",dest="infile",help="whitespace-delimited input file",metavar="in.csv") parser.add_argument("--out", dest="outfile", help="output file", metavar="out.csv") args = parser.parse_args() n=0 with open(args.infile, 'r') as ifs: with open(args.outfile, 'w') as ofs: for line in ifs: if n==0: ofs.write(line) n=1 else: parsed = line.strip().split(',') if ('.' not in parsed[0]): ofs.write(f"{abs_smi(parsed[0])},{parsed[1]}\n")
时间: 2023-12-01 17:02:57 浏览: 45
这段代码是一个用于清除分子中立体化学信息的 Python 脚本。它使用了 OpenEye 包中的 oechem 模块来操作分子,并可以通过命令行参数指定输入输出文件。
脚本的主要功能在于定义了三个函数:`clear_stereochemistry`、`clear_atom_stereochemistry` 和 `clear_bond_sterochemistry`,它们分别用于清除分子中的原子立体信息、键立体信息和所有立体信息。这些函数都是通过遍历分子的原子和键,检查是否存在立体信息并将其清除来实现的。
另外,脚本还定义了一个名为 `abs_smi` 的函数,该函数将 SMILES 格式的分子字符串作为输入,并返回一个没有立体信息的 SMILES 字符串。它首先将输入的 SMILES 字符串转换为分子对象,然后调用之前定义的 `clear_stereochemistry` 函数清除立体信息,最后将分子对象转换回 SMILES 格式并返回。
在 `if __name__ == '__main__':` 语句块中,脚本使用 argparse 模块解析命令行参数,然后打开输入文件读取数据并将结果写入输出文件。对于每一行数据,脚本使用 `abs_smi` 函数先清除分子中的立体信息,然后将处理后的 SMILES 字符串写入输出文件。
相关问题
import numpy as np from IPython import embed from openvino.inference_engine import IECore
这是一段 Python 代码,其中包含了一些 Python 语言的模块和库的导入操作。具体含义如下:
- `import numpy as np`:导入 Python 的 numpy 模块,并将其重命名为 np,用于进行数值计算和矩阵运算。
- `from IPython import embed`:从 IPython 模块中导入 embed 方法,用于在代码中插入交互式的 Python 解释器,方便调试和测试。
- `from openvino.inference_engine import IECore`:从 openvino.inference_engine 模块中导入 IECore 类,用于在 Intel OpenVINO Toolkit 中进行推理和部署。
这段代码中的导入操作并没有实际的功能,只是为了在后续的代码中使用这些模块和库的功能。
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d
import numpy是一个用于科学计算的Python库,它提供了大量用于数组操作和数学计算的函数和工具。使用import numpy as np将numpy库导入为np别名,以便在代码中更方便地使用numpy的函数和工具。
而import matplotlib.pyplot as plt是导入matplotlib库的一种常用方式,matplotlib是一个用于绘制图表和数据可视化的库,import语句将matplotlib.pyplot模块导入为plt别名,以便在代码中更方便地使用matplotlib库的绘图函数和工具。
最后,from mpl_toolkits.mplot3d导入模块同样是用于matplotlib库的一种常见操作,它用于导入mpl_toolkits.mplot3d模块,这是一个用于绘制三维图表和可视化三维数据的工具模块。
综合起来,这些import语句在Python代码中的作用是将numpy和matplotlib库以及其相关的模块导入到代码中,以便使用它们提供的各种函数和工具进行数组操作、数学计算和绘制图表等操作。这样可以极大地方便数据科学家、工程师和研究人员们对数据的处理和可视化工作。