posts - 431,  comments - 344,  trackbacks - 0
分子结构式相似度搜索使用的是fingerprint进行比较,而
fingerprint是一个二进制数据,CDK中使用BitSet来存储该信息,如果要每次比对都去生成BitSet,那也太耗时间了,所以我们需要存储
fingerprint信息到数据库中,比较的时候,直接读取,而MySQL不支持存储BitSet数据,网站找了一下,有人想到把
BitSet转换成Blob信息进行存储,然后取的时候再转换回来,不愧是个好的方法。下面来看看代码实现:

/*
 * Copyright (c) 2010-2020 Founder Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * Founder. You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the agreements
 * you entered into with Founder.
 *
 */
package com.founder.mysql;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.BitSet;
public class MySQLUtil {
public static Blob bitsetToBlob(BitSet myBitSet, Connection con) throws SQLException {
   byte[] byteArray = toByteArray(myBitSet);
   Blob blob = con.createBlob();
   blob.setBytes(1, byteArray);
   return blob;
}
private static byte[] toByteArray(BitSet bits) {
   byte[] bytes = new byte[bits.length()/8+1];
   for (int i=0; i<bits.length(); i++) {
       if (bits.get(i)) {
           bytes[bytes.length-i/8-1] |= 1<<(i%8);
       }
   }
   return bytes;
}
public static BitSet blobToBitSet(Blob blob) throws SQLException {
   byte[] bytes = blob.getBytes(1, (int)blob.length());
   BitSet bitSet = fromByteArray(bytes);
   return bitSet;
}
private static BitSet fromByteArray(byte[] bytes) {
   BitSet bits = new BitSet(1024);  
   for (int i=0; i<bytes.length*8; i++) {
       if ((bytes[bytes.length-i/8-1]&(1<<(i%8))) > 0) {
           bits.set(i);
       }
   }
   return bits;
}
}

通过以上代码,我们就可以把fingerprint的值计算出来,然后存储到MySQL数据库中了。
进行相似度搜索的时候,值需要取出已经存储的值进行比对就可以了。
float coefficient = Tanimoto.calculate(query, MySQLUtil.blobToBitSet(results.getBlob("bits")));
笔者测试了187586条结构数据,大概需要12秒左右,基本满足一般需求。
posted on 2011-06-29 10:20 周锐 阅读(1635) 评论(0)  编辑  收藏 所属分类: ChemistryMySQLCDK

只有注册用户登录后才能发表评论。


网站导航: