2009-04-03 16 views
5

Sto cercando di ottenere alcuni risultati da UniProt, che è un database di proteine ​​(i dettagli non sono importanti). Sto cercando di utilizzare alcuni script che si traducono da un tipo di ID a un altro. Sono stato in grado di farlo manualmente sul browser, ma non potevo farlo in Python.Come posso parlare con UniProt su HTTP in Python?

In http://www.uniprot.org/faq/28 ci sono alcuni esempi di script. Ho provato il Perl e sembra funzionare, quindi il problema sono i miei tentativi Python. L'(di lavoro) script è:

## 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"; 

Le mie domande sono:

1) Come farlo in Python?

2) Sarò in grado di "ridimensionare" in modo massiccio (ad esempio, utilizzare molte voci nel campo query)?

+0

aggiungere il vostro codice di pitone tentativo – nosklo

+0

era praticamente aprendo lo stesso indirizzo come vorrei nel browser, con urllib2.urlopen. –

risposta

8

domanda # 1:

Questo può essere fatto utilizzando urllibs di pitone:

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() 
+0

Ho paura che non funzioni (anche dopo aver sostituito quello '=' con '==') ... Grazie. :) –

1

Supponiamo che si sta utilizzando Python 2.5. Possiamo usare httplib per chiamare direttamente il sito web:

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 

È quindi possibile fare una funzione intorno creando la stringa di query e si dovrebbe essere lontano.

+1

Questo sfortunatamente ha lo stesso effetto dei miei altri tentativi - sospeso per minuti. Inoltre, è "GET", che a mio avviso lo limita alla dimensione url solo .. –

+0

Penso che questo sia il punto del limite e dei valori delle query delle colonne, non sapendo cosa sto restituendo non posso dare buoni valori per questi. Il GET limita ciò che si invia non quello che si riceve. –

1

Probabilmente stai meglio utilizzando il servizio di riferimento incrociato Protein Identifier dall'EBI per convertire un set di ID in un altro. Ha un'ottima interfaccia REST.

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

Vorrei anche ricordare che UniProt ha ottime webservices disponibili. Se per qualche motivo sei legato all'utilizzo di semplici richieste http, probabilmente non è utile.

0

C'è un pacchetto python in pip che fa esattamente quello che vuoi

pip install uniprot-mapper 
0

a complemento O.rka risposta:

Domanda 1:

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

Questo restituisce un dataframe con tutte le informazioni su ogni voce.

Domanda 2: stessa risposta. Questo dovrebbe aumentare.

Diniego: Sono l'autore di Bioservices