posts - 431,  comments - 344,  trackbacks - 0
公告
 Don't Repeat Yourself
座右铭:you can lose your money, you can spent all of it, and if you work hard you get it all back. But if you waste your time, you're never gonna get it back.
公告本博客在此声明部分文章为转摘,只做资料收集使用。


微信: szhourui
QQ:109450684
Email
lsi.zhourui@gmail.com
<2011年6月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

留言簿(15)

随笔分类(1019)

文章分类(3)

文章档案(21)

收藏夹

Link

好友博客

最新随笔

搜索

  •  

积分与排名

  • 积分 - 855781
  • 排名 - 47

最新评论

阅读排行榜

分子结构式相似度搜索使用的是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 周锐 阅读(1683) 评论(0)  编辑  收藏 所属分类: ChemistryMySQLCDK

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


网站导航: