Skip to content

Commit

Permalink
#2417 - The layout is incorrect with retrosynthetic arrow (#2437)
Browse files Browse the repository at this point in the history
  • Loading branch information
AliaksandrDziarkach authored Sep 30, 2024
1 parent 24d8859 commit f6329eb
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 75 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
15 changes: 14 additions & 1 deletion api/tests/integration/tests/formats/ket_retrosynthetic_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def getCML(reaction):

print("output format: " + format_name)

# with open(os.path.join(ref_path, filename), "w") as file:
# file.write(parsed_format)
with open(os.path.join(ref_path, filename), "r") as file:
format_ref = file.read()
diff = find_diff(format_ref, parsed_format)
Expand All @@ -96,7 +98,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 @@ -108,3 +111,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
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VmpDRDAxMDAEAwIBAAAAAAAAAAAAAAAAAAAAAAUIBAAAAB4AGggCAAMAGwgCAAQAAAEkAAAAAgACAOn9BQBBcmlhbAMA6f0PAFRpbWVzIE5ldyBSb21hbgADMgAIAP///////wAAAAAAAP//AAAAAP////8AAAAA//8AAAAA/////wAAAAD/////AAD//wGAAAAAABAIAgABAA8IAgABAAOABAAAAASABQAAAAACCAD+PxAB4GTJAQAABIAGAAAAAAIIAAJA8gDgZMkBAAAEgAcAAAAAAggAAkDjANBprwEAAASACAAAAAIEAgAHACsEAgACAAACCAACQOMA8F/jAQaAAAAAAAACCAACQOMA8F/jASMIAQAAAAcZAAIAAAADAGAAyAAAAAIAAwAgAMgAAABOSDIAAAAABYAQAAAABAYEAAUAAAAFBgQABgAAAAAABYARAAAABAYEAAYAAAAFBgQABwAAAAAABYASAAAABAYEAAYAAAAFBgQACAAAAAAAAAADgAkAAAAEgAoAAAACBAIACAArBAIAAQAAAggAAkDyAPyfVwIGgAAAAAAAAggAAkDyAPyfVwIjCAEAAAAHDgABAAAAAwBgAMgAAABPSAAAAAAEg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AAAIEAgAIACsEAgABAAACCAAAAPT/5EaHAQaAAAAAAAACCAAAAPT/5EaHASMIAQAAAAcOAAEAAAADAGAAyAAAAE9IAAAAAAWAEwAAAAQGBAAKAAAABQYEAAsAAAAAAAWAFAAAAAQGBAALAAAABQYEAAwAAAAAAAAAIYANAAAABAIQAAAAAAByIxYBAAAAAHAjmAA3CgIAAAA1CgIAAgAvCgIAAwAgCgIAWAIwCgIAWAIxCgIAlgAzCgIAWAIHAgwAcqMVAQAAAAAAAAAACAIMAHCjmAAAAAAAAAAAAAAAB4AVAAAAEwAEAA0AAAAACgIAAQACCgIAIAAgCgIAWAIEAhAAAAAAAHKjFQEAAAAAcKOYAAAADYAAAAAADoAAAAAAAQwEAAQAAAACDAQACQAAAAQMBAAVAAAAAAAAAAAAAAAAAA==
24 changes: 12 additions & 12 deletions api/tests/integration/tests/formats/ref/ket_retro_arrow.cdxml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
</colortable>
<page HeightPages="1" WidthPages="1">
<fragment id="4">
<n id="5" p="457.394043 272.249969"/>
<n id="6" p="457.394043 242.250031"/>
<n id="7" p="431.413330 227.250031"/>
<n id="8" Element="7" NumHydrogens="2" p="483.374756 227.250031">
<t p="483.374756 227.250031" LabelJustification="Left">
<n id="5" p="80.569229 36.000000"/>
<n id="6" p="80.569229 -12.000000"/>
<n id="7" p="39.000000 -36.000000"/>
<n id="8" Element="7" NumHydrogens="2" p="122.138428 -36.000000">
<t p="122.138428 -36.000000" LabelJustification="Left">
<s font="3" size="10" face="96">NH</s>
<s font="3" size="10" face="32">2</s>
</t>
Expand All @@ -32,22 +32,22 @@
<b id="18" B="6" E="8"/>
</fragment>
<fragment id="9">
<n id="10" Element="8" NumHydrogens="1" p="599.624939 242.250031">
<t p="599.624939 242.250031" LabelJustification="Left">
<n id="10" Element="8" NumHydrogens="1" p="308.138458 -12.000000">
<t p="308.138458 -12.000000" LabelJustification="Left">
<s font="3" size="10" face="96">OH</s>
</t>
</n>
<n id="11" p="625.604858 257.250000"/>
<n id="12" Element="8" NumHydrogens="1" p="651.586670 242.250031">
<t p="651.586670 242.250031" LabelJustification="Left">
<n id="11" p="349.707703 12.000000"/>
<n id="12" Element="8" NumHydrogens="1" p="391.276917 -12.000000">
<t p="391.276917 -12.000000" LabelJustification="Left">
<s font="3" size="10" face="96">OH</s>
</t>
</n>
<b id="19" B="10" E="11"/>
<b id="20" B="11" E="12"/>
</fragment>
<arrow id="13" BoundingBox="562.505432 245.250000 510.712555 245.250000" FillType="None" ArrowheadHead="Full" ArrowheadType="Angle" HeadSize="600" ArrowheadCenterSize="600" ArrowheadWidth="150" ArrowShaftSpacing="600" Head3D="562.005432 245.250000 0.000000" Tail3D="511.212555 245.250000 0.000000"/>
<graphic id="21" SupersededBy="13" GraphicType="Line" ArrowType="RetroSynthetic" HeadSize="600" BoundingBox="562.005432 245.250000 511.212555 245.250000"/>
<arrow id="13" BoundingBox="278.138458 -0.000000 152.138428 -0.000000" FillType="None" ArrowheadHead="Full" ArrowheadType="Angle" HeadSize="600" ArrowheadCenterSize="600" ArrowheadWidth="150" ArrowShaftSpacing="600" Head3D="277.638458 0.000000 0.000000" Tail3D="152.638428 0.000000 0.000000"/>
<graphic id="21" SupersededBy="13" GraphicType="Line" ArrowType="RetroSynthetic" HeadSize="600" BoundingBox="277.638458 0.000000 152.638428 0.000000"/>
<scheme>
<step ReactionStepReactants="4" ReactionStepProducts="9" ReactionStepArrows="21"/>
</scheme>
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
65 changes: 44 additions & 21 deletions core/indigo-core/layout/src/reaction_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,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 @@ -126,14 +144,16 @@ void ReactionLayout::_updateMetadata()
Vec2f arrow_tail(0, 0);

