org.openscience.cdk.layout.StructureDiagramGenerator Java Examples

The following examples show how to use org.openscience.cdk.layout.StructureDiagramGenerator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Structure2DComponentAWT.java    From mzmine3 with GNU General Public License v2.0 6 votes vote down vote up
public Structure2DComponentAWT(IAtomContainer container, Font font) throws CDKException {
  molecule = container;

  // Suppress the hydrogens
  AtomContainerManipulator.suppressHydrogens(molecule);

  // If the model has no coordinates, let's generate them
  if (!GeometryUtil.has2DCoordinates(molecule)) {
    StructureDiagramGenerator sdg = new StructureDiagramGenerator();
    sdg.setMolecule(molecule, false);
    sdg.generateCoordinates();
  }

  // Generators make the image elements
  List<IGenerator<IAtomContainer>> generators = new ArrayList<IGenerator<IAtomContainer>>();
  generators.add(new BasicSceneGenerator());
  generators.add(new StandardGenerator(font));

  // Renderer needs to have a toolkit-specific font manager
  renderer = new AtomContainerRenderer(generators, new AWTFontManager());

  // Set default atom colors for the renderer
  RendererModel rendererModel = renderer.getRenderer2DModel();
  rendererModel.set(StandardGenerator.AtomColor.class, new CDK2DAtomColors());
}
 
Example #2
Source File: Structure2DComponent.java    From mzmine3 with GNU General Public License v2.0 6 votes vote down vote up
public Structure2DComponent(IAtomContainer container, Font font) throws CDKException {
  molecule = container;

  // Suppress the hydrogens
  AtomContainerManipulator.suppressHydrogens(molecule);

  // If the model has no coordinates, let's generate them
  if (!GeometryUtil.has2DCoordinates(molecule)) {
    StructureDiagramGenerator sdg = new StructureDiagramGenerator();
    sdg.setMolecule(molecule, false);
    sdg.generateCoordinates();
  }

  // Generators make the image elements
  List<IGenerator<IAtomContainer>> generators = new ArrayList<IGenerator<IAtomContainer>>();
  generators.add(new BasicSceneGenerator());
  generators.add(new StandardGenerator(font));

  // Renderer needs to have a toolkit-specific font manager
  renderer = new AtomContainerRenderer(generators, new AWTFontManager());

  // Set default atom colors for the renderer
  RendererModel rendererModel = renderer.getRenderer2DModel();
  rendererModel.set(StandardGenerator.AtomColor.class, new CDK2DAtomColors());
}
 
Example #3
Source File: Structure2DComponent.java    From mzmine2 with GNU General Public License v2.0 6 votes vote down vote up
public Structure2DComponent(IAtomContainer container, Font font) throws CDKException {
  molecule = container;

  // Suppress the hydrogens
  AtomContainerManipulator.suppressHydrogens(molecule);

  // If the model has no coordinates, let's generate them
  if (!GeometryUtil.has2DCoordinates(molecule)) {
    StructureDiagramGenerator sdg = new StructureDiagramGenerator();
    sdg.setMolecule(molecule, false);
    sdg.generateCoordinates();
  }

  // Generators make the image elements
  List<IGenerator<IAtomContainer>> generators = new ArrayList<IGenerator<IAtomContainer>>();
  generators.add(new BasicSceneGenerator());
  generators.add(new StandardGenerator(font));

  // Renderer needs to have a toolkit-specific font manager
  renderer = new AtomContainerRenderer(generators, new AWTFontManager());

  // Set default atom colors for the renderer
  RendererModel rendererModel = renderer.getRenderer2DModel();
  rendererModel.set(StandardGenerator.AtomColor.class, new CDK2DAtomColors());
}
 
Example #4
Source File: LayoutCheck.java    From ReactionDecoder with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 *
 * @param mol
 * @return
 */
