From e1838016b31954ac2e7fd2a3c85c62c80e74042e Mon Sep 17 00:00:00 2001 From: Aliaksandr Dziarkach <18146690+AliaksandrDziarkach@users.noreply.github.com> Date: Sun, 29 Sep 2024 22:15:15 +0300 Subject: [PATCH] Backmerge: #2417 - The layout is incorrect with retrosynthetic arrow Backmerge to master --- .../formats/ket_retrosynthetic_arrow.py.out | 4 +- .../tests/formats/ket_retrosynthetic_arrow.py | 13 +++++- .../formats/reactions/ket_retro_arrow.ket | 36 +++++++-------- .../tests/formats/ref/ket_retro_arrow.cml | 14 +++--- .../formats/ref/ket_retro_arrow_2000.rxn | 14 +++--- .../formats/ref/ket_retro_arrow_3000.rxn | 14 +++--- .../layout/src/reaction_layout.cpp | 46 +++++++++++++++---- 7 files changed, 91 insertions(+), 50 deletions(-) diff --git a/api/tests/integration/ref/formats/ket_retrosynthetic_arrow.py.out b/api/tests/integration/ref/formats/ket_retrosynthetic_arrow.py.out index 85801f5313..661b7d29cd 100644 --- a/api/tests/integration/ref/formats/ket_retrosynthetic_arrow.py.out +++ b/api/tests/integration/ref/formats/ket_retrosynthetic_arrow.py.out @@ -76,4 +76,6 @@ ket_simple_arrow_retro_arrow_sum_of_products.smarts:SUCCEED output format: CML ket_simple_arrow_retro_arrow_sum_of_products.cml:SUCCEED issue 2318 -ket_simple_arrow_retro_arrow_sum_of_products.cml:SUCCEED +ket_retro_arrow.ket:SUCCEED +issue 2417 +ket_retro_arrow.ket:SUCCEED diff --git a/api/tests/integration/tests/formats/ket_retrosynthetic_arrow.py b/api/tests/integration/tests/formats/ket_retrosynthetic_arrow.py index a8981ea386..c527fbbbb0 100644 --- a/api/tests/integration/tests/formats/ket_retrosynthetic_arrow.py +++ b/api/tests/integration/tests/formats/ket_retrosynthetic_arrow.py @@ -100,7 +100,8 @@ def getCML(reaction): print("issue 2318") indigo.setOption("json-use-native-precision", "1") -fname = os.path.join(root_rea, "ket_retro_arrow.ket") +filename = "ket_retro_arrow.ket" +fname = os.path.join(root_rea, filename) rxn = indigo.loadReactionFromFile(fname) # with open(fname, "w") as file: # file.write(rxn.json()) @@ -112,3 +113,13 @@ def getCML(reaction): else: print(filename + ":FAILED") print(diff) +print("issue 2417") +rxn.layout() +# with open(fname, "w") as file: +# file.write(rxn.json()) +diff = find_diff(ref_json, rxn.json()) +if not diff: + print(filename + ":SUCCEED") +else: + print(filename + ":FAILED") + print(diff) diff --git a/api/tests/integration/tests/formats/reactions/ket_retro_arrow.ket b/api/tests/integration/tests/formats/reactions/ket_retro_arrow.ket index 5464fb3676..42019ac0d1 100644 --- a/api/tests/integration/tests/formats/reactions/ket_retro_arrow.ket +++ b/api/tests/integration/tests/formats/reactions/ket_retro_arrow.ket @@ -13,13 +13,13 @@ "mode": "retrosynthetic", "pos": [ { - "x": 17.023752, - "y": -8.175, + "x": 5.071281, + "y": 0.0, "z": 0.0 }, { - "x": 18.750181, - "y": -8.175, + "x": 9.271282, + "y": 0.0, "z": 0.0 } ] @@ -33,32 +33,32 @@ { "label": "C", "location": [ - 15.246468, - -9.074999, + 2.685641, + -1.2, 0.0 ] }, { "label": "C", "location": [ - 15.246468, - -8.075001, + 2.685641, + 0.4, 0.0 ] }, { "label": "C", "location": [ - 14.380445, - -7.575001, + 1.3, + 1.2, 0.0 ] }, { "label": "N", "location": [ - 16.112492, - -7.575001, + 4.071281, + 1.2, 0.0 ] } @@ -93,24 +93,24 @@ { "label": "O", "location": [ - 19.987497, - -8.075001, + 10.271282, + 0.4, 0.0 ] }, { "label": "C", "location": [ - 20.853495, - -8.575, + 11.656923, + -0.4, 0.0 ] }, { "label": "O", "location": [ - 21.719555, - -8.075001, + 13.042563, + 0.4, 0.0 ] } diff --git a/api/tests/integration/tests/formats/ref/ket_retro_arrow.cml b/api/tests/integration/tests/formats/ref/ket_retro_arrow.cml index 354e718394..fdd6393ff0 100644 --- a/api/tests/integration/tests/formats/ref/ket_retro_arrow.cml +++ b/api/tests/integration/tests/formats/ref/ket_retro_arrow.cml @@ -4,9 +4,9 @@ - - - + + + @@ -17,10 +17,10 @@ - - - - + + + + diff --git a/api/tests/integration/tests/formats/ref/ket_retro_arrow_2000.rxn b/api/tests/integration/tests/formats/ref/ket_retro_arrow_2000.rxn index c29e36ea27..b5839a9cd3 100644 --- a/api/tests/integration/tests/formats/ref/ket_retro_arrow_2000.rxn +++ b/api/tests/integration/tests/formats/ref/ket_retro_arrow_2000.rxn @@ -8,9 +8,9 @@ $MOL -INDIGO-01000000002D 3 2 0 0 0 0 0 0 0 0999 V2000 - 19.9875 -8.0750 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0 - 20.8535 -8.5750 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 - 21.7196 -8.0750 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0 + 10.2713 0.4000 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0 + 11.6569 -0.4000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 + 13.0426 0.4000 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0 1 2 1 0 0 0 0 2 3 1 0 0 0 0 M END @@ -19,10 +19,10 @@ $MOL -INDIGO-01000000002D 4 3 0 0 0 0 0 0 0 0999 V2000 - 15.2465 -9.0750 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 - 15.2465 -8.0750 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 - 14.3804 -7.5750 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 - 16.1125 -7.5750 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 + 2.6856 -1.2000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 + 2.6856 0.4000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 + 1.3000 1.2000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 + 4.0713 1.2000 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 1 2 1 0 0 0 0 2 3 1 0 0 0 0 2 4 1 0 0 0 0 diff --git a/api/tests/integration/tests/formats/ref/ket_retro_arrow_3000.rxn b/api/tests/integration/tests/formats/ref/ket_retro_arrow_3000.rxn index 7e11303d38..ce6f4b7e44 100644 --- a/api/tests/integration/tests/formats/ref/ket_retro_arrow_3000.rxn +++ b/api/tests/integration/tests/formats/ref/ket_retro_arrow_3000.rxn @@ -7,9 +7,9 @@ M V30 BEGIN REACTANT M V30 BEGIN CTAB M V30 COUNTS 3 2 0 0 0 M V30 BEGIN ATOM -M V30 1 O 19.9875 -8.075 0.0 0 -M V30 2 C 20.8535 -8.575 0.0 0 -M V30 3 O 21.7196 -8.075 0.0 0 +M V30 1 O 10.2713 0.4 0.0 0 +M V30 2 C 11.6569 -0.4 0.0 0 +M V30 3 O 13.0426 0.4 0.0 0 M V30 END ATOM M V30 BEGIN BOND M V30 1 1 1 2 @@ -21,10 +21,10 @@ M V30 BEGIN PRODUCT M V30 BEGIN CTAB M V30 COUNTS 4 3 0 0 0 M V30 BEGIN ATOM -M V30 1 C 15.2465 -9.075 0.0 0 -M V30 2 C 15.2465 -8.075 0.0 0 -M V30 3 C 14.3804 -7.575 0.0 0 -M V30 4 N 16.1125 -7.575 0.0 0 +M V30 1 C 2.68564 -1.2 0.0 0 +M V30 2 C 2.68564 0.4 0.0 0 +M V30 3 C 1.3 1.2 0.0 0 +M V30 4 N 4.07128 1.2 0.0 0 M V30 END ATOM M V30 BEGIN BOND M V30 1 1 1 2 diff --git a/core/indigo-core/layout/src/reaction_layout.cpp b/core/indigo-core/layout/src/reaction_layout.cpp index 4347f4d22e..3b18eca70a 100644 --- a/core/indigo-core/layout/src/reaction_layout.cpp +++ b/core/indigo-core/layout/src/reaction_layout.cpp @@ -117,17 +117,35 @@ void ReactionLayout::_updateMetadata() Rect2f react_box, product_box, catalyst_box; bool last_single_reactant = false; bool first_single_product = false; + bool is_retrosyntetic = _r.isRetrosyntetic(); if (_r.reactantsCount() > 0) { - processSideBoxes(pluses, react_box, BaseReaction::REACTANT); - for (int i = _r.reactantBegin(); i != _r.reactantEnd(); i = _r.reactantNext(i)) - last_single_reactant = _r.getBaseMolecule(i).vertexCount() == 1; + if (is_retrosyntetic) + { + processSideBoxes(pluses, react_box, BaseReaction::PRODUCT); + for (int i = _r.productBegin(); i != _r.productEnd(); i = _r.productNext(i)) + last_single_reactant = _r.getBaseMolecule(i).vertexCount() == 1; + } + else + { + processSideBoxes(pluses, react_box, BaseReaction::REACTANT); + for (int i = _r.reactantBegin(); i != _r.reactantEnd(); i = _r.reactantNext(i)) + last_single_reactant = _r.getBaseMolecule(i).vertexCount() == 1; + } } if (_r.productsCount() > 0) { - processSideBoxes(pluses, product_box, BaseReaction::PRODUCT); - first_single_product = _r.getBaseMolecule(_r.productBegin()).vertexCount() == 1; + if (is_retrosyntetic) + { + processSideBoxes(pluses, product_box, BaseReaction::REACTANT); + first_single_product = _r.getBaseMolecule(_r.reactantBegin()).vertexCount() == 1; + } + else + { + processSideBoxes(pluses, product_box, BaseReaction::PRODUCT); + first_single_product = _r.getBaseMolecule(_r.productBegin()).vertexCount() == 1; + } } if (_r.catalystCount() > 0) @@ -139,14 +157,18 @@ void ReactionLayout::_updateMetadata() // calculate arrow size and position Vec2f arrow_head(0, 0); Vec2f arrow_tail(0, 0); - if (_r.productsCount() == 0) + + constexpr float shift = 1.0f; + int prod_count = is_retrosyntetic ? _r.reactantsCount() : _r.productsCount(); + int react_count = is_retrosyntetic ? _r.productsCount() : _r.reactantsCount(); + if (prod_count == 0) { arrow_tail.x = react_box.right() + reaction_margin_size + atom_label_margin; arrow_tail.y = react_box.middleY(); arrow_head.x = arrow_tail.x + default_arrow_size + atom_label_margin; arrow_head.y = arrow_tail.y; } - else if (_r.reactantsCount() == 0) + else if (react_count == 0) { arrow_head.x = product_box.left() - reaction_margin_size - atom_label_margin; arrow_head.y = product_box.middleY(); @@ -273,7 +295,10 @@ void ReactionLayout::make() } }; - processReactionElements(_r.reactantBegin(), _r.reactantEnd(), &BaseReaction::reactantNext); + if (_r.isRetrosyntetic()) + processReactionElements(_r.productBegin(), _r.productEnd(), &BaseReaction::productNext); + else + processReactionElements(_r.reactantBegin(), _r.reactantEnd(), &BaseReaction::reactantNext); if (_r.catalystCount()) { @@ -295,7 +320,10 @@ void ReactionLayout::make() else _pushSpace(line, default_arrow_size + reaction_margin_size * 2); - processReactionElements(_r.productBegin(), _r.productEnd(), &BaseReaction::productNext); + if (_r.isRetrosyntetic()) + processReactionElements(_r.reactantBegin(), _r.reactantEnd(), &BaseReaction::reactantNext); + else + processReactionElements(_r.productBegin(), _r.productEnd(), &BaseReaction::productNext); _ml.bondLength = bond_length; _ml.reactionComponentMarginSize = reaction_margin_size;