Using the Open PHACTS API with Python

Prerequisities:

  • Register and get an application id and app key at http://dev.openphacts.org
  • I list the app_key and app_id here for an example. Please don't abuse it. :-)

Wrapper functions for the Open PHACTS API


In [3]:
import json
import urllib2
from IPython import display
from pprint import pprint

app_id = "0e939a76"
app_key = "1004d9ef5f4ee1ab0bbfc02b623cb955"

def getOPSUrlForString(qStr):
    results = json.load(urllib2.urlopen("https://beta.openphacts.org/search/freetext?app_id=" + app_id + "&app_key=" + app_key + "&q="+ qStr + "&_format=json"))
    res = results['result']['primaryTopic']['result'][0]['_about']
    return res

def getCompoundInformation(uri):
    results = json.load(urllib2.urlopen("https://beta.openphacts.org/compound?app_id=" + app_id + "&app_key=" + app_key + "&uri="+ uri + "&_format=json"))
    res = results['result']['primaryTopic']
    return res

def getCompoundImage(compoundInfo):
    for em in compoundInfo['exactMatch']:
        if type(em) == dict:
           if em['inDataset'] == "http://www.chemspider.com":
                csid = em['_about'].lstrip("http://rdf.chemspider.com/")
                return display.Image(url='http://www.chemspider.com/ImagesHandler.ashx?id='+csid)

Example Usage: Getting a picture of Sorafenib


In [4]:
uri = getOPSUrlForString('sorafenib')
res = getCompoundInformation(uri)
getCompoundImage(res)


---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
<ipython-input-4-a8933f3fd302> in <module>()
      1 uri = getOPSUrlForString('sorafenib')
----> 2 res = getCompoundInformation(uri)
      3 getCompoundImage(res)

<ipython-input-3-72403e26df17> in getCompoundInformation(uri)
     13 
     14 def getCompoundInformation(uri):
---> 15     results = json.load(urllib2.urlopen("https://beta.openphacts.org/compound?app_id=" + app_id + "&app_key=" + app_key + "&uri="+ uri + "&_format=json"))
     16     res = results['result']['primaryTopic']
     17     return res

/usr/local/Cellar/python/2.7.3/lib/python2.7/urllib2.pyc in urlopen(url, data, timeout)
    124     if _opener is None:
    125         _opener = build_opener()
--> 126     return _opener.open(url, data, timeout)
    127 
    128 def install_opener(opener):

/usr/local/Cellar/python/2.7.3/lib/python2.7/urllib2.pyc in open(self, fullurl, data, timeout)
    404         for processor in self.process_response.get(protocol, []):
    405             meth = getattr(processor, meth_name)
--> 406             response = meth(req, response)
    407 
    408         return response

/usr/local/Cellar/python/2.7.3/lib/python2.7/urllib2.pyc in http_response(self, request, response)
    517         if not (200 <= code < 300):
    518             response = self.parent.error(
--> 519                 'http', request, response, code, msg, hdrs)
    520 
    521         return response

/usr/local/Cellar/python/2.7.3/lib/python2.7/urllib2.pyc in error(self, proto, *args)
    442         if http_err:
    443             args = (dict, 'default', 'http_error_default') + orig_args
--> 444             return self._call_chain(*args)
    445 
    446 # XXX probably also want an abstract factory that knows when it makes

/usr/local/Cellar/python/2.7.3/lib/python2.7/urllib2.pyc in _call_chain(self, chain, kind, meth_name, *args)
    376             func = getattr(handler, meth_name)
    377 
--> 378             result = func(*args)
    379             if result is not None:
    380                 return result

/usr/local/Cellar/python/2.7.3/lib/python2.7/urllib2.pyc in http_error_default(self, req, fp, code, msg, hdrs)
    525 class HTTPDefaultErrorHandler(BaseHandler):
    526     def http_error_default(self, req, fp, code, msg, hdrs):
--> 527         raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    528 
    529 class HTTPRedirectHandler(BaseHandler):

