Skip to content

Commit 23856dc

Browse files
committed
Add some unit tests
1 parent 032cdd4 commit 23856dc

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

base/standard/src/main/java/org/openscience/cdk/geometry/GeometryUtil.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,52 @@ public static void reflect(Collection<IAtom> atoms, Point2d beg, Point2d end) {
274274
double y = b * (p.x - beg.x) - a * (p.y - beg.y) + beg.y;
275275
p.x = x;
276276
p.y = y;
277+
278+
// flip any bond displays that have end points in the set
279+
for (IBond bond : atom.bonds()) {
280+
281+
// only flip once
282+
if (!bond.getAtom(0).equals(atom))
283+
continue;
284+
285+
IBond.Display flipped = null;
286+
IBond.Stereo flippedStereo = null;
287+
switch (bond.getDisplay()) {
288+
case WedgeBegin:
289+
flipped = IBond.Display.WedgedHashBegin;
290+
flippedStereo = IBond.Stereo.DOWN;
291+
break;
292+
case WedgeEnd:
293+
flipped = IBond.Display.WedgedHashEnd;
294+
flippedStereo = IBond.Stereo.DOWN_INVERTED;
295+
break;
296+
case WedgedHashBegin:
297+
flipped = IBond.Display.WedgeBegin;
298+
flippedStereo = IBond.Stereo.UP;
299+
break;
300+
case WedgedHashEnd:
301+
flipped = IBond.Display.WedgeEnd;
302+
flippedStereo = IBond.Stereo.UP_INVERTED;
303+
break;
304+
case Bold:
305+
flipped = IBond.Display.Hash;
306+
break;
307+
case Hash:
308+
flipped = IBond.Display.Bold;
309+
break;
310+
}
311+
312+
if (flipped == null)
313+
continue;
314+
315+
IAtom nbor = bond.getOther(atom);
316+
// lookup depends on provided collection... but we
317+
// only do that if there was a bond display to flip
318+
if (atoms.contains(nbor)) {
319+
bond.setStereo(flippedStereo); // wish this was cleaner
320+
bond.setDisplay(flipped);
321+
}
322+
}
277323
}
278324
}
279325

@@ -1720,6 +1766,13 @@ public static double getBondLengthMedian(final IAtomContainer container) {
17201766
return getBondLengthMedian(container.bonds(), container.getBondCount());
17211767
}
17221768

