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 java.util.*; public class CDKsim { public CDKsim() {}; private double getTanimotoCoefficient(BitSet fpp1, BitSet fpp2) { BitSet fp1 = (BitSet) fpp1.clone(); BitSet fp2 = (BitSet) fpp2.clone(); int ntarget = fp1.cardinality(); int nquery = fp2.cardinality(); fp1.and(fp2); int ncommon = fp1.cardinality(); double tanicoeff = (double)ncommon / (double)(nquery+ntarget-ncommon); return( tanicoeff ); } public double[] getSimMatrix(String[] mols, int length, int depth) throws CDKException { int nmol = mols.length; Fingerprinter fprinter = new Fingerprinter(length, depth); double[][] sm = new double[ nmol ][ nmol ]; BitSet[] fparray = new BitSet[ nmol ]; for (int i = 0; i < mols.length; i++) { IMolecule mol = null; SmilesParser sp = new SmilesParser(); mol = sp.parseSmiles(mols[i]); if (mol == null) { throw new CDKException("SmilesParser exception"); } if (length <= 0) length = 1204; if (depth < 0) depth = 6; try { fparray[i] = fprinter.getFingerprint(mol); } catch (Exception cdke) { throw new CDKException("Fingerprinter error"); } } for (int i = 0; i < nmol-1; i++) { for (int j = i+1; j < nmol; j++) { sm[i][j] = getTanimotoCoefficient(fparray[i], fparray[j]); sm[j][i] = sm[i][j]; } } for (int i = 0; i < nmol; i++) sm[i][i] = 1.0; double[] retval = new double[nmol*nmol]; for (int i = 0; i < nmol; i++){ String s = ""; for (int j = 0; j < nmol; j++) { retval[i*nmol + j] = sm[i][j]; s = s + sm[i][j]+" "; } System.out.println(s); } return(retval); } public double getSim(String target, String query, int length, int depth) throws CDKException { Fingerprinter fprinter = new Fingerprinter(length, depth); IMolecule mol1 = null; IMolecule mol2 = null; SmilesParser sp1 = new SmilesParser(); SmilesParser sp2 = new SmilesParser(); mol1 = sp1.parseSmiles(target); mol2 = sp2.parseSmiles(query); if (mol1 == null || mol2 == null) { throw new CDKException("SmilesParser exception"); } if (length <= 0) length = 1204; if (depth < 0) depth = 6; BitSet fp1 = null; BitSet fp2 = null; try { fp1 = fprinter.getFingerprint(mol1); fp2 = fprinter.getFingerprint(mol2); } catch (Exception cdke) { throw new CDKException("Fingerprinter error"); } int ntarget = fp1.cardinality(); int nquery = fp2.cardinality(); fp1.and(fp2); int ncommon = fp1.cardinality(); double tanicoeff = (double)ncommon / (double)(nquery+ntarget-ncommon); return( tanicoeff ); } }