Skip to content

Commit

Permalink
#1989 - Convert api does not recognise format during autodetection (#…
Browse files Browse the repository at this point in the history
…2463)

Co-authored-by: Aliakasndr Dziarkach <Aliakasndr.Dziarkach@gmail.com>
  • Loading branch information
AliaksandrDziarkach and Aliakasndr Dziarkach authored Oct 2, 2024
1 parent 0669a87 commit 5782327
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 6 deletions.
17 changes: 17 additions & 0 deletions api/wasm/indigo-ketcher/indigo-ketcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,23 @@ namespace indigo
return IndigoKetcherObject(objectId, IndigoKetcherObject::EKETReaction);
}
exceptionMessages.emplace_back(indigoGetLastError());

if (library >= 0)
{
print_js("try as IDT");
objectId = indigoLoadIdtFromString(data.c_str(), library);
if (objectId >= 0)
{
return IndigoKetcherObject(objectId, IndigoKetcherObject::EKETDocument);
}

print_js("try as HELM");
objectId = indigoLoadHelmFromString(data.c_str(), library);
if (objectId >= 0)
{
return IndigoKetcherObject(objectId, IndigoKetcherObject::EKETDocument);
}
}
}
exceptionMessages.emplace_back(indigoGetLastError());
// Let's try query molecule
Expand Down
1 change: 1 addition & 0 deletions api/wasm/indigo-ketcher/test/helm_maxmgc.ket
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"root":{"nodes":[{"$ref":"monomer0"},{"$ref":"monomer1"},{"$ref":"monomer2"},{"$ref":"monomer3"},{"$ref":"monomer4"},{"$ref":"monomer5"},{"$ref":"monomer6"},{"$ref":"monomer7"},{"$ref":"monomer8"},{"$ref":"monomer9"},{"$ref":"monomer10"},{"$ref":"monomer11"},{"$ref":"monomer12"},{"$ref":"monomer13"}],"connections":[{"connectionType":"single","endpoint1":{"monomerId":"monomer0","attachmentPointId":"R2"},"endpoint2":{"monomerId":"monomer1","attachmentPointId":"R1"}},{"connectionType":"single","endpoint1":{"monomerId":"monomer1","attachmentPointId":"R3"},"endpoint2":{"monomerId":"monomer2","attachmentPointId":"R1"}},{"connectionType":"single","endpoint1":{"monomerId":"monomer1","attachmentPointId":"R2"},"endpoint2":{"monomerId":"monomer3","attachmentPointId":"R1"}},{"connectionType":"single","endpoint1":{"monomerId":"monomer3","attachmentPointId":"R2"},"endpoint2":{"monomerId":"monomer4","attachmentPointId":"R1"}},{"connectionType":"single","endpoint1":{"monomerId":"monomer4","attachmentPointId":"R3"},"endpoint2":{"monomerId":"monomer5","attachmentPointId":"R1"}},{"connectionType":"single","endpoint1":{"monomerId":"monomer4","attachmentPointId":"R2"},"endpoint2":{"monomerId":"monomer6","attachmentPointId":"R1"}},{"connectionType":"single","endpoint1":{"monomerId":"monomer6","attachmentPointId":"R2"},"endpoint2":{"monomerId":"monomer7","attachmentPointId":"R1"}},{"connectionType":"single","endpoint1":{"monomerId":"monomer7","attachmentPointId":"R3"},"endpoint2":{"monomerId":"monomer8","attachmentPointId":"R1"}},{"connectionType":"single","endpoint1":{"monomerId":"monomer7","attachmentPointId":"R2"},"endpoint2":{"monomerId":"monomer9","attachmentPointId":"R1"}},{"connectionType":"single","endpoint1":{"monomerId":"monomer9","attachmentPointId":"R2"},"endpoint2":{"monomerId":"monomer10","attachmentPointId":"R1"}},{"connectionType":"single","endpoint1":{"monomerId":"monomer10","attachmentPointId":"R3"},"endpoint2":{"monomerId":"monomer11","attachmentPointId":"R1"}},{"connectionType":"single","endpoint1":{"monomerId":"monomer10","attachmentPointId":"R2"},"endpoint2":{"monomerId":"monomer12","attachmentPointId":"R1"}},{"connectionType":"single","endpoint1":{"monomerId":"monomer12","attachmentPointId":"R2"},"endpoint2":{"monomerId":"monomer13","attachmentPointId":"R1"}}],"templates":[{"$ref":"monomerTemplate-P___Phosphate"},{"$ref":"monomerTemplate-mR___2'-O-Methyl-Ribose"},{"$ref":"monomerTemplate-A___Adenine"},{"$ref":"monomerTemplate-sP___Phosporothioate"},{"$ref":"monomerTemplate-G___Guanine"},{"$ref":"monomerTemplate-dR___Deoxy-Ribose"},{"$ref":"monomerTemplate-C___Cytosine"},{"$ref":"monomerTemplate-MOE___2'-O-Methoxyethyl ribose"}]},"monomer0":{"type":"monomer","id":"0","seqid":1,"position":{"x":0.000000,"y":-0.000000},"alias":"P","templateId":"P___Phosphate"},"monomer1":{"type":"monomer","id":"1","seqid":2,"position":{"x":1.600000,"y":-0.000000},"alias":"mR","templateId":"mR___2'-O-Methyl-Ribose"},"monomer2":{"type":"monomer","id":"2","seqid":3,"position":{"x":1.600000,"y":-1.600000},"alias":"A","templateId":"A___Adenine"},"monomer3":{"type":"monomer","id":"3","seqid":4,"position":{"x":3.200000,"y":-0.000000},"alias":"sP","templateId":"sP___Phosporothioate"},"monomer4":{"type":"monomer","id":"4","seqid":5,"position":{"x":4.800000,"y":-0.000000},"alias":"mR","templateId":"mR___2'-O-Methyl-Ribose"},"monomer5":{"type":"monomer","id":"5","seqid":6,"position":{"x":4.800000,"y":-1.600000},"alias":"G","templateId":"G___Guanine"},"monomer6":{"type":"monomer","id":"6","seqid":7,"position":{"x":6.400000,"y":-0.000000},"alias":"P","templateId":"P___Phosphate"},"monomer7":{"type":"monomer","id":"7","seqid":8,"position":{"x":8.000000,"y":-0.000000},"alias":"dR","templateId":"dR___Deoxy-Ribose"},"monomer8":{"type":"monomer","id":"8","seqid":9,"position":{"x":8.000000,"y":-1.600000},"alias":"C","templateId":"C___Cytosine"},"monomer9":{"type":"monomer","id":"9","seqid":10,"position":{"x":9.600000,"y":-0.000000},"alias":"P","templateId":"P___Phosphate"},"monomer10":{"type":"monomer","id":"10","seqid":11,"position":{"x":11.200000,"y":-0.000000},"alias":"MOE","templateId":"MOE___2'-O-Methoxyethyl ribose"},"monomer11":{"type":"monomer","id":"11","seqid":12,"position":{"x":11.200000,"y":-1.600000},"alias":"A","templateId":"A___Adenine"},"monomer12":{"type":"monomer","id":"12","seqid":13,"position":{"x":12.800000,"y":-0.000000},"alias":"P","templateId":"P___Phosphate"},"monomer13":{"type":"monomer","id":"13","seqid":14,"position":{"x":14.400001,"y":-0.000000},"alias":"P","templateId":"P___Phosphate"},"monomerTemplate-P___Phosphate":{"type":"monomerTemplate","id":"P___Phosphate","class":"Phosphate","classHELM":"RNA","fullName":"Phosphate","alias":"P","naturalAnalogShort":"P","attachmentPoints":[{"attachmentAtom":0,"type":"left","leavingGroup":{"atoms":[1]}},{"attachmentAtom":0,"type":"right","leavingGroup":{"atoms":[3]}}],"atoms":[{"label":"P","location":[-0.239900,0.000000,0.000000]},{"label":"O","location":[-1.439900,0.000000,0.000000]},{"label":"O","location":[0.359800,-1.039400,0.000000]},{"label":"O","location":[0.960100,0.000000,0.000000]},{"label":"O","location":[0.359800,1.039400,0.000000]}],"bonds":[{"type":1,"atoms":[0,1]},{"type":2,"atoms":[0,2]},{"type":1,"atoms":[0,3]},{"type":1,"atoms":[0,4]}]},"monomerTemplate-mR___2'-O-Methyl-Ribose":{"type":"monomerTemplate","id":"mR___2'-O-Methyl-Ribose","class":"Sugar","classHELM":"RNA","fullName":"2'-O-Methyl-Ribose","alias":"mR","naturalAnalogShort":"R","attachmentPoints":[{"attachmentAtom":9,"type":"left","leavingGroup":{"atoms":[10]}},{"attachmentAtom":5,"type":"right","leavingGroup":{"atoms":[11]}},{"attachmentAtom":2,"type":"side","leavingGroup":{"atoms":[8]}}],"atoms":[{"label":"O","location":[-1.349300,-0.839300,0.000000]},{"label":"C","location":[-0.837200,0.570600,0.000000],"stereoLabel":"abs"},{"label":"C","location":[-0.166600,-1.762000,0.000000],"stereoLabel":"abs"},{"label":"C","location":[0.661900,0.519300,0.000000],"stereoLabel":"abs"},{"label":"C","location":[1.076400,-0.922300,0.000000],"stereoLabel":"abs"},{"label":"O","location":[1.581100,1.702500,0.000000]},{"label":"O","location":[2.483600,-1.436700,0.000000]},{"label":"C","location":[-1.678500,1.810300,0.000000]},{"label":"O","location":[-0.207700,-2.961200,0.000000]},{"label":"O","location":[-3.175400,1.702500,0.000000]},{"label":"H","location":[-3.849200,2.695400,0.000000]},{"label":"H","location":[2.769900,1.539500,0.000000]},{"label":"C","location":[2.691100,-2.618600,0.000000]}],"bonds":[{"type":1,"atoms":[0,1]},{"type":1,"atoms":[0,2]},{"type":1,"atoms":[1,3]},{"type":1,"atoms":[1,7],"stereo":6},{"type":1,"atoms":[2,4]},{"type":1,"atoms":[2,8],"stereo":6},{"type":1,"atoms":[3,4]},{"type":1,"atoms":[3,5],"stereo":1},{"type":1,"atoms":[4,6],"stereo":1},{"type":1,"atoms":[5,11]},{"type":1,"atoms":[7,9]},{"type":1,"atoms":[9,10]},{"type":1,"atoms":[6,12]}]},"monomerTemplate-A___Adenine":{"type":"monomerTemplate","id":"A___Adenine","class":"Base","classHELM":"RNA","fullName":"Adenine","alias":"A","naturalAnalogShort":"A","attachmentPoints":[{"attachmentAtom":6,"type":"left","leavingGroup":{"atoms":[10]}}],"atoms":[{"label":"C","location":[1.035400,0.249800,0.000000]},{"label":"C","location":[-0.079200,-0.754000,0.000000]},{"label":"C","location":[-1.505700,-0.290600,0.000000]},{"label":"N","location":[-1.817700,1.176600,0.000000]},{"label":"C","location":[-0.703100,2.180400,0.000000]},{"label":"N","location":[0.723500,1.717000,0.000000]},{"label":"N","location":[-2.387100,-1.503400,0.000000]},{"label":"C","location":[-1.505300,-2.716800,0.000000]},{"label":"N","location":[-0.078700,-2.253200,0.000000]},{"label":"N","location":[2.176800,-0.120900,0.000000]},{"label":"H","location":[-3.587100,-1.503400,0.000000]}],"bonds":[{"type":1,"atoms":[0,9]},{"type":2,"atoms":[0,5]},{"type":1,"atoms":[0,1]},{"type":1,"atoms":[8,1]},{"type":2,"atoms":[1,2]},{"type":1,"atoms":[6,2]},{"type":1,"atoms":[2,3]},{"type":2,"atoms":[3,4]},{"type":1,"atoms":[4,5]},{"type":1,"atoms":[6,7]},{"type":1,"atoms":[6,10]},{"type":2,"atoms":[7,8]}]},"monomerTemplate-sP___Phosporothioate":{"type":"monomerTemplate","id":"sP___Phosporothioate","class":"Phosphate","classHELM":"RNA","fullName":"Phosporothioate","alias":"sP","naturalAnalogShort":"P","attachmentPoints":[{"attachmentAtom":0,"type":"left","leavingGroup":{"atoms":[1]}},{"attachmentAtom":0,"type":"right","leavingGroup":{"atoms":[3]}}],"atoms":[{"label":"P","location":[-0.239900,0.000000,0.000000]},{"label":"O","location":[-1.439900,0.000000,0.000000]},{"label":"O","location":[0.359800,-1.039400,0.000000]},{"label":"O","location":[0.960100,0.000000,0.000000]},{"label":"S","location":[0.359800,1.039400,0.000000]}],"bonds":[{"type":1,"atoms":[0,1]},{"type":2,"atoms":[0,2]},{"type":1,"atoms":[0,3]},{"type":1,"atoms":[0,4]}]},"monomerTemplate-G___Guanine":{"type":"monomerTemplate","id":"G___Guanine","class":"Base","classHELM":"RNA","fullName":"Guanine","alias":"G","naturalAnalogShort":"G","attachmentPoints":[{"attachmentAtom":6,"type":"left","leavingGroup":{"atoms":[11]}}],"atoms":[{"label":"C","location":[1.035400,0.249800,0.000000]},{"label":"C","location":[-0.079200,-0.754000,0.000000]},{"label":"C","location":[-1.505700,-0.290600,0.000000]},{"label":"N","location":[-1.817700,1.176600,0.000000]},{"label":"C","location":[-0.703100,2.180400,0.000000]},{"label":"N","location":[0.723500,1.717000,0.000000]},{"label":"N","location":[-2.387100,-1.503400,0.000000]},{"label":"C","location":[-1.505300,-2.716800,0.000000]},{"label":"N","location":[-0.078700,-2.253200,0.000000]},{"label":"O","location":[2.176800,-0.120900,0.000000]},{"label":"N","location":[-0.952700,3.354200,0.000000]},{"label":"H","location":[-3.587100,-1.503400,0.000000]}],"bonds":[{"type":2,"atoms":[0,9]},{"type":1,"atoms":[0,5]},{"type":1,"atoms":[0,1]},{"type":1,"atoms":[8,1]},{"type":2,"atoms":[1,2]},{"type":1,"atoms":[6,2]},{"type":1,"atoms":[2,3]},{"type":2,"atoms":[3,4]},{"type":1,"atoms":[4,5]},{"type":1,"atoms":[4,10]},{"type":1,"atoms":[6,7]},{"type":1,"atoms":[6,11]},{"type":2,"atoms":[7,8]}]},"monomerTemplate-dR___Deoxy-Ribose":{"type":"monomerTemplate","id":"dR___Deoxy-Ribose","class":"Sugar","classHELM":"RNA","fullName":"Deoxy-Ribose","alias":"dR","naturalAnalogShort":"R","attachmentPoints":[{"attachmentAtom":8,"type":"left","leavingGroup":{"atoms":[9]}},{"attachmentAtom":5,"type":"right","leavingGroup":{"atoms":[10]}},{"attachmentAtom":2,"type":"side","leavingGroup":{"atoms":[7]}}],"atoms":[{"label":"O","location":[-0.878800,-1.208000,0.000000]},{"label":"C","location":[-0.366800,0.201900,0.000000],"stereoLabel":"abs"},{"label":"C","location":[0.303800,-2.130700,0.000000],"stereoLabel":"abs"},{"label":"C","location":[1.132300,0.150600,0.000000],"stereoLabel":"abs"},{"label":"C","location":[1.546800,-1.291000,0.000000]},{"label":"O","location":[2.051500,1.333800,0.000000]},{"label":"C","location":[-1.208100,1.441700,0.000000]},{"label":"O","location":[0.262800,-3.329900,0.000000]},{"label":"O","location":[-2.705000,1.333800,0.000000]},{"label":"H","location":[-3.378800,2.326700,0.000000]},{"label":"H","location":[3.240300,1.170900,0.000000]}],"bonds":[{"type":1,"atoms":[0,1]},{"type":1,"atoms":[0,2]},{"type":1,"atoms":[1,3]},{"type":1,"atoms":[1,6],"stereo":6},{"type":1,"atoms":[2,4]},{"type":1,"atoms":[2,7],"stereo":6},{"type":1,"atoms":[3,4]},{"type":1,"atoms":[3,5],"stereo":1},{"type":1,"atoms":[5,10]},{"type":1,"atoms":[6,8]},{"type":1,"atoms":[8,9]}]},"monomerTemplate-C___Cytosine":{"type":"monomerTemplate","id":"C___Cytosine","class":"Base","classHELM":"RNA","fullName":"Cytosine","alias":"C","naturalAnalogShort":"C","attachmentPoints":[{"attachmentAtom":3,"type":"left","leavingGroup":{"atoms":[8]}}],"atoms":[{"label":"C","location":[1.861700,1.349900,0.000000]},{"label":"C","location":[1.111700,2.648900,0.000000]},{"label":"C","location":[-0.388200,2.649000,0.000000]},{"label":"N","location":[-1.138200,1.350000,0.000000]},{"label":"C","location":[-0.388300,0.050900,0.000000]},{"label":"N","location":[1.111700,0.050900,0.000000]},{"label":"N","location":[3.061800,1.349900,0.000000]},{"label":"O","location":[-0.988400,-0.988300,0.000000]},{"label":"H","location":[-2.338300,1.350000,0.000000]}],"bonds":[{"type":1,"atoms":[0,1]},{"type":2,"atoms":[0,5]},{"type":1,"atoms":[0,6]},{"type":2,"atoms":[1,2]},{"type":1,"atoms":[2,3]},{"type":1,"atoms":[3,4]},{"type":1,"atoms":[3,8]},{"type":1,"atoms":[4,5]},{"type":2,"atoms":[4,7]}]},"monomerTemplate-MOE___2'-O-Methoxyethyl ribose":{"type":"monomerTemplate","id":"MOE___2'-O-Methoxyethyl ribose","class":"Sugar","classHELM":"RNA","fullName":"2'-O-Methoxyethyl ribose","alias":"MOE","naturalAnalogShort":"R","attachmentPoints":[{"attachmentAtom":9,"type":"left","leavingGroup":{"atoms":[10]}},{"attachmentAtom":5,"type":"right","leavingGroup":{"atoms":[11]}},{"attachmentAtom":2,"type":"side","leavingGroup":{"atoms":[8]}}],"atoms":[{"label":"O","location":[-2.234000,0.032800,0.000000]},{"label":"C","location":[-1.722000,1.442700,0.000000],"stereoLabel":"abs"},{"label":"C","location":[-1.051400,-0.889800,0.000000],"stereoLabel":"abs"},{"label":"C","location":[-0.222900,1.391500,0.000000],"stereoLabel":"abs"},{"label":"C","location":[0.191600,-0.050000,0.000000],"stereoLabel":"abs"},{"label":"O","location":[0.696200,2.574600,0.000000]},{"label":"O","location":[1.598800,-0.564400,0.000000]},{"label":"C","location":[-2.563300,2.682500,0.000000]},{"label":"O","location":[-1.092300,-2.089100,0.000000]},{"label":"O","location":[-4.060200,2.574600,0.000000]},{"label":"H","location":[-4.734100,3.567400,0.000000]},{"label":"H","location":[1.885100,2.411800,0.000000]},{"label":"C","location":[1.858400,-2.042600,0.000000]},{"label":"C","location":[3.268000,-2.557800,0.000000]},{"label":"O","location":[3.527500,-4.036100,0.000000]},{"label":"C","location":[4.654600,-4.448100,0.000000]}],"bonds":[{"type":1,"atoms":[0,1]},{"type":1,"atoms":[0,2]},{"type":1,"atoms":[1,3]},{"type":1,"atoms":[1,7],"stereo":6},{"type":1,"atoms":[2,4]},{"type":1,"atoms":[2,8],"stereo":6},{"type":1,"atoms":[3,4]},{"type":1,"atoms":[3,5],"stereo":1},{"type":1,"atoms":[4,6],"stereo":1},{"type":1,"atoms":[5,11]},{"type":1,"atoms":[7,9]},{"type":1,"atoms":[9,10]},{"type":1,"atoms":[6,12]},{"type":1,"atoms":[12,13]},{"type":1,"atoms":[13,14]},{"type":1,"atoms":[14,15]}]}}
26 changes: 26 additions & 0 deletions api/wasm/indigo-ketcher/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,32 @@ M END
assert.equal(idt, res_idt);
options.delete();
save_options.delete();
// check autodetect
let ad_options = new indigo.MapStringString();
ad_options.set("output-content-type", "application/json");
ad_options.set("monomerLibrary", monomersLib);
const res2 = indigo.convert(idt, "ket", ad_options);
const res2_ket = JSON.parse(res2).struct;
assert.equal(res2_ket, res_ket_ref.toString().trim());
ad_options.delete()
});
}

