📄 smilesvalencychecker.java
字号:
/* $RCSfile$ * $Author: egonw $ * $Date: 2007-01-04 18:46:10 +0100 (Thu, 04 Jan 2007) $ * $Revision: 7636 $ * * Copyright (C) 2004-2007 The Chemistry Development Kit (CDK) project * * Contact: cdk-devel@lists.sourceforge.net * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 * of the License, or (at your option) any later version. * All we ask is that proper credit is given for our work, which includes * - but is not limited to - adding the above copyright notice to the beginning * of your source code files, and to any copyright notice that you may distribute * with programs based on this work. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * */package org.openscience.cdk.tools;import org.openscience.cdk.CDKConstants;import org.openscience.cdk.config.AtomTypeFactory;import org.openscience.cdk.exception.CDKException;import org.openscience.cdk.interfaces.*;import org.openscience.cdk.tools.manipulator.BondManipulator;/** * Small customization of ValencyHybridChecker suggested by Todd Martin * specially tuned for SMILES parsing. * * @author Egon Willighagen * @cdk.created 2004-06-12 * @cdk.keyword atom, valency * @cdk.module valencycheck */public class SmilesValencyChecker implements IValencyChecker, IDeduceBondOrderTool { private String atomTypeList = null; protected AtomTypeFactory structgenATF; protected LoggingTool logger; public SmilesValencyChecker() { this("org/openscience/cdk/config/data/hybridization_atomtypes.xml"); } public SmilesValencyChecker(String atomTypeList) { this.atomTypeList = atomTypeList; logger = new LoggingTool(this); logger.info("Using configuration file: ", atomTypeList); } /** * 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; } public boolean unsaturateByDecreasingBondOrder(IBond bond, double decrement) { if (bond.getOrder() > decrement) { bond.setOrder(bond.getOrder() - decrement); return true; } else { return false; } } /** * 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; } /** * 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[] atoms = BondManipulator.getAtomArray(bond); IAtom atom = atoms[0]; IAtom partner = atoms[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; } /** * Returns wether a bond is saturated. A bond is saturated if
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -