Skip to content

Commit f18ca28

Browse files
authored
Merge pull request biojava#716 from josemduarte/chainTransformAltlocs
Fixing issue with Calc.transform(Chain,Matrix4d)
2 parents f596afb + e0c031b commit f18ca28

File tree

2 files changed

+218
-28
lines changed
  • biojava-structure/src

2 files changed

+218
-28
lines changed

biojava-structure/src/main/java/org/biojava/nbio/structure/Calc.java

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -538,12 +538,11 @@ public static final void transform(Atom atom, Matrix4d m) {
538538
* @param m
539539
*/
540540
public static final void transform(Group group, Matrix4d m) {
541-
AtomIterator iter = new AtomIterator(group);
542-
543-
while (iter.hasNext()) {
544-
Atom atom = iter.next();
541+
for (Atom atom : group.getAtoms()) {
545542
transform(atom, m);
546-
543+
}
544+
for (Group altG : group.getAltLocs()) {
545+
transform(altG, m);
547546
}
548547
}
549548

@@ -556,12 +555,10 @@ public static final void transform(Group group, Matrix4d m) {
556555
* @param m
557556
*/
558557
public static final void transform(Structure structure, Matrix4d m) {
559-
AtomIterator iter = new AtomIterator(structure);
560-
561-
while (iter.hasNext()) {
562-
Atom atom = iter.next();
563-
transform(atom, m);
564-
558+
for (int n=0; n<structure.nrModels();n++) {
559+
for (Chain c : structure.getChains(n)) {
560+
transform(c, m);
561+
}
565562
}
566563
}
567564

@@ -576,9 +573,7 @@ public static final void transform(Structure structure, Matrix4d m) {
576573
public static final void transform(Chain chain, Matrix4d m) {
577574

578575
for (Group g : chain.getAtomGroups()) {
579-
for (Atom atom : g.getAtoms()) {
580-
transform(atom, m);
581-
}
576+
transform(g, m);
582577
}
583578
}
584579

@@ -604,12 +599,12 @@ public static final void translate(Atom atom, Vector3d v) {
604599
* @param v
605600
*/
606601
public static final void translate(Group group, Vector3d v) {
607-
AtomIterator iter = new AtomIterator(group);
608602

609-
while (iter.hasNext()) {
610-
Atom atom = iter.next();
603+
for (Atom atom : group.getAtoms()) {
611604
translate(atom, v);
612-
605+
}
606+
for (Group altG : group.getAltLocs()) {
607+
translate(altG, v);
613608
}
614609
}
615610

@@ -623,9 +618,7 @@ public static final void translate(Group group, Vector3d v) {
623618
public static final void translate(Chain chain, Vector3d v) {
624619

625620
for (Group g : chain.getAtomGroups()) {
626-
for (Atom atom : g.getAtoms()) {
627-
translate(atom, v);
628-
}
621+
translate(g, v);
629622
}
630623
}
631624

@@ -637,12 +630,11 @@ public static final void translate(Chain chain, Vector3d v) {
637630
* @param v
638631
*/
639632
public static final void translate(Structure structure, Vector3d v) {
640-
AtomIterator iter = new AtomIterator(structure);
641-
642-
while (iter.hasNext()) {
643-
Atom atom = iter.next();
644-
translate(atom, v);
645-
633+
634+
for (int n=0; n<structure.nrModels();n++) {
635+
for (Chain c : structure.getChains(n)) {
636+
translate(c, v);
637+
}
646638
}
647639
}
648640

biojava-structure/src/test/java/org/biojava/nbio/structure/TestCalc.java

Lines changed: 199 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import javax.vecmath.Matrix4d;
2626
import javax.vecmath.Point3d;
27+
import javax.vecmath.Vector3d;
2728

2829
import org.biojava.nbio.structure.geometry.Matrices;
2930
import org.biojava.nbio.structure.jama.Matrix;
@@ -157,14 +158,163 @@ public void testVecmathTransformation() {
157158

158159
assertEquals(expected, actual);
159160
}
161+
162+
/**
163+
* Issue https://github.com/biojava/biojava/issues/715
164+
*/
165+
@Test
166+
public void testChainTransform() {
167+
168+
Chain c = createDummyChain();
169+
170+
Matrix4d m = new Matrix4d(1,0,0,1, 0,1,0,0, 0,0,1,0, 0,0,0,1); // shift of 1 in x axis
171+
Calc.transform(c, m);
172+
173+
Group thegroup = c.getAtomGroup(0);
174+
Group thealtlocgroup = thegroup.getAltLocs().get(0);
175+
176+
Atom atom1 = thegroup.getAtom("CA");
177+
Atom atom2 = thealtlocgroup.getAtom("CA");
178+
179+
// x should be shifted by 1
180+
assertEquals(2, atom1.getX(), 0.00001);
181+
assertEquals(1, atom1.getY(), 0.00001);
182+
assertEquals(1, atom1.getZ(), 0.00001);
183+
184+
// x should be shifted by 1
185+
assertEquals(3, atom2.getX(), 0.00001);
186+
assertEquals(2, atom2.getY(), 0.00001);
187+
assertEquals(2, atom2.getZ(), 0.00001);
188+
189+
190+
}
160191

161-
private static Atom getAtom(double x, double y, double z) {
192+
/**
193+
* Issue https://github.com/biojava/biojava/issues/715
194+
*/
195+
@Test
196+
public void testStructureTransform() {
197+
198+
Structure s = createDummyStructure();
199+
200+
Matrix4d m = new Matrix4d(1,0,0,1, 0,1,0,0, 0,0,1,0, 0,0,0,1); // shift of 1 in x axis
201+
Calc.transform(s, m);
202+
203+
// testing 1st chain
204+
Group thegroup = s.getChain("A").getAtomGroup(0);
205+
Group thealtlocgroup = thegroup.getAltLocs().get(0);
206+
207+
Atom atom1 = thegroup.getAtom("CA");
208+
Atom atom2 = thealtlocgroup.getAtom("CA");
209+
210+
// x should be shitfted by 1
211+
assertEquals(2, atom1.getX(), 0.00001);
212+
assertEquals(1, atom1.getY(), 0.00001);
213+
assertEquals(1, atom1.getZ(), 0.00001);
214+
215+
// x should be shitfted by 1
216+
assertEquals(3, atom2.getX(), 0.00001);
217+
assertEquals(2, atom2.getY(), 0.00001);
218+
assertEquals(2, atom2.getZ(), 0.00001);
219+
220+
// testing 2nd chain
221+
thegroup = s.getChain("B").getAtomGroup(0);
222+
thealtlocgroup = thegroup.getAltLocs().get(0);
223+
224+
atom1 = thegroup.getAtom("CA");
225+
atom2 = thealtlocgroup.getAtom("CA");
226+
227+
// x should be shitfted by 1
228+
assertEquals(4, atom1.getX(), 0.00001);
229+
assertEquals(3, atom1.getY(), 0.00001);
230+
assertEquals(3, atom1.getZ(), 0.00001);
231+
232+
// x should be shitfted by 1
233+
assertEquals(5, atom2.getX(), 0.00001);
234+
assertEquals(4, atom2.getY(), 0.00001);
235+
assertEquals(4, atom2.getZ(), 0.00001);
236+
237+
238+
}
239+
240+
@Test
241+
public void testChainTranslate() {
242+
Chain c = createDummyChain();
243+
244+
Vector3d translation = new Vector3d(1, 0, 0);
245+
Calc.translate(c, translation);
246+
247+
Group thegroup = c.getAtomGroup(0);
248+
Group thealtlocgroup = thegroup.getAltLocs().get(0);
249+
250+
Atom atom1 = thegroup.getAtom("CA");
251+
Atom atom2 = thealtlocgroup.getAtom("CA");
252+
253+
// x should be shifted by 1
254+
assertEquals(2, atom1.getX(), 0.00001);
255+
assertEquals(1, atom1.getY(), 0.00001);
256+
assertEquals(1, atom1.getZ(), 0.00001);
257+
258+
// x should be shifted by 1
259+
assertEquals(3, atom2.getX(), 0.00001);
260+
assertEquals(2, atom2.getY(), 0.00001);
261+
assertEquals(2, atom2.getZ(), 0.00001);
262+
}
263+
264+
@Test
265+
public void testStructureTranslate() {
266+
Structure s = createDummyStructure();
267+
268+
Vector3d translation = new Vector3d(1, 0, 0);
269+
Calc.translate(s, translation);
270+
271+
// testing 1st chain
272+
Group thegroup = s.getChain("A").getAtomGroup(0);
273+
Group thealtlocgroup = thegroup.getAltLocs().get(0);
274+
275+
Atom atom1 = thegroup.getAtom("CA");
276+
Atom atom2 = thealtlocgroup.getAtom("CA");
277+
278+
// x should be shitfted by 1
279+
assertEquals(2, atom1.getX(), 0.00001);
280+
assertEquals(1, atom1.getY(), 0.00001);
281+
assertEquals(1, atom1.getZ(), 0.00001);
282+
283+
// x should be shitfted by 1
284+
assertEquals(3, atom2.getX(), 0.00001);
285+
assertEquals(2, atom2.getY(), 0.00001);
286+
assertEquals(2, atom2.getZ(), 0.00001);
287+
288+
// testing 2nd chain
289+
thegroup = s.getChain("B").getAtomGroup(0);
290+
thealtlocgroup = thegroup.getAltLocs().get(0);
291+
292+
atom1 = thegroup.getAtom("CA");
293+
atom2 = thealtlocgroup.getAtom("CA");
294+
295+
// x should be shitfted by 1
296+
assertEquals(4, atom1.getX(), 0.00001);
297+
assertEquals(3, atom1.getY(), 0.00001);
298+
assertEquals(3, atom1.getZ(), 0.00001);
299+
300+
// x should be shitfted by 1
301+
assertEquals(5, atom2.getX(), 0.00001);
302+
assertEquals(4, atom2.getY(), 0.00001);
303+
assertEquals(4, atom2.getZ(), 0.00001);
304+
}
305+
306+
private static Atom getAtom(String name, double x, double y, double z) {
162307
Atom a = new AtomImpl();
163308
a.setX(x);
164309
a.setY(y);
165310
a.setZ(z);
311+
a.setName(name);
166312
return a;
167313
}
314+
315+
private static Atom getAtom(double x, double y, double z) {
316+
return getAtom(null, x, y, z);
317+
}
168318

169319
private static Matrix4d getSampleTransform(){
170320

@@ -174,5 +324,53 @@ private static Matrix4d getSampleTransform(){
174324
0.0,0.0,0.0,1.0});
175325
return sample;
176326
}
327+
328+
private static Chain createDummyChain() {
329+
Group g = new AminoAcidImpl();
330+
Atom a = getAtom("CA", 1, 1, 1);
331+
g.addAtom(a);
332+
Group altLocG = new AminoAcidImpl();
333+
Atom a2 = getAtom("CA", 2, 2, 2);
334+
altLocG.addAtom(a2);
335+
336+
g.addAltLoc(altLocG);
337+
338+
Chain c = new ChainImpl();
339+
c.addGroup(g);
340+
return c;
341+
}
342+
343+
private static Structure createDummyStructure() {
344+
Group g = new AminoAcidImpl();
345+
Atom a = getAtom("CA", 1, 1, 1);
346+
g.addAtom(a);
347+
Group altLocG = new AminoAcidImpl();
348+
Atom a2 = getAtom("CA", 2, 2, 2);
349+
altLocG.addAtom(a2);
350+
351+
g.addAltLoc(altLocG);
352+
353+
Chain c1 = new ChainImpl();
354+
c1.addGroup(g);
355+
c1.setId("A");
356+
357+
Group gc2 = new AminoAcidImpl();
358+
Atom ac2 = getAtom("CA", 3, 3, 3);
359+
gc2.addAtom(ac2);
360+
Group altLocGc2 = new AminoAcidImpl();
361+
Atom ac22 = getAtom("CA", 4, 4, 4);
362+
altLocGc2.addAtom(ac22);
363+
364+
gc2.addAltLoc(altLocGc2);
365+
366+
Chain c2 = new ChainImpl();
367+
c2.addGroup(gc2);
368+
c2.setId("B");
369+
370+
Structure s = new StructureImpl();
371+
s.addChain(c1);
372+
s.addChain(c2);
373+
return s;
374+
}
177375

178376
}

0 commit comments

Comments
 (0)