Skip to content

Commit

Permalink
#1322 Reaction cannon be saved
Browse files Browse the repository at this point in the history
Add 'original_format' support to reactions
Add UTs
  • Loading branch information
AliaksandrDziarkach committed Oct 12, 2023
1 parent 5ee0dbc commit ef6afe7
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 33 deletions.
48 changes: 48 additions & 0 deletions api/c/indigo/src/indigo_misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1484,3 +1484,51 @@ CEXPORT const char* indigoJson(int item)
}
INDIGO_END(0);
}

CEXPORT const char* indigoGetOriginalFormat(int item)
{
INDIGO_BEGIN
{
IndigoObject& obj = self.getObject(item);
int original_format = BaseMolecule::UNKNOWN;
if (IndigoBaseMolecule::is(obj))
{
BaseMolecule& mol = obj.getBaseMolecule();
original_format = mol.original_format;
}
else if (IndigoBaseReaction::is(obj))
{
BaseReaction& rxn = obj.getBaseReaction();
original_format = rxn.original_format;
}
else
throw IndigoError("indigoSaveJson(): expected molecule, got %s", obj.debugInfo());

switch (original_format)
{
case BaseMolecule::CML:
return "chemical/x-cml";
case BaseMolecule::CDXML:
return "chemical/x-cdxml";
case BaseMolecule::CDX:
return "chemical/x-cdx";
case BaseMolecule::RDF:
return "chemical/x-mdl-rdfile";
case BaseMolecule::SMILES:
return "chemical/x-daylight-smiles";
case BaseMolecule::CXSMILES:
return "chemical/x-chemaxon-cxsmiles";
case BaseMolecule::SMARTS:
return "chemical/x-daylight-smarts";
case BaseMolecule::MOL:
return "chemical/x-mdl-molfile";
case BaseMolecule::RXN:
return "chemical/x-mdl-rxnfile";
case BaseMolecule::KET:
return "chemical/x-indigo-ket";
default:
return "unknown";
}
}
INDIGO_END(0);
}
28 changes: 0 additions & 28 deletions api/c/indigo/src/indigo_molecule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3784,34 +3784,6 @@ CEXPORT int indigoCountComponentBonds(int molecule, int index)
INDIGO_END(-1);
}

CEXPORT const char* indigoGetOriginalFormat(int item)
{
INDIGO_BEGIN
{
IndigoObject& obj = self.getObject(item);
if (IndigoBaseMolecule::is(obj))
{
BaseMolecule& mol = obj.getBaseMolecule();
switch (mol.original_format)
{
case BaseMolecule::SMARTS:
return "chemical/x-daylight-smarts";
case BaseMolecule::SMILES:
return "chemical/x-daylight-smiles";
case BaseMolecule::MOL:
return "chemical/x-mdl-molfile";
case BaseMolecule::RXN:
return "chemical/x-mdl-rxnfile";
default:
return "unknown";
}
}
else
throw IndigoError("indigoSaveJson(): expected molecule, got %s", obj.debugInfo());
}
INDIGO_END(0);
}

CEXPORT int indigoCreateMolecule()
{
INDIGO_BEGIN
Expand Down
4 changes: 4 additions & 0 deletions api/tests/integration/ref/basic/get_original_format.py.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
Molecule "N[CH](C)C(=O)O" : got "chemical/x-daylight-smiles" - OK
****** Test get original format: smarts ***
Molecule "([#6]1-[#6]-[#6]-1.[#6])" : got "chemical/x-daylight-smarts" - OK
****** Test get original format: reaction ***
Molecule "N[CH](C)C(=O)O>>N[CH](C)C" : got "chemical/x-daylight-smiles" - OK
****** Test get original format: reaction smarts ***
Molecule "([#6]1-[#6]-[#6]-1.[#6])>>([#6]1-[#6]-[#6]-1.[#6]=O)" : got "chemical/x-daylight-smarts" - OK
18 changes: 14 additions & 4 deletions api/tests/integration/tests/basic/get_original_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
indigo = Indigo()


def test_mol(molecule, mol, expected):
original_format = mol.getOriginalFormat()
def test_mol(smarts, obj, expected):
original_format = obj.getOriginalFormat()
if original_format == expected:
print('Molecule "%s" : got "%s" - OK' % (molecule, expected))
print('Molecule "%s" : got "%s" - OK' % (smarts, expected))
else:
print(
'Molecule "%s" failed: expected original format "%s", got "%s"'
% (molecule, expected, original_format)
% (smarts, expected, original_format)
)


Expand All @@ -31,3 +31,13 @@ def test_mol(molecule, mol, expected):
molecule = "([#6]1-[#6]-[#6]-1.[#6])"
mol = indigo.loadSmarts(molecule)
test_mol(molecule, mol, "chemical/x-daylight-smarts")

print("****** Test get original format: reaction ***")
molecule = "N[CH](C)C(=O)O>>N[CH](C)C"
mol = indigo.loadReaction(molecule)
test_mol(molecule, mol, "chemical/x-daylight-smiles")

print("****** Test get original format: reaction smarts ***")
molecule = "([#6]1-[#6]-[#6]-1.[#6])>>([#6]1-[#6]-[#6]-1.[#6]=O)"
mol = indigo.loadReactionSmarts(molecule)
test_mol(molecule, mol, "chemical/x-daylight-smarts")
17 changes: 17 additions & 0 deletions api/wasm/indigo-ketcher/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,23 @@ M END
options.delete();
});

