Skip to content

Commit 964da3f

Browse files
#1245 Reaction/Molecule autoloaders don't load SMARTS (#1243)
1 parent 490145e commit 964da3f

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

api/tests/integration/ref/ketcher/detect.py.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ detected as molecule
55
detected as query
66
detected as molecule
77
detected as query
8+
detected as query
9+
detected as query reaction

api/tests/integration/tests/ketcher/detect.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,7 @@ def detect(indigo, molstr):
5656
detect(indigo, mol)
5757
mfile.close()
5858
detect(indigo, "[#6]1-[#6]=[#6]-[#6]=[#6]-[#6]=1")
59+
# check SMARTS
60+
detect(indigo, "[$([CX3]=[OX1]),$([CX3+]-[OX1-])]")
61+
# check reaction SMARTS
62+
detect(indigo, "([#8:1].[#6:2])>>([#8:1][#6:2])")

core/indigo-core/molecule/src/molecule_auto_loader.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,20 +433,33 @@ void MoleculeAutoLoader::_loadMolecule(BaseMolecule& mol)
433433
try
434434
{
435435
SmilesLoader loader(*_scanner);
436+
long long start = _scanner->tell();
436437

437438
loader.ignore_closing_bond_direction_mismatch = ignore_closing_bond_direction_mismatch;
438439
loader.stereochemistry_options = stereochemistry_options;
439440
loader.ignore_cistrans_errors = ignore_cistrans_errors;
440441
loader.ignore_no_chiral_flag = ignore_no_chiral_flag;
441442

442443
/*
443-
If exception is thrown, the string is rather an IUPAC name than a SMILES string
444+
If exception is thrown, try the SMARTS, if exception thrown again - the string is rather an IUPAC name than a SMILES string
444445
We catch it and pass down to IUPAC name conversion
445446
*/
446447
if (query)
447-
loader.loadQueryMolecule((QueryMolecule&)mol);
448+
{
449+
try
450+
{
451+
loader.loadQueryMolecule(static_cast<QueryMolecule&>(mol));
452+
}
453+
catch (Exception& e)
454+
{
455+
_scanner->seek(start, SEEK_SET);
456+
loader.loadSMARTS(static_cast<QueryMolecule&>(mol));
457+
}
458+
}
448459
else
449-
loader.loadMolecule((Molecule&)mol);
460+
{
461+
loader.loadMolecule(static_cast<Molecule&>(mol));
462+
}
450463
return;
451464
}
452465
catch (Exception& e)

core/indigo-core/reaction/src/reaction_auto_loader.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ void ReactionAutoLoader::_loadReaction(BaseReaction& reaction)
284284
// check for SMILES format
285285
if (Scanner::isSingleLine(*_scanner))
286286
{
287+
long long pos = _scanner->tell();
287288
RSmilesLoader loader(*_scanner);
288289

289290
loader.ignore_closing_bond_direction_mismatch = ignore_closing_bond_direction_mismatch;
@@ -292,9 +293,23 @@ void ReactionAutoLoader::_loadReaction(BaseReaction& reaction)
292293
loader.ignore_bad_valence = ignore_bad_valence;
293294

294295
if (query)
295-
loader.loadQueryReaction((QueryReaction&)reaction);
296+
{
297+
// Try to load query as SMILES, if error occured - try to load as SMARTS
298+
try
299+
{
300+
loader.loadQueryReaction(static_cast<QueryReaction&>(reaction));
301+
}
302+
catch (Exception& e)
303+
{
304+
loader.smarts_mode = true;
305+
_scanner->seek(pos, SEEK_SET);
306+
loader.loadQueryReaction(static_cast<QueryReaction&>(reaction));
307+
}
308+
}
296309
else
297-
loader.loadReaction((Reaction&)reaction);
310+
{
311+
loader.loadReaction(static_cast<Reaction&>(reaction));
312+
}
298313
}
299314

300315
// default is Rxnfile format

0 commit comments

Comments
 (0)