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);
}
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);
}
Aggregations