{
test("HELM", "basic", () => {
var fs = require('fs');
let options = new indigo.MapStringString();
const monomersLib = fs.readFileSync("monomer_library.ket");
// test autodetect
options.set("output-content-type", "application/json");
options.set("monomerLibrary", monomersLib);
const helm = "RNA1{P.[mR](A)[sP].[mR](G)P.[dR](C)P.[MOE](A)P.P}$$$$V2.0";
const res = indigo.convert(helm, "ket", options);
const res_ket = JSON.parse(res).struct;
// fs.writeFileSync("helm_maxmgc.ket", res_ket);
const res_ket_ref = fs.readFileSync("helm_maxmgc.ket");
assert.equal(res_ket, res_ket_ref.toString().trim());
options.delete();
});
}

Expand Down
35 changes: 33 additions & 2 deletions utils/indigo-service/backend/service/tests/api/indigo_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3529,7 +3529,22 @@ def test_convert_idt(self):

# check
with open(ref_prefix + ".ket", "r") as file:
self.assertEqual(result_ket, file.read())
ref_ket = file.read()
self.assertEqual(result_ket, ref_ket)

# check autodetect IDT
headers, data = self.get_headers(
{
"struct": idt,
"options": {"monomerLibrary": monomer_library},
"output_format": "chemical/x-indigo-ket",
}
)
result = requests.post(
self.url_prefix + "/convert", headers=headers, data=data
)
result_ket = json.loads(result.text)["struct"]
self.assertEqual(result_ket, ref_ket)

