private static void replaceReferencesWithClones(final IChemModel chemModel)
			throws CDKException {
		// we make references in products/reactants clones, since same compounds
		// in different reactions need separate layout (different positions etc)
		if (chemModel.getReactionSet() != null) {
			for (final IReaction reaction : chemModel.getReactionSet()
					.reactions()) {
				int i = 0;
				final IAtomContainerSet products = reaction.getProducts();
				for (final IAtomContainer product : products.atomContainers()) {
					try {
						products.replaceAtomContainer(i, product.clone());
					} catch (final CloneNotSupportedException e) {
					}
					i++;
				}
				i = 0;
				final IAtomContainerSet reactants = reaction.getReactants();
				for (final IAtomContainer reactant : reactants.atomContainers()) {
					try {
						reactants.replaceAtomContainer(i, reactant.clone());
					} catch (final CloneNotSupportedException e) {
					}
					i++;
				}
			}
		}
	}
  /**
   * Get the container which the bond is found on resonance from a IMolecule. It is based on looking
   * if the order of the bond changes. Return null is any is found.
   *
   * @param molecule The IMolecule to analyze
   * @param bond The IBond
   * @return The container with the bond
   */
  @TestMethod("testGetContainer_IMolecule_IBond")
  public IAtomContainer getContainer(IMolecule molecule, IBond bond) {
    IAtomContainerSet setOfCont = getContainers(molecule);
    if (setOfCont == null) return null;

    for (IAtomContainer container : setOfCont.atomContainers()) {
      if (container.contains(bond)) return container;
    }

    return null;
  }
Ejemplo n.º 3
0
  /**
   * Generates a shortest path based BitSet fingerprint for the given AtomContainer.
   *
   * @param ac The AtomContainer for which a fingerprint is generated
   * @exception CDKException if there error in aromaticity perception or other CDK functions
   * @return A {@link BitSet} representing the fingerprint
   */
  @Override
  public IBitFingerprint getBitFingerprint(IAtomContainer ac) throws CDKException {

    IAtomContainer atomContainer = null;
    try {
      atomContainer = (IAtomContainer) ac.clone();
    } catch (CloneNotSupportedException ex) {
      logger.error("Failed to clone the molecule:", ex);
    }
    Aromaticity.cdkLegacy().apply(atomContainer);
    BitSet bitSet = new BitSet(fingerprintLength);
    if (!ConnectivityChecker.isConnected(atomContainer)) {
      IAtomContainerSet partitionedMolecules =
          ConnectivityChecker.partitionIntoMolecules(atomContainer);
      for (IAtomContainer container : partitionedMolecules.atomContainers()) {
        addUniquePath(container, bitSet);
      }
    } else {
      addUniquePath(atomContainer, bitSet);
    }
    return new BitSetFingerprint(bitSet);
  }
	/**
	 * 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();
	}
Ejemplo n.º 5
0
 private List<IAtomContainer> toList(IAtomContainerSet set) {
   return FluentIterable.from(set.atomContainers()).toList();
 }