test("convert", "rsmiles-app-json", () => {
let options = new indigo.MapStringString();
options.set("output-content-type", "application/json");
const rsmiles = indigo.convert(rxn_smiles, "smiles", options);
assert.equal(rsmiles, '{"struct":"C1C=CC=CC=1.N>>C1N=CC=CC=1.[CH3-]","format":"smiles","original_format":"chemical/x-daylight-smiles"}');
options.delete();
});

test("convert", "rsmiles-input-format-smarts", () => {
let options = new indigo.MapStringString();
options.set("output-content-type", "application/json");
options.set("input-format", "chemical/x-daylight-smarts");
const rsmiles = indigo.convert(rxn_smiles, "smiles", options);
assert.equal(rsmiles, '{"struct":"C1C=CC=CC=1.N>>C1N=CC=CC=1.[CH3-]","format":"smiles","original_format":"chemical/x-daylight-smarts"}');
options.delete();
});

}


Expand Down
1 change: 1 addition & 0 deletions core/indigo-core/molecule/src/molecule_json_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,7 @@ void MoleculeJsonLoader::loadMolecule(BaseMolecule& mol, bool load_arrows)
{
_pmol = &pmol->asMolecule();
}
mol.original_format = BaseMolecule::KET;

auto& mol_node = _mol_nodes[node_idx];
if (mol_node.HasMember("atoms"))
Expand Down
2 changes: 2 additions & 0 deletions core/indigo-core/reaction/base_reaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ namespace indigo

Array<char> name;

int original_format;

DECL_ERROR;

protected:
Expand Down
3 changes: 2 additions & 1 deletion core/indigo-core/reaction/src/base_reaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ SideIter SideAuto::end()
}

BaseReaction::BaseReaction()
: reactants(*this, REACTANT), catalysts(*this, CATALYST), products(*this, PRODUCT), intermediates(*this, INTERMEDIATE), undefined(*this, UNDEFINED)
: reactants(*this, REACTANT), catalysts(*this, CATALYST), products(*this, PRODUCT), intermediates(*this, INTERMEDIATE), undefined(*this, UNDEFINED),
original_format(BaseMolecule::UNKNOWN)
{
clear();
}
Expand Down
2 changes: 2 additions & 0 deletions core/indigo-core/reaction/src/reaction_json_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ void ReactionJsonLoader::loadReaction(BaseReaction& rxn)
else
throw Error("unknown reaction type: %s", typeid(rxn).name());

rxn.original_format = BaseMolecule::KET;

rxn.meta().clone(_pmol->meta());
_pmol->meta().resetMetaData();

Expand Down
8 changes: 8 additions & 0 deletions core/indigo-core/reaction/src/rsmiles_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ void RSmilesLoader::loadReaction(Reaction& reaction)
_rxn = &reaction;
_brxn = &reaction;
_qrxn = 0;
if (smarts_mode)
reaction.original_format = BaseMolecule::SMARTS;
else
reaction.original_format = BaseMolecule::SMILES;
_loadReaction();
}

Expand All @@ -72,6 +76,10 @@ void RSmilesLoader::loadQueryReaction(QueryReaction& rxn)
_rxn = 0;
_brxn = &rxn;
_qrxn = &rxn;
if (smarts_mode)
rxn.original_format = BaseMolecule::SMARTS;
else
rxn.original_format = BaseMolecule::SMILES;
_loadReaction();
}

Expand Down

0 comments on commit ef6afe7

Please sign in to comment.