Skip to content
Permalink
Browse files

Added similarity method for Lingo's

  • Loading branch information
rajarshi authored and egonw committed Nov 1, 2010
1 parent f487c30 commit 133f314d325562546fa72e41a4ac836ba61bcd8e
@@ -0,0 +1,61 @@
/* $Revision$ $Author$ $Date$
*
* Copyright (C) 2010 Rajarshi Guha <rajarshi.guha@gmail.com>
*
* Contact: rajarshi.guha@gmail.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
* All I ask is that proper credit is given for my work, which includes
* - but is not limited to - adding the above copyright notice to the beginning
* of your source code files, and to any copyright notice that you may distribute
* with programs based on this work.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.openscience.cdk.similarity;

import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;

import java.util.Map;
import java.util.TreeSet;

/**
* A class to evaluate the similarity between two LINGO's as described in {@cdk.cite Vidal2005}.
*
* @author Rajarshi Guha
* @cdk.githash
* @cdk.keyword lingo
* @cdk.keyword similarity, tanimoto
* @cdk.module fingerprint
*/
@TestClass("org.openscience.cdk.similarity.LingoTest")
public class LingoSimilarity {
@TestMethod("testLingoSim")
public static float calculate(Map<String, Integer> features1, Map<String, Integer> features2) {
TreeSet<String> keys = new TreeSet<String>(features1.keySet());
keys.addAll(features2.keySet());

float sum = 0.0f;
for (String key : keys) {
Integer c1 = features1.get(key);
Integer c2 = features2.get(key);

c1 = c1 == null ? 0 : c1;
c2 = c2 == null ? 0 : c2;
sum += 1.0 - Math.abs(c1 - c2) / (c1 + c2);
}

return sum / keys.size();
}
}
@@ -0,0 +1,57 @@
/* $Revision$ $Author$ $Date$
*
* Copyright (C) 2010 Rajarshi Guha <rajarshi.guha@gmail.com>
*
* Contact: rajarshi.guha@gmail.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
* All I ask is that proper credit is given for my work, which includes
* - but is not limited to - adding the above copyright notice to the beginning
* of your source code files, and to any copyright notice that you may distribute
* with programs based on this work.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/

package org.openscience.cdk.similarity;

import org.junit.Assert;
import org.junit.Test;
import org.openscience.cdk.CDKTestCase;
import org.openscience.cdk.Molecule;
import org.openscience.cdk.fingerprint.LingoFingerprinter;
import org.openscience.cdk.templates.MoleculeFactory;

import java.util.Map;

/**
* @cdk.module test-fingerprint
*/
public class LingoSimilarityTest extends CDKTestCase {

boolean standAlone = false;

@Test
public void testLingoSim() throws Exception {
Molecule mol1 = MoleculeFactory.makeIndole();
Molecule mol2 = MoleculeFactory.makeIndole();
LingoFingerprinter fingerprinter = new LingoFingerprinter();
Map<String, Integer> bs1 = fingerprinter.getRawFingerprint(mol1);
Map<String, Integer> bs2 = fingerprinter.getRawFingerprint(mol2);
float lingosim = LingoSimilarity.calculate(bs1, bs2);
if (standAlone) System.out.println("LingoSimilarity similarity: " + lingosim);
if (!standAlone) Assert.assertEquals(1.0, lingosim, 0.01);
}


}

0 comments on commit 133f314

Please sign in to comment.
You can’t perform that action at this time.