[ PROMPT_NODE_26254 ]
Biopython 高级用法
[ SKILL_DOCUMENTATION ]
# Biopython 高级功能
## 使用 Bio.motifs 进行序列基序分析
### 创建基序
python
from Bio import motifs
from Bio.Seq import Seq
# 从实例创建基序
instances = [
Seq("TACAA"),
Seq("TACGC"),
Seq("TACAC"),
Seq("TACCC"),
Seq("AACCC"),
Seq("AATGC"),
Seq("AATGC"),
]
motif = motifs.create(instances)
### 基序共有序列与简并序列
python
# 获取共有序列
print(motif.counts.consensus)
# 获取简并共有序列 (IUPAC 模糊编码)
print(motif.counts.degenerate_consensus)
# 访问计数矩阵
print(motif.counts)
### 位置权重矩阵 (PWM)
python
# 创建位置权重矩阵
pwm = motif.counts.normalize(pseudocounts=0.5)
print(pwm)
# 计算信息含量
ic = motif.counts.information_content()
print(f"信息含量: {ic:.2f} bits")
### 搜索基序
python
from Bio.Seq import Seq
# 在序列中搜索基序
test_seq = Seq("ATACAGGACAGACATACGCATACAACATTACAC")
# 获取位置特异性评分矩阵 (PSSM)
pssm = pwm.log_odds()
# 搜索序列
for position, score in pssm.search(test_seq, threshold=5.0):
print(f"位置 {position}: 得分 = {score:.2f}")
### 从文件读取基序
python
# 从 JASPAR 格式读取基序
with open("motif.jaspar") as handle:
motif = motifs.read(handle, "jaspar")
# 读取多个基序
with open("motifs.jaspar") as handle:
for m in motifs.parse(handle, "jaspar"):
print(m.name)
# 支持的格式: jaspar, meme, transfac, pfm
### 写入基序
python
# 以 JASPAR 格式写入基序
with open("output.jaspar", "w") as handle:
handle.write(motif.format("jaspar"))
## 使用 Bio.PopGen 进行群体遗传学分析
### 处理 GenePop 文件
python
from Bio.PopGen import GenePop
# 读取 GenePop 文件
with open("data.gen") as handle:
record = GenePop.read(handle)
# 访问群体
print(f"群体数量: {len(record.populations)}")
print(f"基因座: {record.loci_list}")
# 遍历群体
for pop_idx, pop in enumerate(record.populations):
print(f"n群体 {pop_idx + 1}:")
for individual in pop:
print(f" {individual[0]}: {individual[1]}")
### 计算群体统计数据
python
from Bio.PopGen.GenePop.Controller import GenePopController
# 创建控制器
ctrl = GenePopController()
# 计算基础统计数据
result = ctrl.calc_allele_genotype_freqs("data.gen")
# 计算 Fst
fst_result = c