📄 valencychecker.java
字号:
IAtomType[] atomTypes = getAtomTypeFactory(atom.getBuilder()).getAtomTypes(atom.getSymbol()); if (atomTypes.length == 0) { logger.warn("Element not found in configuration file: ", atom); return 0; } logger.debug("Found atomtypes: ", atomTypes.length); for (int f = 0; f < atomTypes.length; f++) { IAtomType type = atomTypes[f]; if (couldMatchAtomType(atom, bondOrderSum, maxBondOrder, type)) { logger.debug("This type matches: ", type); missingHydrogen = (int) (type.getBondOrderSum() - bondOrderSum - singleElectronSum); break; } } logger.debug("missing hydrogens: ", missingHydrogen); return missingHydrogen; } /** * Saturates a molecule by setting appropriate bond orders. * * @cdk.keyword bond order, calculation * * @cdk.created 2003-10-03 */ public void saturate(IAtomContainer atomContainer) throws CDKException { logger.info("Saturating atomContainer by adjusting bond orders..."); boolean allSaturated = allSaturated(atomContainer); if (!allSaturated) { logger.info("Saturating bond orders is needed..."); IBond[] bonds = new IBond[atomContainer.getBondCount()]; for (int i=0; i<bonds.length; i++) bonds[i] = atomContainer.getBond(i); boolean succeeded = saturate(bonds, atomContainer); if (!succeeded) { throw new CDKException("Could not saturate this atomContainer!"); } } } /** * Saturates a set of Bonds in an AtomContainer. */ public boolean saturate(IBond[] bonds, IAtomContainer atomContainer) throws CDKException { logger.debug("Saturating bond set of size: ", bonds.length); boolean bondsAreFullySaturated = false; if (bonds.length > 0) { IBond bond = bonds[0]; // determine bonds left int leftBondCount = bonds.length-1; IBond[] leftBonds = new IBond[leftBondCount]; System.arraycopy(bonds, 1, leftBonds, 0, leftBondCount); // examine this bond logger.debug("Examining this bond: ", bond); if (isSaturated(bond, atomContainer)) { logger.debug("OK, bond is saturated, now try to saturate remaining bonds (if needed)"); bondsAreFullySaturated = saturate(leftBonds, atomContainer); } else if (isUnsaturated(bond, atomContainer)) { logger.debug("Ok, this bond is unsaturated, and can be saturated"); // two options now: // 1. saturate this one directly // 2. saturate this one by saturating the rest logger.debug("Option 1: Saturating this bond directly, then trying to saturate rest"); // considering organic bonds, the max order is 3, so increase twice double increment = 1.0; boolean bondOrderIncreased = saturateByIncreasingBondOrder(bond, atomContainer, increment); bondsAreFullySaturated = bondOrderIncreased && saturate(bonds, atomContainer); if (bondsAreFullySaturated) { logger.debug("Option 1: worked"); } else { logger.debug("Option 1: failed. Trying option 2."); logger.debug("Option 2: Saturing this bond by saturating the rest"); // revert the increase (if succeeded), then saturate the rest if (bondOrderIncreased) unsaturateByDecreasingBondOrder(bond, increment); bondsAreFullySaturated = saturate(leftBonds, atomContainer) && isSaturated(bond, atomContainer); if (!bondsAreFullySaturated) logger.debug("Option 2: failed"); } } else { logger.debug("Ok, this bond is unsaturated, but cannot be saturated"); // try recursing and see if that fixes things bondsAreFullySaturated = saturate(leftBonds, atomContainer) && isSaturated(bond, atomContainer); } } else { bondsAreFullySaturated = true; // empty is saturated by default } return bondsAreFullySaturated; } /** * Tries to saturate a bond by increasing its bond orders by 1.0. * * @return true if the bond could be increased */ public boolean saturateByIncreasingBondOrder(IBond bond, IAtomContainer atomContainer, double increment) throws CDKException { IAtom atom = bond.getAtom(0); IAtom partner = bond.getAtom(1); logger.debug(" saturating bond: ", atom.getSymbol(), "-", partner.getSymbol()); IAtomType[] atomTypes1 = getAtomTypeFactory(bond.getBuilder()).getAtomTypes(atom.getSymbol()); IAtomType[] atomTypes2 = getAtomTypeFactory(bond.getBuilder()).getAtomTypes(partner.getSymbol()); for (int atCounter1=0; atCounter1<atomTypes1.length; atCounter1++) { IAtomType aType1 = atomTypes1[atCounter1]; logger.debug(" condidering atom type: ", aType1); if (couldMatchAtomType(atomContainer, atom, aType1)) { logger.debug(" trying atom type: ", aType1); for (int atCounter2=0; atCounter2<atomTypes2.length; atCounter2++) { IAtomType aType2 = atomTypes2[atCounter2]; logger.debug(" condidering partner type: ", aType1); if (couldMatchAtomType(atomContainer, partner, atomTypes2[atCounter2])) { logger.debug(" with atom type: ", aType2); if (bond.getOrder() < aType2.getMaxBondOrder() && bond.getOrder() < aType1.getMaxBondOrder()) { bond.setOrder(bond.getOrder() + increment); logger.debug("Bond order now ", bond.getOrder()); return true; } } } } } return false; } /** * Saturate atom by adjusting its bond orders. */ public boolean saturate(IBond bond, IAtomContainer atomContainer) throws CDKException { IAtom atom = bond.getAtom(0); IAtom partner = bond.getAtom(1); logger.debug(" saturating bond: ", atom.getSymbol(), "-", partner.getSymbol()); boolean bondOrderIncreased = true; while (bondOrderIncreased && isUnsaturated(bond, atomContainer)) { logger.debug("Can increase bond order"); bondOrderIncreased = saturateByIncreasingBondOrder(bond, atomContainer, 1.0); } return isSaturated(bond, atomContainer); } /** * Determines of all atoms on the AtomContainer are saturated. */ public boolean isSaturated(IAtomContainer container) throws CDKException { return allSaturated(container); } public boolean allSaturated(IAtomContainer ac) throws CDKException { logger.debug("Are all atoms saturated?"); for (int f = 0; f < ac.getAtomCount(); f++) { if (!isSaturated(ac.getAtom(f), ac)) { return false; } } return true; } /** * Returns wether a bond is unsaturated. A bond is unsaturated if * <b>all</b> Atoms in the bond are unsaturated. */ public boolean isUnsaturated(IBond bond, IAtomContainer atomContainer) throws CDKException { logger.debug("isBondUnsaturated?: ", bond); IAtom[] atoms = BondManipulator.getAtomArray(bond); boolean isUnsaturated = true; for (int i=0; i<atoms.length && isUnsaturated; i++) { isUnsaturated = isUnsaturated && !isSaturated(atoms[i], atomContainer); } logger.debug("Bond is unsaturated?: ", isUnsaturated); return isUnsaturated; } /** * Returns wether a bond is saturated. A bond is saturated if * <b>both</b> Atoms in the bond are saturated. */ public boolean isSaturated(IBond bond, IAtomContainer atomContainer) throws CDKException { logger.debug("isBondSaturated?: ", bond); IAtom[] atoms = BondManipulator.getAtomArray(bond); boolean isSaturated = true; for (int i=0; i<atoms.length; i++) { logger.debug("isSaturated(Bond, AC): atom I=", i); isSaturated = isSaturated && isSaturated(atoms[i], atomContainer); } logger.debug("isSaturated(Bond, AC): result=", isSaturated); return isSaturated; } /** * Resets the bond orders of all bonds to 1.0. */ public void unsaturate(IAtomContainer atomContainer) { Iterator bonds = atomContainer.bonds(); while (bonds.hasNext()) ((IBond)bonds.next()).setOrder(CDKConstants.BONDORDER_SINGLE); } public boolean unsaturateByDecreasingBondOrder(IBond bond, double decrement) { if (bond.getOrder() > decrement) { bond.setOrder(bond.getOrder() - decrement); return true; } else { return false; } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -