public static IAtomContainer getMoleculeWithCoordinates(IAtomContainer molecule) throws Exception { StructureDiagramGenerator sdg = new StructureDiagramGenerator(); sdg.setMolecule((IMolecule) molecule); sdg.generateCoordinates(); return sdg.getMolecule(); }
/* * Creates a structure image based on a MOL string. The image rendering is * done with CDK. */ public final Image getImage4MOL(IAtomContainer molAC) throws Exception { // creates CDK Molecule object and get the renderer // Molecule mol = prepareMolecule(molfile); IMolecule molSource = new Molecule(molAC); StructureDiagramGenerator sdg = new StructureDiagramGenerator(); sdg.setMolecule(molSource); sdg.generateCoordinates(); IMolecule mol = sdg.getMolecule(); Java2DRenderer renderer = prepareRenderer(mol); BufferedImage bimage = null; try { // paint molecule to java.awt.BufferedImage bimage = new BufferedImage(vRenderSizeX, vRenderSizeY, BufferedImage.TYPE_BYTE_INDEXED); Graphics2D g = bimage.createGraphics(); g.setBackground(Color.WHITE); g.setColor(Color.WHITE); g.fillRect(0, 0, vRenderSizeX, vRenderSizeY); renderer.paintMolecule(mol, g, new Rectangle(0, 0, vRenderSizeX, vRenderSizeY)); } catch (Exception e) { e.printStackTrace(); throw new Exception("Rendering of structure(s) failed"); } return bimage; }
/* * Creates a structure PDF based on a MOL string */ public final void writeMOL2PDFFile(IMolecule molSource, File epsFile) throws Exception { // creates CDK Molecule object and get the renderer // Molecule mol = prepareMolecule(MOLString); StructureDiagramGenerator sdg = new StructureDiagramGenerator(); sdg.setMolecule(molSource); sdg.generateCoordinates(); IMolecule mol = sdg.getMolecule(); Java2DRenderer renderer = prepareRenderer(mol); try { // paint molecule to java.awt.BufferedImage VectorGraphics vg = new PDFGraphics2D(epsFile, new Dimension(vRenderSizeX, vRenderSizeY)); Properties p = new Properties(); p.setProperty("PageSize", "A5"); vg.setProperties(p); vg.startExport(); renderer.paintMolecule(mol, vg, new Rectangle(vRenderSizeX, vRenderSizeY)); vg.endExport(); } catch (Exception e) { e.printStackTrace(); throw new Exception("PDF rendering of structure(s) failed"); } }
/** * Automatically generate coordinates if a user has provided a molecule without them. * * @param container a molecule * @return if coordinates needed to be generated * @throws CDKException coordinates could not be generated */ private boolean ensure2dLayout(IAtomContainer container) throws CDKException { if (!GeometryUtil.has2DCoordinates(container)) { StructureDiagramGenerator sdg = new StructureDiagramGenerator(); sdg.generateCoordinates(container); return true; } return false; }
private void atomLayout(IAtomContainer atomContainer) { IMolecule mol = atomContainer.getBuilder().newInstance(IMolecule.class, atomContainer); sdg.setMolecule(mol, false); try { sdg.generateCoordinates(); } catch (Exception e) { e.printStackTrace(); } }
private void display(IAtomContainer molecule) { StructureDiagramGenerator sdg = new StructureDiagramGenerator(); try { sdg.setMolecule((IAtomContainer) molecule.clone()); sdg.generateCoordinates(new Vector2d(0, 1)); } catch (Exception exc) { System.out.println("*** Exit due to an unexpected error during coordinate generation ***"); exc.printStackTrace(); } }
/** * Helper method to generate 2d coordinates when JChempaint loads a molecule * without 2D coordinates. Typically happens for SMILES strings. * * @param molecules * @throws Exception */ private static void generate2dCoordinates( final List<IAtomContainer> molecules) { final StructureDiagramGenerator sdg = new StructureDiagramGenerator(); for (int atIdx = 0; atIdx < molecules.size(); atIdx++) { final IAtomContainer mol = molecules.get(atIdx); sdg.setMolecule(mol.getBuilder().newInstance(IAtomContainer.class, mol)); try { sdg.generateCoordinates(); } catch (final Exception e) { e.printStackTrace(); } final IAtomContainer ac = sdg.getMolecule(); for (int i = 0; i < ac.getAtomCount(); i++) { mol.getAtom(i).setPoint2d(ac.getAtom(i).getPoint2d()); } } }
public static void main(final String[] args) { // String mf; // String molfile; try { // BufferedReader reader = new BufferedReader(new FileReader("renderTest.mol")); // StringBuffer sbuff = new StringBuffer(); // String line; // // while ( (line = reader.readLine()) != null ) { // sbuff.append(line + "\n"); // } // reader.close(); // molfile = sbuff.toString(); // System.out.println(molfile); // the graph SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance()); PubChemWebService pw = new PubChemWebService(); String PCID = "1148"; IAtomContainer container = pw.getSingleMol(PCID); container = AtomContainerManipulator.removeHydrogens(container); // IAtomContainer container = sp.parseSmiles("OC1C(O)=COC(CO)C1(O)"); // Render.Draw(container, "test"); IMolecule test = new Molecule(container); StructureDiagramGenerator sdg = new StructureDiagramGenerator(); sdg.setMolecule(test); sdg.generateCoordinates(); IMolecule mol = sdg.getMolecule(); // adds implicit H atoms to molecule // CDKAtomTypeMatcher matcher = CDKAtomTypeMatcher.getInstance(mol.getBuilder()); // for (IAtom atom : mol.atoms()) { // IAtomType type = matcher.findMatchingAtomType(mol, atom); // AtomTypeManipulator.configure(atom, type); // } // // CDKHydrogenAdder adder = CDKHydrogenAdder.getInstance(mol.getBuilder()); // adder.addImplicitHydrogens(mol); // AtomContainerManipulator.convertImplicitToExplicitHydrogens(mol); // detects aromaticity; used to display aromatic ring systems correctly CDKHueckelAromaticityDetector.detectAromaticity(mol); Render.Draw(mol, "test"); DisplayStructureVector test12 = new DisplayStructureVector(200, 200, "/home/swolf/", true, true); // test12.writeMOL2PNGFile(mol, "test_cdk12.png"); // test12.writeMOL2PDFFile(mol, new File("/home/swolf/test_cdk12.pdf")); test12.writeMOL2SVGFile(mol, new File("/home/swolf/" + PCID + ".svg")); // ChemRenderer crender = new ChemRenderer(); // crender.writeMOL2PNGFile(mol, new File("/home/swolf/test_new.png")); // crender.writeMOL2EPSFile(mol, new File("/home/swolf/test_new.eps")); // crender.writeMOL2PDFFile(mol, new File("/home/swolf/test_new.pdf")); // crender.writeMOL2SVGFile(molfile,new File("Output.svg")); } catch (Exception e) { System.out.println("Error: " + e.getMessage() + "."); e.printStackTrace(); } }
/** * Inserts a molecule into the current set, usually from Combobox or Insert * field, with possible shifting of the existing set. * * @param chemPaintPanel * @param molecule * @param generateCoordinates * @param shiftPanel * @throws CDKException */ public static void generateModel( final AbstractJChemPaintPanel chemPaintPanel, IAtomContainer molecule, final boolean generateCoordinates, final boolean shiftPasted) throws CDKException { if (molecule == null) { return; } final IChemModel chemModel = chemPaintPanel.getChemModel(); IAtomContainerSet moleculeSet = chemModel.getMoleculeSet(); if (moleculeSet == null) { moleculeSet = new AtomContainerSet(); } // On copy & paste on top of an existing drawn structure, prevent the // pasted section to be drawn exactly on top or to far away from the // original by shifting it to a fixed position next to it. if (shiftPasted) { double maxXCurr = Double.NEGATIVE_INFINITY; double minXPaste = Double.POSITIVE_INFINITY; for (final IAtomContainer atc : moleculeSet.atomContainers()) { // Detect the right border of the current structure.. for (final IAtom atom : atc.atoms()) { if (atom.getPoint2d().x > maxXCurr) { maxXCurr = atom.getPoint2d().x; } } // Detect the left border of the pasted structure.. for (final IAtom atom : molecule.atoms()) { if (atom.getPoint2d().x < minXPaste) { minXPaste = atom.getPoint2d().x; } } } if (maxXCurr != Double.NEGATIVE_INFINITY && minXPaste != Double.POSITIVE_INFINITY) { // Shift the pasted structure to be nicely next to the existing // one. final int MARGIN = 1; final double SHIFT = maxXCurr - minXPaste; for (final IAtom atom : molecule.atoms()) { atom.setPoint2d(new Point2d(atom.getPoint2d().x + MARGIN + SHIFT, atom.getPoint2d().y)); } } } if (generateCoordinates) { // now generate 2D coordinates final StructureDiagramGenerator sdg = new StructureDiagramGenerator(); sdg.setTemplateHandler(new TemplateHandler(moleculeSet.getBuilder())); try { sdg.setMolecule(molecule); sdg.generateCoordinates(new Vector2d(0, 1)); molecule = sdg.getMolecule(); } catch (final Exception exc) { JOptionPane.showMessageDialog(chemPaintPanel, GT._("Structure could not be generated")); throw new CDKException("Cannot depict structure"); } } if (moleculeSet.getAtomContainer(0).getAtomCount() == 0) { moleculeSet.getAtomContainer(0).add(molecule); } else { moleculeSet.addAtomContainer(molecule); } final IUndoRedoFactory undoRedoFactory = chemPaintPanel.get2DHub() .getUndoRedoFactory(); final UndoRedoHandler undoRedoHandler = chemPaintPanel.get2DHub() .getUndoRedoHandler(); if (undoRedoFactory != null) { final IUndoRedoable undoredo = undoRedoFactory .getAddAtomsAndBondsEdit(chemPaintPanel.get2DHub() .getIChemModel(), molecule, null, "Paste", chemPaintPanel.get2DHub()); undoRedoHandler.postEdit(undoredo); } chemPaintPanel.getChemModel().setMoleculeSet(moleculeSet); chemPaintPanel.updateUndoRedoControls(); chemPaintPanel.get2DHub().updateView(); }
/* * this is a test contributed by mario baseda / see bug #1610997 * @cdk.bug 1610997 */ @Test public void testModel3D_bug_1610997() throws Exception { Assume.assumeTrue(runSlowTests()); boolean notCalculatedResults = false; List inputList = new ArrayList(); //////////////////////////////////////////////////////////////////////////////////////////// // generate the input molecules. This are molecules without x, y, z coordinats String[] smiles = new String[] { "CC", "OCC", "O(C)CCC", "c1ccccc1", "C(=C)=C", "OCC=CCc1ccccc1(C=C)", "O(CC=C)CCN", "CCCCCCCCCCCCCCC", "OCC=CCO", "NCCCCN" }; SmilesParser sp = new SmilesParser(NoNotificationChemObjectBuilder.getInstance()); IAtomContainer[] atomContainer = new IAtomContainer[smiles.length]; for (int i = 0; i < smiles.length; i++) { atomContainer[i] = sp.parseSmiles(smiles[i]); inputList.add(atomContainer[i]); } System.out.println(inputList.size()); /////////////////////////////////////////////////////////////////////////////////////////// // Generate 2D coordinats for the input molecules with the Structure Diagram Generator StructureDiagramGenerator str; List resultList = new ArrayList(); for (Iterator iter = inputList.iterator(); iter.hasNext(); ) { IAtomContainer molecules = (IAtomContainer) iter.next(); str = new StructureDiagramGenerator(); str.setMolecule((IMolecule) molecules); str.generateCoordinates(); resultList.add(str.getMolecule()); } inputList = resultList; ///////////////////////////////////////////////////////////////////////////////////////////// // Delete x and y coordinats for (Iterator iter = inputList.iterator(); iter.hasNext(); ) { IAtomContainer molecules = (IAtomContainer) iter.next(); for (Iterator atom = molecules.atoms().iterator(); atom.hasNext(); ) { Atom last = (Atom) atom.next(); last.setPoint2d(null); } } //////////////////////////////////////////////////////////////////////////////////////////////////// // Test for the method Model3DBuildersWithMM2ForceField ModelBuilder3D mb3d = ModelBuilder3D.getInstance(); for (Iterator iter = inputList.iterator(); iter.hasNext(); ) { IAtomContainer molecules = (IAtomContainer) iter.next(); IMolecule mol = molecules.getBuilder().newInstance(IMolecule.class, molecules); mol = mb3d.generate3DCoordinates(mol, false); System.out.println("Calculation done"); } for (Iterator iter = inputList.iterator(); iter.hasNext(); ) { IAtomContainer molecule = (IAtomContainer) iter.next(); checkAverageBondLength(molecule); for (Iterator atom = molecule.atoms().iterator(); atom.hasNext(); ) { Atom last = (Atom) atom.next(); if (last.getPoint3d() == null) notCalculatedResults = true; } } Assert.assertFalse(notCalculatedResults); }
/** * Automatically generate coordinates if a user has provided reaction without them. * * @param rxn reaction * @throws CDKException coordinates could not be generated */ private void ensure2dLayout(IReaction rxn) throws CDKException { StructureDiagramGenerator sdg = new StructureDiagramGenerator(); sdg.setAlignMappedReaction(alignMappedReactions); sdg.generateCoordinates(rxn); }