Skip to content

Commit

Permalink
Backmerge: #2417 - The layout is incorrect with retrosynthetic arrow
Browse files Browse the repository at this point in the history
Backmerge to master
  • Loading branch information
AliaksandrDziarkach committed Sep 29, 2024
1 parent d09a7d3 commit e183801
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 12 additions & 1 deletion api/tests/integration/tests/formats/ket_retrosynthetic_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)
36 changes: 18 additions & 18 deletions api/tests/integration/tests/formats/reactions/ket_retro_arrow.ket
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
Expand All @@ -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
]
}
Expand Down Expand Up @@ -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
]
}
Expand Down
14 changes: 7 additions & 7 deletions api/tests/integration/tests/formats/ref/ket_retro_arrow.cml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<reactantList>
<molecule>
<atomArray>
<atom id="a0" elementType="O" x2="19.9875" y2="-8.075"/>
<atom id="a1" elementType="C" x2="20.8535" y2="-8.575"/>
<atom id="a2" elementType="O" x2="21.7196" y2="-8.075"/>
<atom id="a0" elementType="O" x2="10.2713" y2="0.4"/>
<atom id="a1" elementType="C" x2="11.6569" y2="-0.4"/>
<atom id="a2" elementType="O" x2="13.0426" y2="0.4"/>
</atomArray>
<bondArray>
<bond atomRefs2="a0 a1" order="1"/>
Expand All @@ -17,10 +17,10 @@
<productList>
<molecule>
<atomArray>
<atom id="a0" elementType="C" x2="15.2465" y2="-9.075"/>
<atom id="a1" elementType="C" x2="15.2465" y2="-8.075"/>
<atom id="a2" elementType="C" x2="14.3804" y2="-7.575"/>
<atom id="a3" elementType="N" x2="16.1125" y2="-7.575"/>
<atom id="a0" elementType="C" x2="2.68564" y2="-1.2"/>
<atom id="a1" elementType="C" x2="2.68564" y2="0.4"/>
<atom id="a2" elementType="C" x2="1.3" y2="1.2"/>
<atom id="a3" elementType="N" x2="4.07128" y2="1.2"/>
</atomArray>
<bondArray>
<bond atomRefs2="a0 a1" order="1"/>
Expand Down
14 changes: 7 additions & 7 deletions api/tests/integration/tests/formats/ref/ket_retro_arrow_2000.rxn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions api/tests/integration/tests/formats/ref/ket_retro_arrow_3000.rxn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
46 changes: 37 additions & 9 deletions core/indigo-core/layout/src/reaction_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();
Expand Down Expand Up @@ -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())
{
Expand All @@ -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;
Expand Down

0 comments on commit e183801

Please sign in to comment.