Skip to content

Commit 8f5d03d

Browse files
committed
Back porting fix and test for #716
1 parent fe8c716 commit 8f5d03d

File tree

2 files changed

+99
-25
lines changed
  • biojava-structure/src

2 files changed

+99
-25
lines changed

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

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,8 @@ public static final void rotate(Structure structure, Matrix m){
450450
* @param t transformation Matrix4d
451451
*/
452452
public static void transform(Atom[] ca, Matrix4d t) {
453-
for (Atom atom : ca) Calc.transform(atom, t);
453+
for (Atom atom : ca)
454+
Calc.transform(atom, t);
454455
}
455456

456457
/**
@@ -479,13 +480,14 @@ public static final void transform (Atom atom, Matrix4d m) {
479480
* @param group
480481
* @param m
481482
*/
482-
public static final void transform (Group group, Matrix4d m) {
483-
AtomIterator iter = new AtomIterator(group) ;
484-
485-
while (iter.hasNext()) {
486-
Atom atom = iter.next() ;
487-
transform(atom,m);
488-
483+
public static final void transform(Group group, Matrix4d m) {
484+
for (Atom atom : group.getAtoms()) {
485+
transform(atom, m);
486+
}
487+
for (Group altG : group.getAltLocs()) {
488+
for (Atom atom : altG.getAtoms()) {
489+
transform(atom, m);
490+
}
489491
}
490492
}
491493

@@ -497,13 +499,11 @@ public static final void transform (Group group, Matrix4d m) {
497499
* @param structure
498500
* @param m
499501
*/
500-
public static final void transform (Structure structure, Matrix4d m) {
501-
AtomIterator iter = new AtomIterator(structure) ;
502-
503-
while (iter.hasNext()) {
504-
Atom atom = iter.next() ;
505-
transform(atom,m);
506-
502+
public static final void transform(Structure structure, Matrix4d m) {
503+
for (int n=0; n<structure.nrModels();n++) {
504+
for (Chain c : structure.getChains(n)) {
505+
transform(c, m);
506+
}
507507
}
508508
}
509509

@@ -517,15 +517,8 @@ public static final void transform (Structure structure, Matrix4d m) {
517517
*/
518518
public static final void transform (Chain chain, Matrix4d m) {
519519

520-
for (Group g:chain.getAtomGroups()) {
521-
for (Atom atom: g.getAtoms()) {
522-
transform(atom,m);
523-
}
524-
for (Group altG : g.getAltLocs()) {
525-
for (Atom atom : altG.getAtoms()) {
526-
transform(atom, m);
527-
}
528-
}
520+
for (Group g : chain.getAtomGroups()) {
521+
transform(g, m);
529522
}
530523
}
531524

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

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,94 @@ public void testChainTransform() {
204204

205205
}
206206

207-
private static Atom getAtom(double x, double y, double z) {
207+
/**
208+
* Issue https://github.com/biojava/biojava/issues/715
209+
*/
210+
@Test
211+
public void testStructureTransform() throws StructureException {
212+
Group g = new AminoAcidImpl();
213+
Atom a = getAtom("CA", 1, 1, 1);
214+
g.addAtom(a);
215+
Group altLocG = new AminoAcidImpl();
216+
Atom a2 = getAtom("CA", 2, 2, 2);
217+
altLocG.addAtom(a2);
218+
219+
g.addAltLoc(altLocG);
220+
221+
Chain c1 = new ChainImpl();
222+
c1.addGroup(g);
223+
c1.setChainID("A");
224+
225+
Group gc2 = new AminoAcidImpl();
226+
Atom ac2 = getAtom("CA", 3, 3, 3);
227+
gc2.addAtom(ac2);
228+
Group altLocGc2 = new AminoAcidImpl();
229+
Atom ac22 = getAtom("CA", 4, 4, 4);
230+
altLocGc2.addAtom(ac22);
231+
232+
gc2.addAltLoc(altLocGc2);
233+
234+
Chain c2 = new ChainImpl();
235+
c2.addGroup(gc2);
236+
c2.setChainID("B");
237+
238+
Structure s = new StructureImpl();
239+
s.addChain(c1);
240+
s.addChain(c2);
241+
242+
243+
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
244+
Calc.transform(s, m);
245+
246+
// testing 1st chain
247+
Group thegroup = s.getChainByPDB("A").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 shitfted 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 shitfted 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+
// testing 2nd chain
264+
thegroup = s.getChainByPDB("B").getAtomGroup(0);
265+
thealtlocgroup = thegroup.getAltLocs().get(0);
266+
267+
atom1 = thegroup.getAtom("CA");
268+
atom2 = thealtlocgroup.getAtom("CA");
269+
270+
// x should be shitfted by 1
271+
assertEquals(4, atom1.getX(), 0.00001);
272+
assertEquals(3, atom1.getY(), 0.00001);
273+
assertEquals(3, atom1.getZ(), 0.00001);
274+
275+
// x should be shitfted by 1
276+
assertEquals(5, atom2.getX(), 0.00001);
277+
assertEquals(4, atom2.getY(), 0.00001);
278+
assertEquals(4, atom2.getZ(), 0.00001);
279+
280+
281+
}
282+
283+
private static Atom getAtom(String name, double x, double y, double z) {
208284
Atom a = new AtomImpl();
209285
a.setX(x);
210286
a.setY(y);
211287
a.setZ(z);
288+
a.setName(name);
212289
return a;
213290
}
291+
292+
private static Atom getAtom(double x, double y, double z) {
293+
return getAtom(null, x, y, z);
294+
}
214295

215296
private static Matrix4d getSampleTransform(){
216297

0 commit comments

Comments
 (0)