PubchemFingerprinter extends AbstractFingerprinter implements IFingerprinter { public static BitSet decode(String enc) { byte[] fp = base64Decode(enc); if (fp.length < 4) { throw new IllegalArgumentException("Input is not a proper PubChem base64 encoded fingerprint"); } int len = (fp[0] << 24) | (fp[1] << 16) | (fp[2] << 8) | (fp[3] & 0xff); if (len != FP_SIZE) { throw new IllegalArgumentException("Input is not a proper PubChem base64 encoded fingerprint"); } PubchemFingerprinter pc = new PubchemFingerprinter(null); for (int i = 0; i < pc.m_bits.length; ++i) { pc.m_bits[i] = fp[i + 4]; } BitSet ret = new BitSet(FP_SIZE); for (int i = 0; i < FP_SIZE; i++) { if (pc.isBitOn(i)) ret.set(i); } return ret; } PubchemFingerprinter(IChemObjectBuilder builder); @Override IBitFingerprint getBitFingerprint(IAtomContainer atomContainer); @Override Map<String, Integer> getRawFingerprint(IAtomContainer iAtomContainer); @Override int getSize(); byte[] getFingerprintAsBytes(); static BitSet decode(String enc); @Override ICountFingerprint getCountFingerprint(IAtomContainer container); static final int FP_SIZE; }
@Test(expected = IllegalArgumentException.class) public void testDecode_invalid() { PubchemFingerprinter.decode("a"); }
@Test public void testDecode() { BitSet bitSet = PubchemFingerprinter .decode("AAADcYBgAAAAAAAAAAAAAAAAAAAAAAAAAAAwAAAAAAAAAAABAAAAGAAAAAAACACAEAAwAIAAAACAACBCAAACAAAgAAAIiAAAAIgIICKAERCAIAAggAAIiAcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=="); int[] setBits = new int[]{0, 9, 10, 178, 179, 255, 283, 284, 332, 344, 355, 370, 371, 384, 416, 434, 441, 446, 470, 490, 516, 520, 524, 552, 556, 564, 570, 578, 582, 584, 595, 599, 603, 608, 618, 634, 640, 660, 664, 668, 677, 678, 679}; for (int set : setBits) { Assert.assertTrue("bit " + set + " was not set", bitSet.get(set)); } }