1769+
/**
1770+
* Calculate the median bond length of some bonds.
1771+
*
1772+
* @param bonds the bonds
1773+
* @return median bond length
1774+
* @throws java.lang.IllegalArgumentException unset coordinates or no bonds
1775+
*/
17231776
public static double getBondLengthMedian(final Iterable<IBond> bonds, int cap) {
17241777
Iterator<IBond> iter = bonds.iterator();
17251778
if (!iter.hasNext()) throw new IllegalArgumentException("No bonds");

base/test-standard/src/test/java/org/openscience/cdk/geometry/GeometryUtilTest.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
package org.openscience.cdk.geometry;
2525

26+
import org.hamcrest.number.IsCloseTo;
2627
import org.junit.Assert;
2728
import org.junit.Test;
2829
import org.openscience.cdk.Atom;
@@ -41,13 +42,22 @@
4142
import org.openscience.cdk.interfaces.IRingSet;
4243
import org.openscience.cdk.io.IChemObjectReader.Mode;
4344
import org.openscience.cdk.io.MDLV2000Reader;
45+
import org.openscience.cdk.io.MDLV2000Writer;
4446
import org.openscience.cdk.isomorphism.AtomMappingTools;
47+
import org.openscience.cdk.silent.SilentChemObjectBuilder;
4548
import org.openscience.cdk.tools.diff.AtomContainerDiff;
49+
import org.openscience.cdk.tools.manipulator.AtomContainerManipulator;
4650

4751
import javax.vecmath.Point2d;
4852
import javax.vecmath.Point3d;
53+
import java.io.IOException;
4954
import java.io.InputStream;
55+
import java.io.StringReader;
56+
import java.util.ArrayList;
57+
import java.util.Arrays;
58+
import java.util.Collections;
5059
import java.util.HashMap;
60+
import java.util.List;
5161
import java.util.Map;
5262

5363
import static org.hamcrest.CoreMatchers.is;
@@ -880,6 +890,64 @@ public void medianBondLengthWithZeroLengthBonds() {
880890
assertThat(GeometryUtil.getBondLengthMedian(container), is(1d));
881891
}
882892

893+
@Test public void testBasicReflection() {
894+
IAtomContainer container = SilentChemObjectBuilder.getInstance().newAtomContainer();
895+
container.addAtom(atomAt(new Point2d(-1, 1)));
896+
container.addAtom(atomAt(new Point2d(-1, -1)));
897+
container.addAtom(atomAt(new Point2d(1, 1)));
898+
container.addAtom(atomAt(new Point2d(1, -1)));
899+
assertThat(container.getAtom(0).getPoint2d().x, IsCloseTo.closeTo(-1, 0.001));
900+
assertThat(container.getAtom(0).getPoint2d().y, IsCloseTo.closeTo(1, 0.001));
901+
assertThat(container.getAtom(3).getPoint2d().x, IsCloseTo.closeTo(1, 0.001));
902+
assertThat(container.getAtom(3).getPoint2d().y, IsCloseTo.closeTo(-1, 0.001));
903+
GeometryUtil.reflect(Arrays.asList(container.getAtom(0),
904+
container.getAtom(3)),
905+
new Point2d(-1,-1), new Point2d(1,1));
906+
assertThat(container.getAtom(0).getPoint2d().x, IsCloseTo.closeTo(1, 0.001));
907+
assertThat(container.getAtom(0).getPoint2d().y, IsCloseTo.closeTo(-1, 0.001));
908+
assertThat(container.getAtom(3).getPoint2d().x, IsCloseTo.closeTo(-1, 0.001));
909+
assertThat(container.getAtom(3).getPoint2d().y, IsCloseTo.closeTo(1, 0.001));
910+
}
911+
912+
@Test public void testBasicReflectionBonds() throws IOException, CDKException {
913+
String molfile = "\n" +
914+
" Mrv1810 05052109112D \n" +
915+
"\n" +
916+
" 10 10 0 0 0 0 999 V2000\n" +
917+
" -1.5138 -0.0596 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
918+
" -2.2282 -0.4722 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
919+
" -2.2282 -1.2972 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
920+
" -1.5138 -1.7097 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
921+
" -0.7993 -1.2972 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
922+
" -0.7993 -0.4722 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
923+
" -1.5138 0.7654 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
924+
" -0.7993 1.1779 0.0000 C 0 0 1 0 0 0 0 0 0 0 0 0\n" +
925+
" -0.7993 2.0029 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n" +
926+
" -0.0848 0.7654 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0\n" +
927+
" 1 2 1 0 0 0 0\n" +
928+
" 2 3 1 0 0 0 0\n" +
929+
" 3 4 1 0 0 0 0\n" +
930+
" 4 5 1 0 0 0 0\n" +
931+
" 5 6 1 0 0 0 0\n" +
932+
" 1 6 1 0 0 0 0\n" +
933+
" 7 8 1 0 0 0 0\n" +
934+
" 8 9 1 0 0 0 0\n" +
935+
" 8 10 1 1 0 0 0\n" +
936+
" 1 7 2 0 0 0 0\n" +
937+
"M END\n";
938+
try (MDLV2000Reader mdlr = new MDLV2000Reader(new StringReader(molfile))) {
939+
IAtomContainer mol = mdlr.read(SilentChemObjectBuilder.getInstance().newAtomContainer());
940+
List<IAtom> atoms = new ArrayList<>();
941+
atoms.add(mol.getAtom(6));
942+
atoms.add(mol.getAtom(7));
943+
atoms.add(mol.getAtom(8));
944+
atoms.add(mol.getAtom(9));
945+
GeometryUtil.reflect(atoms, mol.getBond(9));
946+
assertThat(mol.getBond(8).getDisplay(), is(IBond.Display.WedgedHashBegin));
947+
assertThat(mol.getBond(8).getStereo(), is(IBond.Stereo.DOWN));
948+
}
949+
}
950+
883951
private IAtom atomAt(Point2d p) {
884952
IAtom atom = new Atom("C");
885953
atom.setPoint2d(p);

0 commit comments

Comments
 (0)