Search in sources :

Example 1 with IntArrayFingerprint

use of org.openscience.cdk.fingerprint.IntArrayFingerprint in project cdk by cdk.

the class TanimotoTest method testCompareBitSetandBitFingerprintTanimoto.

@Test
public void testCompareBitSetandBitFingerprintTanimoto() throws Exception {
    IAtomContainer mol1 = TestMoleculeFactory.make123Triazole();
    IAtomContainer mol2 = TestMoleculeFactory.makeImidazole();
    Fingerprinter fingerprinter = new Fingerprinter(1024, 8);
    BitSet bs1 = fingerprinter.getBitFingerprint(mol1).asBitSet();
    BitSet bs2 = fingerprinter.getBitFingerprint(mol2).asBitSet();
    float tanimoto = Tanimoto.calculate(bs1, bs2);
    BitSetFingerprint fp1 = new BitSetFingerprint(bs1);
    BitSetFingerprint fp2 = new BitSetFingerprint(bs2);
    double tanimoto2 = Tanimoto.calculate(fp1, fp2);
    Assert.assertEquals(tanimoto, tanimoto2, 0.01);
    IntArrayFingerprint ifp1 = new IntArrayFingerprint(fp1);
    IntArrayFingerprint ifp2 = new IntArrayFingerprint(fp2);
    tanimoto2 = Tanimoto.calculate(ifp1, ifp2);
    Assert.assertEquals(tanimoto, tanimoto2, 0.01);
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) Fingerprinter(org.openscience.cdk.fingerprint.Fingerprinter) LingoFingerprinter(org.openscience.cdk.fingerprint.LingoFingerprinter) BitSetFingerprint(org.openscience.cdk.fingerprint.BitSetFingerprint) BitSet(java.util.BitSet) IntArrayFingerprint(org.openscience.cdk.fingerprint.IntArrayFingerprint) Test(org.junit.Test)

Example 2 with IntArrayFingerprint

use of org.openscience.cdk.fingerprint.IntArrayFingerprint in project cdk by cdk.

the class Tanimoto method calculate.

/**
 * Evaluates Tanimoto coefficient for two <code>IBitFingerprint</code>.
 * <p>
 * @param fingerprint1 fingerprint for the first molecule
 * @param fingerprint2 fingerprint for the second molecule
 * @return The Tanimoto coefficient
 * @throws IllegalArgumentException if bitsets are not of the same length
 */
public static double calculate(IBitFingerprint fingerprint1, IBitFingerprint fingerprint2) {
    if (fingerprint1.size() != fingerprint2.size()) {
        throw new IllegalArgumentException("Fingerprints must have the same size");
    }
    int cardinality1 = fingerprint1.cardinality();
    int cardinality2 = fingerprint2.cardinality();
    // If the fingerprint is an IntArrayFingeprint that could mean a big
    // fingerprint so let's take the safe way out and create a
    // new IntArrayfingerprint
    IBitFingerprint one_and_two = fingerprint1 instanceof IntArrayFingerprint ? new IntArrayFingerprint(fingerprint1) : new BitSetFingerprint(fingerprint1);
    one_and_two.and(fingerprint2);
    double cardinalityCommon = one_and_two.cardinality();
    return cardinalityCommon / (cardinality1 + cardinality2 - cardinalityCommon);
}
Also used : IBitFingerprint(org.openscience.cdk.fingerprint.IBitFingerprint) BitSetFingerprint(org.openscience.cdk.fingerprint.BitSetFingerprint) IntArrayFingerprint(org.openscience.cdk.fingerprint.IntArrayFingerprint) BitSetFingerprint(org.openscience.cdk.fingerprint.BitSetFingerprint) ICountFingerprint(org.openscience.cdk.fingerprint.ICountFingerprint) IBitFingerprint(org.openscience.cdk.fingerprint.IBitFingerprint) IntArrayFingerprint(org.openscience.cdk.fingerprint.IntArrayFingerprint)

Aggregations

BitSetFingerprint (org.openscience.cdk.fingerprint.BitSetFingerprint)2 IntArrayFingerprint (org.openscience.cdk.fingerprint.IntArrayFingerprint)2 BitSet (java.util.BitSet)1 Test (org.junit.Test)1 Fingerprinter (org.openscience.cdk.fingerprint.Fingerprinter)1 IBitFingerprint (org.openscience.cdk.fingerprint.IBitFingerprint)1 ICountFingerprint (org.openscience.cdk.fingerprint.ICountFingerprint)1 LingoFingerprinter (org.openscience.cdk.fingerprint.LingoFingerprinter)1 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)1