用biopython从entrez获取基因序列

2024-10-01 09:27:01 发布

您现在位置:Python中文网/ 问答频道 /正文

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

在查找entrez的biopython帮助和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'所以:

^{pr2}$

这给了我我想要的信息,包括序列。在

现在我们来回答这个问题: 我怎样才能搜索基因名(因为我没有id号)或者很容易地将我的基因名转换成id来获得我拥有的基因列表的序列。在

谢谢你


Tags: apiid列表基因教程序列entrezout
2条回答

首先是基因名,例如:ATK1

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

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

^{pr2}$

如果没有找到id,则返回id作为一个列表。现在假设它返回列表中的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'))

看看本教程的第8.3节,似乎有一个函数可以让您搜索术语并获得相应的id(我对这个库一无所知,对生物学更不了解,因此这可能完全错误:))。在

>>> handle = Entrez.esearch(db="nucleotide",term="Cypripedioideae[Orgn] AND matK[Gene]")
>>> record = Entrez.read(handle)
>>> record["Count"]
'25'
>>> record["IdList"]
['126789333', '37222967', '37222966', '37222965', ..., '61585492']

据我所知,id指的是esearch函数(在响应的IdList属性中)返回的实际ID号。但是,如果使用term关键字,则可以运行搜索并获取匹配项的ID。完全未测试,但假设搜索支持布尔运算符(看起来AND有效),您可以尝试使用如下查询:

^{pr2}$

要生成要插入的术语,可以执行以下操作:

In [1]: l = ['ITGB1', 'RELA', 'NFKBIA']

In [2]: ' OR '.join('%s[Gene]' % i for i in l)
Out[2]: 'ITGB1[Gene] OR RELA[Gene] OR NFKBIA[Gene]'

然后,record["IdList"]可以转换为逗号分隔的字符串,并通过如下方法传递给原始查询中的id参数:

In [3]: r = ['1234', '5678', '91011']

In [4]: ids = ','.join(r)

In [5]: ids
Out[5]: '1234,5678,91011'

相关问题 更多 >