📄 forcefieldconfigurator.java
字号:
/* $Revision: 8313 $ $Author: egonw $ $Date: 2007-05-08 13:48:40 +0200 (Tue, 08 May 2007) $ * * Copyright (C) 2004-2007 Christian Hoppe <chhoppe@users.sf.net> * * 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.modeling.builder3d;import org.openscience.cdk.CDKConstants;import org.openscience.cdk.aromaticity.HueckelAromaticityDetector;import org.openscience.cdk.exception.CDKException;import org.openscience.cdk.exception.NoSuchAtomTypeException;import org.openscience.cdk.interfaces.*;import org.openscience.cdk.ringsearch.SSSRFinder;import org.openscience.cdk.tools.HOSECodeGenerator;import org.openscience.cdk.tools.manipulator.RingSetManipulator;import java.io.InputStream;import java.util.Hashtable;import java.util.Iterator;import java.util.Vector;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * Reads in a force field configuration file, set the atom types into a vector, and the data into a hashtable * Therfore it uses the class MM2BasedParamterSetReader * private Hashtable parameterSet; * key=nameofdatafield+atomid1+;atomid2;atomxid * * <p>MM2 and MMFF94 force field are implemented * With force field data it configures the cdk atom (assign atomtype, van der Waals radius, charge...) * * @author chhoppe * @cdk.created 2004-09-07 * @cdk.module forcefield */public class ForceFieldConfigurator { private String ffName = "mmff94"; private Vector atomTypes; private Hashtable parameterSet=null; private MM2BasedParameterSetReader mm2 = null; private MMFF94BasedParameterSetReader mmff94= null; private InputStream ins = null; private String[] fftypes = {"mm2","mmff94"}; /** *Constructor for the ForceFieldConfigurator object */ public ForceFieldConfigurator() { } /** * Sets the inputStream attribute of the ForceFieldConfigurator object * * @param ins The new inputStream value */ public void setInputStream(InputStream ins) { this.ins = ins; } /** * gives a list of possible force field types * *@return the list */ public String[] getFfTypes(){ return fftypes; } /** * Sets the forceFieldType attribute of the ForceFieldConfigurator object * * @param ffname The new forceFieldType name */ public boolean checkForceFieldType(String ffname) { boolean check=false; for (int i = 0; i <= fftypes.length; i++) { if (fftypes[i].equals(ffname)) { check=true; break; } } if (!check) {// logger.debug("FFError:checkForceFieldType> Unknown forcefield:" + ffname + "Take default:"+ffName); return false; } return true; } /** *Constructor for the ForceFieldConfigurator object * * @param ffname name of the force field data file */ public void setForceFieldConfigurator(String ffname) throws CDKException { ffname=ffname.toLowerCase(); boolean check=false; if (ffname==ffName && parameterSet!=null){ }else{ check=this.checkForceFieldType(ffname); ffName=ffname; if (ffName.equals("mm2")) { //logger.debug("ForceFieldConfigurator: open Force Field mm2"); //f = new File(mm2File); //readFile(f); ins = this.getClass().getClassLoader().getResourceAsStream("org/openscience/cdk/modeling/forcefield/data/mm2.prm"); //logger.debug("ForceFieldConfigurator: open Force Field mm2 ... READY"); mm2 = new MM2BasedParameterSetReader(); mm2.setInputStream(ins); //logger.debug("ForceFieldConfigurator: mm2 set input stream ... READY"); try{ this.setMM2Parameters(); }catch (Exception ex1){ throw new CDKException("Problems with set MM2Parameters due to "+ex1.toString(), ex1); } }else if (ffName.equals("mmff94") || !check) { //logger.debug("ForceFieldConfigurator: open Force Field mmff94"); //f = new File(mmff94File); //readFile(f); ins = this.getClass().getClassLoader().getResourceAsStream("org/openscience/cdk/modeling/forcefield/data/mmff94.prm"); mmff94= new MMFF94BasedParameterSetReader(); mmff94.setInputStream(ins); try{ this.setMMFF94Parameters(); }catch (Exception ex2){ throw new CDKException("Problems with set MM2Parameters due to"+ex2.toString(), ex2); } } } //throw new CDKException("Data file for "+ffName+" force field could not be found"); } /** * Sets the atomTypes attribute of the ForceFieldConfigurator object * * @param atomtypes The new atomTypes */ public void setAtomTypes(Vector atomtypes) { atomTypes = atomtypes; } /** * Sets the parameters attribute of the ForceFieldConfigurator object * * @param parameterset The new parameter values */ public void setParameters(Hashtable parameterset) { parameterSet = parameterset; } /** * Sets the parameters attribute of the ForceFieldConfigurator object, default is mm2 force field */ public void setMM2Parameters() throws CDKException{ try{ mm2.readParameterSets(); }catch(Exception ex1){ throw new CDKException("Problem within readParameterSets due to:"+ex1.toString(), ex1); } parameterSet = mm2.getParamterSet(); atomTypes = mm2.getAtomTypes(); } public void setMMFF94Parameters() throws Exception{ mmff94.readParameterSets(); parameterSet = mmff94.getParamterSet(); atomTypes = mmff94.getAtomTypes(); } /** * Gets the atomTypes attribute of the ForceFieldConfigurator object * * @return The atomTypes vector */ public Vector getAtomTypes() { return atomTypes; } /** * Gets the parameterSet attribute of the ForceFieldConfigurator object * * @return The parameterSet hashtable */ public Hashtable getParameterSet() { return this.parameterSet; } /** * Find the atomType for a id * * @param ID Atomtype id of the forcefield * @return The atomType * @exception NoSuchAtomTypeException atomType is not known */ private IAtomType getAtomType(String ID) throws NoSuchAtomTypeException { IAtomType at = null; for (int i = 0; i < atomTypes.size(); i++) { at = (IAtomType) atomTypes.get(i); if (at.getAtomTypeName().equals(ID)) { return at; } } throw new NoSuchAtomTypeException("AtomType " + ID + " could not be found"); } /** * Method assigns atom types to atoms (calculates sssr and aromaticity) * *@return sssrf set *@exception Exception Description of the Exception */ public IRingSet assignAtomTyps(IMolecule molecule) throws Exception { org.openscience.cdk.interfaces.IAtom atom = null; String hoseCode = ""; HOSECodeGenerator hcg = new HOSECodeGenerator(); int NumberOfRingAtoms = 0; IRingSet ringSetA = null; IRingSet ringSetMolecule = new SSSRFinder(molecule).findSSSR(); boolean isInHeteroRing = false; try { HueckelAromaticityDetector.detectAromaticity(molecule); } catch (Exception cdk1) { System.out.println("AROMATICITYError: Cannot determine aromaticity due to: " + cdk1.toString()); } for (int i = 0; i < molecule.getAtomCount(); i++) { atom = molecule.getAtom(i); if (ringSetMolecule.contains(atom)) { NumberOfRingAtoms = NumberOfRingAtoms + 1; atom.setFlag(CDKConstants.ISINRING, true); atom.setFlag(CDKConstants.ISALIPHATIC, false); ringSetA = ringSetMolecule.getRings(atom); RingSetManipulator.sort(ringSetA); IRing sring = (IRing) ringSetA.getAtomContainer(ringSetA.getAtomContainerCount()-1); atom.setProperty("RING_SIZE", new Integer(sring.getRingSize())); isInHeteroRing = false; Iterator containers = RingSetManipulator.getAllAtomContainers(ringSetA).iterator(); while (!isInHeteroRing && containers.hasNext()) { isInHeteroRing = isHeteroRingSystem((IAtomContainer) containers.next()); } } else { atom.setFlag(CDKConstants.ISALIPHATIC, true); atom.setFlag(CDKConstants.ISINRING, false); isInHeteroRing = false; } atom.setProperty("MAX_BOND_ORDER", new Double(molecule.getMaximumBondOrder(atom))); try { hoseCode = hcg.getHOSECode(molecule, atom, 3); //logger.debug("HOSECODE GENERATION: ATOM "+i+" HoseCode: "+hoseCode+" "); } catch (CDKException ex1) { System.out.println("Could not build HOSECode from atom " + i + " due to " + ex1.toString()); throw new CDKException("Could not build HOSECode from atom "+ i + " due to " + ex1.toString(), ex1); } try { configureAtom(atom, hoseCode, isInHeteroRing); } catch (CDKException ex2) { System.out.println("Could not final configure atom " + i + " due to " + ex2.toString()); throw new Exception("Could not final configure atom due to problems with force field", ex2); } } // IBond[] bond = molecule.getBonds(); String bondType; Iterator bonds = molecule.bonds(); while (bonds.hasNext()) { IBond bond = (IBond) bonds.next(); //logger.debug("bond[" + i + "] properties : " + molecule.getBond(i).getProperties()); bondType = "0"; if (bond.getOrder() == 1) { if ((bond.getAtom(0).getAtomTypeName().equals("Csp2")) & ((bond.getAtom(1).getAtomTypeName().equals("Csp2")) | (bond.getAtom(1).getAtomTypeName().equals("C=")))) { bondType = "1"; } if ((bond.getAtom(0).getAtomTypeName().equals("C=")) & ((bond.getAtom(1).getAtomTypeName().equals("Csp2")) | (bond.getAtom(1).getAtomTypeName().equals("C=")))) { bondType = "1";} if ((bond.getAtom(0).getAtomTypeName().equals("Csp")) & (bond.getAtom(1).getAtomTypeName().equals("Csp"))) { bondType = "1";} }// molecule.getBond(i).setProperty("MMFF94 bond type", bondType); bond.setProperty("MMFF94 bond type", bondType); //logger.debug("bond[" + i + "] properties : " + molecule.getBond(i).getProperties()); } return ringSetMolecule; } /** * Returns true if atom is in hetero ring system * *@param ac AtomContainer *@return true/false */ private boolean isHeteroRingSystem(IAtomContainer ac) { if (ac != null) { for (int i = 0; i < ac.getAtomCount(); i++) { if (!(ac.getAtom(i).getSymbol()).equals("H") && !(ac.getAtom(i).getSymbol()).equals("C")) { return true; } } } return false; } /** * Assigns an atom type to an atom * * @param atom The atom to be aasigned * @param ID the atom type id * @return the assigned atom */ private org.openscience.cdk.interfaces.IAtom setAtom(org.openscience.cdk.interfaces.IAtom atom, String ID) throws Exception { IAtomType at = null; String key = ""; Vector data = null; Double value = null; at = getAtomType(ID); if (atom.getSymbol()==null){ atom.setSymbol(at.getSymbol()); } atom.setAtomTypeName(at.getAtomTypeName()); atom.setFormalNeighbourCount(at.getFormalNeighbourCount()); key = "vdw" + ID; data = (Vector) parameterSet.get(key); value = (Double) data.firstElement(); atom.setVanderwaalsRadius(value.doubleValue()); key = "charge" + ID; if (parameterSet.containsKey(key)) { data = (Vector) parameterSet.get(key); value = (Double) data.firstElement(); atom.setCharge(value.doubleValue()); } Object color = at.getProperty("org.openscience.cdk.renderer.color"); if (color != null) { atom.setProperty("org.openscience.cdk.renderer.color", color); } if (at.getAtomicNumber() != 0) { atom.setAtomicNumber(at.getAtomicNumber()); } if (at.getExactMass() > 0.0) { atom.setExactMass(at.getExactMass()); } return atom; } public org.openscience.cdk.interfaces.IAtom configureAtom(org.openscience.cdk.interfaces.IAtom atom, String hoseCode, boolean _boolean) throws Exception {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -