You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Biopython的Entrez.efetch()获取假单胞菌基因组遇HTTP 400错误求助

解决NCBI假单胞菌基因组16S rRNA序列提取问题

方法1:通过Assembly数据库获取对应核苷酸ID后抓取序列

你遇到的400错误核心是efetch参数组合不匹配——Assembly数据库的ID无法直接用于nucleotide库,且需明确指定数据返回格式。以下是修正后的代码流程:

from Bio import Entrez

# NCBI强制要求设置邮箱,否则可能被限制访问
Entrez.email = "your_email@example.com"

# 搜索目标Assembly号,获取Assembly ID
handle = Entrez.esearch(db="assembly", term="GCA_000474765.1")
record = Entrez.read(handle)
handle.close()
assembly_id = record["IdList"][0]

# 从Assembly摘要中提取对应的GenBank核苷酸访问号
handle = Entrez.esummary(db="assembly", id=assembly_id)
summary = Entrez.read(handle)
handle.close()
# 该基因组对应的核苷酸访问号为CP007047.1,可从摘要中直接提取
nucl_accession = summary["DocumentSummarySet"]["DocumentSummary"][0]["Genbank"][0]["AssemblyAccession"]

# 用核苷酸访问号抓取全基因组序列
handle = Entrez.efetch(db="nucleotide", id=nucl_accession, rettype="fasta", retmode="text")
genome_seq = handle.read()
handle.close()

# 保存序列到本地文件
with open("pseudomonas_genome.fasta", "w") as f:
    f.write(genome_seq)

方法2:直接搜索并提取16S rRNA序列(无需全基因组)

若仅需16S序列,可跳过全基因组下载,直接关联Assembly号搜索目标序列:

from Bio import Entrez, SeqIO

Entrez.email = "your_email@example.com"

# 搜索该基因组对应的16S rRNA条目
handle = Entrez.esearch(db="nucleotide", term="GCA_000474765.1 AND 16S rRNA")
record = Entrez.read(handle)
handle.close()

# 抓取并保存第一条匹配序列
if record["IdList"]:
    handle = Entrez.efetch(db="nucleotide", id=record["IdList"][0], rettype="fasta", retmode="text")
    seq_record = SeqIO.read(handle, "fasta")
    handle.close()
    # 打印或保存序列
    print(seq_record.format("fasta"))
    SeqIO.write(seq_record, "pseudomonas_16S.fasta", "fasta")
else:
    print("未找到对应16S rRNA序列")

关键注意事项

  • 必须设置邮箱:NCBI对无邮箱的Entrez请求会做访问限制
  • 参数匹配:Assembly ID仅适用于assembly库的搜索/摘要请求,获取序列需切换到nucleotide库,使用对应的核苷酸访问号
  • 返回格式:efetch必须明确指定rettype="fasta"和retmode="text",避免因格式不兼容触发400错误

内容的提问来源于stack exchange,提问作者Sergio Cohecha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 07:05:20