diff --git a/src/main/org/openscience/cdk/similarity/Tanimoto.java b/src/main/org/openscience/cdk/similarity/Tanimoto.java index 9bb63c773a5..6397b172dc8 100644 --- a/src/main/org/openscience/cdk/similarity/Tanimoto.java +++ b/src/main/org/openscience/cdk/similarity/Tanimoto.java @@ -225,23 +225,27 @@ public static float method2( ICountFingerprint fp1, j = 0; while ( i < fp1.numOfPopulatedbins() || j < fp2.numOfPopulatedbins() ) { - int hash1 = fp1.getHash(i); - int hash2 = fp2.getHash(j); - int count1 = fp1.getCount(i); - int count2 = fp2.getCount(j); + Integer hash1 = i < fp1.numOfPopulatedbins() ? fp1.getHash(i) + : null; + Integer hash2 = j < fp2.numOfPopulatedbins() ? fp2.getHash(j) + : null; + Integer count1 = i < fp1.numOfPopulatedbins() ? fp1.getCount(i) + : null; + Integer count2 = j < fp2.numOfPopulatedbins() ? fp2.getCount(j) + : null; - if ( hash1 < hash2) { + if ( count2 == null || (hash1 != null && hash1 < hash2) ) { maxSum += count1; i++; continue; } - if ( hash1 > hash2 ) { + if ( count1 == null || (hash2 != null && hash1 > hash2 ) ) { maxSum += count2; j++; continue; } - if ( hash1 == hash2 ) { + if ( hash1.equals(hash2) ) { maxSum += Math.max(count1, count2); minSum += Math.min(count1, count2); i++; diff --git a/src/test/org/openscience/cdk/similarity/TanimotoTest.java b/src/test/org/openscience/cdk/similarity/TanimotoTest.java index 3848087cdb0..267402736df 100644 --- a/src/test/org/openscience/cdk/similarity/TanimotoTest.java +++ b/src/test/org/openscience/cdk/similarity/TanimotoTest.java @@ -188,18 +188,26 @@ public void testRawTanimotoBetween0and1() throws Exception { } @Test - public void testCountMethod1and2() { - IntArrayCountFingerprint fp1 = new IntArrayCountFingerprint( - new HashMap() {{ - put("A", 3); - }} - ); - IntArrayCountFingerprint fp2 = new IntArrayCountFingerprint( - new HashMap() {{ - put("A", 4); - }} - ); + public void testCountMethod1and2() throws CDKException { + ICountFingerprint fp1 = new IntArrayCountFingerprint( + new HashMap() {{ + put("A", 3); + }} + ); + ICountFingerprint fp2 = new IntArrayCountFingerprint( + new HashMap() {{ + put("A", 4); + }} + ); Assert.assertEquals(0.923, Tanimoto.method1(fp1, fp2), 0.001 ); Assert.assertEquals(0.75, Tanimoto.method2(fp1, fp2), 0.001 ); + + Molecule mol1 = MoleculeFactory.makeIndole(); + Molecule mol2 = MoleculeFactory.makeIndole(); + SignatureFingerprinter fingerprinter = new SignatureFingerprinter(); + fp1 = fingerprinter.getCountFingerprint(mol1); + fp2 = fingerprinter.getCountFingerprint(mol2); + Assert.assertEquals(1.0, Tanimoto.method1(fp1, fp2), 0.001); + Assert.assertEquals(1.0, Tanimoto.method2(fp1, fp2), 0.001); } } \ No newline at end of file