Does anyone here regularly access uniprot info using python? If so how?
I tried downloading
https://github.com/boscoh/uniprot through github but was unable to figure out the installation. What does everyone here use?
Does anyone here regularly access uniprot info using python? If so how?
I tried downloading
https://github.com/boscoh/uniprot through github but was unable to figure out the installation. What does everyone here use?
It amazes me that this simple thing is not answered correctly on any of the biostars questions related to this issue ("how can I get uniprot sequences via python"). It took me some time to find out how to add multiple ids in the query... hopefully this is useful for future visitors ...
def get_uniprot_sequences(uniprot_ids: List) -> pd.DataFrame:
"""
Retrieve uniprot sequences based on a list of uniprot sequence identifier.
For large lists it is recommended to perform batch retrieval.
documentation which columns are available:
https://www.uniprot.org/help/uniprotkb%5Fcolumn%5Fnames
this python script is based on
https://www.biostars.org/p/67822/
Parameters:
uniprot_ids: List, list of uniprot identifier
Returns:
pd.DataFrame, pandas dataframe with uniprot id column and sequence
"""
import urllib
url = 'https://www.uniprot.org/uploadlists/' # This is the webserver to retrieve the Uniprot data
params = {
'from': "ACC",
'to': 'ACC',
'format': 'tab',
'query': " ".join(uniprot_ids),
'columns': 'id,sequence'}
data = urllib.parse.urlencode(params)
data = data.encode('ascii')
request = urllib.request.Request(url, data)
with urllib.request.urlopen(request) as response:
res = response.read()
df_fasta = pd.read_csv(StringIO(res.decode("utf-8")), sep="\t")
df_fasta.columns = ["Entry", "Sequence", "Query"]
# it might happen that 2 different ids for a single query id are returned, split these rows
return df_fasta.assign(Query=df_fasta['Query'].str.split(',')).explode('Query')
Hey Vasam Manjveekar, I guess uniprot changed their API. I havent used the snippet for a while. When you go manually to uniprot and search "human" -> share (main window, left tab opens) -> generate URL for API you will see the new style which is probably also somewhere documented for id-mapping.
I do not regularly access Uniprot from Python, but just today solved a matching Rosalind task. My solution uses the urllib library to download the data:
import urllib
code = "Q7Z7W5"
data = urllib.urlopen("http://www.uniprot.org/uniprot/" + code + ".txt").read()
And then uses split()
to process the file line by line. Each line has some structure and starts with a two character code, like "DR". The content of the lines is reasonably well structured, and, as the Rosalind task requires, allows you to extract GO ontology term annotation.
This script will work correctly for an exercise, but if you want to download many entries you should consider adding a time.sleep() condition after the call, in order to not overload the Uniprot server. You should also consider adding your email address to the User-Agent header, as requested in the Uniprot's FAQs (http://www.uniprot.org/faq/28)
Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
which operating system do you have? On Ubuntu, try sudo apt-get install python-setuptools , and then sudo easy_install uniprot
Yes, the instructions assume that you have the pip package installer.
I have windows 7 with pip installed. When trying to install I typed 'pip install uniprot'
The installation through pip or easy_install works fine for me, in Ubuntu. In any case you can probably simply download the uniprot.py script from there, and import it from the same folder.