/** * Gets the reactionIDs attribute of the CreateReactionAction object * *@param reactionSet Description of the Parameter *@return The reactionIDs value */ private Object[] getReactionIDs(IReactionSet reactionSet) { if (reactionSet != null) { String[] ids = new String[reactionSet.getReactionCount()]; for (int i = 0; i < reactionSet.getReactionCount(); i++) { ids[i] = reactionSet.getReaction(i).getID(); } return ids; } else { return new String[0]; } } }
@Test public void testGetReaction_int() { IReactionSet reactionSet = (IReactionSet) newChemObject(); reactionSet.addReaction(reactionSet.getBuilder().newInstance(IReaction.class)); // 1 reactionSet.addReaction(reactionSet.getBuilder().newInstance(IReaction.class)); // 2 reactionSet.addReaction(reactionSet.getBuilder().newInstance(IReaction.class)); // 3 reactionSet.addReaction(reactionSet.getBuilder().newInstance(IReaction.class)); // 4 for (int i = 0; i < reactionSet.getReactionCount(); i++) { Assert.assertNotNull(reactionSet.getReaction(i)); } }
/** * Get all Reactions object containing a Molecule from a set of Reactions. * * @param reactSet The set of reaction to inspect * @param molecule The molecule to find * @return The IReactionSet */ public static IReactionSet getRelevantReactions(IReactionSet reactSet, IAtomContainer molecule) { IReactionSet newReactSet = reactSet.getBuilder().newInstance(IReactionSet.class); IReactionSet reactSetProd = getRelevantReactionsAsProduct(reactSet, molecule); for (IReaction reaction : reactSetProd.reactions()) newReactSet.addReaction(reaction); IReactionSet reactSetReact = getRelevantReactionsAsReactant(reactSet, molecule); for (IReaction reaction : reactSetReact.reactions()) newReactSet.addReaction(reaction); return newReactSet; }
/** * Calculate the average bond length for the bonds in a reaction set. * @param reactionSet the reaction set to use * @return the average bond length */ public static double calculateAverageBondLength(IReactionSet reactionSet) { double averageBondModelLength = 0.0; for (IReaction reaction : reactionSet.reactions()) { averageBondModelLength += calculateAverageBondLength(reaction); } return averageBondModelLength / reactionSet.getReactionCount(); }
/** * * @param reactionSet * @throws java.lang.Exception */ public synchronized void standardize(IReactionSet reactionSet) throws Exception { for (IReaction reaction : reactionSet.reactions()) { IReaction standardizedReaction = standardize(reaction); reactionSet.addReaction(standardizedReaction); } }
@Test public void testGetRelevantReactionsAsReactant_IReactionSet_IAtomContainer() { IReactionSet set = builder.newInstance(IReactionSet.class); IReaction reaction1 = builder.newInstance(IReaction.class); set.addReaction(reaction1); IAtomContainer mol1a = builder.newInstance(IAtomContainer.class); IAtomContainer mol1b = builder.newInstance(IAtomContainer.class); reaction1.addReactant(mol1a); reaction1.addReactant(mol1b); reaction1.addProduct(builder.newInstance(IAtomContainer.class)); reaction1.addProduct(builder.newInstance(IAtomContainer.class)); IReaction reaction2 = builder.newInstance(IReaction.class); reaction2.addReactant(mol1b); reaction2.addProduct(builder.newInstance(IAtomContainer.class)); set.addReaction(reaction2); IReaction reaction3 = builder.newInstance(IReaction.class); reaction3.addReactant(builder.newInstance(IAtomContainer.class)); reaction3.addProduct(builder.newInstance(IAtomContainer.class)); set.addReaction(reaction3); Assert.assertEquals(3, set.getReactionCount()); IReactionSet reactionSet2 = ReactionSetManipulator.getRelevantReactionsAsReactant(set, mol1b); Assert.assertEquals(2, reactionSet2.getReactionCount()); Assert.assertEquals(reaction1, reactionSet2.getReaction(0)); Assert.assertEquals(reaction2, reactionSet2.getReaction(1)); IReactionSet reactionSet1 = ReactionSetManipulator.getRelevantReactionsAsReactant(set, mol1a); Assert.assertEquals(1, reactionSet1.getReactionCount()); Assert.assertEquals(reaction1, reactionSet1.getReaction(0)); }
/** * Extract a set of Reactions which are in top of a IReactionScheme. The top reactions are those * which any of their reactants are participating in other reactions as a products. * * @param reactionScheme The IReactionScheme * @return The set of top reactions */ public static IReactionSet extractTopReactions(IReactionScheme reactionScheme) { IReactionSet reactionSet = reactionScheme.getBuilder().newInstance(IReactionSet.class); IReactionSet allSet = getAllReactions(reactionScheme); for (IReaction reaction : allSet.reactions()) { IReactionSet precuSet = extractPrecursorReaction(reaction, allSet); if (precuSet.getReactionCount() == 0) { boolean found = false; for (IReaction reactIn : reactionSet.reactions()) { if (reactIn.equals(reaction)) found = true; } if (!found) reactionSet.addReaction(reaction); } } return reactionSet; }
/** * Writes an array of Reaction to an OutputStream in MDL rdf format. * * @param reactions Array of Reactions that is written to an OutputStream */ private void writeReactionSet(IReactionSet reactions) throws CDKException { for (IReaction iReaction : reactions.reactions()) { writeReaction(iReaction); } }
@Override public <T extends IChemObject> T read(T object) throws CDKException { if (object instanceof IReaction) { return (T) readReaction(object.getBuilder()); } else if (object instanceof IChemModel) { IChemModel model = object.getBuilder().newInstance(IChemModel.class); IReactionSet reactionSet = object.getBuilder().newInstance(IReactionSet.class); reactionSet.addReaction(readReaction(object.getBuilder())); model.setReactionSet(reactionSet); return (T) model; } else { throw new CDKException("Only supported are Reaction and ChemModel, and not " + object.getClass().getName() + "."); } }
@Test public void testRemoveAllReactions() { IReactionSet reactionSet = (IReactionSet) newChemObject(); reactionSet.addReaction(reactionSet.getBuilder().newInstance(IReaction.class)); reactionSet.removeAllReactions(); Assert.assertEquals(0, reactionSet.getReactionCount()); }
@Test public void testGetReactionCount() { IReactionSet reactionSet = (IReactionSet) newChemObject(); reactionSet.addReaction(reactionSet.getBuilder().newInstance(IReaction.class)); // 1 reactionSet.addReaction(reactionSet.getBuilder().newInstance(IReaction.class)); // 2 reactionSet.addReaction(reactionSet.getBuilder().newInstance(IReaction.class)); // 3 reactionSet.addReaction(reactionSet.getBuilder().newInstance(IReaction.class)); // 4 reactionSet.addReaction(reactionSet.getBuilder().newInstance(IReaction.class)); // 5 reactionSet.addReaction(reactionSet.getBuilder().newInstance(IReaction.class)); // 6 (force growing) Assert.assertEquals(6, reactionSet.getReactionCount()); }
@Override public void endDocument() { // cdo.endDocument(); if (currentReactionSet != null && currentReactionSet.getReactionCount() == 0 && currentReaction != null) { logger.debug("Adding reaction to ReactionSet"); currentReactionSet.addReaction(currentReaction); } if (currentReactionSet != null && currentChemModel.getReactionSet() == null) { logger.debug("Adding SOR to ChemModel"); currentChemModel.setReactionSet(currentReactionSet); } if (currentMoleculeSet != null && currentMoleculeSet.getAtomContainerCount() != 0) { logger.debug("Adding reaction to MoleculeSet"); currentChemModel.setMoleculeSet(currentMoleculeSet); } if (currentChemSequence.getChemModelCount() == 0) { logger.debug("Adding ChemModel to ChemSequence"); currentChemSequence.addChemModel(currentChemModel); } if (currentChemFile.getChemSequenceCount() == 0) { // assume there is one non-animation ChemSequence // addChemSequence(currentChemSequence); currentChemFile.addChemSequence(currentChemSequence); } logger.info("End XML Doc"); }
/** * Create a IReactionScheme give a IReactionSet object. * * @param reactionSet The IReactionSet * @return The IReactionScheme */ public static IReactionScheme createReactionScheme(IReactionSet reactionSet) { IReactionScheme reactionScheme = reactionSet.getBuilder().newInstance(IReactionScheme.class); // Looking for those reactants which doesn't have any precursor. They are the top. ArrayList<IReaction> listTopR = new ArrayList<IReaction>(); for (IReaction reaction : reactionSet.reactions()) { if (extractPrecursorReaction(reaction, reactionSet).getReactionCount() == 0) listTopR.add(reaction); } for (IReaction reaction : listTopR) { reactionScheme.addReaction(reaction); IReactionScheme newReactionScheme = setScheme(reaction, reactionSet); if (newReactionScheme.getReactionCount() != 0 || newReactionScheme.getReactionSchemeCount() != 0) reactionScheme.add(newReactionScheme); } return reactionScheme; }
/** * Tells the position of an atom in a reaction. Format is "reaction/product:numberofreaction/product_atomnumber". * * @param reactionSet The reactionSet in which to search. * @param chemObject The atom to search for. * @return The position in the said format. * @throws CDKException Atom not found in reactionSet. */ private String getAtomNumber(IReactionSet reactionSet, IChemObject chemObject) throws CDKException { for (int i = 0; i < reactionSet.getReaction(0).getReactantCount(); i++) { for (int k = 0; k < reactionSet.getReaction(0).getReactants().getAtomContainer(i).getAtomCount(); k++) { if (reactionSet.getReaction(0).getReactants().getAtomContainer(i).getAtom(k).equals(chemObject)) return "reactant:" + i + "_" + k; } } for (int i = 0; i < reactionSet.getReaction(0).getProductCount(); i++) { for (int k = 0; k < reactionSet.getReaction(0).getProducts().getAtomContainer(i).getAtomCount(); k++) { if (reactionSet.getReaction(0).getProducts().getAtomContainer(i).getAtom(k).equals(chemObject)) return "product:" + i + "_" + k; } } throw new CDKException("not found"); } }
@Test public void testIsEmpty() { IReactionSet set = (IReactionSet) newChemObject(); Assert.assertTrue("new reaction set should be empty", set.isEmpty()); set.addReaction(set.getBuilder().newInstance(IReaction.class)); Assert.assertFalse("reaction set with a single reaction should not be empty", set.isEmpty()); set.removeAllReactions(); Assert.assertTrue("reaction set with all reactions removed should be empty", set.isEmpty()); }
/** * get all Molecules object from a set of Reactions. * * @param set The set of reaction to inspect * @return The IAtomContanerSet */ public static IAtomContainerSet getAllMolecules(IReactionSet set) { IAtomContainerSet moleculeSet = set.getBuilder().newInstance(IAtomContainerSet.class); for (IReaction reaction : set.reactions()) { IAtomContainerSet molecules = ReactionManipulator.getAllMolecules(reaction); for (IAtomContainer ac : molecules.atomContainers()) { boolean contain = false; for (IAtomContainer atomContainer : moleculeSet.atomContainers()) { if (atomContainer.equals(ac)) { contain = true; break; } } if (!contain) moleculeSet.addAtomContainer(ac); } } return moleculeSet; }
public static String newReactionId(IReactionSet reactionSet, String startName) { final Set<String> ids = currentIds(reactionSet); if (!ids.contains(startName)) return startName; for (int i = 2; i < reactionSet.getReactionCount() + 1; i++) { String potentialName = startName + " (" + i + ")"; if (!ids.contains(potentialName)) return potentialName; } return "reaction-" + System.currentTimeMillis(); }
@Test @Override public void testStateChanged_IChemObjectChangeEvent() { ChemObjectListenerImpl listener = new ChemObjectListenerImpl(); IReactionSet chemObject = (IReactionSet) newChemObject(); chemObject.addListener(listener); chemObject.addReaction(chemObject.getBuilder().newInstance(IReaction.class)); Assert.assertTrue(listener.changed); listener.reset(); Assert.assertFalse(listener.changed); }
/** * Method to test whether the class complies with RFC #9. */ @Test public void testToString() { IReactionSet reactionSet = (IReactionSet) newChemObject(); String description = reactionSet.toString(); for (int i = 0; i < description.length(); i++) { Assert.assertTrue(description.charAt(i) != '\n'); Assert.assertTrue(description.charAt(i) != '\r'); } IReaction reaction = reactionSet.getBuilder().newInstance(IReaction.class); reactionSet.addReaction(reaction); description = reactionSet.toString(); for (int i = 0; i < description.length(); i++) { Assert.assertTrue(description.charAt(i) != '\n'); Assert.assertTrue(description.charAt(i) != '\r'); } }