如何在Jupyter中进行药物分子相似性分析?

解读

面试官问的是“在 Jupyter 中”而非“用 Python”或“用 RDKit”,重点考察三点:

  1. 能否把化学信息学任务无缝嵌入 Notebook 的交互式、可复现、可分享范式;
  2. 是否熟悉国内合规数据源(药审中心 CDE、上海有机所 COVID-19 共享库、长三角数据港等)与本地部署习惯;
  3. 能否把“实验-可视化-报告”做成一键 Notebook,让非编程同事也能复跑。
    回答时要体现“Jupyter 特色”:单元魔法、实时 Widget、并行 Kernel、Hub 权限、nbviewer 分享、reproducible 环境。

知识点

  1. 国内合规数据源获取:CDE 药品目录、ChEMBL-CN 镜像、长三角数据港、北大药学院分子库。
  2. 环境构建:conda-forge 频道安装 rdkit、openff、psi4、ipywidgets、nglview;国内镜像清华/中科大加速;jupyterlab-extension 安装 nglview-js、smiles-viewer。
  3. 核心算法:
    • 指纹相似性:Morgan/ECFP4、RDKit 2D、MACCS、AtomPair、TopologicalTorsion;
    • 距离度量:Tanimoto、Dice、Cosine;
    • 多视角融合:指纹+形状+药效团;
    • 快速筛选:LSH、BALLTree、Faiss GPU 版。
  4. Notebook 交互:
    • ipywidgets 下拉框选靶点、滑块调阈值、smiles 文本框即输即画;
    • nglview 3D 对齐、py3Dmol 备选;
    • 自定义魔法命令 %%similarity 批量计算;
    • ipyparallel 或 dask-gateway 对接 K8s 做百万级并行。
  5. 可复现与合规:
    • 环境锁定:conda-lock.yml + dockerfile 含国内源;
    • 数据脱敏:剔除人员姓名、临床批号;
    • 审计日志:jupyter-server 记录 cell 执行时间、输入输出 hash;
    • 分享:nbviewer 内网地址 + VPN 白名单,或 Hub 分配只读镜像。

答案

下面给出一个可直接在国产麒麟 + JupyterLab 3.6 跑通的范例框架,兼顾交互、性能与合规。
步骤 1:创建可复现环境

在终端单元执行

!conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
%conda create -n molsim python=3.10 rdkit openff-toolkit nglview ipywidgets dask-ml ipyparallel -y
%conda activate molsim
%pip install faiss-cpu ballpark # GPU 版用 faiss-gpu

步骤 2:合规数据读取(以 CDE 公开库为例)
from rdkit import Chem
from rdkit.Chem import PandasTools
import pandas as pd
df = pd.read_csv('/data/cde_public/cleaned_smi.csv') # 已脱敏
PandasTools.AddMoleculeColumnToFrame(df, smilesCol='canonical_smi', molCol='mol')

步骤 3:指纹计算与缓存
from rdkit.Chem import AllChem, DataStructs
import pickle, os
def calc_fp(mol, radius=2, nBits=2048):
if mol is None: return None
return AllChem.GetMorganFingerprintAsBitVect(mol, radius, nBits)
fp_path = '/tmp/fp2048.pkl'
if os.path.exists(fp_path):
fps = pickle.load(open(fp_path,'rb'))
else:
fps = [calc_fp(m) for m in df.mol]
pickle.dump(fps, open(fp_path,'wb'))

步骤 4:交互式相似性搜索
import ipywidgets as w
from IPython.display import display, clear_output
def search_similar(ref_smiles, threshold=0.7, topk=20):
ref_mol = Chem.MolFromSmiles(ref_smiles)
if not ref_mol:
print('SMILES 解析失败'); return
ref_fp = calc_fp(ref_mol)
scores = [DataStructs.TanimotoSimilarity(ref_fp, fp) if fp else 0 for fp in fps]
idx = pd.Series(scores).nlargest(topk).index
return df.iloc[idx].assign(similarity=scores)
smiles_box = w.Text(placeholder='输入参考分子 SMILES')
thresh_slider = w.FloatSlider(value=0.7, min=0.3, max=1.0, step=0.05, description='阈值')
go_btn = w.Button(description='搜索')
output = w.Output()
def on_go(_):
with output: clear_output(wait=True)
res = search_similar(smiles_box.value, thresh_slider.value)
display(res[['mol','similarity','approval_no']]) # approval_no 已脱敏
go_btn.on_click(on_go)
display(w.VBox([w.HBox([smiles_box, thresh_slider, go_btn]), output]))

步骤 5:3D 对齐可视化
import nglview as nv
def view_3d_pair(ref_smiles, hit_id):
ref = Chem.MolFromSmiles(ref_smiles)
hit = df.loc[hit_id, 'mol']
AllChem.EmbedMolecule(ref); AllChem.EmbedMolecule(hit)
view = nv.NGLWidget()
view.add_component(ref, name='ref'); view.add_component(hit, name='hit')
return view

在另一单元用 interact 绑定

步骤 6:百万级加速(可选)
from faiss import IndexBinaryFlat
import numpy as np
bin_fps = np.vstack([np.packbits(fp) for fp in fps if fp])
index = IndexBinaryFlat(2048)
index.add(bin_fps)
D, I = index.search(np.packbits(ref_fp).reshape(1,-1), k=100) # 毫秒级

步骤 7:一键生成报告
!jupyter nbconvert --to html --execute similarity_search.ipynb --output report.html
报告通过 nbviewer 内网地址分享给注册审批同事,实现“代码-结果-解释”三合一。

拓展思考

  1. 多模态相似:把分子图 GNN 嵌入与指纹拼接,用 JupyterLab 的 real-time MLflow 插件做在线 A/B,观察不同嵌入对虚拟筛选命中率的影响。
  2. 联邦学习场景:总部与长三角分中心数据不出域,用 JupyterHub + FATE 的 NN 模块,在 Notebook 里写 Pipeline,把相似性模型拆成 guest/host 两方训练。
  3. 大模型辅助:在 Notebook 里调用“本草”大模型(国内合规)生成 SMILES,再立即用上述流程计算与阳性对照的相似性,实现“生成-评估”闭环。
  4. 审计与追溯:给每个 cell 加 Git-hash 钩子,自动把执行结果 push 到内网 Gitea;当药审中心复查时,可直接在 JupyterHub 打开当时镜像,秒级复现。