public static IChemModel getChemModelWithMoleculeWithLayoutCheck(IAtomContainer mol) {
    IChemModel chemModel = new ChemModel();
    chemModel.setMoleculeSet(partitionIntoMolecules(mol));
    for (IAtomContainer molecule : getAllAtomContainers(chemModel.getMoleculeSet())) {
        if (has2DCoordinates(molecule)) {
            try {
                StructureDiagramGenerator sdg = new StructureDiagramGenerator(new AtomContainer(molecule));
                sdg.generateCoordinates();
                chemModel = (IChemModel) sdg.getMolecule();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return chemModel;
}
 
Example #5
Source File: MDLFileReader.java    From ReactionDecoder with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Returns molecule with new layout
 *
 * @return cleaned model
 */
public IChemModel getChemModelWithMoleculeWithLayoutCheck() {
    IChemModel chemModel = new ChemModel();
    chemModel.setMoleculeSet(partitionIntoMolecules(molecule));
    for (IAtomContainer ac : getAllAtomContainers(chemModel.getMoleculeSet())) {
        if (!has2DCoordinates(ac)) {
            try {
                IAtomContainer mol = ac.getBuilder().newInstance(IAtomContainer.class, ac);
                percieveAtomTypesAndConfigureAtoms(mol);
                StructureDiagramGenerator sdg = new StructureDiagramGenerator(new AtomContainer(mol));
                sdg.generateCoordinates();
                ac = sdg.getMolecule();
            } catch (IllegalArgumentException | CDKException e) {
                e.printStackTrace();
            }
        }
    }
    return chemModel;
}
 
Example #6
Source File: SiriusCompound.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Method returns image generated from Chemical Structure IAtomContainer Better to use 3:2
 * relation of width:height
 * 
 * @param width of the image
 * @param height of the image
 * @return new Image object
 */
public Image generateImage(int width, int height) {
  IAtomContainer molecule = this.annotation.getChemicalStructure();
  if (molecule == null)
    return null;

  /* Form an area for future image */
  Rectangle drawArea = new Rectangle(width, height);
  Image image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

  /* Transform AtomContainer into image */
  StructureDiagramGenerator sdg = new StructureDiagramGenerator();
  try {
    sdg.setMolecule(molecule, false);
    sdg.generateCoordinates();
    List generators = new ArrayList();
    generators.add(new BasicSceneGenerator());
    generators.add(new BasicBondGenerator());
    generators.add(new BasicAtomGenerator());
    // the renderer needs to have a toolkit-specific font manager
    AtomContainerRenderer renderer = new AtomContainerRenderer(generators, new AWTFontManager());
    renderer.setup(molecule, drawArea);

    Graphics2D g2 = (Graphics2D) image.getGraphics();
    g2.setColor(Color.WHITE);
    g2.fillRect(0, 0, width, height);
    renderer.paint(molecule, new AWTDrawVisitor(g2));
    return image;
  } catch (Exception ex) {
    logger.info("Exception during ImageIcon construction occured");
  }
  return null;
}
 
Example #7
Source File: Structure2DComponentAWT.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
public Structure2DComponentAWT(String structure) throws CDKException, IOException {

    // Create a silend CDK builder
    IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();

    // Create a new molecule instance
    molecule = builder.newInstance(IAtomContainer.class);

    // Load the structure into the molecule
    MDLV2000Reader molReader = new MDLV2000Reader(new StringReader(structure));
    molReader.read(molecule);
    molReader.close();

    // Suppress the hydrogens
    AtomContainerManipulator.suppressHydrogens(molecule);

    // If the model has no coordinates, let's generate them
    if (!GeometryUtil.has2DCoordinates(molecule)) {
      StructureDiagramGenerator sdg = new StructureDiagramGenerator();
      sdg.setMolecule(molecule, false);
      sdg.generateCoordinates();
    }

    // Generators make the image elements
    Font font = new Font("Verdana", Font.PLAIN, 14);
    List<IGenerator<IAtomContainer>> generators = new ArrayList<IGenerator<IAtomContainer>>();
    generators.add(new BasicSceneGenerator());
    generators.add(new StandardGenerator(font));

    // Renderer needs to have a toolkit-specific font manager
    renderer = new AtomContainerRenderer(generators, new AWTFontManager());

    // Set default atom colors for the renderer
    RendererModel rendererModel = renderer.getRenderer2DModel();
    rendererModel.set(StandardGenerator.AtomColor.class, new CDK2DAtomColors());

  }
 
Example #8
Source File: Structure2DComponent.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
public Structure2DComponent(String structure) throws CDKException, IOException {

    // Create a silend CDK builder
    IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();

    // Create a new molecule instance
    molecule = builder.newInstance(IAtomContainer.class);

    // Load the structure into the molecule
    MDLV2000Reader molReader = new MDLV2000Reader(new StringReader(structure));
    molReader.read(molecule);
    molReader.close();

    // Suppress the hydrogens
    AtomContainerManipulator.suppressHydrogens(molecule);

    // If the model has no coordinates, let's generate them
    if (!GeometryUtil.has2DCoordinates(molecule)) {
      StructureDiagramGenerator sdg = new StructureDiagramGenerator();
      sdg.setMolecule(molecule, false);
      sdg.generateCoordinates();
    }

    // Generators make the image elements
    Font font = new Font("Verdana", Font.PLAIN, 14);
    List<IGenerator<IAtomContainer>> generators = new ArrayList<IGenerator<IAtomContainer>>();
    generators.add(new BasicSceneGenerator());
    generators.add(new StandardGenerator(font));

    // Renderer needs to have a toolkit-specific font manager
    renderer = new AtomContainerRenderer(generators, new AWTFontManager());

    // Set default atom colors for the renderer
    RendererModel rendererModel = renderer.getRenderer2DModel();
    rendererModel.set(StandardGenerator.AtomColor.class, new CDK2DAtomColors());


  }
 
Example #9
Source File: SiriusCompound.java    From mzmine2 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Method returns image generated from Chemical Structure IAtomContainer Better to use 3:2
 * relation of width:height
 * 
 * @param width of the image
 * @param height of the image
 * @return new Image object
 */
public Image generateImage(int width, int height) {
  IAtomContainer molecule = this.annotation.getChemicalStructure();
  if (molecule == null)
    return null;

  /* Form an area for future image */
  Rectangle drawArea = new Rectangle(width, height);
  Image image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

  /* Transform AtomContainer into image */
  StructureDiagramGenerator sdg = new StructureDiagramGenerator();
  try {
    sdg.setMolecule(molecule, false);
    sdg.generateCoordinates();
    List generators = new ArrayList();
    generators.add(new BasicSceneGenerator());
    generators.add(new BasicBondGenerator());
    generators.add(new BasicAtomGenerator());
    // the renderer needs to have a toolkit-specific font manager
    AtomContainerRenderer renderer = new AtomContainerRenderer(generators, new AWTFontManager());
    renderer.setup(molecule, drawArea);

    Graphics2D g2 = (Graphics2D) image.getGraphics();
    g2.setColor(Color.WHITE);
    g2.fillRect(0, 0, width, height);
    renderer.paint(molecule, new AWTDrawVisitor(g2));
    return image;
  } catch (Exception ex) {
    logger.info("Exception during ImageIcon construction occured");
  }
  return null;
}
 
Example #10
Source File: Structure2DComponent.java    From mzmine2 with GNU General Public License v2.0 5 votes vote down vote up
public Structure2DComponent(String structure) throws CDKException, IOException {

    // Create a silend CDK builder
    IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();

    // Create a new molecule instance
    molecule = builder.newInstance(IAtomContainer.class);

    // Load the structure into the molecule
    MDLV2000Reader molReader = new MDLV2000Reader(new StringReader(structure));
    molReader.read(molecule);
    molReader.close();

    // Suppress the hydrogens
    AtomContainerManipulator.suppressHydrogens(molecule);

    // If the model has no coordinates, let's generate them
    if (!GeometryUtil.has2DCoordinates(molecule)) {
      StructureDiagramGenerator sdg = new StructureDiagramGenerator();
      sdg.setMolecule(molecule, false);
      sdg.generateCoordinates();
    }

    // Generators make the image elements
    Font font = new Font("Verdana", Font.PLAIN, 14);
    List<IGenerator<IAtomContainer>> generators = new ArrayList<IGenerator<IAtomContainer>>();
    generators.add(new BasicSceneGenerator());
    generators.add(new StandardGenerator(font));

    // Renderer needs to have a toolkit-specific font manager
    renderer = new AtomContainerRenderer(generators, new AWTFontManager());

    // Set default atom colors for the renderer
    RendererModel rendererModel = renderer.getRenderer2DModel();
    rendererModel.set(StandardGenerator.AtomColor.class, new CDK2DAtomColors());

  }
 
Example #11
Source File: FingerprintGenerator.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 *
 * @param mol
 * @return
 * @throws CDKException
 */
@Override
public synchronized BitSet getFingerprint(IAtomContainer mol) throws CDKException {
    if (!has2DCoordinates(mol)) {
        StructureDiagramGenerator structureDiagramGenerator = new StructureDiagramGenerator();
        structureDiagramGenerator.setMolecule(mol, true);
        if (isConnected(mol)) {
            structureDiagramGenerator.generateCoordinates();
            mol = structureDiagramGenerator.getMolecule();
        } else {
            LOGGER.debug("Disconnected components needs to be layout separately");
        }
    }
    return fingerprinter.getBitFingerprint(mol).asBitSet();
}
 
Example #12
Source File: LayoutCheck.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 *
 * @param mol
 * @return
 */
public static IAtomContainer getMoleculeWithLayoutCheck(IAtomContainer mol) {
    if (!has2DCoordinates(mol)) {
        try {
            StructureDiagramGenerator sdg = new StructureDiagramGenerator(new AtomContainer(mol));
            sdg.generateCoordinates();
            mol = sdg.getMolecule();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return mol;
}
 
Example #13
Source File: MDLFileReader.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns moecule with cleaned Layout
 *
 * @return cleaned Layout molecule
 */
public IAtomContainer getMoleculeWithLayoutCheck() {
    if (!has2DCoordinates(molecule)) {
        try {
            StructureDiagramGenerator sdg = new StructureDiagramGenerator(new AtomContainer(molecule));
            sdg.generateCoordinates();
            molecule = sdg.getMolecule();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return molecule;
}
 
Example #14
Source File: TestUtility.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 *
 * @param AtomContainer
 * @return
 */
protected IAtomContainer layout(IAtomContainer AtomContainer) {
    try {
        StructureDiagramGenerator sdg = new StructureDiagramGenerator();
        sdg.setMolecule(AtomContainer, true);
        sdg.generateCoordinates();
        return sdg.getMolecule();
    } catch (CDKException e) {
        return AtomContainer;
    }
}
 
Example #15
Source File: LinearMoleculeSetLayout.java    From ReactionDecoder with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 *
 * @param atomContainerSet
 * @param moleculeSetAxis
 * @return
 */
@Override
public BoundsTree layout(IAtomContainerSet atomContainerSet, Vector2d moleculeSetAxis) {
    int bondLength = params.bondLength;
    int molGap = 2 * params.plusGap;

    // if the molecules don't have labels, need to label them
    int molLabel = 0;

    String rootLabel = atomContainerSet.getID();
    boundsTree = new BoundsTree(rootLabel);
    Point2d curr = new Point2d(0, 0);
    int i = 0;
    for (IAtomContainer molecule : atomContainerSet.atomContainers()) {
        if (!has2DCoordinates(molecule)) {
            //Added by Asad for 3D to 2D

            StructureDiagramGenerator sdg
                    = new StructureDiagramGenerator(new AtomContainer(molecule));
            try {
                sdg.generateCoordinates();
            } catch (CDKException ex) {
                getLogger(LinearMoleculeSetLayout.class.getName()).log(SEVERE, null, ex);
            }
            molecule = sdg.getMolecule();

        }
        invert(molecule);
        if (params.alignMolecules && moleculeAxis != null) {
            align(molecule, moleculeAxis);
        }
        scaleMolecule(molecule,
                getScaleFactor(molecule, bondLength));
        Rectangle2D bounds = getRectangle2D(molecule);

        double boundsWidth = bounds.getWidth();
        double halfBoundsWidth = boundsWidth / 2;

        curr.scaleAdd(halfBoundsWidth, moleculeSetAxis, curr);
        translateTo(molecule, curr.x, curr.y, bounds);
        curr.scaleAdd(halfBoundsWidth, moleculeSetAxis, curr);
        curr.scaleAdd(molGap, moleculeSetAxis, curr);

        String moleculeLabel = molecule.getID();
        if (moleculeLabel == null || moleculeLabel.isEmpty()) {
            moleculeLabel = "mol" + valueOf(molLabel);
            molLabel++;
        } else {
            moleculeLabel += ":" + i;
        }

        boundsTree.add(rootLabel + "_" + moleculeLabel, bounds);
        i++;
        shouldInvert = true;
    }
    return boundsTree;
}
 
Example #16
Source File: SingleMoleculeLayout.java    From ReactionDecoder with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 *
 * @param params
 * @param forceRelayout
 */
public SingleMoleculeLayout(Params params, boolean forceRelayout) {
    setParams(params);
    sdg = new StructureDiagramGenerator();
    this.forceRelayout = forceRelayout;
}