pythonweb-servicesbioservices

How to interconvert InChI and InChIKey?


I would like to retrieve IDs from several databases using InChI as an input, e.g.

InChI=1S/C6H14N2O2/c7-4-2-1-3-5(8)6(9)10/h5H,1-4,7-8H2,(H,9,10)/t5-/m0/s1

One can use unichem from bioservices for this, however, these functions all require the InChIKey as input, e.g.

KDXKERNSBIXSRK-YFKPBYRVSA-N

Is it possible to interconvert the two using bioservices and if not is it possible to somehow use the functions in unichem with InChI rather than the InChIKey?

I tried:

from bioservices import *
u = UniChem()
u.get_src_compound_ids_from_inchikey('KDXKERNSBIXSRK-YFKPBYRVSA-N')

which works fine, however,

u.get_src_compound_ids_from_inchikey('InChI=1S/C6H14N2O2/c7-4-2-1-3-5(8)6(9)10/h5H,1-4,7-8H2,(H,9,10)/t5-/m0/s1')

does not work and returns 400.


Solution

  • Not sure if directly possible in bioservices but one can do the following workaround using chemspider:

    import requests
    
    host = "http://www.chemspider.com"
    getstring = "/InChI.asmx/InChIToInChIKey?inchi="
    inchi = 'InChI=1S/C6H14N2O2/c7-4-2-1-3-5(8)6(9)10/h5H,1-4,7-8H2,(H,9,10)/t5-/m0/s1'
    
    r = requests.get('{}{}{}'.format(host, getstring, inchi))
    if r.ok:
        res = str(r.text.replace('<?xml version="1.0" encoding="utf-8"?>\r\n<string xmlns="http://www.chemspider.com/">', '').replace('</string>', '').strip())
    else:
        print "provide a valid inchi!"
    

    This will give the desired InChIKey

    'KDXKERNSBIXSRK-YFKPBYRVSA-N'
    

    which can be used in unichem.