# Ket to IDT
headers, data = self.get_headers(
Expand Down Expand Up @@ -3578,7 +3593,23 @@ def test_convert_helm(self):

# check
with open(ref_prefix + ".ket", "r") as file:
self.assertEqual(result_ket, file.read())
ref_ket = file.read()
self.assertEqual(result_ket, ref_ket)

# HELM autodetect
headers, data = self.get_headers(
{
"struct": helm_struct,
"options": {"monomerLibrary": monomer_library},
"output_format": "chemical/x-indigo-ket",
}
)

result = requests.post(
self.url_prefix + "/convert", headers=headers, data=data
)
result_ket = json.loads(result.text)["struct"]
self.assertEqual(result_ket, ref_ket)

# Ket to HELM
headers, data = self.get_headers(
Expand Down
24 changes: 20 additions & 4 deletions utils/indigo-service/backend/service/v2/indigo_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,26 @@ def load_moldata(
md.struct = indigo.loadQueryReaction(molstr)
md.is_query = True
except IndigoException:
raise HttpException(
"struct data not recognized as molecule, query, reaction or reaction query",
400,
)
if library is None:
raise HttpException(
"struct data not recognized as molecule, query, reaction or reaction query",
400,
)
else: # has library try to load IDT and HELM
try:
md.struct = indigo.loadIdt(molstr, library)
md.is_rxn = False
except IndigoException:
try:
md.struct = indigo.loadHelm(
molstr, library
)
except IndigoException:
raise HttpException(
"struct data not recognized as molecule, query, reaction or reaction query",
400,
)

return md


Expand Down

0 comments on commit 5782327

Please sign in to comment.