From: <sh...@us...> - 2008-11-27 18:05:47
|
Revision: 13354 http://cdk.svn.sourceforge.net/cdk/?rev=13354&view=rev Author: shk3 Date: 2008-11-27 18:05:41 +0000 (Thu, 27 Nov 2008) Log Message: ----------- added create smiles Modified Paths: -------------- jchempaint/trunk/src/main/org/openscience/jchempaint/resources/JChemPaintResources.properties Added Paths: ----------- jchempaint/trunk/src/main/org/openscience/jchempaint/action/CreateSmilesAction.java jchempaint/trunk/src/main/org/openscience/jchempaint/dialog/TextViewDialog.java Added: jchempaint/trunk/src/main/org/openscience/jchempaint/action/CreateSmilesAction.java =================================================================== --- jchempaint/trunk/src/main/org/openscience/jchempaint/action/CreateSmilesAction.java (rev 0) +++ jchempaint/trunk/src/main/org/openscience/jchempaint/action/CreateSmilesAction.java 2008-11-27 18:05:41 UTC (rev 13354) @@ -0,0 +1,127 @@ +/* + * $RCSfile$ + * $Author: egonw $ + * $Date: 2007-01-04 17:26:00 +0000 (Thu, 04 Jan 2007) $ + * $Revision: 7634 $ + * + * Copyright (C) 1997-2008 Christoph Steinbeck, Stefan Kuhn + * + * 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.jchempaint.action; + +import java.awt.event.ActionEvent; +import java.io.IOException; +import java.util.Iterator; + +import javax.swing.JFrame; + +import org.openscience.cdk.Molecule; +import org.openscience.cdk.exception.CDKException; +import org.openscience.cdk.geometry.GeometryTools; +import org.openscience.cdk.interfaces.IAtomContainer; +import org.openscience.cdk.interfaces.IChemModel; +import org.openscience.cdk.layout.HydrogenPlacer; +import org.openscience.cdk.smiles.SmilesGenerator; +import org.openscience.cdk.tools.CDKHydrogenAdder; +import org.openscience.cdk.tools.manipulator.AtomContainerManipulator; +import org.openscience.cdk.tools.manipulator.ChemModelManipulator; +import org.openscience.jchempaint.dialog.TextViewDialog; + + +/** + * Creates a SMILES from the current model + * + */ +public class CreateSmilesAction extends JCPAction +{ + + private static final long serialVersionUID = -4886982931009753342L; + + TextViewDialog dialog = null; + JFrame frame = null; + + public void actionPerformed(ActionEvent e) + { + logger.debug("Trying to create smile: ", type); + if (dialog == null) + { + dialog = new TextViewDialog(frame, "SMILES", null, false, 40, 2); + } + String smiles = ""; + String chiralsmiles =""; + try + { + smiles=getSmiles(jcpPanel.getChemModel()); + chiralsmiles=getChiralSmiles(jcpPanel.getChemModel()); + dialog.setMessage("Generated SMILES:", "SMILES: "+smiles+System.getProperty("line.separator")+"chiral SMILES: "+chiralsmiles); + } catch (Exception exception) + { + String message = "Error while creating SMILES: " + exception.getMessage(); + logger.error(message); + logger.debug(exception); + dialog.setMessage("Error", message); + } + dialog.setVisible(true); + } + + public static String getSmiles(IChemModel model) throws CDKException, ClassNotFoundException, IOException, CloneNotSupportedException{ + String smiles=""; + SmilesGenerator generator = new SmilesGenerator(); + Iterator<IAtomContainer> containers = ChemModelManipulator.getAllAtomContainers(model).iterator(); + while (containers.hasNext()) { + IAtomContainer container = (IAtomContainer)containers.next(); + Molecule molecule = new Molecule(container); + smiles += generator.createSMILES(molecule); + if (containers.hasNext()) { + smiles += "."; + } + } + return smiles; + } + + public static String getChiralSmiles(IChemModel model) throws CDKException, ClassNotFoundException, IOException, CloneNotSupportedException { + String chiralsmiles=""; + SmilesGenerator generator = new SmilesGenerator(); + Iterator<IAtomContainer> containers = ChemModelManipulator.getAllAtomContainers(model).iterator(); + while (containers.hasNext()) { + IAtomContainer container = (IAtomContainer)containers.next(); + Molecule molecule = new Molecule(container); + Molecule moleculewithh=(Molecule)molecule.clone(); + CDKHydrogenAdder.getInstance(moleculewithh.getBuilder()).addImplicitHydrogens(moleculewithh); + AtomContainerManipulator.convertImplicitToExplicitHydrogens(moleculewithh); + double bondLength = GeometryTools.getBondLengthAverage(container); + new HydrogenPlacer().placeHydrogens2D(moleculewithh, bondLength); + boolean[] bool=new boolean[moleculewithh.getBondCount()]; + for(int i=0;i<bool.length;i++){ + if (generator.isValidDoubleBondConfiguration(moleculewithh, moleculewithh.getBond(i))) + bool[i]=true; + } + chiralsmiles += generator.createChiralSMILES(moleculewithh,bool); + if (containers.hasNext()) { + chiralsmiles += "."; + } + } + return chiralsmiles; + } + +} + Property changes on: jchempaint/trunk/src/main/org/openscience/jchempaint/action/CreateSmilesAction.java ___________________________________________________________________ Added: svn:executable + * Added: jchempaint/trunk/src/main/org/openscience/jchempaint/dialog/TextViewDialog.java =================================================================== --- jchempaint/trunk/src/main/org/openscience/jchempaint/dialog/TextViewDialog.java (rev 0) +++ jchempaint/trunk/src/main/org/openscience/jchempaint/dialog/TextViewDialog.java 2008-11-27 18:05:41 UTC (rev 13354) @@ -0,0 +1,147 @@ +/* + * $RCSfile$ + * $Author: egonw $ + * $Date: 2007-01-04 17:26:00 +0000 (Thu, 04 Jan 2007) $ + * $Revision: 7634 $ + * + * Copyright (C) 1997-2008 Stefan Kuhn + * + * 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.jchempaint.dialog; + +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.awt.FlowLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTextArea; + +/** + * A simple text viewing dialog for general use. + * + * @cdk.module jchempaint + * @cdk.created 2003-08-24 + */ +public class TextViewDialog extends JDialog { + + private static final long serialVersionUID = -1900643115385413976L; + + private JTextArea textArea; + private JLabel textCaption; + + /** + * @see #TextViewDialog(JFrame, String, Dimension, boolean, int, int) + */ + public TextViewDialog(JFrame fr, String title) { + this(fr, title, null); + } + + /** + * @see #TextViewDialog(JFrame, String, Dimension, boolean, int, int) + */ + public TextViewDialog(JFrame fr, String title, Dimension dim) { + this(fr, title, dim, true); + } + + /** + * @see #TextViewDialog(JFrame, String, Dimension, boolean, int, int) + */ + public TextViewDialog(JFrame fr, String title, Dimension dim, boolean wrap) { + this(fr, title, dim, wrap, 20, 60); + } + + /** + * Constructs a new JTextViewDialog. + * + * @param fr Parent JFrame + * @param title String that will appear in the title bar + * @param dim Dimension of the dialog + * @param wrap If true, then the lines will be wrapped + * @param width Number of chars per line + * @param height Number of lines + */ + public TextViewDialog(JFrame fr, String title, Dimension dim, boolean wrap, + int width, int height) { + super(fr, title, true); + textArea = new JTextArea(width,height); + textArea.setEditable(false); + if (wrap) { + textArea.setLineWrap(wrap); + textArea.setWrapStyleWord(true); + } + JScrollPane scroller = new JScrollPane(); + if (dim != null) + scroller.setPreferredSize(dim); + else + scroller.setPreferredSize(new Dimension(400,200)); + scroller.getViewport().add(textArea); + + JPanel textViewer = new JPanel(new BorderLayout()); + //textViewer.setAlignmentX(LEFT_ALIGNMENT); + textViewer.add(scroller, BorderLayout.CENTER); + + JPanel buttonPanel = new JPanel(); + buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); + JButton ok = new JButton("OK"); + ok.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + OKPressed(); + } + }); + buttonPanel.add(ok); + getRootPane().setDefaultButton(ok); + + JPanel container = new JPanel(); + container.setLayout(new BorderLayout()); + container.validate(); + + textCaption = new JLabel(""); + + container.add(textCaption, BorderLayout.NORTH); + container.add(textViewer, BorderLayout.CENTER); + container.add(buttonPanel, BorderLayout.SOUTH); + + getContentPane().add(container); + pack(); + } + + public void OKPressed() { + this.setVisible(false); + } + + public void setText(String text) { + textArea.setText(text); + } + + public void setMessage(String caption, String text) { + textCaption.setText(caption); + textArea.setText(text); + } + +} Property changes on: jchempaint/trunk/src/main/org/openscience/jchempaint/dialog/TextViewDialog.java ___________________________________________________________________ Added: svn:executable + * Modified: jchempaint/trunk/src/main/org/openscience/jchempaint/resources/JChemPaintResources.properties =================================================================== --- jchempaint/trunk/src/main/org/openscience/jchempaint/resources/JChemPaintResources.properties 2008-11-27 17:11:30 UTC (rev 13353) +++ jchempaint/trunk/src/main/org/openscience/jchempaint/resources/JChemPaintResources.properties 2008-11-27 18:05:41 UTC (rev 13354) @@ -180,7 +180,7 @@ clearValidateAction=org.openscience.cdk.applications.jchempaint.action.ValidateAction@clear toggleBasicValAction=org.openscience.cdk.applications.jchempaint.action.ValidateAction@toggleBasic toggleCDKValAction=org.openscience.cdk.applications.jchempaint.action.ValidateAction@toggleCDK -createSMILESAction=org.openscience.cdk.applications.jchempaint.action.CreateSmilesAction +createSMILESAction=org.openscience.jchempaint.action.CreateSmilesAction createInChIAction=org.openscience.cdk.applications.jchempaint.action.CreateInChIAction createIChIAction=org.openscience.cdk.applications.jchempaint.action.CreateIChIAction createIUPACAction=org.openscience.cdk.applications.jchempaint.action.CreateIUPACNameAction This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |