如何在Python中通过HTTP与UniProt交谈?

时间:2009-04-03 20:11:22

标签: python http user-agent bioinformatics

我正在尝试从UniProt获得一些结果,这是一个蛋白质数据库(细节并不重要)。我正在尝试使用一种从一种ID转换为另一种ID的脚本。我能够在浏览器上手动执行此操作,但无法在Python中执行此操作。

http://www.uniprot.org/faq/28中有一些示例脚本。我尝试了Perl,它似乎工作,所以问题是我的Python尝试。 (工作)脚本是:

## tool_example.pl ##
use strict;
use warnings;
use LWP::UserAgent;

my $base = 'http://www.uniprot.org';
my $tool = 'mapping';
my $params = {
  from => 'ACC', to => 'P_REFSEQ_AC', format => 'tab',
  query => 'P13368 P20806 Q9UM73 P97793 Q17192'
};

my $agent = LWP::UserAgent->new;
push @{$agent->requests_redirectable}, 'POST';
print STDERR "Submitting...\n";
my $response = $agent->post("$base/$tool/", $params);

while (my $wait = $response->header('Retry-After')) {
  print STDERR "Waiting ($wait)...\n";
  sleep $wait;
  print STDERR "Checking...\n";
  $response = $agent->get($response->base);
}

$response->is_success ?
  print $response->content :
  die 'Failed, got ' . $response->status_line . 
    ' for ' . $response->request->uri . "\n";

我的问题是:

1)你会如何在Python中做到这一点?

2)我是否可以大规模“缩放”它(即在查询字段中使用大量条目)?

6 个答案:

答案 0 :(得分:8)

问题#1:

这可以使用python urllib:

完成
import urllib, urllib2
import time
import sys

query = ' '.join(sys.argv)   

# encode params as a list of 2-tuples
params = ( ('from','ACC'), ('to', 'P_REFSEQ_AC'), ('format','tab'), ('query', query))
# url encode them
data = urllib.urlencode(params)    
url = 'http://www.uniprot.org/mapping/'

# fetch the data
try:
    foo = urllib2.urlopen(url, data)
except urllib2.HttpError, e:
    if e.code == 503:
        # blah blah get the value of the header...
        wait_time = int(e.hdrs.get('Retry-after', 0))
        print 'Sleeping %i seconds...' % (wait_time,)
        time.sleep(wait_time)
        foo = urllib2.urlopen(url, data)


# foo is a file-like object, do with it what you will.
foo.read()

答案 1 :(得分:1)

假设您使用的是Python 2.5。 我们可以使用httplib直接呼叫网站:

import httplib, urllib
querystring = {}
#Build the query string here from the following keys (query, format, columns, compress, limit, offset)
querystring["query"] = "" 
querystring["format"] = "" # one of html | tab | fasta | gff | txt | xml | rdf | rss | list
querystring["columns"] = "" # the columns you want comma seperated
querystring["compress"] = "" # yes or no
## These may be optional
querystring["limit"] = "" # I guess if you only want a few rows
querystring["offset"] = "" # bring on paging 

##From the examples - query=organism:9606+AND+antigen&format=xml&compress=no
##Delete the following and replace with your query
querystring = {}
querystring["query"] =  "organism:9606 AND antigen" 
querystring["format"] = "xml" #make it human readable
querystring["compress"] = "no" #I don't want to have to unzip

conn = httplib.HTTPConnection("www.uniprot.org")
conn.request("GET", "/uniprot/?"+ urllib.urlencode(querystring))
r1 = conn.getresponse()
if r1.status == 200:
   data1 = r1.read()
   print data1  #or do something with it

然后你可以创建一个关于创建查询字符串的函数,你应该离开。

答案 2 :(得分:1)

您最好使用EBI中的蛋白质标识符交叉参考服务将一组ID转换为另一组ID。它有一个非常好的REST接口。

http://www.ebi.ac.uk/Tools/picr/

我还应该提一下,UniProt有很好的网络服务。虽然如果你因某些原因使用简单的http请求,那么它可能没用。

答案 3 :(得分:1)

查看bioservices。他们通过Python连接了很多数据库。 https://pythonhosted.org/bioservices/_modules/bioservices/uniprot.html

conda install bioservices --yes

答案 4 :(得分:1)

补充O.rka答案:

问题1:

from bioservices import UniProt
u = UniProt()
res = u.get_df("P13368 P20806 Q9UM73 P97793 Q17192".split())

这将返回一个包含每个条目的所有信息的数据框。

问题2:相同的答案。这应该扩大规模。

免责声明:我是生物服务的作者

答案 5 :(得分:0)

pip中有一个python包,可以完全按照你想要的那样进行

pip install uniprot-mapper