Skip to content

Instantly share code, notes, and snippets.

@sjcockell
Created March 11, 2010 22:11
Show Gist options
  • Save sjcockell/329730 to your computer and use it in GitHub Desktop.
Save sjcockell/329730 to your computer and use it in GitHub Desktop.
A Python method to map protein IDs via the UniProt mapping service
import urllib
import urllib2
def uniprot_mapping(fromtype, totype, identifier):
"""Takes an identifier, and types of identifier
(to and from), and calls the UniProt mapping service"""
base = 'http://www.uniprot.org'
tool = 'mapping'
params = {'from':fromtype,
'to':totype,
'format':'tab',
'query':identifier,
}
#urllib turns the dictionary params into an encoded url suffix
data = urllib.urlencode(params)
#construct the UniProt URL
url = base+'/'+tool+'?'+data
#and grab the mapping
response = urllib2.urlopen(url)
#response.read() provides tab-delimited output of the mapping
return response.read()
if __name__ == '__main__':
print uniprot_mapping('EMBL_ID', 'ID', 'X02469')
#see http://www.uniprot.org/faq/28#id_mapping_examples for from&to codes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment