Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 1572
Kernel: Python 2 (SageMath)

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:

import urllib2 request = urllib2.Request('http://rest.ensembl.org') response = urllib2.urlopen(request) html = response.read() print html
--------------------------------------------------------------------------- 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

- Parse the response Store the Ensembl gene ID in a variable - Request all transcripts of P53 Use the Ensembl gene ID variable Print the content of the response
# Type your code here

Ensembl REST API – JSON – parsing

Try to understand the script below:

import json json_string = '[{"key":"value","key_2":3.0},{"key_3":[2, 4]}]' json_parsed = json.loads(json_string) print "JSON_STRING =", json_string print "JSON_PARSED =", json_parsed print json_parsed[0] print json_parsed[1]['key_3']

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

# Read the file with Python and parse the file # Type your code here
# Print the following information: # Assembly name and date # Type your code here
# Name and length of the chromosomes # Type your code here

Ensembl REST API – urllib2 and JSON

Use and try to understand the following script:

import urllib2 import json server = "http://rest.ensembl.org" endpoint = "/xrefs/symbol/homo_sapiens/BRCA2" headers = {} headers['Content-Type'] = 'application/json' request = urllib2.Request(server + endpoint, headers=headers) response = urllib2.urlopen(request) content = response.read() data = json.loads(content) print data[0]['id']

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)

# Type your code here