[ PROMPT_NODE_26256 ]
alignment
[ SKILL_DOCUMENTATION ]
# 使用 Bio.Align 和 Bio.AlignIO 进行序列比对
## 概述
Bio.Align 提供了使用各种算法进行双序列比对的工具,而 Bio.AlignIO 则用于处理多种格式的多序列比对文件的读写。
## 使用 Bio.Align 进行双序列比对
### PairwiseAligner 类
`PairwiseAligner` 类使用 Needleman-Wunsch(全局)、Smith-Waterman(局部)、Gotoh(三状态)和 Waterman-Smith-Beyer 算法执行双序列比对。算法会根据空位得分参数自动选择。
### 创建比对器
python
from Bio import Align
# 使用默认参数创建比对器
aligner = Align.PairwiseAligner()
# 默认得分 (Biopython 1.85+):
# - 匹配得分: +1.0
# - 不匹配得分: 0.0
# - 所有空位得分: -1.0
### 自定义比对参数
python
# 设置评分参数
aligner.match_score = 2.0
aligner.mismatch_score = -1.0
aligner.gap_score = -0.5
# 或使用单独的空位开放/延伸惩罚
aligner.open_gap_score = -2.0
aligner.extend_gap_score = -0.5
# 单独设置内部空位得分
aligner.internal_open_gap_score = -2.0
aligner.internal_extend_gap_score = -0.5
# 设置末端空位得分 (用于半全局比对)
aligner.left_open_gap_score = 0.0
aligner.left_extend_gap_score = 0.0
aligner.right_open_gap_score = 0.0
aligner.right_extend_gap_score = 0.0
### 比对模式
python
# 全局比对 (默认)
aligner.mode = 'global'
# 局部比对
aligner.mode = 'local'
### 执行比对
python
from Bio.Seq import Seq
seq1 = Seq("ACCGGT")
seq2 = Seq("ACGGT")
# 获取所有最优比对
alignments = aligner.align(seq1, seq2)
# 遍历比对结果
for alignment in alignments:
print(alignment)
print(f"得分: {alignment.score}")
# 仅获取得分
score = aligner.score(seq1, seq2)
### 使用替换矩阵
python
from Bio.Align import substitution_matrices
# 加载替换矩阵
matrix = substitution_matrices.load("BLOSUM62")
aligner.substitution_matrix = matrix
# 比对蛋白质序列
protein1 = Seq("KEVLA")
protein2 = Seq("KSVLA")
alignments = aligner.align(protein1, protein2)
### 可用的替换矩阵
常用矩阵包括:
- **BLOSUM** 系列 (BLOSUM45, BLOSUM50, BLOSUM62, BLOSUM80, BLOSUM90)
- **PAM** 系列 (PAM30, PAM70, PAM250)
- **MATCH** - 简单的匹配/不匹配矩阵
python
# 列出可用矩阵
available = subs