Entrez epost + elink使用Biopython无序返回结果

时间:2014-09-10 21:22:02

标签: python bioinformatics biopython ncbi

我今天碰到了这个,想把它扔到那里。似乎在NCBI使用Biopython接口到Entrez,它不可能以正确(相同的输入)顺序返回结果(至少从elink)。请参阅下面的代码以获取示例。我有数以千计的地理标志,我需要获取分类信息,并且由于NCBI的限制,单独查询它们非常缓慢。

from Bio import Entrez
Entrez.email = "my@email.com"
ids = ["148908191", "297793721", "48525513", "507118461"]
search_results = Entrez.read(Entrez.epost("protein", id=','.join(ids)))
webenv = search_results["WebEnv"]
query_key = search_results["QueryKey"] 
print Entrez.read(Entrez.elink(webenv=webenv, 
                         query_key=query_key,
                         dbfrom="protein",
                         db="taxonomy"))

print "-------"

for i in ids:
    search_results = Entrez.read(Entrez.epost("protein", id=i))
    webenv = search_results["WebEnv"]
    query_key = search_results["QueryKey"] 
    print Entrez.read(Entrez.elink(webenv=webenv, 
                         query_key=query_key,
                         dbfrom="protein",
                         db="taxonomy"))

结果:

[{u'LinkSetDb': [{u'DbTo': 'taxonomy', u'Link': [{u'Id': '211604'}, {u'Id': '81972'}, {u'Id': '32630'}, {u'Id': '3332'}], u'LinkName': 'protein_taxonomy'}], u'DbFrom': 'protein', u'IdList': ['148908191', '297793721', '48525513', '507118461'], u'LinkSetDbHistory': [], u'ERROR': []}]
-------
[{u'LinkSetDb': [{u'DbTo': 'taxonomy', u'Link': [{u'Id': '3332'}], u'LinkName': 'protein_taxonomy'}], u'DbFrom': 'protein', u'IdList': ['148908191'], u'LinkSetDbHistory': [], u'ERROR': []}]
[{u'LinkSetDb': [{u'DbTo': 'taxonomy', u'Link': [{u'Id': '81972'}], u'LinkName': 'protein_taxonomy'}], u'DbFrom': 'protein', u'IdList': ['297793721'], u'LinkSetDbHistory': [], u'ERROR': []}]
[{u'LinkSetDb': [{u'DbTo': 'taxonomy', u'Link': [{u'Id': '211604'}], u'LinkName': 'protein_taxonomy'}], u'DbFrom': 'protein', u'IdList': ['48525513'], u'LinkSetDbHistory': [], u'ERROR': []}]
[{u'LinkSetDb': [{u'DbTo': 'taxonomy', u'Link': [{u'Id': '32630'}], u'LinkName': 'protein_taxonomy'}], u'DbFrom': 'protein', u'IdList': ['507118461'], u'LinkSetDbHistory': [], u'ERROR': []}]

NCBI的elink文档(http://www.ncbi.nlm.nih.gov/books/NBK25499/)说这应该是可能的, 但只能传递多个' id =',但这在Biopython epost界面中看起来不可能。有没有人看过这个,或者我错过了一些明显的东西。

谢谢!

1 个答案:

答案 0 :(得分:1)

from Bio import Entrez


Entrez.email = "my@email.com"
ids = ["148908191", "297793721", "48525513", "507118461"]
search_results = Entrez.read(Entrez.epost("protein", id=','.join(ids)))

xml = Entrez.efetch("protein",
                    query_key=search_results["QueryKey"],
                    WebEnv=search_results["WebEnv"],
                    rettype="gp",
                    retmode="xml")

for record in Entrez.read(xml):
    print [x[3:] for x in record["GBSeq_other-seqids"] if x.startswith("gi")]
    gb_quals = record["GBSeq_feature-table"][0]["GBFeature_quals"]
    for qualifier in gb_quals:
        if qualifier["GBQualifier_name"] == "db_xref":
            print qualifier["GBQualifier_value"]

     # Or with list comprehension
     # print [q["GBQualifier_value"] for q in
     #        record["GBSeq_feature-table"][0]["GBFeature_quals"] if
     #        q["GBQualifier_name"] == "db_xref"]


xml.close()

efetch查询,然后用Entrez.read()读取后解析xml。这是事情变得混乱的地方,你必须潜水xml-dict-list。我想有一种方法可以提取" GBFeature_quals"其中" GBQualifier_name"是" db_xref"比我的好...但这有效(到现在为止)。输出:

['148908191']
taxon:3332

['297793721']
taxon:81972

['48525513']
taxon:211604

['507118461']
taxon:32630