⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 isoak1example.java

📁 a molecular graph kernel based on iterative graph similarity and optimal assignments
💻 JAVA
字号:
/** * ISOAK - Iterative similarity optimal assignment kernel. *  * Written by Matthias Rupp 2006-2007. * Copyright (c) 2006-2007, Matthias Rupp, Ewgenij Proschak, Gisbert Schneider. *  * All rights reserved. *  * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: *  *  * The above copyright notice, this permission notice and the following disclaimers *    shall be included in all copies or substantial portions of this software. *  * The names of the authors may not be used to endorse or promote products *    derived from this software without specific prior written permission. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. *  * Please cite *  * Matthias Rupp, Ewgenij Proschak, Gisbert Schneider: * A Kernel Approach to Molecular Similarity Based on Iterative Graph Similarity, * Journal of Chemical Information and Molecular Modeling, 47(6): 2280-2286, 2007,  * DOI http://dx.doi.org/10.1021/ci700274r. * * if you use this software. *  * Thanks to Samuel Wieczorek for reporting a locale related bug in parsing floating point numbers. */package info.mrupp.isoak1;import java.util.ArrayList;import java.util.List;import static java.lang.System.out;import static java.lang.System.err;/** * Sample program demonstrating the usage of the {@link IsoaKernel1 IsoaKernel1} class. *  * Run this class with the file 'example.sdf' in the current directory.  * */public class Isoak1Example {    static private void prettyPrintMatrix(Array2Float m, float prec) {        // Determine number of digits to print.        final int postdigits = (int) Math.round(Math.abs(Math.log10(prec)));        int predigits = 0; for (int i = 0; i < m.elems(); ++i) predigits = Math.max(predigits, (int)Math.ceil(Math.log10(m.get(i))));        final int totaldigits = predigits + 1 + postdigits;                // Print table.        for (int row = 0; row < m.rows(); ++row) {            for (int col = 0; col < m.cols(); ++col) {                out.printf(String.format("%%%d.%df ",totaldigits,postdigits), m.get(row,col) );            }            out.println();        }        out.println();    }        static private void prettyPrintAtomAssignment(int[] a) {        StringBuilder s1 = new StringBuilder();        StringBuilder s2 = new StringBuilder();        for (int i = 0; i < a.length; ++i) {            final int digits = (int) Math.ceil(Math.log10(Math.max(i+1, a[i]+1)));            s1.append(String.format(String.format("%%%dd ", digits), i));            s2.append(String.format(String.format("%%%dd ", digits), a[i]));        }        out.printf("%s\n%s\n\n", s1, s2);    }    static private void doKernelMatrixExperiment(List<MolecularGraph> moldb, IsoaKernel1 isoaKernel, boolean normalized, IVertexEdgeKernel vkernel, IVertexEdgeKernel ekernel, float alpha, float prec) {        final int numMolecules = moldb.size();        Array2Float kernelMatrix = new Array2Float(numMolecules, numMolecules);        out.printf("Kernel matrix (%s values, vertex kernel = %s, edge kernel = %s, alpha = %f, precision = %f):\n\n",                normalized ? "normalized" : "raw", vkernel.name(), ekernel.name(), alpha, prec);        isoaKernel.setVertexKernel(vkernel);        isoaKernel.setEdgeKernel(ekernel);        for (int row = 0; row < numMolecules; ++row) {            for (int col = row; col < numMolecules; ++col) {                final float entry = normalized ?                        isoaKernel.evalNorm(moldb.get(row), moldb.get(col), alpha, prec, null, null)                        : isoaKernel.evalRaw(moldb.get(row), moldb.get(col), alpha, prec, null, null);                kernelMatrix.set(row, col, entry);                kernelMatrix.set(col, row, entry);  // Exploit symmetry of kernel.            }        }        prettyPrintMatrix(kernelMatrix, prec);        out.println();            }        /** Just run the {@link #main #main} method of this class. */    public Isoak1Example() {}        /** Run this class with the file 'example.sdf' in the current directory. */    public static void main(String[] args) {        // Check input arguments & initialize.        String filename = "example.sdf";        IsoaKernel1 isoaKernel = new IsoaKernel1();        // Load sdf file into memory.        out.printf("Loading file '%s'... ", filename);        List<MolecularGraph> moldb = new ArrayList<MolecularGraph>();        try {            java.io.LineNumberReader sdfReader =                 new java.io.LineNumberReader(                new java.io.InputStreamReader(                new java.io.FileInputStream(                new java.io.File(filename))));            while (sdfReader.ready()) { moldb.add(new MolecularGraph(sdfReader, MolecularGraph.FileFormat.MDL_MOL)); }            sdfReader.close();        } catch (java.io.FileNotFoundException e) {            err.printf("The file '%s' could not be found.", e.getMessage());            return;             } catch (java.io.IOException e) {            err.printf("An error occurred while loading file '%s': '%s'.", filename, e.getMessage());            return;        } catch (java.text.ParseException e) {            err.printf("An error occurred while parsing file '%s': '%s'.", filename, e.getMessage());            return;        }        out.printf("Done\n\n");        // Do experiments.        final float alpha1 = 0.875f;        final float prec1 = 0.001f;        final IVertexEdgeKernel vkernel1 = VertexEdgeKernel.NONE.create(null);        final IVertexEdgeKernel ekernel1 = VertexEdgeKernel.NONE.create(null);        float[] args1 = {0.f, 0.f};        float[] args2 = {1.f, 0.f};        // Compute some kernel matrices.        doKernelMatrixExperiment(moldb, isoaKernel, false, VertexEdgeKernel.NONE.create(null), VertexEdgeKernel.NONE.create(null), alpha1, prec1);        doKernelMatrixExperiment(moldb, isoaKernel, true, VertexEdgeKernel.NONE.create(null), VertexEdgeKernel.NONE.create(null), alpha1, prec1);        doKernelMatrixExperiment(moldb, isoaKernel, true, VertexEdgeKernel.DIRACV.create(args1), VertexEdgeKernel.DIRACE.create(args1), alpha1, prec1);        doKernelMatrixExperiment(moldb, isoaKernel, true, VertexEdgeKernel.DIRACV.create(args2), VertexEdgeKernel.NONE.create(null), alpha1, prec1);               // Compute assignment details for one molecule pair.        final MolecularGraph molA = moldb.get(3);        final MolecularGraph molB = moldb.get(2);        int[] assignment1 = new int[Math.min(molA.numAtoms(), molB.numAtoms())];        Array2Float atomMatrix1 = new Array2Float(molA.numAtoms(), molB.numAtoms());        out.printf("Atom assignment and atom similarities (%s vs. %s, normalized values, vertex kernel = %s, edge kernel = %s, alpha = %f, precision = %f):\n\n",            molA.name(), molB.name(), vkernel1.name(), ekernel1.name(), alpha1, prec1);        isoaKernel.setVertexKernel(vkernel1);        isoaKernel.setEdgeKernel(ekernel1);        isoaKernel.evalNorm(molA, molB, alpha1, prec1, assignment1, atomMatrix1);        out.printf("Atom assignments:\n\n"); prettyPrintAtomAssignment(assignment1);        out.printf("Atom similarities:\n\n"); prettyPrintMatrix(atomMatrix1, prec1);                out.println("Done.");    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -