diff --git a/src/main/org/openscience/cdk/smsd/algorithm/vflib/VFlibMCSHandler.java b/src/main/org/openscience/cdk/smsd/algorithm/vflib/VFlibMCSHandler.java index 0dc32222c22..4f33e510ec7 100644 --- a/src/main/org/openscience/cdk/smsd/algorithm/vflib/VFlibMCSHandler.java +++ b/src/main/org/openscience/cdk/smsd/algorithm/vflib/VFlibMCSHandler.java @@ -274,7 +274,6 @@ private void searchVFMCSMappings() { vfLibSolutions = new ArrayList>(mapper.getMaps(ac1)); RONP = false; } - setVFMCSMappings(RONP, query); this.vfMCSSize = allMCSCopy.isEmpty() ? 0 : allMCSCopy.get(0).size(); } diff --git a/src/main/org/openscience/cdk/smsd/algorithm/vflib/VFlibTurboMCSHandler.java b/src/main/org/openscience/cdk/smsd/algorithm/vflib/VFlibTurboMCSHandler.java deleted file mode 100644 index 94ecc731b43..00000000000 --- a/src/main/org/openscience/cdk/smsd/algorithm/vflib/VFlibTurboMCSHandler.java +++ /dev/null @@ -1,368 +0,0 @@ -/* Copyright (C) 2009-2010 Syed Asad Rahman {asad@ebi.ac.uk} - * - * 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 commonAtomList 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.smsd.algorithm.vflib; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; -import java.util.logging.Level; -import org.openscience.cdk.annotations.TestClass; -import org.openscience.cdk.annotations.TestMethod; -import org.openscience.cdk.exception.CDKException; -import org.openscience.cdk.interfaces.IMolecule; -import org.openscience.cdk.smsd.algorithm.mcgregor.McGregor; -import org.openscience.cdk.smsd.algorithm.vflib.interfaces.IMapper; -import org.openscience.cdk.smsd.algorithm.vflib.interfaces.INode; -import org.openscience.cdk.smsd.algorithm.vflib.interfaces.IQuery; -import org.openscience.cdk.smsd.algorithm.vflib.map.VFMCSMapper; -import org.openscience.cdk.smsd.algorithm.vflib.query.TemplateCompiler; -import org.openscience.cdk.smsd.helper.MolHandler; -import org.openscience.cdk.smsd.interfaces.AbstractMCSAlgorithm; -import org.openscience.cdk.interfaces.IAtom; -import org.openscience.cdk.interfaces.IAtomContainer; -import org.openscience.cdk.smsd.interfaces.IMCSBase; -import org.openscience.cdk.tools.ILoggingTool; -import org.openscience.cdk.tools.LoggingToolFactory; - -/** - * This class should be used to find MCS between query - * graph and target graph. - * - * First the algorithm runs VF lib {@link org.openscience.cdk.smsd.algorithm.vflib.map.VFMCSMapper} - * and reports MCS between - * run query and target graphs. Then these solutions are extented - * using McGregor {@link org.openscience.cdk.smsd.algorithm.mcgregor.McGregor} - * algorithm whereever required. - * - * @cdk.module smsd - * @cdk.githash - * @author Syed Asad Rahman - */ -@TestClass("org.openscience.cdk.smsd.algorithm.vflib.VFlibMCSHandlerTest") -public class VFlibTurboMCSHandler extends AbstractMCSAlgorithm implements IMCSBase { - - private static List> allAtomMCS = null; - private static Map atomsMCS = null; - private static List> allAtomMCSCopy = null; - private static Map firstMCS = null; - private static List> allMCS = null; - private static List> allMCSCopy = null; - private IAtomContainer ac1 = null; - private IAtomContainer ac2 = null; - private Map vfLibSolutions = null; - private int vfMCSSize = 0; - private final static ILoggingTool logger = - LoggingToolFactory.createLoggingTool(VFlibTurboMCSHandler.class); - - /** - * Constructor for an extended VF Algorithm for the MCS search - */ - @TestMethod("setMCSAlgorithm") - public VFlibTurboMCSHandler() { - allAtomMCS = new ArrayList>(); - allAtomMCSCopy = new ArrayList>(); - atomsMCS = new HashMap(); - firstMCS = new TreeMap(); - allMCS = new ArrayList>(); - allMCSCopy = new ArrayList>(); - } - - /** - *{@inheritDoc} - * - */ - @Override - @TestMethod("testSearchMCS") - public void searchMCS() { - searchVFMCSMappings(); - boolean flag = mcgregorFlag(); - if (flag) { - try { - searchMcGregorMapping(); - } catch (CDKException ex) { - logger.error(Level.SEVERE, null, ex); - } catch (IOException ex) { - logger.error(Level.SEVERE, null, ex); - } - - } else if (!allAtomMCSCopy.isEmpty()) { - allAtomMCS.addAll(allAtomMCSCopy); - allMCS.addAll(allMCSCopy); - } - setFirstMappings(); - } - - private void setFirstMappings() { - if (!allAtomMCS.isEmpty()) { - atomsMCS.putAll(allAtomMCS.get(0)); - firstMCS.putAll(allMCS.get(0)); - } - - } - - private boolean mcgregorFlag() { - int commonAtomCount = checkCommonAtomCount(ac1, ac2); - if (commonAtomCount > vfMCSSize && commonAtomCount > vfMCSSize) { - return true; - } - return false; - } - - /** {@inheritDoc} - * - * Set the VFLibMCS software - * - * @param reactant - * @param product - */ - @Override - @TestMethod("testSet_IAtomContainer_IAtomContainer") - public void set(IAtomContainer reactant, IAtomContainer product) { - - IAtomContainer mol1 = reactant; - IAtomContainer mol2 = product; - - MolHandler Reactant = new MolHandler(mol1, false); - MolHandler Product = new MolHandler(mol2, false); - this.set(Reactant, Product); - - } - - /** {@inheritDoc} - * - * Set the VFLib MCS software - * - * @param Reactant - * @param Product - */ - @Override - @TestMethod("testSet_MolHandler_MolHandler") - public void set(MolHandler Reactant, MolHandler Product) { - ac1 = Reactant.getMolecule(); - ac2 = Product.getMolecule(); - } - - /** {@inheritDoc} - * - * Creates atoms new instance of SearchCliques - * @param ReactantMolFileName - * @param ProductMolFileName - */ - @Override - @TestMethod("testSet_String_String") - public void set(String ReactantMolFileName, String ProductMolFileName) { - - String mol1 = ReactantMolFileName; - String mol2 = ProductMolFileName; - - MolHandler Reactant = new MolHandler(mol1, false); - MolHandler Product = new MolHandler(mol2, false); - this.set(Reactant, Product); - } - - /** {@inheritDoc} - * - * Set the VFLib MCS software - * - * @param source - * @param target - */ - @TestMethod("testSet_IMolecule_IMolecule") - public void set(IMolecule source, IMolecule target) throws CDKException { - MolHandler Reactant = new MolHandler(source, false); - MolHandler Product = new MolHandler(target, false); - this.set(Reactant, Product); - } - - private boolean hasMap(Map map, List> mapGlobal) { - - for (Map test : mapGlobal) { - if (test.equals(map)) { - return true; - } - } - return false; - } - - /** {@inheritDoc} - * - */ - @Override - @TestMethod("testGetAllAtomMapping") - public List> getAllAtomMapping() { - return Collections.unmodifiableList(allAtomMCS); - } - - /** {@inheritDoc} - */ - @Override - @TestMethod("testGetAllMapping") - public List> getAllMapping() { - return Collections.unmodifiableList(allMCS); - } - - /** {@inheritDoc} - */ - @Override - @TestMethod("testGetFirstAtomMapping") - public Map getFirstAtomMapping() { - return Collections.unmodifiableMap(atomsMCS); - } - - /** {@inheritDoc} - */ - @Override - @TestMethod("testGetFirstMapping") - public Map getFirstMapping() { - return Collections.unmodifiableMap(firstMCS); - } - - private int checkCommonAtomCount(IAtomContainer reactantMolecule, IAtomContainer productMolecule) { - ArrayList atoms = new ArrayList(); - for (int i = 0; i < reactantMolecule.getAtomCount(); i++) { - atoms.add(reactantMolecule.getAtom(i).getSymbol()); - - } - int common = 0; - for (int i = 0; i < productMolecule.getAtomCount(); i++) { - String symbol = productMolecule.getAtom(i).getSymbol(); - if (atoms.contains(symbol)) { - atoms.remove(symbol); - common++; - } - } - return common; - } - - private void searchVFMCSMappings() { - IQuery query = null; - IMapper mapper = null; - boolean RONP = false; - if (ac1.getAtomCount() <= ac2.getAtomCount()) { - query = TemplateCompiler.compile(ac1); - mapper = new VFMCSMapper(query); - vfLibSolutions = new HashMap(mapper.getFirstMap(ac2)); - RONP = true; - - } else { - query = TemplateCompiler.compile(ac2); - mapper = new VFMCSMapper(query); - vfLibSolutions = new HashMap(mapper.getFirstMap(ac1)); - RONP = false; - } - - setVFMCSMappings(RONP, query); - this.vfMCSSize = allMCSCopy.isEmpty() ? 0 : allMCSCopy.get(0).size(); - } - - private void searchMcGregorMapping() throws CDKException, IOException { - List> mappings = new ArrayList>(); - for (Map firstPassMappings : allMCSCopy) { - McGregor mgit = new McGregor(ac1, ac2, mappings); - mgit.startMcGregorIteration(mgit.getMCSSize(), firstPassMappings); //Start McGregor search - mappings = mgit.getMappings(); - mgit = null; - } - setMcGregorMappings(mappings); - } - - private void setVFMCSMappings(boolean RONP, IQuery query) { - int counter = 0; - - - Map atomatomMapping = new HashMap(); - TreeMap indexindexMapping = new TreeMap(); - - for (Map.Entry mapping : vfLibSolutions.entrySet()) { - IAtom qAtom = null; - IAtom tAtom = null; - if (RONP) { - qAtom = query.getAtom(mapping.getKey()); - tAtom = mapping.getValue(); - - } else { - tAtom = query.getAtom(mapping.getKey()); - qAtom = mapping.getValue(); - } - - Integer qIndex = Integer.valueOf(ac1.getAtomNumber(qAtom)); - Integer tIndex = Integer.valueOf(ac2.getAtomNumber(tAtom)); - if (qIndex != null && tIndex != null) { - atomatomMapping.put(qAtom, tAtom); - indexindexMapping.put(qIndex, tIndex); - } else { - try { - throw new CDKException("Atom index pointing to NULL"); - } catch (CDKException ex) { - logger.error(Level.SEVERE, null, ex); - } - } - - } - if (!atomatomMapping.isEmpty()) { - allAtomMCSCopy.add(counter, atomatomMapping); - allMCSCopy.add(counter, indexindexMapping); - counter++; - } - - } - - private void setMcGregorMappings(List> mappings) throws CDKException { - int counter = 0; - for (List mapping : mappings) { - - Map atomatomMapping = new HashMap(); - Map indexindexMapping = new TreeMap(); - - for (int index = 0; index < mapping.size(); index += 2) { - IAtom qAtom = null; - IAtom tAtom = null; - - qAtom = ac1.getAtom(mapping.get(index)); - tAtom = ac2.getAtom(mapping.get(index + 1)); - - - Integer qIndex = mapping.get(index); - Integer tIndex = mapping.get(index + 1); - - - if (qIndex != null && tIndex != null) { - atomatomMapping.put(qAtom, tAtom); - indexindexMapping.put(qIndex, tIndex); - } else { - throw new CDKException("Atom index pointing to NULL"); - } - } - - if (!atomatomMapping.isEmpty() && !hasMap(indexindexMapping, allMCS)) { - allAtomMCS.add(counter, atomatomMapping); - allMCS.add(counter, indexindexMapping); - counter++; - } - } - } -} diff --git a/src/main/org/openscience/cdk/smsd/algorithm/vflib/map/VFMCSMapper.java b/src/main/org/openscience/cdk/smsd/algorithm/vflib/map/VFMCSMapper.java index 1c783e51520..4c64971c24c 100644 --- a/src/main/org/openscience/cdk/smsd/algorithm/vflib/map/VFMCSMapper.java +++ b/src/main/org/openscience/cdk/smsd/algorithm/vflib/map/VFMCSMapper.java @@ -115,8 +115,8 @@ public int countMaps(IAtomContainer target) { private void addMapping(IState state, boolean isGoal) { Map map = state.getMap(); - if ((isGoal && !hasMap(map) && isMCS(map)) - || (!isGoal && !map.isEmpty() && !hasMap(map) && isMCS(map))) { + if ((isGoal && isMCS(map)) + || (!isGoal && !map.isEmpty() && isMCS(map))) { maps.add(map); } } @@ -140,7 +140,6 @@ private void mapAll(IState state) { IState nextState = state.nextState(candidate); mapAll(nextState); nextState.backTrack(); - } } } @@ -168,7 +167,6 @@ private boolean mapFirst(IState state) { } } - return found; } @@ -179,7 +177,6 @@ private boolean isMCS(Map map) { int mapSize = map.size(); if (!maps.isEmpty() && currentMCSSize > mapSize) { - flag = false; } //Comment this if to get all the subgraphs @@ -187,7 +184,6 @@ private boolean isMCS(Map map) { currentMCSSize = mapSize; maps.clear(); } - return flag; } @@ -197,7 +193,6 @@ private boolean hasMap(Map map) { return true; } } - return false; } } diff --git a/src/main/org/openscience/cdk/smsd/algorithm/vflib/map/VFMapper.java b/src/main/org/openscience/cdk/smsd/algorithm/vflib/map/VFMapper.java index ea4c02b3b0e..7d6a62cbd79 100644 --- a/src/main/org/openscience/cdk/smsd/algorithm/vflib/map/VFMapper.java +++ b/src/main/org/openscience/cdk/smsd/algorithm/vflib/map/VFMapper.java @@ -203,11 +203,9 @@ private boolean mapFirst(IState state) { if (state.isMatchFeasible(candidate)) { IState nextState = state.nextState(candidate); found = mapFirst(nextState); - nextState.backTrack(); } } - return found; } diff --git a/src/main/org/openscience/cdk/smsd/factory/SubStructureSearchAlgorithms.java b/src/main/org/openscience/cdk/smsd/factory/SubStructureSearchAlgorithms.java index c85a43df6ee..e026b1f5fcf 100644 --- a/src/main/org/openscience/cdk/smsd/factory/SubStructureSearchAlgorithms.java +++ b/src/main/org/openscience/cdk/smsd/factory/SubStructureSearchAlgorithms.java @@ -86,6 +86,8 @@ public class SubStructureSearchAlgorithms extends AbstractMCS { private boolean removeHydrogen = false; private final static ILoggingTool logger = LoggingToolFactory.createLoggingTool(SubStructureSearchAlgorithms.class); + private double bondSensitiveTimeOut = 0.10;//mins + private double bondInSensitiveTimeOut = 0.15;//mins /** * This is the algorithm factory and entry port for all the MCS algorithm in the SMSD @@ -144,11 +146,14 @@ private void chooseAlgorithm(int rBondCount, int pBondCount) { subStructureAlgorithm(rBondCount, pBondCount); break; case VFLibMCS: - vfLibMCSAlgorithm(rBondCount, pBondCount); + vfLibMCSAlgorithm(); break; case TURBOMCS: - vfLibTurboMCSAlgorithm(rBondCount, pBondCount); - break; + try { + throw new CDKException("This mode is no longer supported"); + } catch (CDKException ex) { + logger.error(Level.SEVERE, null, ex); + } } } @@ -228,20 +233,6 @@ private void vfLibMCS() { allAtomMCS.addAll(mcs.getAllAtomMapping()); } - private void vfLibTurboMCS() { - VFlibMCSHandler mcs = null; - mcs = new VFlibMCSHandler(); - mcs.set(rMol, pMol); - mcs.searchMCS(); - - clearMaps(); - firstSolution.putAll(mcs.getFirstMapping()); - allMCS.addAll(mcs.getAllMapping()); - - firstAtomMCS.putAll(mcs.getFirstAtomMapping()); - allAtomMCS.addAll(mcs.getAllAtomMapping()); - } - private void singleMapping() { SingleMappingHandler mcs = null; @@ -316,29 +307,17 @@ private void subStructureAlgorithm(int rBondCount, int pBondCount) { } } - private void vfLibMCSAlgorithm(int rBondCount, int pBondCount) { - if (rBondCount >= 6 && pBondCount >= 6) { - vfLibMCS(); - } else { - mcsPlusAlgorithm(); - } - } - - private void vfLibTurboMCSAlgorithm(int rBondCount, int pBondCount) { - if (rBondCount >= 6 && pBondCount >= 6) { - vfLibTurboMCS(); - } else { - mcsPlusAlgorithm(); - } + private void vfLibMCSAlgorithm() { + vfLibMCS(); } private void setTime(boolean bondTypeFlag) { if (bondTypeFlag) { TimeOut tmo = TimeOut.getInstance(); - tmo.setTimeOut(0.10); + tmo.setTimeOut(getBondSensitiveTimeOut()); } else { TimeOut tmo = TimeOut.getInstance(); - tmo.setTimeOut(0.15); + tmo.setTimeOut(getBondInSensitiveTimeOut()); } } @@ -639,13 +618,39 @@ public double getEuclideanDistance() throws IOException { target = pMol.getMolecule().getAtomCount() - getHCount(pMol.getMolecule()); } double common = getFirstMapping().size(); - double euclidean = Math.sqrt(source + target - 2 * common); BigDecimal dist = new BigDecimal(euclidean); - dist = dist.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP); euclidean = dist.doubleValue(); return euclidean; } + + /** + * @return the bondSensitiveTimeOut + */ + public double getBondSensitiveTimeOut() { + return bondSensitiveTimeOut; + } + + /** + * @param bondSensitiveTimeOut the bond Sensitive Timeout in mins (default 0.15 min) + */ + public void setBondSensitiveTimeOut(double bondSensitiveTimeOut) { + this.bondSensitiveTimeOut = bondSensitiveTimeOut; + } + + /** + * @return the bondInSensitiveTimeOut + */ + public double getBondInSensitiveTimeOut() { + return bondInSensitiveTimeOut; + } + + /** + * @param bondInSensitiveTimeOut the bond insensitive Timeout in mins (default 0.15 min) + */ + public void setBondInSensitiveTimeOut(double bondInSensitiveTimeOut) { + this.bondInSensitiveTimeOut = bondInSensitiveTimeOut; + } } diff --git a/src/main/org/openscience/cdk/smsd/interfaces/AbstractMCS.java b/src/main/org/openscience/cdk/smsd/interfaces/AbstractMCS.java index 1b0d8a38a26..e1e7548395a 100644 --- a/src/main/org/openscience/cdk/smsd/interfaces/AbstractMCS.java +++ b/src/main/org/openscience/cdk/smsd/interfaces/AbstractMCS.java @@ -37,7 +37,6 @@ * @cdk.githash * @author Syed Asad Rahman */ - @TestClass("org.openscience.cdk.smsd.interfaces.AbstractMCSTest") public abstract class AbstractMCS { @@ -179,4 +178,24 @@ public abstract class AbstractMCS { * @return Best Mapping Index */ public abstract Map getFirstMapping(); + + /** + * @return the bondSensitive TimeOut + */ + public abstract double getBondSensitiveTimeOut(); + + /** + * @param bondSensitiveTimeOut the bond Sensitive Timeout in mins (default 0.15 min) + */ + public abstract void setBondSensitiveTimeOut(double bondSensitiveTimeOut); + + /** + * @return the bondInSensitive TimeOut + */ + public abstract double getBondInSensitiveTimeOut(); + + /** + * @param bondInSensitiveTimeOut the bond insensitive Timeout in mins (default 0.15 min) + */ + public abstract void setBondInSensitiveTimeOut(double bondInSensitiveTimeOut); }