python - 使用biopython从entrez获取基因序列

标签 python biopython ncbi

这就是我想要做的。 我有一个基因名称列表,例如:[ITGB1、RELA、NFKBIA]

查找 biopython 中的帮助和 entrez 的 API 教程,我想到了这个:

x = ['ITGB1', 'RELA', 'NFKBIA']
for item in x:
    handle = Entrez.efetch(db="nucleotide", id=item ,rettype="gb")
    record = handle.read()
    out_handle = open('genes/'+item+'.xml', 'w') #to create a file with gene name
    out_handle.write(record)
    out_handle.close

但这总是出错。我发现如果 id 是一个数字 id(虽然你必须将它变成一个字符串才能使用,'186972394' 所以:

handle = Entrez.efetch(db="nucleotide", id='186972394' ,rettype="gb")

这让我得到了我想要的信息,其中包括序列。

那么现在回答这个问题: 我如何搜索基因名称(因为我没有 ID 号)或轻松地将我的基因名称转换为 ID 以获得我拥有的基因列表的序列。

谢谢,

最佳答案

首先是基因名eg: ATK1

item = 'ATK1'
animal = 'Homo sapien' 
search_string = item+"[Gene] AND "+animal+"[Organism] AND mRNA[Filter] AND RefSeq[Filter]"

现在我们有一个搜索字符串来搜索 id

handle = Entrez.esearch(db="nucleotide", term=search_string)
record = Entrez.read(handleA)
ids = record['IdList']

如果没有找到 id,则返回 ids 作为列表,它是 []。现在假设它返回列表中的 1 个项目。

seq_id = ids[0] #you must implement an if to deal with <0 or >1 cases
handle = Entrez.efetch(db="nucleotide", id=seq_id, rettype="fasta", retmode="text")
record = handleA.read()

这将为您提供一个可以保存到文件中的 fasta 字符串

out_handle = open('myfasta.fasta', 'w')
out_handle.write(record.rstrip('\n'))

关于python - 使用biopython从entrez获取基因序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13557981/

相关文章:

python - 如何使用 Biopython 查找蛋白质的核苷酸序列?

python - Biopython 1.60 中的 Bio.Entrez 和蛋白质问题

python - 如何按比例将数组调整到一定长度?

python - 两只 turtle 随机运动

python - 从 Genbank 记录或 Entrez 查询中提取病毒宿主

python - 从 fasta 文件估计 Biopython 中的字母表

python - 在 Python 中从 FASTA 制作 Blast 数据库

python - 使用curl来POST并更新多个数据

Python Scrapy - Ajax 分页 Tripadvisor

python - 将距离矩阵转换为 newick 字符串格式的系统发育树