posts - 431,  comments - 344,  trackbacks - 0
          分子結(jié)構(gòu)式相似度搜索使用的是fingerprint進(jìn)行比較,而
          fingerprint是一個(gè)二進(jìn)制數(shù)據(jù),CDK中使用BitSet來存儲(chǔ)該信息,如果要每次比對(duì)都去生成BitSet,那也太耗時(shí)間了,所以我們需要存儲(chǔ)
          fingerprint信息到數(shù)據(jù)庫(kù)中,比較的時(shí)候,直接讀取,而MySQL不支持存儲(chǔ)BitSet數(shù)據(jù),網(wǎng)站找了一下,有人想到把
          BitSet轉(zhuǎn)換成Blob信息進(jìn)行存儲(chǔ),然后取的時(shí)候再轉(zhuǎn)換回來,不愧是個(gè)好的方法。下面來看看代碼實(shí)現(xiàn):

          /*
           * 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的值計(jì)算出來,然后存儲(chǔ)到MySQL數(shù)據(jù)庫(kù)中了。
          進(jìn)行相似度搜索的時(shí)候,值需要取出已經(jīng)存儲(chǔ)的值進(jìn)行比對(duì)就可以了。
          float coefficient = Tanimoto.calculate(query, MySQLUtil.blobToBitSet(results.getBlob("bits")));
          筆者測(cè)試了187586條結(jié)構(gòu)數(shù)據(jù),大概需要12秒左右,基本滿足一般需求。
          posted on 2011-06-29 10:20 周銳 閱讀(1708) 評(píng)論(0)  編輯  收藏 所屬分類: Chemistry 、MySQLCDK
          主站蜘蛛池模板: 温宿县| 成安县| 平乐县| 家居| 桂东县| 宜都市| 彝良县| 贡嘎县| 格尔木市| 大城县| 卢龙县| 汉阴县| 衢州市| 普兰县| 林西县| 盱眙县| 莱芜市| 龙井市| 甘谷县| 保靖县| 嵊泗县| 大渡口区| 宜川县| 章丘市| 奉节县| 庄河市| 城口县| 将乐县| 焦作市| 光泽县| 扎兰屯市| 资中县| 阆中市| 搜索| 台南市| 海城市| 天台县| 砀山县| 江门市| 长沙县| 彭山县|