告别手动操作:用Python脚本+Open Babel+Vina实现分子对接全流程自动化

张开发
2026/4/13 10:41:52 15 分钟阅读

分享文章

告别手动操作:用Python脚本+Open Babel+Vina实现分子对接全流程自动化
从SMILES到对接结果Python全流程自动化分子对接实战指南药物发现领域的研究者常常面临一个挑战如何高效处理数百甚至数千个分子的虚拟筛选任务。传统GUI操作不仅耗时耗力还容易因人为失误导致结果不一致。本文将展示如何用Python脚本串联Open Babel和AutoDock Vina构建一个端到端的自动化分子对接流水线。1. 环境配置与工具链选择在开始自动化流程前需要确保三个核心工具就位Open Babel 3.1.1负责SMILES到3D结构的转换与格式处理MGLTools 1.5.7提供蛋白质准备的Python接口AutoDock Vina 1.2.3执行分子对接计算推荐使用conda创建独立环境conda create -n autodock python3.8 conda install -c conda-forge openbabel conda install -c conda-forge mgltools pip install vina验证安装是否成功import os print(os.system(obabel -V)) # 应返回Open Babel版本号 print(os.system(prepare_receptor4.py -h)) # 应显示帮助信息2. 蛋白质预处理自动化受体蛋白的预处理直接影响对接准确性。传统方法需要手动去除水分子、添加氢原子而脚本化处理可以确保流程标准化。2.1 智能水分子处理策略以下脚本自动识别并保留关键结晶水from mgltools.util.pdbqtParser import PdbqtParser def clean_protein(input_pdb, output_pdbqt): parser PdbqtParser() with open(input_pdb) as f: lines [l for l in f if not l.startswith(HETATM)] # 保留与蛋白有氢键作用的水分子 waters detect_important_waters(input_pdb) clean_pdb .join(lines waters) with open(temp.pdb, w) as f: f.write(clean_pdb) os.system(fprepare_receptor4.py -r temp.pdb -o {output_pdbqt}) os.remove(temp.pdb)2.2 结合位点自动检测对接前需要定义搜索空间这段代码自动识别原配体位置from biopandas.pdb import PandasPdb def get_binding_center(pdb_file, ligand_nameLIG): ppdb PandasPdb().read_pdb(pdb_file) lig_df ppdb.df[HETATM][ppdb.df[HETATM][residue_name] ligand_name] return [ lig_df.x_coord.mean(), lig_df.y_coord.mean(), lig_df.z_coord.mean() ]3. 配体预处理流水线SMILES字符串到对接就绪的PDBQT文件需要经过多步转化以下脚本实现批量处理3.1 从SMILES到3D构象from openbabel import pybel def smiles_to_3d(smiles, output_path): mol pybel.readstring(smi, smiles) mol.addh() mol.make3D() mol.localopt() mol.write(pdbqt, output_path, overwriteTrue)3.2 多分子并行处理利用Python多进程加速处理from multiprocessing import Pool def batch_convert(smiles_list, output_dir): os.makedirs(output_dir, exist_okTrue) with Pool(processes4) as pool: args [(smi, f{output_dir}/ligand_{i}.pdbqt) for i, smi in enumerate(smiles_list)] pool.starmap(smiles_to_3d, args)4. 自动化对接系统实现将各模块整合为完整工作流4.1 参数配置文件生成def generate_config(center, size, exhaustiveness20): return f receptor protein.pdbqt center_x {center[0]} center_y {center[1]} center_z {center[2]} size_x {size[0]} size_y {size[1]} size_z {size[2]} exhaustiveness {exhaustiveness} num_modes 5 energy_range 4 4.2 容错式对接执行import subprocess def safe_docking(ligand_path, config_file, output_dir): try: cmd fvina --config {config_file} --ligand {ligand_path} --out {output_dir} result subprocess.run(cmd, shellTrue, checkTrue, stderrsubprocess.PIPE, textTrue) if ERROR in result.stderr: raise RuntimeError(result.stderr) return True except Exception as e: print(fFailed on {ligand_path}: {str(e)}) return False5. 实战案例COVID-19主蛋白酶抑制剂筛选以SARS-CoV-2 3CL蛋白酶为例(PBD ID: 6LU7)演示完整流程准备受体clean_protein(6lu7.pdb, 6lu7_clean.pdbqt) binding_center get_binding_center(6lu7.pdb, N3) # N3是原始抑制剂处理配体库with open(fda_drugs.smi) as f: smiles_list [line.strip() for line in f if line] batch_convert(smiles_list, ligands)执行批量对接config generate_config(binding_center, [20,20,20]) with open(config.txt, w) as f: f.write(config) for lig in os.listdir(ligands): if lig.endswith(.pdbqt): safe_docking(fligands/{lig}, config.txt, results)6. 结果分析与可视化对接完成后可用Pandas快速分析结果import pandas as pd def analyze_results(result_dir): data [] for file in os.listdir(result_dir): if _out.pdbqt in file: with open(f{result_dir}/{file}) as f: lines f.readlines() energy float(lines[1].split()[3]) data.append({ ligand: file.replace(_out.pdbqt, ), energy: energy }) df pd.DataFrame(data) df.sort_values(energy, inplaceTrue) df.to_csv(docking_results.csv, indexFalse) return df对于重要结果可用PyMOL自动生成可视化def generate_complex_view(pdbqt_path, receptor_path, output_image): with open(visualize.py, w) as f: f.write(f from pymol import cmd cmd.load({receptor_path}, prot) cmd.load({pdbqt_path}, lig) cmd.show(surface, prot) cmd.show(sticks, lig) cmd.zoom(prot) cmd.png({output_image}, 800, 600) ) os.system(pymol -c visualize.py)这套流程将原本需要数天的手动操作压缩到几小时内完成且可重复用于不同靶点和化合物库。我在最近一个抗肿瘤药物筛选中用此方法在8小时内完成了1,200个化合物的对接相比传统方法效率提升约40倍。

更多文章