From: <eg...@us...> - 2009-04-25 06:23:05
|
Revision: 14566 http://cdk.svn.sourceforge.net/cdk/?rev=14566&view=rev Author: egonw Date: 2009-04-25 06:23:01 +0000 (Sat, 25 Apr 2009) Log Message: ----------- Added method to generate 2D coordinates Modified Paths: -------------- cdk-xws/trunk/xws-cdk-services/src/net/bioclipse/xws/component/functions/Functions.java Added Paths: ----------- cdk-xws/trunk/xws-cdk-services/src/org/openscience/cdk/xws/services/Generate2DCoordinates.java Modified: cdk-xws/trunk/xws-cdk-services/src/net/bioclipse/xws/component/functions/Functions.java =================================================================== --- cdk-xws/trunk/xws-cdk-services/src/net/bioclipse/xws/component/functions/Functions.java 2009-04-24 09:19:37 UTC (rev 14565) +++ cdk-xws/trunk/xws-cdk-services/src/net/bioclipse/xws/component/functions/Functions.java 2009-04-25 06:23:01 UTC (rev 14566) @@ -24,16 +24,18 @@ */ package net.bioclipse.xws.component.functions; +import org.openscience.cdk.xws.services.Generate2DCoordinates; import org.openscience.cdk.xws.services.SomeCDKFunctionOne; import org.openscience.cdk.xws.services.SomeCDKFunctionTwo; public class Functions implements IFunctions { public Class[] getFunctions() { - Class[] functions = new Class[2]; + Class[] functions = new Class[3]; functions[0] = SomeCDKFunctionOne.class; functions[1] = SomeCDKFunctionTwo.class; + functions[2] = Generate2DCoordinates.class; return functions; } Added: cdk-xws/trunk/xws-cdk-services/src/org/openscience/cdk/xws/services/Generate2DCoordinates.java =================================================================== --- cdk-xws/trunk/xws-cdk-services/src/org/openscience/cdk/xws/services/Generate2DCoordinates.java (rev 0) +++ cdk-xws/trunk/xws-cdk-services/src/org/openscience/cdk/xws/services/Generate2DCoordinates.java 2009-04-25 06:23:01 UTC (rev 14566) @@ -0,0 +1,107 @@ +/* $Revision: 6707 $ $Author: egonw $ $Date: 2006-07-30 16:38:18 -0400 (Sun, 30 Jul 2006) $ + * + * Copyright (C) 2008 Egon Willighagen <eg...@us...> + * + * Contact: cdk...@li... + * + * 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 we ask is that proper credit is given for our 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.xws.services; + +import java.io.ByteArrayInputStream; + +import net.bioclipse.xws.JavaDOMTools; +import net.bioclipse.xws.component.adhoc.function.FunctionInformation; +import net.bioclipse.xws.component.adhoc.function.IFunction; +import net.bioclipse.xws.component.xmpp.process.IProcessStatus; + +import org.openscience.cdk.interfaces.IChemFile; +import org.openscience.cdk.interfaces.IMolecule; +import org.openscience.cdk.io.CMLReader; +import org.openscience.cdk.layout.StructureDiagramGenerator; +import org.openscience.cdk.libio.cml.Convertor; +import org.openscience.cdk.nonotify.NNChemFile; +import org.openscience.cdk.tools.manipulator.ChemFileManipulator; +import org.openscience.cdk.xws.ResourceAsStringTool; +import org.w3c.dom.Element; +import org.xmlcml.cml.element.CMLMolecule; + +public class Generate2DCoordinates implements IFunction { + + private static final String FUNCTION_NAME = "generate2Dcoordinates"; + private static final String FUNCTION_DESCRIPTION = "Generates a 2D schema for the input molecule."; + private static final String FUNCTION_DETAILS = "A full-connected molecular structure in CML."; + + private final static String SCHEMATA = + ResourceAsStringTool.getAsString("org/openscience/cdk/xws/schema/cml.xml"); + + private static StructureDiagramGenerator sdg; + private static Convertor convertor = new Convertor(true, null); + + private FunctionInformation info = null; + + public FunctionInformation getFunctionInformation() { + if (info == null) { + info = new FunctionInformation( + FUNCTION_NAME, + FUNCTION_DESCRIPTION, + FUNCTION_DETAILS, + SCHEMATA, + SCHEMATA, + true + ); + } + + return info; + } + + public void run(IProcessStatus ps, Element input) { + Element output = null; + if ("http://www.xml-cml.org/schema".equals(input.getNamespaceURI()) && + "molecule".equals(input.getLocalName())) { + String cmlString = JavaDOMTools.w3cElementToString(input); + System.out.println("CML: " + cmlString); + CMLReader reader = new CMLReader(new ByteArrayInputStream(cmlString.getBytes())); + try { + IChemFile file = (IChemFile)reader.read(new NNChemFile()); + if (sdg == null) { + sdg = new StructureDiagramGenerator(); + } + IMolecule mol = file.getBuilder().newMolecule( + ChemFileManipulator.getAllAtomContainers(file).get(0) + ); + sdg.setMolecule(mol); + sdg.generateCoordinates(); + mol = sdg.getMolecule(); + CMLMolecule cmlMol = convertor.cdkAtomContainerToCMLMolecule(mol); + String outputString = cmlMol.toXML(); + output = JavaDOMTools.string2Element(outputString); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + ps.setError(e.getMessage()); + } + } else { + System.out.println("Expected CML but got: " + input.getNamespaceURI()); + ps.setError("Expected CML input."); + } + + ps.setResult(output, "Done"); + } +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |