Skip to content

Commit 95616a5

Browse files
committed
Resolve issue with cloning SRU groups that have brackets with coordinates defined.
1 parent c939223 commit 95616a5

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

base/core/src/main/java/org/openscience/cdk/tools/manipulator/SgroupManipulator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ public static List<Sgroup> copy(Collection<Sgroup> sgroups,
103103
}
104104
break;
105105
case CtabBracket: {
106-
Collection<IAtom> orgVal = orgSgroup.getValue(key);
107-
if (orgVal != null) {
108-
cpySgroup.putValue(key,
109-
new SgroupBracket((SgroupBracket) orgVal));
106+
Collection<SgroupBracket> orgVals = orgSgroup.getValue(key);
107+
if (orgVals != null) {
108+
for (SgroupBracket bracket : orgVals)
109+
cpySgroup.addBracket(new SgroupBracket(bracket));
110110
}
111111
}
112112
break;

base/test-interfaces/src/test/java/org/openscience/cdk/interfaces/AbstractAtomContainerTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import org.openscience.cdk.CDKConstants;
3434
import org.openscience.cdk.exception.NoSuchAtomException;
3535
import org.openscience.cdk.sgroup.Sgroup;
36+
import org.openscience.cdk.sgroup.SgroupBracket;
37+
import org.openscience.cdk.sgroup.SgroupKey;
3638
import org.openscience.cdk.sgroup.SgroupType;
3739
import org.openscience.cdk.stereo.DoubleBondStereochemistry;
3840
import org.openscience.cdk.stereo.TetrahedralChirality;
@@ -3202,6 +3204,57 @@ public void changeConnectedAtomsAfterAddBond() {
32023204
assertTrue(clonedSgroup.getBonds().contains(clone.getBond(1)));
32033205
}
32043206

3207+
@Test public void cloneSgroupsBrackets() throws CloneNotSupportedException {
3208+
IAtomContainer mol = (IAtomContainer) newChemObject();
3209+
IAtom a1 = mol.getBuilder().newAtom();
3210+
IAtom a2 = mol.getBuilder().newAtom();
3211+
IAtom a3 = mol.getBuilder().newAtom();
3212+
IBond b1 = mol.getBuilder().newBond();
3213+
IBond b2 = mol.getBuilder().newBond();
3214+
b1.setAtom(a1, 0);
3215+
b1.setAtom(a2, 1);
3216+
b2.setAtom(a2, 0);
3217+
b2.setAtom(a3, 1);
3218+
mol.addAtom(a1);
3219+
mol.addAtom(a2);
3220+
mol.addAtom(a3);
3221+
mol.addBond(b1);
3222+
mol.addBond(b2);
3223+
Sgroup sgroup = new Sgroup();
3224+
sgroup.setType(SgroupType.CtabStructureRepeatUnit);
3225+
sgroup.setSubscript("n");
3226+
sgroup.addAtom(a2);
3227+
sgroup.addBond(b1);
3228+
sgroup.addBond(b2);
3229+
SgroupBracket bracket1 = new SgroupBracket(0, 1, 2, 3);
3230+
SgroupBracket bracket2 = new SgroupBracket(1, 2, 3, 4);
3231+
sgroup.addBracket(bracket1);
3232+
sgroup.addBracket(bracket2);
3233+
mol.setProperty(CDKConstants.CTAB_SGROUPS,
3234+
Collections.singletonList(sgroup));
3235+
IAtomContainer clone = mol.clone();
3236+
Collection<Sgroup> sgroups = clone.getProperty(CDKConstants.CTAB_SGROUPS);
3237+
assertNotNull(sgroups);
3238+
assertThat(sgroups.size(), is(1));
3239+
Sgroup clonedSgroup = sgroups.iterator().next();
3240+
assertThat(clonedSgroup.getType(), is(SgroupType.CtabStructureRepeatUnit));
3241+
assertThat(clonedSgroup.getSubscript(), is("n"));
3242+
assertFalse(clonedSgroup.getAtoms().contains(a2));
3243+
assertFalse(clonedSgroup.getBonds().contains(b1));
3244+
assertFalse(clonedSgroup.getBonds().contains(b2));
3245+
assertTrue(clonedSgroup.getAtoms().contains(clone.getAtom(1)));
3246+
assertTrue(clonedSgroup.getBonds().contains(clone.getBond(0)));
3247+
assertTrue(clonedSgroup.getBonds().contains(clone.getBond(1)));
3248+
List<SgroupBracket> brackets = clonedSgroup.getValue(SgroupKey.CtabBracket);
3249+
assertThat(brackets.size(), is(2));
3250+
assertThat(brackets.get(0), is(not(sameInstance(bracket1))));
3251+
assertThat(brackets.get(1), is(not(sameInstance(bracket2))));
3252+
assertEquals(brackets.get(0).getFirstPoint(), new Point2d(0, 1), 0.01);
3253+
assertEquals(brackets.get(0).getSecondPoint(), new Point2d(2, 3), 0.01);
3254+
assertEquals(brackets.get(1).getFirstPoint(), new Point2d(1, 2), 0.01);
3255+
assertEquals(brackets.get(1).getSecondPoint(), new Point2d(3, 4), 0.01);
3256+
}
3257+
32053258
@Test
32063259
public void getSelfBond() {
32073260
IAtomContainer mol = (IAtomContainer) newChemObject();

0 commit comments

Comments
 (0)