From 7467fe005f5524c8a6f4440a796afc3f12b10002 Mon Sep 17 00:00:00 2001 From: Greg Landrum Date: Sun, 11 May 2025 05:10:18 +0200 Subject: [PATCH] Fixes #8508 (#8514) * Fixes #8508 * suggestion from review * Update inchi.cpp --- External/INCHI-API/Wrap/pyInchi.cpp | 3 +++ External/INCHI-API/inchi.cpp | 9 +++++---- External/INCHI-API/inchi.h | 6 +++++- External/INCHI-API/python/inchi.py | 5 ++++- rdkit/Chem/UnitTestInchi.py | 18 ++++++++++++------ 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/External/INCHI-API/Wrap/pyInchi.cpp b/External/INCHI-API/Wrap/pyInchi.cpp index 2f8c13e83..82d8ab16d 100644 --- a/External/INCHI-API/Wrap/pyInchi.cpp +++ b/External/INCHI-API/Wrap/pyInchi.cpp @@ -147,4 +147,7 @@ BOOST_PYTHON_MODULE(rdinchi) { "MolToInchiKey", RDKit::MolToInchiKey, (boost::python::arg("mol"), boost::python::arg("options") = ""), docString.c_str()); + + boost::python::def("GetInchiVersion", RDKit::getInchiVersion, + "returns the version of the InChI software being used"); } diff --git a/External/INCHI-API/inchi.cpp b/External/INCHI-API/inchi.cpp index a7b59d70f..fd8175cdf 100644 --- a/External/INCHI-API/inchi.cpp +++ b/External/INCHI-API/inchi.cpp @@ -1,5 +1,5 @@ // -// Copyright (c) 2011-2022 Novartis Institutes for BioMedical Research Inc. and +// Copyright (c) 2011-2025 Novartis Institutes for BioMedical Research Inc. and // other RDkit contributors // All rights reserved. // @@ -62,6 +62,7 @@ #include #include #include +#include #include #include #include @@ -2145,9 +2146,7 @@ std::string InchiToInchiKey(const std::string &inchi) { char inchiKey[29]; char xtra1[65], xtra2[65]; int ret = 0; - { - ret = GetINCHIKeyFromINCHI(inchi.c_str(), 0, 0, inchiKey, xtra1, xtra2); - } + ret = GetINCHIKeyFromINCHI(inchi.c_str(), 0, 0, inchiKey, xtra1, xtra2); std::string error; switch (ret) { case INCHIKEY_OK: @@ -2174,4 +2173,6 @@ std::string InchiToInchiKey(const std::string &inchi) { BOOST_LOG(rdErrorLog) << error << " in generating InChI Key" << std::endl; return std::string(); } + +std::string getInchiVersion() { return CURRENT_VER; } } // namespace RDKit diff --git a/External/INCHI-API/inchi.h b/External/INCHI-API/inchi.h index 641c5b5ba..697a2f765 100644 --- a/External/INCHI-API/inchi.h +++ b/External/INCHI-API/inchi.h @@ -1,5 +1,6 @@ // -// Copyright (c) 2011-2014, Novartis Institutes for BioMedical Research Inc. +// Copyright (c) 2011-2025, Novartis Institutes for BioMedical Research Inc. +// and other RDkit contributors // All rights reserved. // // Redistribution and use in source and binary forms, with or without @@ -34,6 +35,7 @@ #ifndef RDKIT_INCHI_30JUNE2011 #define RDKIT_INCHI_30JUNE2011 #include + #include namespace RDKit { struct RDKIT_RDINCHILIB_EXPORT ExtraInchiReturnValues { @@ -107,5 +109,7 @@ inline std::string MolToInchiKey(const ROMol &mol, const char *options = NULL) { return InchiToInchiKey(MolToInchi(mol, rv, options)); }; +RDKIT_RDINCHILIB_EXPORT std::string getInchiVersion(); + } // namespace RDKit #endif diff --git a/External/INCHI-API/python/inchi.py b/External/INCHI-API/python/inchi.py index 5c3a1ce8a..19f242825 100644 --- a/External/INCHI-API/python/inchi.py +++ b/External/INCHI-API/python/inchi.py @@ -233,7 +233,10 @@ def MolToInchiKey(mol, options=""): return rdinchi.MolToInchiKey(mol, options) +GetInchiVersion = rdinchi.GetInchiVersion + __all__ = [ 'MolToInchiAndAuxInfo', 'MolToInchi', 'MolBlockToInchiAndAuxInfo', 'MolBlockToInchi', - 'MolFromInchi', 'InchiReadWriteError', 'InchiToInchiKey', 'MolToInchiKey', 'INCHI_AVAILABLE' + 'MolFromInchi', 'InchiReadWriteError', 'InchiToInchiKey', 'MolToInchiKey', 'GetInchiVersion', + 'INCHI_AVAILABLE' ] diff --git a/rdkit/Chem/UnitTestInchi.py b/rdkit/Chem/UnitTestInchi.py index 3182a3829..2bbdc22a0 100755 --- a/rdkit/Chem/UnitTestInchi.py +++ b/rdkit/Chem/UnitTestInchi.py @@ -1,5 +1,6 @@ # -# Copyright (c) 2011, Novartis Institutes for BioMedical Research Inc. +# Copyright (c) 2011-2025, Novartis Institutes for BioMedical Research Inc. +# and other RDKit contributors # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -42,7 +43,7 @@ from rdkit.Chem import (INCHI_AVAILABLE, ForwardSDMolSupplier, MolFromMolBlock, if INCHI_AVAILABLE: from rdkit.Chem import (InchiReadWriteError, InchiToInchiKey, MolBlockToInchi, MolFromInchi, - MolToInchi, MolToInchiKey) + MolToInchi, MolToInchiKey, GetInchiVersion) COLOR_RED = '\033[31m' COLOR_GREEN = '\033[32m' @@ -204,11 +205,11 @@ class TestCase(unittest.TestCase): # InChI messed up the radical? unsanitizedInchiMol = MolFromInchi(x, sanitize=False) if sum([ - a.GetNumRadicalElectrons() * a.GetAtomicNum() - for a in m.GetAtoms() if a.GetNumRadicalElectrons() != 0 + a.GetNumRadicalElectrons() * a.GetAtomicNum() for a in m.GetAtoms() + if a.GetNumRadicalElectrons() != 0 ]) != sum([ - a.GetNumRadicalElectrons() * a.GetAtomicNum() - for a in unsanitizedInchiMol.GetAtoms() if a.GetNumRadicalElectrons() != 0 + a.GetNumRadicalElectrons() * a.GetAtomicNum() for a in unsanitizedInchiMol.GetAtoms() + if a.GetNumRadicalElectrons() != 0 ]): reasonable += 1 continue @@ -324,6 +325,11 @@ M END""" inchi2 = MolBlockToInchi(mb2, options="/FixedH") self.assertEqual(inchi2, "InChI=1/C8H8N2/c1-6-7-4-2-3-5-8(7)10-9-6/h2-5H,1H3,(H,9,10)/f/h10H") + def test6GetInchiVersion(self): + version = GetInchiVersion() + self.assertIsInstance(version, str) + self.assertGreaterEqual(version, "1.07.2") + if __name__ == '__main__': # pragma: nocover # only run the test if InChI is available