import java.io.File; import java.io.FileReader; import java.io.FileWriter; import org.openscience.cdk.AtomContainer; import org.openscience.cdk.ChemFile; import org.openscience.cdk.ChemObject; import org.openscience.cdk.Molecule; import org.openscience.cdk.aromaticity.HueckelAromaticityDetector; import org.openscience.cdk.tools.manipulator.ChemFileManipulator; import org.openscience.cdk.io.*; /** MolToSmiles * * Command-line utility that translates MDL mol files to SMILES * strings * * * @author irilenia * @version 27/02/2005 compiled with CDK 25/01/2005 * */ public class MolToSmiles { public static void main(String[] args) { if (args.length <1) { System.err.println("syntax: MolToSmiles "); System.exit(0); } // check the first argument - must be a file String mol_filename = args[0]; File mol_file = new File(mol_filename); if (!(mol_file.isFile())) { System.err.println("Error: First argument must be a file! " + "Argument 1= " + args[0]); System.exit(0); } // create a reader and read the contents of the query file // only allow one molecule in the query file! try { Molecule mol =null; ChemObjectReader reader = new ReaderFactory().createReader(new FileReader(mol_file)); if (reader.accepts(new Molecule())) { ChemFile content = (ChemFile)reader.read((ChemObject)new ChemFile()); AtomContainer[] containers = ChemFileManipulator.getAllAtomContainers(content); //will only read the first container and ignore the rest! if (containers.length<1) { System.err.println("Error: No molecules were found in " + mol_file.getName()); System.exit(0); } mol = new Molecule(containers[0]); if (mol == null) { System.err.println("Error: Molecule returned is null from file " + mol_file.getName()); System.exit(0); } if (HueckelAromaticityDetector.detectAromaticity(mol)) { //REQUIRED to recognise aromatic flags //System.out.println("Query is aromatic!"); } } else { System.err.println("Error: Could not read molecule from file " + mol_file.getName()); System.exit(0); } //print out the target molecule SMILESWriter writer = new SMILESWriter(new FileWriter(new File("target.smi"))); writer.write(mol); writer.close(); }//try catch (Exception exception) { exception.printStackTrace(); } } //main }