constexpr float shift = 1.0f;
if (_r.productsCount() == 0)
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() + shift;
arrow_tail.y = react_box.middleY();
arrow_head.x = arrow_tail.x + shift;
arrow_head.y = arrow_tail.y;
}
else if (_r.reactantsCount() == 0)
else if (react_count == 0)
{
arrow_head.x = product_box.left() - shift;
arrow_head.y = product_box.middleY();
Expand Down Expand Up @@ -216,13 +236,19 @@ void ReactionLayout::make()

// layout molecules in a row with the intervals specified
Metalayout::LayoutLine& line = _ml.newLine();
for (int i = _r.reactantBegin(); i < _r.reactantEnd(); i = _r.reactantNext(i))
{
bool single_atom = _getMol(i).vertexCount() == 1;
if (i != _r.reactantBegin())
_pushSpace(line, plus_interval_factor);
_pushMol(line, i);
}
auto processReactionElements = [this, &line](int begin, int end, std::function<int(BaseReaction&, int)> next) {
for (int i = begin; i < end; i = next(_r, i))
{
if (i != begin)
_pushSpace(line, plus_interval_factor);
_pushMol(line, i);
}
};

if (_r.isRetrosyntetic())
processReactionElements(_r.productBegin(), _r.productEnd(), &BaseReaction::productNext);
else
processReactionElements(_r.reactantBegin(), _r.reactantEnd(), &BaseReaction::reactantNext);

if (_r.catalystCount())
{
Expand All @@ -241,13 +267,10 @@ void ReactionLayout::make()

_pushSpace(line, bond_length);

for (int i = _r.productBegin(); i < _r.productEnd(); i = _r.productNext(i))
{
bool single_atom = _getMol(i).vertexCount() == 1;
if (i != _r.productBegin())
_pushSpace(line, plus_interval_factor);
_pushMol(line, i);
}
if (_r.isRetrosyntetic())
processReactionElements(_r.reactantBegin(), _r.reactantEnd(), &BaseReaction::reactantNext);
else
processReactionElements(_r.productBegin(), _r.productEnd(), &BaseReaction::productNext);

_ml.bondLength = bond_length;
_ml.horizontalIntervalFactor = horizontal_interval_factor;
Expand Down

0 comments on commit f6329eb

Please sign in to comment.