Skip to content

Commit

Permalink
Redo @parit's changes for net/undirected reaction depiction on the ne…
Browse files Browse the repository at this point in the history
…w reaction drawing code.
  • Loading branch information
johnmay committed Aug 29, 2023
1 parent 07a0595 commit 789c4dd
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ ReactionDimensions getDimensions(double padding) {
int nCol;
double arrowHeight = plus.height();
double arrowHeadLength = plus.height();
double minArrowWidth = direction != null ? 5 * arrowHeight : 0;
double minArrowWidth = direction != null && direction != IReaction.Direction.UNDIRECTED ? 5 * arrowHeight : arrowHeight;

double[] xOffsets, yOffsets;
double[] xOffsetSide, yOffsetSide;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,12 @@ static Bounds createArrow(IReaction.Direction direction,
double minHeight) {
if (direction == null)
return new Bounds();
Bounds arrow = new Bounds();
Path2D path = new Path2D.Double();
final double headThickness = minHeight / 3;
final double inset = 0.8;
final double headLength = minHeight;
double strokeWidth = minHeight / 14;
Bounds arrow = new Bounds(0, -headThickness, length, +headThickness);
switch (direction) {
case FORWARD:
arrow.add(new LineElement(0, 0, length - 0.5 * headLength, 0, strokeWidth, color));
Expand All @@ -407,6 +407,13 @@ static Bounds createArrow(IReaction.Direction direction,
path.closePath();
arrow.add(GeneralPath.shapeOf(path, color));
break;
case UNDIRECTED:
double x1 = headThickness;
double x2 = length-2*headThickness;
double y = 0.5 * headThickness;
arrow.add(new LineElement(x1, -(y), x2, -(y), strokeWidth, color));
arrow.add(new LineElement(x1, +(y), x2, +(y), strokeWidth, color));
break;
case BIDIRECTIONAL: // equilibrium?
arrow.add(new LineElement(0, +0.5 * headThickness, length - 0.5 * headLength, +0.5 * headThickness, strokeWidth, color));
path.moveTo(length, 0.5 * headThickness - 0.5 * strokeWidth);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@
import org.openscience.cdk.silent.SilentChemObjectBuilder;
import org.openscience.cdk.smiles.SmilesParser;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand Down Expand Up @@ -124,4 +130,28 @@ void connectMolWithTitlesInSvg() throws CDKException
.filter(el -> stringsToFind.indexOf(el) != -1).collect(Collectors.toList());
Assertions.assertIterableEquals(stringsToFind, foundmatches);
}

@Test
void depictUndirectedReactionAsSVG()
throws CDKException, IOException
{
List<String> source = readResourceFile("rhea-net-reaction.svg");
final SmilesParser smilesParser = new SmilesParser(SilentChemObjectBuilder.getInstance());
IReaction rxn = smilesParser.parseReactionSmiles("O.CCCCC(N)=O>>[NH4+].CCCCC([O-])=O");
rxn.setDirection(IReaction.Direction.UNDIRECTED);
DepictionGenerator dg = new DepictionGenerator();
String svg = dg.depict(rxn).toSvgStr("px");
String[] targetLines = svg.split("\n");
Assertions.assertIterableEquals(source, Arrays.asList(targetLines));
}

private List<String> readResourceFile(String resourceName)
throws IOException
{
try(InputStream in = getClass().getResourceAsStream(resourceName);
Reader rdr = new InputStreamReader(in, StandardCharsets.UTF_8);
BufferedReader buff = new BufferedReader(rdr)) {
return buff.lines().collect(Collectors.toList());
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ enum Direction {
RETRO_SYNTHETIC,
/** Reaction shows interconversion between resonance forms. Usually
* denoted by a double-headed arrow. */
RESONANCE
RESONANCE,
/**
* Reaction equilibrium is not known to be fully on reactant/product
* or in equilibrium state.
* Also used to represent a "net reaction" composed of several
* elementary steps.
* Often denoted by a "=" sign
*/
UNDIRECTED
}

/**
Expand Down

0 comments on commit 789c4dd

Please sign in to comment.