Skip to content

Commit

Permalink
#1245 Reaction/Molecule autoloaders don't load SMARTS (#1243)
Browse files Browse the repository at this point in the history
  • Loading branch information
AliaksandrDziarkach authored Aug 24, 2023
1 parent 490145e commit 964da3f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
2 changes: 2 additions & 0 deletions api/tests/integration/ref/ketcher/detect.py.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ detected as molecule
detected as query
detected as molecule
detected as query
detected as query
detected as query reaction
4 changes: 4 additions & 0 deletions api/tests/integration/tests/ketcher/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])")
19 changes: 16 additions & 3 deletions core/indigo-core/molecule/src/molecule_auto_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,20 +433,33 @@ 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;
loader.ignore_cistrans_errors = ignore_cistrans_errors;
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<QueryMolecule&>(mol));
}
catch (Exception& e)
{
_scanner->seek(start, SEEK_SET);
loader.loadSMARTS(static_cast<QueryMolecule&>(mol));
}
}
else
loader.loadMolecule((Molecule&)mol);
{
loader.loadMolecule(static_cast<Molecule&>(mol));
}
return;
}
catch (Exception& e)
Expand Down
19 changes: 17 additions & 2 deletions core/indigo-core/reaction/src/reaction_auto_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<QueryReaction&>(reaction));
}
catch (Exception& e)
{
loader.smarts_mode = true;
_scanner->seek(pos, SEEK_SET);
loader.loadQueryReaction(static_cast<QueryReaction&>(reaction));
}
}
else
loader.loadReaction((Reaction&)reaction);
{
loader.loadReaction(static_cast<Reaction&>(reaction));
}
}

// default is Rxnfile format
Expand Down

0 comments on commit 964da3f

Please sign in to comment.