This notebook is a collection of useful functions used in other notebooks.
You should generally use the production URL of the API.
In [1]:
PDBE_API_URL = "http://www.ebi.ac.uk/pdbe/api"
The API also has a development instance meant for beta testing and pre-release hosting, available at :
In [8]:
PDBE_API_URL = "http://www.ebi.ac.uk/pdbe/api"
At the time of writing this notebook (Nov 2014), the API was not released on www. So let us use the wwwdev instance, otherwise just comment out the line above.
Browse the API documentation interactively at PDBE_API_URL + "/doc", i.e. http://www.ebi.ac.uk/pdbe/api/doc
In [9]:
import logging, sys
# configure the logger
# btw, reload is just a hack to make logging work in the notebook, it's usually uncessary
reload(logging)
logging.basicConfig(
level=logging.DEBUG, stream=sys.stdout,
format='LOG|%(asctime)s|%(levelname)s %(message)s', datefmt='%d-%b-%Y %H:%M:%S'
)
The API call you made can fail (very rarely) due to a variety of reasons beyond your control, e.g. network, overloading of our http servers, temporary failure of our databases, etc. Hence it is always more robust to try the call, say thrice, before raising an exception, or reporting a bug to the PDBe. Let us write a function for this which will return python object for equivalent to the json returned by the API.
In [10]:
import json, urllib2
def get_PDBe_API_data(api_url) :
num_trials = 3
for trial_number in range(num_trials) :
try :
return json.loads(urllib2.urlopen(api_url).read())
except Exception, err :
logging.warn("Error fetching PDBe-API data! Trial number %d for call %s" % (trial_number, api_url))
if trial_number == num_trials-1 :
raise err