package smsdcmd.examples; import java.io.File; import java.io.FileInputStream; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import org.openscience.cdk.aromaticity.CDKHueckelAromaticityDetector; import org.openscience.cdk.interfaces.IAtom; import org.openscience.cdk.interfaces.IAtomContainer; import org.openscience.cdk.interfaces.IMolecule; import org.openscience.cdk.smsd.SMSD; import org.openscience.cdk.smsd.interfaces.IMCS.Algorithm; import org.openscience.cdk.smsd.tools.ExtAtomContainerManipulator; import org.openscience.cdk.smsd.tools.MDLFileReader; /* * This example file covers the MCS search part of the SMSD. * A result is obtained if query and template structure has atleast one common MCS. * Query molecule is always the first molecule and template is the second molecule. * Use the isSubgraph() method to find if query is a subgraph/substructure of the template mol. * * * * MCSSearch.java * @modified 3 March, 2010, 11.06 AM * * @author Syed Asad Rahman, EMBL-EBI, Cambridge, UK * contact asad@ebi.ac.uk * */ public class MCSSearch { /** Creates a new instance of SMSD */ public MCSSearch() { } /** * @param args the command line arguments * @throws Exception */ public static void main(String[] args) throws Exception { try { String mol1 = "Data/ATP.mol"; String mol2 = "Data/ADP.mol"; boolean first_MCS = false; boolean exists = (new File(mol1)).exists(); if (!exists) { System.err.println("Error: The Assigned File Path is not Correct " + mol1); System.exit(1); } exists = (new File(mol2)).exists(); if (!exists) { System.err.println("Error: The Assigned File Path is not Correct " + mol2); System.exit(1); } MDLFileReader R1 = new MDLFileReader(new FileInputStream(mol1)); IMolecule A1 = R1.getMolecule(); MDLFileReader R2 = new MDLFileReader(new FileInputStream(mol2)); IMolecule A2 = R2.getMolecule(); ExtAtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(A1); ExtAtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(A2); A1 = (IMolecule) ExtAtomContainerManipulator.removeHydrogensAndPreserveAtomID(A1); A2 = (IMolecule) ExtAtomContainerManipulator.removeHydrogensAndPreserveAtomID(A2); // Calling the main algorithm to perform MCS cearch CDKHueckelAromaticityDetector.detectAromaticity(A1); CDKHueckelAromaticityDetector.detectAromaticity(A2); boolean bondSensitive = true; boolean removeHydrogen = true; boolean stereoMatch = true; boolean fragmentMinimization = true; boolean energyMinimization = true; SMSD comparison = new SMSD(Algorithm.DEFAULT, bondSensitive); comparison.init(A1, A2, removeHydrogen); comparison.setChemFilters(stereoMatch, fragmentMinimization, energyMinimization); // Get modified Query and Target Molecules as Mappings will correspond to these molecules IAtomContainer Query = comparison.getReactantMolecule(); IAtomContainer Target = comparison.getProductMolecule(); //Print all MCS solutions if first_MCS is false if (first_MCS == false) { int count_final_sol = 1; System.out.println("Output of the final Mappings: "); try { if (comparison.getAllMapping() != null) { for (Map final_solution : comparison.getAllMapping()) { int final_solution_size = final_solution.size(); System.out.println("Final mapping Nr. " + ++count_final_sol + " Size:" + final_solution_size); for (Map.Entry mapping : final_solution.entrySet()) { System.out.println((mapping.getKey() + 1) + " " + (mapping.getValue() + 1)); //Get the mapped atom number in Query Molecule int queryMappingNumber = mapping.getKey(); //Get the mapped atom number in Target Molecule int targetMappingNumber = mapping.getValue(); //Get the mapped atom in Query Molecule IAtom queryAtom = Query.getAtom(queryMappingNumber); //Get the mapped atom in Target Molecule IAtom targetAtom = Target.getAtom(targetMappingNumber); //Print mapped atom numbers System.out.println(queryMappingNumber + " " + (targetMappingNumber)); //Print mapped atoms System.out.println(queryAtom.getSymbol() + " " + targetAtom.getSymbol()); } System.out.println(""); System.out.println("Stereo Match: " + comparison.getStereoScore(count_final_sol - 1)); System.out.println("Stereo different: " + comparison.isStereoMisMatch()); System.out.println("Fragment Size: " + comparison.getFragmentSize(count_final_sol - 1)); System.out.println("Tanimoto Similarity Score: " + comparison.getTanimotoSimilarity()); System.out.println("Tanimoto Euclidean Distance: " + comparison.getEuclideanDistance()); count_final_sol++; } System.out.println(""); } } catch (Exception ex) { ex.printStackTrace(); } } //Output only first solution if (first_MCS) { try { for (Map.Entry mappings : comparison.getFirstMapping().entrySet()) { //Get the mapped atom number in Query Molecule int queryMappingNumber = mappings.getKey(); //Get the mapped atom number in Target Molecule int targetMappingNumber = mappings.getValue(); //Get the mapped atom in Query Molecule IAtom queryAtom = Query.getAtom(queryMappingNumber); //Get the mapped atom in Target Molecule IAtom targetAtom = Target.getAtom(targetMappingNumber); //Print mapped atom numbers System.out.println(queryMappingNumber + " " + (targetMappingNumber)); //Print mapped atoms System.out.println(queryAtom.getSymbol() + " " + targetAtom.getSymbol()); } System.out.println(""); System.out.println(""); System.out.println("Stereo Match: " + comparison.getStereoScore(0)); System.out.println("Stereo different: " + comparison.isStereoMisMatch()); System.out.println("Fragment Size: " + comparison.getFragmentSize(0)); System.out.println("Tanimoto Similarity Score: " + comparison.getTanimotoSimilarity()); System.out.println("Tanimoto Euclidean Distance: " + comparison.getEuclideanDistance()); System.out.println(""); } catch (Exception ex) { ex.printStackTrace(); } System.out.println(""); } System.out.println(""); System.out.println(""); } catch (Exception ex) { Logger.getLogger(SMSDExample.class.getName()).log(Level.SEVERE, null, ex); } } /** * * @param Molecule1 * @param Molecule2 */ private static void printMolecules(IAtomContainer Molecule1, IAtomContainer Molecule2) { System.out.println("Molecule 1"); for (int i = 0; i < Molecule1.getAtomCount(); i++) { System.out.print(Molecule1.getAtom(i).getSymbol() + " "); } System.out.println(); System.out.println("Molecule 2"); for (int i = 0; i < Molecule2.getAtomCount(); i++) { System.out.print(Molecule2.getAtom(i).getSymbol() + " "); } System.out.println(); } }