<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ChemHack 中文频道 &#187; Java</title>
	<atom:link href="http://chemhack.com/cn/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://chemhack.com/cn</link>
	<description>ChemHack.com中文版</description>
	<lastBuildDate>Tue, 19 Apr 2011 16:48:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Faster Fingerprint Search with Java &amp; CDK</title>
		<link>http://chemhack.com/cn/2008/11/faster-fingerprint-search-with-java-cdk/</link>
		<comments>http://chemhack.com/cn/2008/11/faster-fingerprint-search-with-java-cdk/#comments</comments>
		<pubDate>Tue, 11 Nov 2008 10:54:27 +0000</pubDate>
		<dc:creator>段炼</dc:creator>
				<category><![CDATA[Work]]></category>
		<category><![CDATA[CDK]]></category>
		<category><![CDATA[Cheminformatics]]></category>
		<category><![CDATA[Fingerprint]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[化学信息学]]></category>

		<guid isPermaLink="false">http://chemhack.com/?p=110</guid>
		<description><![CDATA[Rich Apodaca wrote a great serious posts named Fast Substructure Search Using Open Source Tools providing details on substructure search with MySQL. But, however, poor binary data operation functions of MySQL limited the implementation of similar structure search which typically depends on the calculation of Tanimato coefficient. We are going to use Java &#38; CDK to add this feature. As default [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://depth-first.com/">Rich Apodaca</a> wrote a great serious posts named <em>Fast Substructure Search Using Open Source Tools</em> providing details on substructure search with MySQL. But, however, poor binary data operation functions of MySQL limited the implementation of similar structure search which typically depends on the calculation of Tanimato coefficient. We are going to use Java &amp; CDK to add this feature.</p>
<p>As default output of CDK fingerprint, <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/BitSet.html">java.util.BitSet</a> with <a title="interface in java.io" href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/Serializable.html">Serializable</a> interface is perfect data format of fingerprint data storage. Java itself provides several collections such as <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html">ArrayList</a>, <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedList.html">LinkedList</a>, <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Vector.html">Vector</a> class in package java.util. To provide web access to the search engine, thread unsafe ArrayList and LinkedList have to be kicked out. How about Vector? Once all the fingerprint data is well prepared, the collection  function we need to do similarity search is just iteration. No add, no delete. So, a light weight array is enough.</p>
<p>Most of the molecule information is stored in MySQL database, so we are going to map fingerprint to corresponding row in data table. Here is the MolDFData class, we use a long variable to store corresponding primary key in data table.</p>
<pre lang="java">public class MolDFData implements Serializable {
    private long id;
    private BitSet fingerprint;

    public MolDFData(long id, BitSet fingerprint) {
        this.id = id;
        this.fingerprint = fingerprint;
    }
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public BitSet getFingerprint() {
        return fingerprint;
    }

    public void setFingerprint(BitSet fingerprint) {
        this.fingerprint = fingerprint;
    }
}</pre>
<p>This is how we storage our fingerprints.</p>
<pre lang="java">private MolFPData[] arrayData;</pre>
<p>No big deal with similarity search. Just calculate the Tanimoto coefficient, if it&#8217;s bigger than minimal  similarity you set, add this one into result.</p>
<pre lang="java">    public List searchTanimoto(BitSet bt, float minSimlarity) {
        List resultList = new LinkedList();
        int i;
        for (i = 0; i &lt; arrayData.length; i++) {
            MolDFData aListData = arrayData[i];
            try {
                float coefficient = Tanimoto.calculate(aListData.getFingerprint(), bt);
                if (coefficient &gt; minSimlarity) {
                    resultList.add(new SearchResultData(aListData.getId(), coefficient));
                }
            } catch (CDKException e) {

            }
            Collections.sort(resultList);
        }
        return resultList;
    }</pre>
<p>Pretty ugly code?  Maybe. But it really works, at a acceptable speed. Tests were done using the code blow on a macbook(Intel Core Due 1.83 GHz, 2G RAM).<span style="font-family: 'Courier New'; line-height: 18px; white-space: pre; "> </span></p>
<pre lang="java">                long t3 = System.currentTimeMillis();
                List&lt;SearchResultData&gt; listResult = se.searchTanimoto(bs, 0.8f);
                long t4 = System.currentTimeMillis();
                System.out.println("Thread: Search done in " + (t4 - t3) + " ms.");</pre>
<p>In my database of 87364 commercial compounds, it takes 335 ms.</p>
]]></content:encoded>
			<wfw:commentRss>http://chemhack.com/cn/2008/11/faster-fingerprint-search-with-java-cdk/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

