/* * 03/03/2006 - Added methods to provide MW and MF webservices * */ import org.openscience.cdk.interfaces.IMolecule; import org.openscience.cdk.interfaces.IAtom; import org.openscience.cdk.exception.CDKException; import org.openscience.cdk.fingerprint.Fingerprinter; import org.openscience.cdk.smiles.SmilesParser; import org.openscience.cdk.tools.MFAnalyser; import org.openscience.cdk.tools.HydrogenAdder; import java.util.*; import java.io.*; public class CDKws { public CDKws() {}; public float getMolecularWeight(String s) throws CDKException { try { SmilesParser sp = new SmilesParser(); IMolecule mol = sp.parseSmiles(s); HydrogenAdder hAdder=new HydrogenAdder(); hAdder.addExplicitHydrogensToSatisfyValency(mol); MFAnalyser mfa = new MFAnalyser(mol); return(mfa.getCanonicalMass()); } catch (CDKException cdke) { throw new CDKException(cdke.toString()); } catch (IOException ioe) { throw new CDKException(ioe.toString()); } catch (ClassNotFoundException cnfe) { throw new CDKException(cnfe.toString()); } } public String getMolecularFormula(String s) throws CDKException { try { SmilesParser sp = new SmilesParser(); IMolecule mol = sp.parseSmiles(s); HydrogenAdder hAdder=new HydrogenAdder(); hAdder.addExplicitHydrogensToSatisfyValency(mol); MFAnalyser mfa = new MFAnalyser(mol); return(mfa.getMolecularFormula()); } catch (CDKException cdke) { throw new CDKException(cdke.toString()); } catch (IOException ioe) { throw new CDKException(ioe.toString()); } catch (ClassNotFoundException cnfe) { throw new CDKException(cnfe.toString()); } } public String getHTMLMolecularFormula(String s) throws CDKException { try { SmilesParser sp = new SmilesParser(); IMolecule mol = sp.parseSmiles(s); HydrogenAdder hAdder=new HydrogenAdder(); hAdder.addExplicitHydrogensToSatisfyValency(mol); MFAnalyser mfa = new MFAnalyser(mol); return(mfa.getHTMLMolecularFormula()); } catch (CDKException cdke) { throw new CDKException(cdke.toString()); } catch (IOException ioe) { throw new CDKException(ioe.toString()); } catch (ClassNotFoundException cnfe) { throw new CDKException(cnfe.toString()); } } public String getFingerprintString(String s) throws CDKException { String print = ""; try { print = getFingerprintString(s, 1024, 6); } catch (CDKException cdke) { throw new CDKException("SmilesParser error"); } return(print); } public String getFingerprintString(String s, int length) throws CDKException { String print = ""; if (length <= 0) length = 1204; try { print = getFingerprintString(s,length,6); } catch (CDKException cdke) { throw new CDKException("SmilesParser error"); } return(print); } public String getFingerprintString(String s, int length, int depth) throws CDKException { String print = ""; IMolecule mol = null; SmilesParser sp = new SmilesParser(); mol = sp.parseSmiles(s); if (mol == null) { throw new CDKException("SmilesParser exception"); } if (length <= 0) length = 1204; if (depth < 0) depth = 6; Fingerprinter fprinter = new Fingerprinter(length, depth); try { print = fprinter.getFingerprint(mol).toString(); } catch (Exception e) { throw new CDKException("Fingerprinter error"); } return(print); } public Vector getFingerprintVector(String s) throws CDKException { String fp; try { fp = getFingerprintString(s,1024,6); } catch (CDKException cdke) { throw new CDKException("SmilesParser error"); } String[] st = fp.substring(1,fp.length()-1).split("[\\s,]"); Vector v = new Vector(); for (int i = 0; i < st.length; i++) { if (st[i].equals("")) continue; v.add( new Integer(st[i]) ); } return(v); } public Vector getFingerprintVector(String s, int length) throws CDKException { String fp; if (length <= 0) length = 1204; try { fp = getFingerprintString(s,length,6); } catch (CDKException cdke) { throw new CDKException("SmilesParser error"); } String[] st = fp.substring(1,fp.length()-1).split("[\\s,]"); Vector v = new Vector(); for (int i = 0; i < st.length; i++) { if (st[i].equals("")) continue; v.add( new Integer(st[i]) ); } return(v); } public Vector getFingerprintVector(String s, int length, int depth) throws CDKException { IMolecule mol = null; SmilesParser sp = new SmilesParser(); mol = sp.parseSmiles(s); if (mol == null) { throw new CDKException("SmilesParser exception"); } if (length <= 0) length = 1204; if (depth < 0) depth = 6; Fingerprinter fprinter = new Fingerprinter(length, depth); String fp = null; try { fp = fprinter.getFingerprint(mol).toString(); } catch (Exception e) { throw new CDKException("Fingerprinter error"); } String[] st = fp.substring(1,fp.length()-1).split("[\\s,]"); Vector v = new Vector(); for (int i = 0; i < st.length; i++) { if (st[i].equals("")) continue; v.add( new Integer(st[i]) ); } return(v); } }