From 964da3f97fbd2d3cdf813fcbda4a1696a6247afe Mon Sep 17 00:00:00 2001 From: Aliaksandr Dziarkach <18146690+AliaksandrDziarkach@users.noreply.github.com> Date: Thu, 24 Aug 2023 12:21:41 +0300 Subject: [PATCH] #1245 Reaction/Molecule autoloaders don't load SMARTS (#1243) --- .../integration/ref/ketcher/detect.py.out | 2 ++ api/tests/integration/tests/ketcher/detect.py | 4 ++++ .../molecule/src/molecule_auto_loader.cpp | 19 ++++++++++++++++--- .../reaction/src/reaction_auto_loader.cpp | 19 +++++++++++++++++-- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/api/tests/integration/ref/ketcher/detect.py.out b/api/tests/integration/ref/ketcher/detect.py.out index 461a55fe5a..53a44e0460 100644 --- a/api/tests/integration/ref/ketcher/detect.py.out +++ b/api/tests/integration/ref/ketcher/detect.py.out @@ -5,3 +5,5 @@ detected as molecule detected as query detected as molecule detected as query +detected as query +detected as query reaction diff --git a/api/tests/integration/tests/ketcher/detect.py b/api/tests/integration/tests/ketcher/detect.py index de6274aec3..fd3ed4f028 100644 --- a/api/tests/integration/tests/ketcher/detect.py +++ b/api/tests/integration/tests/ketcher/detect.py @@ -56,3 +56,7 @@ def detect(indigo, molstr): detect(indigo, mol) mfile.close() detect(indigo, "[#6]1-[#6]=[#6]-[#6]=[#6]-[#6]=1") + # check SMARTS + detect(indigo, "[$([CX3]=[OX1]),$([CX3+]-[OX1-])]") + # check reaction SMARTS + detect(indigo, "([#8:1].[#6:2])>>([#8:1][#6:2])") diff --git a/core/indigo-core/molecule/src/molecule_auto_loader.cpp b/core/indigo-core/molecule/src/molecule_auto_loader.cpp index 2e8e0aecbf..c38d270af7 100644 --- a/core/indigo-core/molecule/src/molecule_auto_loader.cpp +++ b/core/indigo-core/molecule/src/molecule_auto_loader.cpp @@ -433,6 +433,7 @@ void MoleculeAutoLoader::_loadMolecule(BaseMolecule& mol) try { SmilesLoader loader(*_scanner); + long long start = _scanner->tell(); loader.ignore_closing_bond_direction_mismatch = ignore_closing_bond_direction_mismatch; loader.stereochemistry_options = stereochemistry_options; @@ -440,13 +441,25 @@ void MoleculeAutoLoader::_loadMolecule(BaseMolecule& mol) loader.ignore_no_chiral_flag = ignore_no_chiral_flag; /* - If exception is thrown, the string is rather an IUPAC name than a SMILES string + If exception is thrown, try the SMARTS, if exception thrown again - the string is rather an IUPAC name than a SMILES string We catch it and pass down to IUPAC name conversion */ if (query) - loader.loadQueryMolecule((QueryMolecule&)mol); + { + try + { + loader.loadQueryMolecule(static_cast(mol)); + } + catch (Exception& e) + { + _scanner->seek(start, SEEK_SET); + loader.loadSMARTS(static_cast(mol)); + } + } else - loader.loadMolecule((Molecule&)mol); + { + loader.loadMolecule(static_cast(mol)); + } return; } catch (Exception& e) diff --git a/core/indigo-core/reaction/src/reaction_auto_loader.cpp b/core/indigo-core/reaction/src/reaction_auto_loader.cpp index cf02832d6f..ddd0609205 100644 --- a/core/indigo-core/reaction/src/reaction_auto_loader.cpp +++ b/core/indigo-core/reaction/src/reaction_auto_loader.cpp @@ -284,6 +284,7 @@ void ReactionAutoLoader::_loadReaction(BaseReaction& reaction) // check for SMILES format if (Scanner::isSingleLine(*_scanner)) { + long long pos = _scanner->tell(); RSmilesLoader loader(*_scanner); loader.ignore_closing_bond_direction_mismatch = ignore_closing_bond_direction_mismatch; @@ -292,9 +293,23 @@ void ReactionAutoLoader::_loadReaction(BaseReaction& reaction) loader.ignore_bad_valence = ignore_bad_valence; if (query) - loader.loadQueryReaction((QueryReaction&)reaction); + { + // Try to load query as SMILES, if error occured - try to load as SMARTS + try + { + loader.loadQueryReaction(static_cast(reaction)); + } + catch (Exception& e) + { + loader.smarts_mode = true; + _scanner->seek(pos, SEEK_SET); + loader.loadQueryReaction(static_cast(reaction)); + } + } else - loader.loadReaction((Reaction&)reaction); + { + loader.loadReaction(static_cast(reaction)); + } } // default is Rxnfile format