Skip to content

Commit

Permalink
Keep stereochemistry correct through layout changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmay committed Sep 6, 2017
1 parent 46e0539 commit e64d095
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import javax.vecmath.Point2d;
import javax.vecmath.Vector2d;

import com.google.common.collect.FluentIterable;
import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.DefaultChemObjectBuilder;
import org.openscience.cdk.atomtype.CDKAtomTypeMatcher;
Expand Down Expand Up @@ -67,6 +68,8 @@
import org.openscience.cdk.layout.StructureDiagramGenerator;
import org.openscience.cdk.renderer.generators.IGenerator;
import org.openscience.cdk.renderer.selection.IChemObjectSelection;
import org.openscience.cdk.stereo.Projection;
import org.openscience.cdk.stereo.StereoElementFactory;
import org.openscience.cdk.tools.SaturationChecker;
import org.openscience.cdk.tools.manipulator.AtomContainerManipulator;
import org.openscience.cdk.tools.manipulator.AtomContainerSetManipulator;
Expand Down Expand Up @@ -1248,14 +1251,24 @@ public static void avoidOverlap(IChemModel chemModel){
// OK
public void cleanup() {
Map<IAtom, Point2d[]> coords = new HashMap<IAtom, Point2d[]>();
for (IAtomContainer container : ChemModelManipulator
.getAllAtomContainers(chemModel)) {
Map<IBond, IBond.Stereo> stereo = new HashMap<>();
for (IAtomContainer container : ChemModelManipulator.getAllAtomContainers(chemModel)) {

// ensure current stereo from 2D is set
container.setStereoElements(StereoElementFactory.using2DCoordinates(container)
.interpretProjections(Projection.Haworth, Projection.Chair)
.createAll());

for (IAtom atom : container.atoms()) {
Point2d[] coordsforatom = new Point2d[2];
coordsforatom[1] = atom.getPoint2d();
coords.put(atom, coordsforatom);
atom.setPoint2d(null);
}
for (IBond bond : container.bonds()) {
stereo.put(bond, bond.getStereo());
bond.setStereo(Stereo.NONE);
}

if (ConnectivityChecker.isConnected(container)) {
generateNewCoordinates(container);
Expand All @@ -1277,7 +1290,7 @@ public void cleanup() {
coordinatesChanged();
if (getUndoRedoFactory() != null && getUndoRedoHandler() != null) {
IUndoRedoable undoredo = getUndoRedoFactory().getChangeCoordsEdit(
coords, "Clean Up");
coords, stereo,"Clean Up");
getUndoRedoHandler().postEdit(undoredo);
}
}
Expand All @@ -1303,6 +1316,10 @@ public static void generateNewCoordinates(IAtomContainer container) {
container.getAtom(i).setPoint2d(
cleanedMol.getAtom(i).getPoint2d());
}
for (int i = 0; i < cleanedMol.getBondCount(); i++) {
container.getBond(i).setStereo(
cleanedMol.getBond(i).getStereo());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Expand Down Expand Up @@ -2014,6 +2031,7 @@ public void clearValidation() {
// OK
public void flip(boolean horizontal) {
HashMap<IAtom, Point2d[]> atomCoordsMap = new HashMap<IAtom, Point2d[]>();
Map<IBond, IBond.Stereo> bondStereo = new HashMap<IBond, IBond.Stereo>();
JChemPaintRendererModel renderModel = renderer.getRenderer2DModel();
IAtomContainer toflip;
if (renderModel.getSelection().getConnectedAtomContainer() != null
Expand Down Expand Up @@ -2048,6 +2066,7 @@ public void flip(boolean horizontal) {
}
// Stereo bonds must be flipped as well to keep the structure
for (IBond bond : toflip.bonds()) {
bondStereo.put(bond, bond.getStereo());
if (bond.getStereo() == IBond.Stereo.UP)
bond.setStereo(IBond.Stereo.DOWN);
else if (bond.getStereo() == IBond.Stereo.DOWN)
Expand All @@ -2060,7 +2079,7 @@ else if (bond.getStereo() == IBond.Stereo.DOWN_INVERTED)
coordinatesChanged();
if (getUndoRedoFactory() != null && getUndoRedoHandler() != null) {
IUndoRedoable undoredo = getUndoRedoFactory().getChangeCoordsEdit(
atomCoordsMap, "Clean Up");
atomCoordsMap, bondStereo, "Clean Up");
getUndoRedoHandler().postEdit(undoredo);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IBond;
import org.openscience.cdk.tools.ILoggingTool;
import org.openscience.cdk.tools.LoggingToolFactory;
import org.openscience.jchempaint.controller.undoredo.IUndoRedoFactory;
Expand Down Expand Up @@ -247,7 +248,7 @@ public void mouseClickedUp(Point2d worldCoord) {
UndoRedoHandler handler = chemModelRelay.getUndoRedoHandler();
if (factory != null && handler != null) {
IUndoRedoable undoredo = factory.getChangeCoordsEdit(
atomCoordsMap, "Rotation");
atomCoordsMap, new HashMap<IBond, IBond.Stereo>(), "Rotation");
handler.postEdit(undoredo);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import javax.vecmath.Point2d;

import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IBond;

/**
* Undo/Redo Edit superclass for all edit classes for coordinate changing
Expand All @@ -44,15 +45,18 @@ public class ChangeCoordsEdit implements IUndoRedoable {

private Map<IAtom, Point2d[]> atomCoordsMap;

private Map<IBond, IBond.Stereo> bondStereo;

private String type;

/**
* @param atomCoordsMap
* A HashMap containing the changed atoms as key and an Array
* with the former and the changed coordinates as Point2ds
*/
public ChangeCoordsEdit(Map<IAtom, Point2d[]> atomCoordsMap, String type) {
public ChangeCoordsEdit(Map<IAtom, Point2d[]> atomCoordsMap, Map<IBond, IBond.Stereo> bondStereo, String type) {
this.atomCoordsMap = atomCoordsMap;
this.bondStereo = bondStereo;
this.type=type;
}

Expand All @@ -66,6 +70,11 @@ public void redo() {
atom.setPoint2d(coords[0]);
atom.setNotification(true);
}
for (Map.Entry<IBond, IBond.Stereo> e : bondStereo.entrySet()) {
IBond.Stereo prev = e.getKey().getStereo();
e.getKey().setStereo(e.getValue());
e.setValue(prev);
}
}

public void undo() {
Expand All @@ -76,6 +85,11 @@ public void undo() {
Point2d[] coords = atomCoordsMap.get(atom);
atom.setPoint2d(coords[1]);
}
for (Map.Entry<IBond, IBond.Stereo> e : bondStereo.entrySet()) {
IBond.Stereo prev = e.getKey().getStereo();
e.getKey().setStereo(e.getValue());
e.setValue(prev);
}
}

public boolean canRedo() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public IUndoRedoable getAdjustBondOrdersEdit(Map<IBond,
public IUndoRedoable getSingleElectronEdit(IAtomContainer relevantContainer, IElectronContainer electronContainer, boolean add, IChemModelRelay chemModelRelay, IAtom atom, String type);
public IUndoRedoable getChangeIsotopeEdit(IAtom atom, Integer formerIsotopeNumber, Integer newIstopeNumber, String type);
public IUndoRedoable getClearAllEdit(IChemModel chemModel, IAtomContainerSet som, IReactionSet sor, String type);
public IUndoRedoable getChangeCoordsEdit(Map<IAtom, Point2d[]> atomCoordsMap, String type);
public IUndoRedoable getChangeCoordsEdit(Map<IAtom, Point2d[]> atomCoordsMap, Map<IBond, IBond.Stereo> bondStereo, String type);
public IUndoRedoable getMakeReactantOrProductInNewReactionEdit(IChemModel chemModel, IAtomContainer ac, IAtomContainer oldcontainer, boolean reactantOrProduct, String type);
public IUndoRedoable getMakeReactantOrProductInExistingReactionEdit(
IChemModel chemModel, IAtomContainer newContainer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import javax.vecmath.Point2d;

import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IBond;
import org.openscience.jchempaint.controller.undoredo.ChangeCoordsEdit;

/**
Expand All @@ -42,8 +43,10 @@
*/
public class SwingChangeCoordsEdit extends ChangeCoordsEdit implements UndoableEdit{

public SwingChangeCoordsEdit(Map<IAtom, Point2d[]> atomCoordsMap, String type) {
super(atomCoordsMap, type);
public SwingChangeCoordsEdit(Map<IAtom, Point2d[]> atomCoordsMap,
Map<IBond, IBond.Stereo> bondStereo,
String type) {
super(atomCoordsMap, bondStereo, type);
}

public boolean addEdit(UndoableEdit arg0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ public IUndoRedoable getClearAllEdit(IChemModel chemModel,
}

public IUndoRedoable getChangeCoordsEdit(Map<IAtom, Point2d[]> atomCoordsMap,
Map<IBond, IBond.Stereo> bondStereo,
String type) {
return new SwingChangeCoordsEdit(atomCoordsMap, type);
return new SwingChangeCoordsEdit(atomCoordsMap, bondStereo, type);
}

public IUndoRedoable getMakeReactantOrProductInNewReactionEdit(IChemModel chemModel,
Expand Down

0 comments on commit e64d095

Please sign in to comment.