/** * Selects an optimum edge for elimination in structures without N2 nodes. * * <p>This might be severely broken! Would have helped if there was an explanation of how this * algorithm worked. * * @param ring * @param molecule */ private IBond checkEdges(IRing ring, IAtomContainer molecule) { IRing r1, r2; IRingSet ringSet = ring.getBuilder().newInstance(IRingSet.class); IBond bond; int minMaxSize = Integer.MAX_VALUE; int minMax = 0; logger.debug("Molecule: " + molecule); Iterator<IBond> bonds = ring.bonds().iterator(); while (bonds.hasNext()) { bond = (IBond) bonds.next(); molecule.removeElectronContainer(bond); r1 = getRing(bond.getAtom(0), molecule); r2 = getRing(bond.getAtom(1), molecule); logger.debug("checkEdges: " + bond); if (r1.getAtomCount() > r2.getAtomCount()) { ringSet.addAtomContainer(r1); } else { ringSet.addAtomContainer(r2); } molecule.addBond(bond); } for (int i = 0; i < ringSet.getAtomContainerCount(); i++) { if (((IRing) ringSet.getAtomContainer(i)).getBondCount() < minMaxSize) { minMaxSize = ((IRing) ringSet.getAtomContainer(i)).getBondCount(); minMax = i; } } return (IBond) ring.getElectronContainer(minMax); }
/** * removes all bonds connected to the given atom leaving it with degree zero. * * @param atom The atom to be disconnecred * @param molecule The molecule containing the atom */ private void trim(IAtom atom, IAtomContainer molecule) { List<IBond> bonds = molecule.getConnectedBondsList(atom); for (int i = 0; i < bonds.size(); i++) { molecule.removeElectronContainer((IBond) bonds.get(i)); } // you are erased! Har, har, har..... >8-) }
/** * Eliminates one bond of this atom from the molecule * * @param atom The atom one bond is eliminated of * @param molecule The molecule that contains the atom */ private void breakBond(IAtom atom, IAtomContainer molecule) { Iterator<IBond> bonds = molecule.bonds().iterator(); while (bonds.hasNext()) { IBond bond = (IBond) bonds.next(); if (bond.contains(atom)) { molecule.removeElectronContainer(bond); break; } } }
/** * Finds the Smallest Set of Smallest Rings. * * @param mol the molecule to be searched for rings * @return a RingSet containing the rings in molecule */ public IRingSet findSSSR(IAtomContainer mol) { IBond brokenBond = null; IChemObjectBuilder builder = mol.getBuilder(); IRingSet sssr = builder.newInstance(IRingSet.class); IAtomContainer molecule = builder.newInstance(IAtomContainer.class); molecule.add(mol); IAtom smallest; int smallestDegree, nodesToBreakCounter, degree; IAtom[] rememberNodes; IRing ring; // Two Vectors - as defined in the article. One to hold the // full set of atoms in the structure and on to store the numbers // of the nodes that have been trimmed away. // Furhter there is a Vector nodesN2 to store the number of N2 nodes List<IAtom> fullSet = new ArrayList<IAtom>(); List<IAtom> trimSet = new ArrayList<IAtom>(); List<IAtom> nodesN2 = new ArrayList<IAtom>(); initPath(molecule); logger.debug("molecule.getAtomCount(): " + molecule.getAtomCount()); // load fullSet with the numbers of our atoms for (int f = 0; f < molecule.getAtomCount(); f++) { fullSet.add(molecule.getAtom(f)); } logger.debug("fullSet.size(): " + fullSet.size()); do { // Add nodes of degree zero to trimset. // Also add nodes of degree 2 to nodesN2. // In the same run, check, which node has the lowest degree // greater than zero. smallestDegree = 7; smallest = null; nodesN2.clear(); for (int f = 0; f < molecule.getAtomCount(); f++) { IAtom atom = molecule.getAtom(f); degree = molecule.getConnectedBondsCount(atom); if (degree == 0) { if (!trimSet.contains(atom)) { logger.debug("Atom of degree 0"); trimSet.add(atom); } } if (degree == 2) { nodesN2.add(atom); } if (degree < smallestDegree && degree > 0) { smallest = atom; smallestDegree = degree; } } if (smallest == null) break; // If there are nodes of degree 1, trim them away if (smallestDegree == 1) { trimCounter++; trim(smallest, molecule); trimSet.add(smallest); } // if there are nodes of degree 2, find out of which rings // they are part of. else if (smallestDegree == 2) { rememberNodes = new IAtom[nodesN2.size()]; nodesToBreakCounter = 0; for (int f = 0; f < nodesN2.size(); f++) { ring = getRing((IAtom) nodesN2.get(f), molecule); if (ring != null) { // check, if this ring already is in SSSR if (!RingSetManipulator.ringAlreadyInSet(ring, sssr)) { sssr.addAtomContainer(ring); rememberNodes[nodesToBreakCounter] = (IAtom) nodesN2.get(f); nodesToBreakCounter++; } } } if (nodesToBreakCounter == 0) { nodesToBreakCounter = 1; rememberNodes[0] = (IAtom) nodesN2.get(0); } for (int f = 0; f < nodesToBreakCounter; f++) { breakBond(rememberNodes[f], molecule); } if (brokenBond != null) { molecule.addBond(brokenBond); brokenBond = null; } } // if there are nodes of degree 3 else if (smallestDegree == 3) { ring = getRing(smallest, molecule); if (ring != null) { // check, if this ring already is in SSSR if (!RingSetManipulator.ringAlreadyInSet(ring, sssr)) { sssr.addAtomContainer(ring); } brokenBond = checkEdges(ring, molecule); molecule.removeElectronContainer(brokenBond); } } } while (trimSet.size() < fullSet.size()); logger.debug("fullSet.size(): " + fullSet.size()); logger.debug("trimSet.size(): " + trimSet.size()); logger.debug("trimCounter: " + trimCounter); // molecule.setProperty(CDKConstants.SMALLEST_RINGS, sssr); return sssr; }