AF3 make_template_features函数解读
make_template_features
函数的主要作用是基于输入序列 (input_sequence
) 和模板匹配结果 (hits,
Sequence[TemplateHit]格式),通过模板特征化器 (template_featurizer
,通常是 CustomHitFeaturizer
),生成用于 AlphaFold3 预测的模板特征。
源代码:
def make_template_features(
input_sequence: str,
hits: Sequence[Any],
template_featurizer: Any,
) -> FeatureDict:
hits_cat = sum(hits.values(), [])
if template_featurizer is None or (
len(hits_cat) == 0 and not isinstance(template_featurizer, CustomHitFeaturizer)
):
template_features = empty_template_feats(len(input_sequence))
else:
templates_result = template_featurizer.get_templates(
query_sequence=input_sequence,
hits=hits_cat,
)
template_features = templates_result.features
return template_features
函数参数
def make_template_features(
input_sequence: str,
hits: Sequence[Any],
template_featurizer: Any,
) -> FeatureDict:
input_sequence
:输入的蛋白质序列(氨基酸字符串)。hits
:模板比对结果,是来自 HMM 或 HHsearch 的比对命中。template_featurizer
:模板特征生成器,通常是CustomHitFeaturizer
,负责从比对结果中生成结构模板特征。
核心逻辑解析
1. 合并 hits
:
hits_cat = sum(hits.values(), [])
hits
是一个字典,键是键为比对文件名,值是Sequence[TemplateHit]。- 通过
sum
将所有方法的比对结果合并为单个列表hits_cat
。
假设 hits
为:
hits = {
"example.hhr": [hit1, hit2],
"hmm_output.sto": [hit3, hit4]
}
执行 sum(hits.values(), [])
后,hits_cat
结果为:
hits_cat = [hit1, hit2, hit3, hit4]
注:AlphaFold3中 hit 为TemplateHit数据类型。
2. 无模板特征化器时,生成空模板特征:
if template_featurizer is None or (
len(hits_cat) == 0 and not isinstance(template_featurizer, CustomHitFeaturizer)
):
template_features = empty_template_feats(len(input_sequence))
- 情况 1:
template_featurizer
为None
,说明不使用模板特征。 - 情况 2:
hits_cat
为空且特征化器不是自定义的CustomHitFeaturizer
。 - 此时调用
empty_template_feats
生成空模板特征,保持维度一致但无有效信息。
3. 使用模板特征化器生成特征:
else:
templates_result = template_featurizer.get_templates(
query_sequence=input_sequence,
hits=hits_cat,
)
template_features = templates_result.features
- 如果存在比对结果和模板特征化器,则调用
get_templates
方法。 template_featurizer
会根据输入序列和比对结果,从 mmCIF 或 PDB 文件中提取模板特征。- 提取的模板特征保存在
templates_result.features
中,包含比对分数、序列比对情况、结构信息等。
4. 返回模板特征:
return template_features
最终返回的 template_features
是一个特征字典FeatureDict,包含以下关键特征:
template_aatype
:模板氨基酸类型,形状为[num_templates, num_residues, 22]
。template_all_atom_positions
:模板原子坐标,形状为[num_templates, num_residues, 37, 3]
。template_all_atom_mask
:原子存在掩码,标识哪些位置有原子,形状为[num_templates, num_residues, 37]
。template_sum_probs
:比对置信度,形状为[num_templates,]
。
执行流程总结
- 合并比对结果
hits
。 - 如果没有模板特征化器或比对失败,生成空模板特征。
- 如果存在有效比对,调用
template_featurizer.get_templates
生成特征。 - 返回包含模板结构的特征字典。
应用场景
- FASTA 输入:比对 HMM/HHsearch 结果生成模板特征。
- PDB/mmCIF 输入:通过链 ID 限制,生成匹配的结构特征。
- 缺失模板时:生成空特征,保持模型输入维度一致。