HTTPError: HTTP Error 404: Not Found

Example Usage: Just output the json we get back from getCompoundInformation()


In [8]:
res


Out[8]:
{u'_about': u'http://www.conceptwiki.org/concept/38932552-111f-4a4e-a46a-4ed1d7bdf9d5',
 u'exactMatch': [u'http://www.conceptwiki.org/concept/38932552-111f-4a4e-a46a-4ed1d7bdf9d5',
  {u'_about': u'http://data.kasabi.com/dataset/chembl-rdf/chemblid/CHEMBL1336',
   u'full_mwt': 464.825,
   u'inDataset': u'http://data.kasabi.com/dataset/chembl-rdf',
   u'molform': u'C21H16ClF3N4O3',
   u'mw_freebase': 464.825,
   u'rtb': 6},
  {u'_about': u'http://rdf.chemspider.com/187440',
   u'hba': 7,
   u'hbd': 3,
   u'inDataset': u'http://www.chemspider.com',
   u'inchi': u'InChI=1S/C21H16ClF3N4O3/c1-26-19(30)18-11-15(8-9-27-18)32-14-5-2-12(3-6-14)28-20(31)29-13-4-7-17(22)16(10-13)21(23,24)25/h2-11H,1H3,(H,26,30)(H2,28,29,31)',
   u'inchikey': u'MLDQJTXFUGDVEO-UHFFFAOYSA-N',
   u'logp': 4.818,
   u'psa': 9.235e-18,
   u'ro5_violations': 0,
   u'smiles': u'CNC(=O)c1cc(ccn1)Oc2ccc(cc2)NC(=O)Nc3ccc(c(c3)C(F)(F)F)Cl'},
  {u'_about': u'http://www4.wiwiss.fu-berlin.de/drugbank/resource/drugs/DB00398',
   u'biotransformation': u'Sorafenib is metabolized primarily in the liver, undergoing oxidative metabolism, mediated by CYP3A4, as well as glucuronidation mediated by UGT1A9. Sorafenib accounts for approximately 70-85% of the circulating analytes in plasma at steady- state. Eight metabolites of sorafenib have been identified, of which five have been detected in plasma. The main circulating metabolite of sorafenib in plasma, the pyridine N-oxide, shows <i>in vitro</i> potency similar to that of sorafenib. This metabolite comprises approximately 9-16% of circulating analytes at steady-state.',
   u'description': u'Sorafenib (rINN), marketed as Nexavar by Bayer, is a drug approved for the treatment of advanced renal cell carcinoma (primary kidney cancer). It has also received "Fast Track" designation by the FDA for the treatment of advanced hepatocellular carcinoma (primary liver cancer), and has since performed well in Phase III trials.\nSorafenib is a small molecular inhibitor of Raf kinase, PDGF (platelet-derived growth factor), VEGF receptor 2 & 3 kinases and c Kit the receptor for Stem cell factor. A growing number of drugs target most of these pathways. The originality of Sorafenib lays in its simultaneous targeting of the Raf/Mek/Erk pathway.',
   u'inDataset': u'http://linkedlifedata.com/resource/drugbank',
   u'proteinBinding': u'99.5%',
   u'toxicity': u'The highest dose of sorafenib studied clinically is 800 mg twice daily. The adverse reactions observed at this dose were primarily diarrhea and dermatologic events. No information is available on symptoms of acute overdose in animals because of the saturation of absorption in oral acute toxicity studies conducted in animals.'}],
 u'inDataset': u'http://www.conceptwiki.org',
 u'isPrimaryTopicOf': u'https://beta.openphacts.org/compound?_format=json&uri=http%3A%2F%2Fwww.conceptwiki.org%2Fconcept%2F38932552-111f-4a4e-a46a-4ed1d7bdf9d5&app_id=a409dcc9&app_key=ad4fca9111f258325e3ca50e7217dcbc',
 u'prefLabel': u'Sorafenib',
 u'prefLabel_en': u'Sorafenib'}