Unable to get INCHIKEY value for an inchi string

30 views
Skip to first unread message

Abhimanyu Sirohi

unread,
Apr 6, 2015, 6:54:58 AM4/6/15
to indig...@googlegroups.com

Hi,

The following code returns a NULL pointer for inchikey:

int mol = indigoLoadMoleculeFromString("C1CCCCC1");
const char* inchi = indigoInchiGetInchi(mol); // InChI=1S/C6H12/c1-2-4-6-5-3-1/h1-6H2
const char* inchik = indigoInchiGetInchiKey(inchi); // NULL pointer


The problem seems to be indigoInchiGetInchiKey method of indigo_inchi_api.cpp file. The passed in inchi_string is somehow assigned to self.tmp_string which is cleared for output in IndigoInchi::InChIKeymethod of indigo_inchi_core.cpp.


Regards,

Abhi


Savelyev Alexander

unread,
Apr 6, 2015, 7:55:31 AM4/6/15
to indig...@googlegroups.com

Hi

The C interface has slightly different usage. In the current example it is not a null pointer but a error message. But the tmp buffer issue also affects the specified issue. The general usage:

int mol = indigoLoadMoleculeFromString("C1CCCCC1");

     
if(mol < 0 ) {
         printf
("%s\n", indigoGetLastError());
         
exit(-1);

     
}
     
const char* inchi = indigoInchiGetInchi(mol); // InChI=1S/C6H12/c1-2-4-6-5-3-1/h1-6H2

     
if(inchi == 0) {
         printf
("%s\n", indigoGetLastError());
         
exit(-1);
     
}
      printf
("%s\n", inchi);

     
const char* inchik = indigoInchiGetInchiKey(inchi); // NULL pointer

     
if (inchik == 0) {
         printf
("%s\n", indigoGetLastError());
         
exit(-1);
     
}
      printf
("%s\n", inchik);

(the returned result should be verified each time)

Output

InChI=1S/C6H12/c1-2-4-6-5-3-1/h1-6H2
indigo
-inchi: INCHIKEY_INVALID_INCHI_PREFIX



The example above still incorrect, because inchi string is passed into the indigoInchiGetInchiKey() funciton. Each returned string should be copied! Because, it will be lost otherwise. indigoGetLastError() also uses tmp_buffer

I will use cpp code in order to decrease number of lines. But the same logic can be implemented using simple C. Also, a handler for errors (which will throw exception in our examples) can be passed as a callback, to short the code. Here is the correct usage:

static void errorHandling(const char* message, void* context) {
   
throw Exception(message);
}
.......
   
try {
       qword id
= indigoAllocSessionId();
       indigoSetSessionId
(id);
       indigoSetErrorHandler
(errorHandling, 0);


       
int mol = indigoLoadMoleculeFromString("C1CCCCC1");

       std
::string inchi = indigoInchiGetInchi(mol); // copy inchi string
       std
::cout << inchi << std::endl;;
       
       std
::string inchik = indigoInchiGetInchiKey(inchi.c_str()); // pass inchi string, copy inchikey
       std
::cout << inchik << std::endl;;

   
catch(Exception& ex) {
         std
::cout << ex.message();
   
}



InChI=1S/C6H12/c1-2-4-6-5-3-1/h1-6H2
XDTMQSROBMDMFD
-UHFFFAOYSA-N



Thanks
Alexander

Abhimanyu Sirohi

unread,
Apr 7, 2015, 11:15:25 AM4/7/15
to indig...@googlegroups.com
Thanks a lot. I got a hint after I posted this question that the inchiKey method requires a copy of the string. When I debugged the indigo code, I noticed that the passed-in string gets cleared automatically.

Thanks
Abhi
Reply all
Reply to author
Forward
0 new messages