import net.guha.util.cdk.ViewMolecule2D; import org.openscience.cdk.DefaultChemObjectBuilder; import org.openscience.cdk.exception.CDKException; import org.openscience.cdk.graph.ConnectivityChecker; import org.openscience.cdk.interfaces.IAtom; import org.openscience.cdk.interfaces.IAtomContainer; import org.openscience.cdk.interfaces.IMoleculeSet; import org.openscience.cdk.isomorphism.UniversalIsomorphismTester; import org.openscience.cdk.isomorphism.mcss.RMap; import org.openscience.cdk.smiles.SmilesParser; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * @author Rajarshi Guha */ public class NoMCSS { NoMCSS() { } private IAtomContainer getMCSS(IAtomContainer mol1, IAtomContainer mol2) throws CDKException { IAtomContainer mcss = null; int maxac = -999; List ssList = UniversalIsomorphismTester.getOverlaps(mol1, mol2); for (Object aSsList : ssList) { IAtomContainer ss = (IAtomContainer) aSsList; if (ss.getAtomCount() > maxac) { mcss = ss; maxac = ss.getAtomCount(); } } return mcss; } public IMoleculeSet getUncommon(IAtomContainer mol, IAtomContainer mcss) throws CDKException { ArrayList atomSerialsToDelete = new ArrayList(); List matches = UniversalIsomorphismTester.getSubgraphAtomsMaps(mol, mcss); List mapList = (List) matches.get(0); for (Object o : mapList) { RMap rmap = (RMap) o; atomSerialsToDelete.add(rmap.getId1()); } // at this point we have the serial numbers of the bonds to delete // we should get the actual bonds rather than delete by serial numbers ArrayList atomsToDelete = new ArrayList(); for (Integer serial : atomSerialsToDelete) { atomsToDelete.add(mol.getAtom(serial)); } // now lets get rid of the bonds themselves for (IAtom atom : atomsToDelete) { mol.removeAtomAndConnectedElectronContainers(atom); } // now we probably have a set of disconnected components // so lets get a set of individual atom containers for // corresponding to each component return ConnectivityChecker.partitionIntoMolecules(mol); } public static void main(String[] args) throws CDKException { String smi1 = "CSC1CC1NC"; String smi2 = "C1CC1CCC"; SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance()); IAtomContainer mol1 = sp.parseSmiles(smi1); IAtomContainer mol2 = sp.parseSmiles(smi2); NoMCSS nomcss = new NoMCSS(); IAtomContainer mcss = nomcss.getMCSS(mol1, mol2); IMoleculeSet remainder1 = nomcss.getUncommon(mol1, mcss); System.out.println("remainder1.getMoleculeCount() = " + remainder1.getMoleculeCount()); IMoleculeSet remainder2 = nomcss.getUncommon(mol2, mcss); System.out.println("remainder2.getMoleculeCount() = " + remainder2.getMoleculeCount()); /* Iterator mols = remainder1.molecules(); while (mols.hasNext()) { IAtomContainer container = mols.next(); ViewMolecule2D viewer = new ViewMolecule2D(container); viewer.draw(); } */ } }