private double caclModelScale(IReaction rxn) { List<IAtomContainer> mols = new ArrayList<>(); for (IAtomContainer mol : rxn.getReactants().atomContainers()) mols.add(mol); for (IAtomContainer mol : rxn.getProducts().atomContainers()) mols.add(mol); for (IAtomContainer mol : rxn.getAgents().atomContainers()) mols.add(mol); return caclModelScale(mols); }
@Test public void testReaction() throws Exception { String cmlString = "<reaction>" + "<reactantList><reactant><molecule id='react'/></reactant></reactantList>" + "<productList><product><molecule id='product'/></product></productList>" + "<substanceList><substance><molecule id='water'/></substance></substanceList>" + "</reaction>"; IChemFile chemFile = parseCMLString(cmlString); IReaction reaction = checkForSingleReactionFile(chemFile); Assert.assertEquals(1, reaction.getReactantCount()); Assert.assertEquals(1, reaction.getProductCount()); Assert.assertEquals(1, reaction.getAgents().getMoleculeCount()); Assert.assertEquals("react", reaction.getReactants().getMolecule(0).getID()); Assert.assertEquals("product", reaction.getProducts().getMolecule(0).getID()); Assert.assertEquals("water", reaction.getAgents().getMolecule(0).getID()); }
/** * Depict a reaction. * * @param rxn reaction instance * @return depiction * @throws CDKException a depiction could not be generated */ public Depiction depict(IReaction rxn) throws CDKException { ensure2dLayout(rxn); // can reorder components! final Color fgcol = getParameterValue(StandardGenerator.AtomColor.class) .getAtomColor(rxn.getBuilder().newInstance(IAtom.class, "C")); final List<IAtomContainer> reactants = toList(rxn.getReactants()); final List<IAtomContainer> products = toList(rxn.getProducts()); final List<IAtomContainer> agents = toList(rxn.getAgents()); // set ids for tagging elements int molId = 0; for (IAtomContainer mol : reactants) { setIfMissing(mol, MarkedElement.ID_KEY, "mol" + ++molId); setIfMissing(mol, MarkedElement.CLASS_KEY, "reactant"); } for (IAtomContainer mol : products) { setIfMissing(mol, MarkedElement.ID_KEY, "mol" + ++molId); setIfMissing(mol, MarkedElement.CLASS_KEY, "product"); } for (IAtomContainer mol : agents) { setIfMissing(mol, MarkedElement.ID_KEY, "mol" + ++molId); setIfMissing(mol, MarkedElement.CLASS_KEY, "agent"); } final Map<IChemObject, Color> myHighlight = new HashMap<>(); if (highlightAtomMap) { myHighlight.putAll(makeHighlightAtomMap(reactants, products)); } // user highlight buffer pushes out the atom-map highlight if provided myHighlight.putAll(highlight); highlight.clear(); final List<Double> reactantScales = prepareCoords(reactants); final List<Double> productScales = prepareCoords(products); final List<Double> agentScales = prepareCoords(agents); // highlight parts for (Map.Entry<IChemObject, Color> e : myHighlight.entrySet()) e.getKey().setProperty(StandardGenerator.HIGHLIGHT_COLOR, e.getValue()); // setup the model scale based on bond length final double scale = this.caclModelScale(rxn); final DepictionGenerator copy = this.withParam(BasicSceneGenerator.Scale.class, scale); final RendererModel model = copy.getModel(); // reactant/product/agent element generation, we number the reactants, then products then agents List<Bounds> reactantBounds = copy.generate(reactants, model, 1); List<Bounds> productBounds = copy.generate(toList(rxn.getProducts()), model, rxn.getReactantCount()); List<Bounds> agentBounds = copy.generate( toList(rxn.getAgents()), model, rxn.getReactantCount() + rxn.getProductCount()); // remove current highlight buffer for (IChemObject obj : myHighlight.keySet()) obj.removeProperty(StandardGenerator.HIGHLIGHT_COLOR); // generate a 'plus' element Bounds plus = copy.generatePlusSymbol(scale, fgcol); // reset the coordinates to how they were before we invoked depict resetCoords(reactants, reactantScales); resetCoords(products, productScales); resetCoords(agents, agentScales); final Bounds emptyBounds = new Bounds(); final Bounds title = copy.getParameterValue(BasicSceneGenerator.ShowReactionTitle.class) ? copy.generateTitle(rxn, scale) : emptyBounds; final List<Bounds> reactantTitles = new ArrayList<>(); final List<Bounds> productTitles = new ArrayList<>(); if (copy.getParameterValue(BasicSceneGenerator.ShowMoleculeTitle.class)) { for (IAtomContainer reactant : reactants) reactantTitles.add(copy.generateTitle(reactant, scale)); for (IAtomContainer product : products) productTitles.add(copy.generateTitle(product, scale)); } final Bounds conditions = generateReactionConditions(rxn, fgcol, model.get(BasicSceneGenerator.Scale.class)); return new ReactionDepiction( model, reactantBounds, productBounds, agentBounds, plus, rxn.getDirection(), dimensions, reactantTitles, productTitles, title, conditions, fgcol); }