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 周銳 閱讀(1692) 評論(0)  編輯  收藏 所屬分類: ChemistryMySQLCDK
          主站蜘蛛池模板: 咸阳市| 远安县| 晋州市| 姜堰市| 兰州市| 郑州市| 阿合奇县| 龙山县| 元江| 建德市| 天门市| 元谋县| 屏南县| 竹溪县| 凯里市| 聂荣县| 灵武市| 库尔勒市| 安塞县| 纳雍县| 苍溪县| 泗水县| 象山县| 台东县| 浦县| 大同市| 安康市| 保康县| 东辽县| 八宿县| 石城县| 商南县| 龙南县| 建水县| 松原市| 双流县| 正定县| 东台市| 岑巩县| 合阳县| 策勒县|