1 /* Copyright (C) 1997-2007  The Chemistry Development Kit (CDK) project
2  *
3  * Contact: cdk-devel@lists.sourceforge.net
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public License
7  * as published by the Free Software Foundation; either version 2.1
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package org.openscience.cdk.structgen;
21 
22 import java.awt.event.ActionEvent;
23 
24 import javax.swing.AbstractAction;
25 import javax.vecmath.Vector2d;
26 
27 import org.openscience.cdk.AtomContainer;
28 import org.openscience.cdk.interfaces.IAtomContainer;
29 import org.openscience.cdk.layout.StructureDiagramGenerator;
30 import org.openscience.cdk.templates.TestMoleculeFactory;
31 import org.openscience.cdk.tools.manipulator.MolecularFormulaManipulator;
32 
33 /**
34  * @cdk.module test-structgen
35  */
36 public class SingleStructureRandomGeneratorTest {
37 
38     String                         mf;
39     SingleStructureRandomGenerator ssrg;
40 
SingleStructureRandomGeneratorTest()41     public SingleStructureRandomGeneratorTest() throws Exception {
42         System.out.println("Instantiating MoleculeListViewer");
43         System.out.println("Instantiating SingleStructureRandomGenerator");
44         ssrg = new SingleStructureRandomGenerator();
45         System.out.println("Assining unbonded set of atoms");
46         AtomContainer ac = getBunchOfUnbondedAtoms();
47         mf = MolecularFormulaManipulator.getString(MolecularFormulaManipulator.getMolecularFormula(ac));
48         System.out.println("Molecular Formula is: " + mf);
49         ssrg.setAtomContainer(ac);
50     }
51 
showIt(IAtomContainer molecule, String name)52     private boolean showIt(IAtomContainer molecule, String name) throws Exception {
53         StructureDiagramGenerator sdg = new StructureDiagramGenerator();
54         sdg.setMolecule((IAtomContainer) molecule.clone());
55         sdg.generateCoordinates(new Vector2d(0, 1));
56         return true;
57     }
58 
getBunchOfUnbondedAtoms()59     private AtomContainer getBunchOfUnbondedAtoms() {
60         IAtomContainer molecule = TestMoleculeFactory.makeAlphaPinene();
61         fixCarbonHCount(molecule);
62         molecule.removeAllElectronContainers();
63         return (AtomContainer) molecule;
64     }
65 
fixCarbonHCount(IAtomContainer mol)66     private void fixCarbonHCount(IAtomContainer mol) {
67         /*
68          * the following line are just a quick fix for this particluar
69          * carbon-only molecule until we have a proper hydrogen count
70          * configurator
71          */
72         double bondCount = 0;
73         org.openscience.cdk.interfaces.IAtom atom;
74         for (int f = 0; f < mol.getAtomCount(); f++) {
75             atom = mol.getAtom(f);
76             bondCount = mol.getBondOrderSum(atom);
77             if (bondCount > 4) System.out.println("bondCount: " + bondCount);
78             atom.setImplicitHydrogenCount(4 - (int) bondCount
79                     - (atom.getCharge() == null ? 0 : atom.getCharge().intValue()));
80         }
81     }
82 
83     class MoreAction extends AbstractAction {
84 
85         private static final long serialVersionUID = -7405706755621468840L;
86 
87         @Override
actionPerformed(ActionEvent e)88         public void actionPerformed(ActionEvent e) {
89             try {
90                 IAtomContainer ac = ssrg.generate();
91                 showIt(ac, "Randomly generated for " + mf);
92             } catch (Exception ex) {
93                 System.err.println(ex.getMessage());
94             }
95         }
96     }
97 }
98