Skip to content

Commit

Permalink
Merge pull request #980 from parit/molToTitle
Browse files Browse the repository at this point in the history
Propogating molecule id in svg to the corresponding title element
  • Loading branch information
johnmay committed Jun 23, 2023
2 parents 82d8098 + 7176da1 commit 855b3ae
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Expand Up @@ -682,8 +682,10 @@ private Bounds generateTitle(IChemObject chemObj, double scale) {
if (title == null || title.isEmpty())
return new Bounds();
scale = 1 / scale * getParameterValue(RendererModel.TitleFontScale.class);
String refId = chemObj.getProperty(MarkedElement.ID_KEY);
String classStr = refId != null ? ("title " + refId) : "title";
return new Bounds(MarkedElement.markup(StandardGenerator.embedText(font, title, getParameterValue(RendererModel.TitleColor.class), scale),
"title"));
classStr));
}

private Bounds generateReactionConditions(IReaction chemObj, Color fg, double scale) {
Expand Down
Expand Up @@ -25,11 +25,23 @@

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IReaction;
import org.openscience.cdk.silent.SilentChemObjectBuilder;
import org.openscience.cdk.smiles.SmilesParser;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


class DepictionTest {

@Test
Expand Down Expand Up @@ -80,4 +92,36 @@ void depictAsSvg() throws CDKException {
Assertions.assertEquals("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">", lines[1]);
}

@Test
void connectMolWithTitlesInSvg() throws CDKException
{
final SmilesParser smilesParser = new SmilesParser(SilentChemObjectBuilder.getInstance());
IReaction rxn = smilesParser.parseReactionSmiles("O.CCCCC(N)=O>>[NH4+].CCCCC([O-])=O");
int count = 0;
for(IAtomContainer reactant : rxn.getReactants().atomContainers()) {
reactant.setProperty(CDKConstants.TITLE, "Reactant-" + ++count);
}
count = 0;
for(IAtomContainer agent : rxn.getAgents().atomContainers()) {
agent.setProperty(CDKConstants.TITLE, "Agent-" + ++count);
}
count = 0;
for(IAtomContainer product : rxn.getProducts().atomContainers()) {
product.setProperty(CDKConstants.TITLE, "Product-" + ++count);
}
DepictionGenerator dg = new DepictionGenerator().withMolTitle();
String[] targetLines = dg.depict(rxn).toSvgStr("px").split("\n");

List<String> stringsToFind = Arrays.asList(
"<g class='title mol1'>",
"<g class='title mol2'>",
"<g class='title mol3'>",
"<g class='title mol4'>"
);
List<String> foundmatches =
Stream.of(targetLines)
.map(el -> el.trim())
.filter(el -> stringsToFind.indexOf(el) != -1).collect(Collectors.toList());
Assertions.assertIterableEquals(stringsToFind, foundmatches);
}
}

0 comments on commit 855b3ae

Please sign in to comment.