技能简介
DNA/RNA序列分析是生物信息学的核心技能。传统方式需要1周时间学习BioPython, 再花3天写代码处理各种格式转换...
今天介绍两个序列分析神器:BioPython 和 pysam。 使用AI辅助,30分钟即可完成序列读取、比对、翻译、BAM文件处理等全流程!
🛠️ 核心工具介绍
BioPython
生物序列处理库
- ✓ 序列读写:FASTA、GenBank、FASTQ
- ✓ 序列比对:BLAST、多序列比对
- ✓ 序列翻译:DNA→蛋白质、ORF查找
- ✓ 格式转换:各种生物序列格式互转
pysam
SAM/BAM/VCF文件处理
- ✓ BAM文件读写:比对结果处理
- ✓ 区域提取:特定染色体/区间reads
- ✓ 统计分析:比对率、覆盖度计算
- ✓ VCF处理:变异信息读取
💡 使用场景
🔍 序列统计分析
统计序列数量、长度分布、GC含量等基本特征
🧬 序列比对
与参考基因组比对,找到最佳匹配位置和相似度
🔄 翻译分析
查找ORF、翻译蛋白质、预测功能域
📊 BAM文件处理
测序数据分析必备,统计比对率、覆盖度
🎨 结果可视化
生成序列比对图、覆盖度图、ORF分布图
⚡ 批量处理
一次性处理多个序列文件,自动化分析流程
🛠️ 核心技能调用
💡 调用方式: 在 Claude Code 中直接用自然语言描述需求,AI会自动调用相应的技能并生成代码。
Step 1: 序列读取 FASTA/GenBank/FASTQ
读取FASTA文件并统计基本信息:
# 使用 BioPython 读取序列 from Bio import SeqIO # 读取FASTA文件 sequences = list(SeqIO.parse("input.fasta", "fasta")) # 统计序列数量 print(f"序列数量: {len(sequences)}") # 统计长度分布 lengths = [len(seq) for seq in sequences] print(f"平均长度: {sum(lengths) / len(lengths):.2f}") print(f"最短: {min(lengths)}, 最长: {max(lengths)}") # 计算GC含量 from Bio.SeqUtils import gc_fraction for seq in sequences[:5]: # 展示前5个 gc = gc_fraction(seq.seq) * 100 print(f"{seq.id}: GC含量 = {gc:.2f}%")
Step 2: 序列比对 BLAST在线比对
将序列与参考基因组进行BLAST比对:
from Bio.Blast import NCBIWWW # 在线BLAST比对(需要联网) sequence = "ATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG" result = NCBIWWW.qblast("blastn", "nt", sequence) # 解析比对结果 from Bio.Blast import NCBIXML blast_records = NCBIXML.parse(result) for record in blast_records: for alignment in record.alignments: for hsp in alignment.hsps: print(f"匹配: {alignment.title}") print(f"相似度: {hsp.identities / hsp.align_length * 100:.2f}%") print(f"E值: {hsp.expect}")
Step 3: 翻译分析 ORF查找 + 蛋白质翻译
查找开放阅读框(ORF)并翻译成蛋白质:
from Bio import Seq from Bio.SeqUtils import seq1 # 创建DNA序列对象 dna_seq = Seq("ATGGCCATTGTAATGGGCCGCCTGAAAGGGTGCCCGATAG") # 翻译成蛋白质 protein = dna_seq.translate() print(f"蛋白质序列: {protein}") # 查找所有ORF(开放阅读框) def find_orfs(sequence, min_len=100): orfs = [] for frame, strand in [(1, 1), (2, 1), (3, 1), (1, -1), (2, -1), (3, -1)]: # 6个读码框 if strand == 1: seq = sequence[frame-1:] else: seq = sequence.reverse_complement()[frame-1:] for i in range(0, len(seq) - 2, 3): codon = seq[i:i+3] if len(codon) == 3 and codon == "ATG": # 起始密码子 for j in range(i+3, len(seq) - 2, 3): if seq[j:j+3] in ["TAA", "TAG", "TGA"]: # 终止密码子 orf_seq = seq[i:j+3] if len(orf_seq) >= min_len: orfs.append(orf_seq.translate()) break return orfs orfs = find_orfs(dna_seq) print(f"找到 {len(orfs)} 个ORF")
Step 4: BAM文件处理 pysam处理比对结果
使用pysam处理BAM文件:
import pysam # 打开BAM文件 bamfile = pysam.AlignmentFile("aligned.bam", "rb") # 统计比对率 total = 0 mapped = 0 for read in bamfile.fetch(): total += 1 if not read.is_unmapped: mapped += 1 mapping_rate = mapped / total * 100 print(f"比对率: {mapping_rate:.2f}%") # 提取特定区域的reads chr_name = "chr1" start = 1000000 end = 1005000 reads = bamfile.count_coverage(chr_name, start, end) print(f"区域 {chr_name}:{start}-{end} 的覆盖度: {reads}") # 计算平均覆盖度 avg_coverage = sum(reads[0]) / len(reads[0]) print(f"平均覆盖度: {avg_coverage:.2f}x") bamfile.close()
Step 5: 结果可视化 序列比对图 + 覆盖度图
生成序列分析可视化图表:
import matplotlib.pyplot as plt import pysam # 1. 覆盖度图 bamfile = pysam.AlignmentFile("aligned.bam", "rb") chr_name = "chr1" start, end = 1000000, 1005000 coverage = [] for pos in range(start, end, 100): cov = bamfile.count(chr_name, pos, pos+100) coverage.append(cov) plt.figure(figsize=(12, 4)) plt.plot(range(start, end, 100), coverage) plt.xlabel("Position") plt.ylabel("Coverage") plt.title(f"Coverage of {chr_name}:{start}-{end}") plt.savefig("coverage.png", dpi=300) # 2. GC含量分布图 from Bio.SeqUtils import gc_fraction gc_contents = [gc_fraction(seq.seq) for seq in sequences] plt.figure(figsize=(8, 6)) plt.hist(gc_contents, bins=20, edgecolor='black') plt.xlabel('GC Content') plt.ylabel('Frequency') plt.title('GC Content Distribution') plt.savefig("gc_distribution.png", dpi=300)
📋 常用序列格式
| 格式 | 说明 | 用途 | 处理工具 |
|---|---|---|---|
FASTA |
序列 + 描述 | 通用序列存储 | BioPython |
FASTQ |
序列 + 质量值 | 测序原始数据 | BioPython |
GenBank |
序列 + 注释 | 基因组注释 | BioPython |
SAM/BAM |
比对结果 | 测序比对 | pysam |
VCF |
变异信息 | 变异检测 | pysam |
⚠️ 注意事项
📌 BLAST限制: 在线BLAST有请求频率限制,大量序列比对建议使用本地BLAST+。
⚠️ BAM文件索引:
使用pysam前需要用 samtools index 为BAM文件创建索引,否则无法高效查询。
💡 内存优化:
处理大型BAM文件时,使用 fetch() 指定区域而非读取整个文件,可大幅降低内存占用。
🔗 相关技能链接
📝 效率对比
😰 传统方式
- • 学习BioPython:1周
- • 编写代码:3天
- • 调试优化:2天
- 总计:约2周
🚀 AI辅助
- • 自然语言描述需求:5分钟
- • AI生成代码:5分钟
- • 运行验证:20分钟
- 总计:约30分钟
⚡ 效率提升 20倍 以上,代码可复用!