AlphaFold 3源码阅读路线:从run_alphafold.py到模型核心
AlphaFold 3作为蛋白质结构预测领域的革命性工具,其源码架构复杂但逻辑清晰。本文将从入口脚本run_alphafold.py出发,系统梳理数据处理、模型构建到结构预测的全流程代码路径,帮助开发者快速掌握核心模块设计。
一、程序入口与参数解析
run_alphafold.py作为执行入口,通过Abseil Flags定义了12类关键参数:
- 输入输出路径:
--json_path指定单个输入文件,--input_dir支持批量处理 - 数据库配置:
--db_dir指定多数据库路径,通过replace_db_dir()函数动态替换路径模板 - 计算资源控制:
--jackhmmer_n_cpu限制CPU使用(默认≤8核),--gpu_device指定GPU设备ID - 模型调优参数:
--num_recycles控制推理迭代次数(默认10次),--flash_attention_implementation选择注意力实现(Triton/cuDNN/XLA)
参数验证逻辑在main()函数中实现,例如检查GPU计算能力需≥6.0,对7.x架构强制启用--xla_disable_hlo_passes优化。
二、数据处理管道:从原始序列到特征矩阵
数据处理核心在pipeline.py中实现,包含三个关键阶段:
2.1 MSA与模板搜索
- 多序列比对:通过
jackhmmer工具搜索UniRef90等数据库,结果在msa_features.py中处理为特征矩阵 - 模板检索:
_max_template_date参数过滤新版PDB结构,模板特征提取见template_store.py
2.2 特征工程
featurisation.py实现特征构建:
# 关键特征生成代码示例
def create_target_feat(batch: feat_batch.Batch, append_per_atom_features: bool) -> chex.ArrayDevice:
# 生成序列特征矩阵 (num_tokens, 22)
seq_feats = create_seq_features(batch.seq_features)
if append_per_atom_features:
# 拼接原子级特征 (num_tokens, max_atoms_per_token, 3)
atom_feats = create_atom_features(batch.atom_features)
return jnp.concatenate([seq_feats, atom_feats], axis=-1)
return seq_feats
2.3 数据增强
通过random_augmentation()函数实现坐标扰动,在features.py中定义:
def random_augmentation(positions: np.ndarray, random_state: np.random.RandomState) -> np.ndarray:
# 随机旋转和平移
rot = random_rotation(random_state)
trans = random_state.normal(scale=0.1, size=(3,))
return positions @ rot + trans
三、模型核心架构:Evoformer与扩散Transformer
3.1 主干网络
evoformer.py实现核心进化 transformer,包含:
- MSA处理:通过
_embed_process_msa()生成256维序列特征 - 成对特征:
_seq_pair_embedding()构建残基间关系矩阵 - 迭代模块:
evoformer_fn()实现12层堆叠网络,每层包含:- 多头自注意力(attention.py)
- 三角乘法-加法模块(modules.py)
3.2 扩散模型
diffusion_transformer.py实现坐标预测:
- 噪声调度:
noise_schedule()采用余弦退火策略 - 采样过程:
sample()函数通过250步扩散生成原子坐标 - 交叉注意力:
atom_cross_att_decoder()融合序列与结构信息
3.3 置信度评估
confidences.py计算关键指标:
- pTM分数:
predicted_tm_score()评估结构准确性 - PAE矩阵:
chain_pair_pae()预测残基间位置误差 - 聚类分析:
get_ranking_score()综合多模型结果排序
四、推理流程与结果输出
4.1 模型执行
model.py中Model类封装推理逻辑:
def __call__(self, batch: features.BatchDict, key: jax.Array | None = None) -> ModelResult:
# 特征嵌入
target_feat = create_target_feat_embedding(batch, self.config.evoformer, self.config.global_config)
# Evoformer前向传播
embeddings = self.evoformer(batch, target_feat=target_feat, key=key)
# 扩散采样
diffusion_output = self.diffusion_head.sample(
denoising_step=self._denoising_step,
batch=batch,
key=key,
config=self.config.heads.diffusion.eval
)
return ModelResult(
atom_positions=diffusion_output['positions'],
confidence=compute_confidence_metrics(batch, diffusion_output)
)
4.2 结果处理
- PDB文件生成:
write_output()保存预测结构 - 置信度报告:生成pLDDT柱状图数据
- 嵌入向量存储:
--save_embeddings选项保存注意力权重
五、关键模块速查表
| 功能领域 | 核心文件 | 关键函数 |
|---|---|---|
| 参数解析 | run_alphafold.py | main()、process_fold_input() |
| 数据管道 | pipeline.py | DataPipeline.process() |
| MSA处理 | msa_pairing.py | create_paired_features() |
| 注意力机制 | flash_attention.py | flash_attention() |
| 结构预测 | diffusion_head.py | sample() |
| 结果评估 | confidences.py | predicted_tm_score() |
六、扩展阅读与资源
- 官方文档:docs/installation.md提供环境配置指南
- 测试数据:test_data/包含微型数据库和特征示例
- 性能调优:通过
--buckets参数优化JAX编译缓存,参考run_alphafold_test.py
建议阅读顺序:先理解run_alphafold.py主流程,再深入model.py核心逻辑,最后通过test_data/featurised_example.pkl可视化特征结构。掌握这些内容后,可进一步探索attention.py中的高效注意力实现。
提示:使用
list_code_definition_names src/alphafold3/model命令可快速概览模型模块结构,结合search_files --path src --regex "def [A-Za-z0-9_]*\("定位关键函数实现。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



