我怎样才能跟的UniProt通过HTTP在Python?
-
23-08-2019 - |
题
我想从UniProt的,这是一种蛋白质数据库(细节并不重要)的一些结果。我试图用一些脚本,从一种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)I是否能够大规模“刻度”即(即,使用了大量的条目在查询字段)?
解决方案
<强>问题#1:强>
此可以使用Python的urllibs来完成:
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()
其他提示
您可能会更好过使用从EBI蛋白质标识交叉引用服务于一组ID,以另一种的转换。它具有非常好的REST接口。
http://www.ebi.ac.uk/Tools/picr/一>
我还要提到的是UniProt的具有可很好的web服务。但如果你是绑用简单的HTTP请求,出于某种原因,然后它可能是没有用的。
让我们假设你正在使用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
您可以再作函数围绕创建查询字符串,你应该离开。
检查了这一点bioservices
。他们对接了很多数据库通过Python的。
https://pythonhosted.org/bioservices/_modules/bioservices/uniprot.html
conda install bioservices --yes
在补充O.rka答案:
<强>问题1:强>
from bioservices import UniProt
u = UniProt()
res = u.get_df("P13368 P20806 Q9UM73 P97793 Q17192".split())
此返回一个数据帧与关于每个条目的所有信息。
<强>问题2:强>相同的答案。这应该扩大。
<强>声明强>:我bioservices的作者
有一个Python包在PIP其中不正是你想要的。
pip install uniprot-mapper
不隶属于 StackOverflow