/** * A unit test suite for JUnit for Ethene. Reaction: O=C-C-H => O(H)-C=C. Automatic looking for * active center. * * @cdk.inchi InChI=1/C2H4/c1-2/h1-2H2 * @return The test suite */ @Test public void testInitiate_IMoleculeSet_IMoleculeSet() throws Exception { IReactionProcess type = new AdductionProtonPBReaction(); IMolecule molecule = getEthene(); IMoleculeSet setOfReactants = DefaultChemObjectBuilder.getInstance().newMoleculeSet(); setOfReactants.addMolecule(molecule); /* initiate */ List<IParameterReact> paramList = new ArrayList<IParameterReact>(); IParameterReact param = new SetReactionCenter(); param.setParameter(Boolean.FALSE); paramList.add(param); type.setParameterList(paramList); IReactionSet setOfReactions = type.initiate(setOfReactants, null); Assert.assertEquals(2, setOfReactions.getReactionCount()); Assert.assertEquals(1, setOfReactions.getReaction(0).getProductCount()); IMolecule product = setOfReactions.getReaction(0).getProducts().getMolecule(0); IMolecule molecule2 = getExpected(); IQueryAtomContainer queryAtom = QueryAtomContainerCreator.createSymbolAndChargeQueryContainer(product); Assert.assertTrue(UniversalIsomorphismTester.isIsomorph(molecule2, queryAtom)); }
/** * A unit test suite for JUnit. * * @cdk.inchi InChI=1/C2H4/c1-2/h1-2H2 * @return The test suite */ @Test public void testCDKConstants_REACTIVE_CENTER() throws Exception { IReactionProcess type = new AdductionProtonPBReaction(); IMoleculeSet setOfReactants = builder.newMoleculeSet(); IMolecule molecule = getEthene(); /*manually putting the active center*/ molecule.getAtom(0).setFlag(CDKConstants.REACTIVE_CENTER, true); molecule.getAtom(1).setFlag(CDKConstants.REACTIVE_CENTER, true); molecule.getBond(0).setFlag(CDKConstants.REACTIVE_CENTER, true); setOfReactants.addMolecule(molecule); List<IParameterReact> paramList = new ArrayList<IParameterReact>(); IParameterReact param = new SetReactionCenter(); param.setParameter(Boolean.TRUE); paramList.add(param); type.setParameterList(paramList); /* initiate */ IReactionSet setOfReactions = type.initiate(setOfReactants, null); IMolecule reactant = setOfReactions.getReaction(0).getReactants().getMolecule(0); Assert.assertTrue(molecule.getAtom(0).getFlag(CDKConstants.REACTIVE_CENTER)); Assert.assertTrue(reactant.getAtom(0).getFlag(CDKConstants.REACTIVE_CENTER)); Assert.assertTrue(molecule.getAtom(1).getFlag(CDKConstants.REACTIVE_CENTER)); Assert.assertTrue(reactant.getAtom(1).getFlag(CDKConstants.REACTIVE_CENTER)); Assert.assertTrue(molecule.getBond(0).getFlag(CDKConstants.REACTIVE_CENTER)); Assert.assertTrue(reactant.getBond(0).getFlag(CDKConstants.REACTIVE_CENTER)); }
/** * Modules for cleaning a molecule * * @param molecule * @return cleaned AtomContainer */ @TestMethod("testCheckAndCleanMolecule") public static IAtomContainer checkAndCleanMolecule(IAtomContainer molecule) { boolean isMarkush = false; for (IAtom atom : molecule.atoms()) { if (atom.getSymbol().equals("R")) { isMarkush = true; break; } } if (isMarkush) { System.err.println("Skipping Markush structure for sanity check"); } // Check for salts and such if (!ConnectivityChecker.isConnected(molecule)) { // lets see if we have just two parts if so, we assume its a salt and just work // on the larger part. Ideally we should have a check to ensure that the smaller // part is a metal/halogen etc. IMoleculeSet fragments = ConnectivityChecker.partitionIntoMolecules(molecule); if (fragments.getMoleculeCount() > 2) { System.err.println("More than 2 components. Skipped"); } else { IMolecule frag1 = fragments.getMolecule(0); IMolecule frag2 = fragments.getMolecule(1); if (frag1.getAtomCount() > frag2.getAtomCount()) { molecule = frag1; } else { molecule = frag2; } } } configure(molecule); return molecule; }
/** * Initiate process. It is needed to call the addExplicitHydrogensToSatisfyValency from the class * tools.HydrogenAdder. * * @param reactants reactants of the reaction * @param agents agents of the reaction (Must be in this case null) * @exception CDKException Description of the Exception */ @TestMethod("testInitiate_IMoleculeSet_IMoleculeSet") public IReactionSet initiate(IMoleculeSet reactants, IMoleculeSet agents) throws CDKException { logger.debug("initiate reaction: HeterolyticCleavagePBReaction"); if (reactants.getMoleculeCount() != 1) { throw new CDKException("HeterolyticCleavagePBReaction only expects one reactant"); } if (agents != null) { throw new CDKException("HeterolyticCleavagePBReaction don't expects agents"); } IReactionSet setOfReactions = DefaultChemObjectBuilder.getInstance().newInstance(IReactionSet.class); IMolecule reactant = reactants.getMolecule(0); /* if the parameter hasActiveCenter is not fixed yet, set the active centers*/ IParameterReact ipr = super.getParameterClass(SetReactionCenter.class); if (ipr != null && !ipr.isSetParameter()) setActiveCenters(reactant); Iterator<IBond> bondis = reactant.bonds().iterator(); while (bondis.hasNext()) { IBond bondi = bondis.next(); IAtom atom1 = bondi.getAtom(0); IAtom atom2 = bondi.getAtom(1); if (bondi.getFlag(CDKConstants.REACTIVE_CENTER) && bondi.getOrder() != IBond.Order.SINGLE && atom1.getFlag(CDKConstants.REACTIVE_CENTER) && atom2.getFlag(CDKConstants.REACTIVE_CENTER) && (atom1.getFormalCharge() == CDKConstants.UNSET ? 0 : atom1.getFormalCharge()) == 0 && (atom2.getFormalCharge() == CDKConstants.UNSET ? 0 : atom2.getFormalCharge()) == 0 && reactant.getConnectedSingleElectronsCount(atom1) == 0 && reactant.getConnectedSingleElectronsCount(atom2) == 0) { /**/ for (int j = 0; j < 2; j++) { ArrayList<IAtom> atomList = new ArrayList<IAtom>(); if (j == 0) { atomList.add(atom1); atomList.add(atom2); } else { atomList.add(atom2); atomList.add(atom1); } ArrayList<IBond> bondList = new ArrayList<IBond>(); bondList.add(bondi); IMoleculeSet moleculeSet = reactant.getBuilder().newInstance(IMoleculeSet.class); moleculeSet.addMolecule(reactant); IReaction reaction = mechanism.initiate(moleculeSet, atomList, bondList); if (reaction == null) continue; else setOfReactions.addReaction(reaction); } } } return setOfReactions; }
private IChemFile readChemFile() throws CDKException { IChemSequence seq = file.getBuilder().newChemSequence(); IChemModel model = file.getBuilder().newChemModel(); IMoleculeSet containerSet = file.getBuilder().newMoleculeSet(); IMolecule container = file.getBuilder().newMolecule(); int lineNumber = 0; try { String line = input.readLine(); while (input.ready() && line != null) { logger.debug((lineNumber++) + ": ", line); String command = null; if (isCommand(line)) { command = getCommand(line); int lineCount = getContentLinesCount(line); if ("ATOMS".equals(command)) { processAtomsBlock(lineCount, container); } else if ("BONDS".equals(command)) { processBondsBlock(lineCount, container); } else if ("IDENT".equals(command)) { processIdentBlock(lineCount, container); } else if ("NAME".equals(command)) { processNameBlock(lineCount, container); } else { // skip lines logger.warn("Dropping block: ", command); for (int i = 0; i < lineCount; i++) input.readLine(); } } else { logger.warn("Unexpected content at line: ", lineNumber); } line = input.readLine(); } containerSet.addAtomContainer(container); model.setMoleculeSet(containerSet); seq.addChemModel(model); file.addChemSequence(seq); } catch (Exception exception) { String message = "Error while parsing CTX file: " + exception.getMessage(); logger.error(message); logger.debug(exception); throw new CDKException(message, exception); } return file; }
/** * Get the resonance structures from an IMolecule. * * @param molecule The IMolecule to analyze * @return The different resonance structures */ @TestMethod("testGetStructures_IMolecule") public IMoleculeSet getStructures(IMolecule molecule) { int countStructure = 0; IMoleculeSet setOfMol = molecule.getBuilder().newMoleculeSet(); setOfMol.addMolecule(molecule); for (int i = 0; i < setOfMol.getMoleculeCount(); i++) { IMolecule mol = setOfMol.getMolecule(i); for (IReactionProcess aReactionsList : reactionsList) { IReactionProcess reaction = aReactionsList; IMoleculeSet setOfReactants = molecule.getBuilder().newMoleculeSet(); setOfReactants.addMolecule(mol); try { IReactionSet setOfReactions = reaction.initiate(setOfReactants, null); if (setOfReactions.getReactionCount() != 0) for (int k = 0; k < setOfReactions.getReactionCount(); k++) for (int j = 0; j < setOfReactions.getReaction(k).getProducts().getAtomContainerCount(); j++) { IMolecule product = setOfReactions.getReaction(k).getProducts().getMolecule(j); if (!existAC(setOfMol, product)) { setOfMol.addMolecule(product); countStructure++; if (countStructure > maxStructures) return setOfMol; } } } catch (CDKException e) { e.printStackTrace(); } } } return setOfMol; }
/** * A unit test suite for JUnit. * * @cdk.inchi InChI=1/C2H4/c1-2/h1-2H2 * @return The test suite */ @Test public void testMapping() throws Exception { IReactionProcess type = new AdductionProtonPBReaction(); IMoleculeSet setOfReactants = DefaultChemObjectBuilder.getInstance().newMoleculeSet(); IMolecule molecule = getEthene(); setOfReactants.addMolecule(molecule); /*automatic looking for active center*/ List<IParameterReact> paramList = new ArrayList<IParameterReact>(); IParameterReact param = new SetReactionCenter(); param.setParameter(Boolean.FALSE); paramList.add(param); type.setParameterList(paramList); /* initiate */ IReactionSet setOfReactions = type.initiate(setOfReactants, null); IMolecule product = setOfReactions.getReaction(0).getProducts().getMolecule(0); Assert.assertEquals(3, setOfReactions.getReaction(0).getMappingCount()); IAtom mappedProductA1 = (IAtom) ReactionManipulator.getMappedChemObject( setOfReactions.getReaction(0), molecule.getAtom(0)); Assert.assertEquals(mappedProductA1, product.getAtom(0)); mappedProductA1 = (IAtom) ReactionManipulator.getMappedChemObject( setOfReactions.getReaction(0), molecule.getAtom(1)); Assert.assertEquals(mappedProductA1, product.getAtom(1)); IBond mappedProductB1 = (IBond) ReactionManipulator.getMappedChemObject( setOfReactions.getReaction(0), molecule.getBond(0)); Assert.assertEquals(mappedProductB1, product.getBond(0)); }
private IMolecule checkForXMoleculeFile(IChemFile chemFile, int numberOfMolecules) { Assert.assertNotNull(chemFile); Assert.assertEquals(chemFile.getChemSequenceCount(), 1); org.openscience.cdk.interfaces.IChemSequence seq = chemFile.getChemSequence(0); Assert.assertNotNull(seq); Assert.assertEquals(seq.getChemModelCount(), 1); org.openscience.cdk.interfaces.IChemModel model = seq.getChemModel(0); Assert.assertNotNull(model); org.openscience.cdk.interfaces.IMoleculeSet moleculeSet = model.getMoleculeSet(); Assert.assertNotNull(moleculeSet); Assert.assertEquals(moleculeSet.getMoleculeCount(), numberOfMolecules); IMolecule mol = null; for (int i = 0; i < numberOfMolecules; i++) { mol = moleculeSet.getMolecule(i); Assert.assertNotNull(mol); } return mol; }
/** * Initiate process. * * @param reactants reactants of the reaction. * @param agents agents of the reaction (Must be in this case null). * @exception CDKException Description of the Exception */ @TestMethod("testInitiate_IMoleculeSet_IMoleculeSet") public IReactionSet initiate(IMoleculeSet reactants, IMoleculeSet agents) throws CDKException { logger.debug("initiate reaction: RadicalChargeSiteInitiationReaction"); if (reactants.getMoleculeCount() != 1) { throw new CDKException("RadicalChargeSiteInitiationReaction only expects one reactant"); } if (agents != null) { throw new CDKException("RadicalChargeSiteInitiationReaction don't expects agents"); } IReactionSet setOfReactions = DefaultChemObjectBuilder.getInstance().newReactionSet(); IMolecule reactant = reactants.getMolecule(0); /* if the parameter hasActiveCenter is not fixed yet, set the active centers*/ IParameterReact ipr = super.getParameterClass(SetReactionCenter.class); if (ipr != null && !ipr.isSetParameter()) setActiveCenters(reactant); Iterator<IAtom> atoms = reactants.getMolecule(0).atoms().iterator(); while (atoms.hasNext()) { IAtom atomi = atoms.next(); if (atomi.getFlag(CDKConstants.REACTIVE_CENTER) && reactant.getConnectedSingleElectronsCount(atomi) == 1 && atomi.getFormalCharge() == 1) { Iterator<IBond> bondis = reactant.getConnectedBondsList(atomi).iterator(); while (bondis.hasNext()) { IBond bondi = bondis.next(); if (bondi.getFlag(CDKConstants.REACTIVE_CENTER) && bondi.getOrder() == IBond.Order.SINGLE) { IAtom atomj = bondi.getConnectedAtom(atomi); if (atomj.getFlag(CDKConstants.REACTIVE_CENTER) && atomj.getFormalCharge() == 0) { Iterator<IBond> bondjs = reactant.getConnectedBondsList(atomj).iterator(); while (bondjs.hasNext()) { IBond bondj = bondjs.next(); if (bondj.equals(bondi)) continue; if (bondj.getFlag(CDKConstants.REACTIVE_CENTER) && bondj.getOrder() == IBond.Order.SINGLE) { IAtom atomk = bondj.getConnectedAtom(atomj); if (atomk.getFlag(CDKConstants.REACTIVE_CENTER) && atomk.getSymbol().equals("C") && atomk.getFormalCharge() == 0) { ArrayList<IAtom> atomList = new ArrayList<IAtom>(); atomList.add(atomi); atomList.add(atomj); atomList.add(atomk); ArrayList<IBond> bondList = new ArrayList<IBond>(); bondList.add(bondi); bondList.add(bondj); IMoleculeSet moleculeSet = reactant.getBuilder().newMoleculeSet(); moleculeSet.addMolecule(reactant); IReaction reaction = mechanism.initiate(moleculeSet, atomList, bondList); if (reaction == null) continue; else setOfReactions.addReaction(reaction); } } } } } } } } return setOfReactions; }
/** * Get the container which is found resonance from a IMolecule. It is based on looking if the * order of the bond changes. * * @param molecule The IMolecule to analyze * @return The different containers */ @TestMethod("testGetContainers_IMolecule") public IAtomContainerSet getContainers(IMolecule molecule) { IAtomContainerSet setOfCont = molecule.getBuilder().newAtomContainerSet(); IMoleculeSet setOfMol = getStructures(molecule); if (setOfMol.getMoleculeCount() == 0) return setOfCont; /*extraction of all bonds which has been produced a changes of order*/ List<IBond> bondList = new ArrayList<IBond>(); for (int i = 1; i < setOfMol.getMoleculeCount(); i++) { IMolecule mol = setOfMol.getMolecule(i); for (int j = 0; j < mol.getBondCount(); j++) { IBond bond = molecule.getBond(j); if (!mol.getBond(j).getOrder().equals(bond.getOrder())) { if (!bondList.contains(bond)) bondList.add(bond); } } } if (bondList.size() == 0) return null; int[] flagBelonging = new int[bondList.size()]; for (int i = 0; i < flagBelonging.length; i++) flagBelonging[i] = 0; int[] position = new int[bondList.size()]; int maxGroup = 1; /*Analysis if the bond are linked together*/ List<IBond> newBondList = new ArrayList<IBond>(); newBondList.add(bondList.get(0)); int pos = 0; for (int i = 0; i < newBondList.size(); i++) { if (i == 0) flagBelonging[i] = maxGroup; else { if (flagBelonging[position[i]] == 0) { maxGroup++; flagBelonging[position[i]] = maxGroup; } } IBond bondA = newBondList.get(i); for (int ato = 0; ato < 2; ato++) { IAtom atomA1 = bondA.getAtom(ato); List<IBond> bondA1s = molecule.getConnectedBondsList(atomA1); for (int j = 0; j < bondA1s.size(); j++) { IBond bondB = bondA1s.get(j); if (!newBondList.contains(bondB)) for (int k = 0; k < bondList.size(); k++) if (bondList.get(k).equals(bondB)) if (flagBelonging[k] == 0) { flagBelonging[k] = maxGroup; pos++; newBondList.add(bondB); position[pos] = k; } } } // if it is final size and not all are added if (newBondList.size() - 1 == i) for (int k = 0; k < bondList.size(); k++) if (!newBondList.contains(bondList.get(k))) { newBondList.add(bondList.get(k)); position[i + 1] = k; break; } } /*creating containers according groups*/ for (int i = 0; i < maxGroup; i++) { IAtomContainer container = molecule.getBuilder().newAtomContainer(); for (int j = 0; j < bondList.size(); j++) { if (flagBelonging[j] != i + 1) continue; IBond bond = bondList.get(j); IAtom atomA1 = bond.getAtom(0); IAtom atomA2 = bond.getAtom(1); if (!container.contains(atomA1)) container.addAtom(atomA1); if (!container.contains(atomA2)) container.addAtom(atomA2); container.addBond(bond); } setOfCont.addAtomContainer(container); } return setOfCont; }