Ensembl REST API – urllib2
HTTP is based on requests and responses Using urllib2 we can make http request using python and get a response from the server. Try the example below and see what it does:
---------------------------------------------------------------------------
URLError Traceback (most recent call last)
<ipython-input-2-21b203025b8a> in <module>()
1 import urllib2
2 request = urllib2.Request('http://rest.ensembl.org')
----> 3 response = urllib2.urlopen(request)
4 html = response.read()
5 print html
/ext/sage/sage-8.0/local/lib/python2.7/urllib2.pyc in urlopen(url, data, timeout, cafile, capath, cadefault, context)
152 else:
153 opener = _opener
--> 154 return opener.open(url, data, timeout)
155
156 def install_opener(opener):
/ext/sage/sage-8.0/local/lib/python2.7/urllib2.pyc in open(self, fullurl, data, timeout)
427 req = meth(req)
428
--> 429 response = self._open(req, data)
430
431 # post-process response
/ext/sage/sage-8.0/local/lib/python2.7/urllib2.pyc in _open(self, req, data)
445 protocol = req.get_type()
446 result = self._call_chain(self.handle_open, protocol, protocol +
--> 447 '_open', req)
448 if result:
449 return result
/ext/sage/sage-8.0/local/lib/python2.7/urllib2.pyc in _call_chain(self, chain, kind, meth_name, *args)
405 func = getattr(handler, meth_name)
406
--> 407 result = func(*args)
408 if result is not None:
409 return result
/ext/sage/sage-8.0/local/lib/python2.7/urllib2.pyc in http_open(self, req)
1226
1227 def http_open(self, req):
-> 1228 return self.do_open(httplib.HTTPConnection, req)
1229
1230 http_request = AbstractHTTPHandler.do_request_
/ext/sage/sage-8.0/local/lib/python2.7/urllib2.pyc in do_open(self, http_class, req, **http_conn_args)
1196 except socket.error, err: # XXX what error?
1197 h.close()
-> 1198 raise URLError(err)
1199 else:
1200 try:
URLError: <urlopen error [Errno 110] Connection timed out>
Ensembl REST API – urllib2
Use urllib2 to: - Find the Ensembl ID of P53 Print the content of the response
Ensembl REST API – JSON – parsing
Try to understand the script below:
Ensembl REST API – JSON exercise
Lets download a JSON file and parse it
Use wget to store, or download via the browser: http://rest.ensembl.org/info/assembly/homo_sapiens?content-type=application/json
Read the file with Python and parse the file
Print the following information:
Assembly name and date
Name and length of the chromosomes
Ensembl REST API – urllib2 and JSON
Use and try to understand the following script:
Ensembl REST API – Last exercise
Use urllib2 and json to make a small tool:
Ask the user for a gene symbol (e.g. BRCA2)
Find the ensembl id of that gene
Print the gene and Ensembl ID
Request all transcripts of the gene using the Ensembl ID Print for each transcript (separated by a tab):
ID
Location = chr:start-end
Biotype
Extra - Make functions for:
Rest requests -> EnsemblRestRequest(endpoint)
Get Ensembl stable gene ID -> GetEnsemblGeneId(gene_symbol)
Get transcripts -> GetTranscripts(gene_ensembl_id)