Answers.Body: <p>Recently I've developed a python lib for converting InChI to InChIKey: <a href="https://github.com/liwt31/chembl_ikey" rel="nofollow noreferrer">https://github.com/liwt31/chembl_ikey</a>. Maybe it will help.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-16T13:45:19.193Z
Answers.Text: That's a fork. Could link to the original : https://github.com/mnowotka/chembl_ikey
Answers.UserDisplayName: gilleain
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-18T05:03:47.353Z
Answers.Text: @gilleain it is indeed a fork. I added python3 support and fixed a bug (InChI without sminor yields wrong InChIKey).
Answers.UserDisplayName: liwt31
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-18T07:50:08.467Z
Answers.Text: ok, that sounds like a great contribution - but not that you developed it... Sounds like the original author could benefit from a pull request? Just a thought - I'm not the github police! :)
Answers.UserDisplayName: gilleain
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-06T13:50:23.207Z
Answers.Text: ChemDraw's ChemScript is a valuable tool. However, it still runs on Python 3.2. Thank you for this fork - I agree that a pull request would be very beneficial!
Answers.UserDisplayName: tstone-1
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-14T01:23:55.137Z
Answers.LastActivityDate: 2017-10-18T05:07:44.617Z
Answers.LastEditDate: 2017-10-18T05:07:44.617Z
Answers.UserDisplayName: liwt31
Answers.Body: <p>Not sure if directly possible in <code>bioservices</code> but one can do the following workaround using <a href="https://www.chemspider.com/InChI.asmx" rel="nofollow noreferrer">chemspider</a>:</p>
  
  <pre><code>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('&lt;?xml version="1.0" encoding="utf-8"?&gt;\r\n&lt;string xmlns="http://www.chemspider.com/"&gt;', '').replace('&lt;/string&gt;', '').strip())
  else:
      print "provide a valid inchi!"
  </code></pre>
  
  <p>This will give the desired InChIKey</p>
  
  <pre><code>'KDXKERNSBIXSRK-YFKPBYRVSA-N'
  </code></pre>
  
  <p>which can be used in <code>unichem</code>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-09-01T12:48:19.873Z
Answers.LastActivityDate: 2017-09-01T12:48:19.873Z
Answers.UserDisplayName: Cleb
Body: <p>I would like to retrieve IDs from several databases using <code>InChI</code> as an input, e.g.</p>
  
  <pre><code>InChI=1S/C6H14N2O2/c7-4-2-1-3-5(8)6(9)10/h5H,1-4,7-8H2,(H,9,10)/t5-/m0/s1
  </code></pre>
  
  <p>One can use <a href="https://pythonhosted.org/bioservices/_modules/bioservices/unichem.html" rel="nofollow noreferrer">unichem</a> from <code>bioservices</code> for this, however, these functions all require the <code>InChIKey</code> as input, e.g.</p>
  
  <pre><code>KDXKERNSBIXSRK-YFKPBYRVSA-N
  </code></pre>
  
  <p>Is it possible to interconvert the two using <code>bioservices</code> and if not is it possible to somehow use the functions in <code>unichem</code> with <code>InChI</code> rather than the <code>InChIKey</code>?</p>
  
  <p>I tried:</p>
  
  <pre><code>from bioservices import *
  u = UniChem()
  u.get_src_compound_ids_from_inchikey('KDXKERNSBIXSRK-YFKPBYRVSA-N')
  </code></pre>
  
  <p>which works fine, however, </p>
  
  <pre><code>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')
  </code></pre>
  
  <p>does not work and returns <code>400</code>.</p>
  
  <p>If it is not passible via bioservices, is there any other python API available for this?</p>
  
Tags: <python><kegg><data-retrieval>
Title: How to interconvert InChI and InChIKey?
ThreadText: # How to interconvert InChI and InChIKey?
  
  (Asked by: username_0 on 2017-09-01)
  
  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](https://pythonhosted.org/bioservices/_modules/bioservices/unichem.html) 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`.
  
  If it is not passible via bioservices, is there any other python API available for this?
  
  **Tags:** python, kegg, data-retrieval
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-10-14. Score: 4
  
  Recently I've developed a python lib for converting InChI to InChIKey: <https://github.com/username_1/chembl_ikey>. Maybe it will help.
  
  > **Comments:**
  > **username_2**: That's a fork. Could link to the original : https://github.com/mnowotka/chembl_ikey  
  > **username_1**: @username_2 it is indeed a fork. I added python3 support and fixed a bug (InChI without sminor yields wrong InChIKey).  
  > **username_2**: ok, that sounds like a great contribution - but not that you developed it... Sounds like the original author could benefit from a pull request? Just a thought - I'm not the github police! :)  
  > **username_3**: ChemDraw's ChemScript is a valuable tool. However, it still runs on Python 3.2. Thank you for this fork - I agree that a pull request would be very beneficial!  
  
  ---
  
  ### Answer by username_0 on 2017-09-01. Score: 1
  
  Not sure if directly possible in `bioservices` but one can do the following workaround using [chemspider](https://www.chemspider.com/InChI.asmx):
  
  ```
  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`.
  
  
  ---
  
  