Skip to content

Commit 8a7e22c

Browse files
committed
Fixes for translate functions, similar to biojava#715
1 parent 3a1ad26 commit 8a7e22c

File tree

2 files changed

+131
-54
lines changed
  • biojava-structure/src

2 files changed

+131
-54
lines changed

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -601,12 +601,14 @@ public static final void translate(Atom atom, Vector3d v) {
601601
* @param v
602602
*/
603603
public static final void translate(Group group, Vector3d v) {
604-
AtomIterator iter = new AtomIterator(group);
605604

606-
while (iter.hasNext()) {
607-
Atom atom = iter.next();
605+
for (Atom atom : group.getAtoms()) {
608606
translate(atom, v);
609-
607+
}
608+
for (Group altG : group.getAltLocs()) {
609+
for (Atom atom : altG.getAtoms()) {
610+
translate(atom, v);
611+
}
610612
}
611613
}
612614

@@ -620,9 +622,7 @@ public static final void translate(Group group, Vector3d v) {
620622
public static final void translate(Chain chain, Vector3d v) {
621623

622624
for (Group g : chain.getAtomGroups()) {
623-
for (Atom atom : g.getAtoms()) {
624-
translate(atom, v);
625-
}
625+
translate(g, v);
626626
}
627627
}
628628

@@ -634,12 +634,11 @@ public static final void translate(Chain chain, Vector3d v) {
634634
* @param v
635635
*/
636636
public static final void translate(Structure structure, Vector3d v) {
637-
AtomIterator iter = new AtomIterator(structure);
638-
639-
while (iter.hasNext()) {
640-
Atom atom = iter.next();
641-
translate(atom, v);
642-
637+
638+
for (int n=0; n<structure.nrModels();n++) {
639+
for (Chain c : structure.getChains(n)) {
640+
translate(c, v);
641+
}
643642
}
644643
}
645644

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

Lines changed: 119 additions & 41 deletions
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;
@@ -163,17 +164,8 @@ public void testVecmathTransformation() {
163164
*/
164165
@Test
165166
public void testChainTransform() {
166-
Group g = new AminoAcidImpl();
167-
Atom a = getAtom("CA", 1, 1, 1);
168-
g.addAtom(a);
169-
Group altLocG = new AminoAcidImpl();
170-
Atom a2 = getAtom("CA", 2, 2, 2);
171-
altLocG.addAtom(a2);
172-
173-
g.addAltLoc(altLocG);
174167

175-
Chain c = new ChainImpl();
176-
c.addGroup(g);
168+
Chain c = createDummyChain();
177169

178170
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
179171
Calc.transform(c, m);
@@ -184,12 +176,12 @@ public void testChainTransform() {
184176
Atom atom1 = thegroup.getAtom("CA");
185177
Atom atom2 = thealtlocgroup.getAtom("CA");
186178

187-
// x should be shitfted by 1
179+
// x should be shifted by 1
188180
assertEquals(2, atom1.getX(), 0.00001);
189181
assertEquals(1, atom1.getY(), 0.00001);
190182
assertEquals(1, atom1.getZ(), 0.00001);
191183

192-
// x should be shitfted by 1
184+
// x should be shifted by 1
193185
assertEquals(3, atom2.getX(), 0.00001);
194186
assertEquals(2, atom2.getY(), 0.00001);
195187
assertEquals(2, atom2.getZ(), 0.00001);
@@ -202,36 +194,8 @@ public void testChainTransform() {
202194
*/
203195
@Test
204196
public void testStructureTransform() {
205-
Group g = new AminoAcidImpl();
206-
Atom a = getAtom("CA", 1, 1, 1);
207-
g.addAtom(a);
208-
Group altLocG = new AminoAcidImpl();
209-
Atom a2 = getAtom("CA", 2, 2, 2);
210-
altLocG.addAtom(a2);
211-
212-
g.addAltLoc(altLocG);
213-
214-
Chain c1 = new ChainImpl();
215-
c1.addGroup(g);
216-
c1.setId("A");
217-
218-
Group gc2 = new AminoAcidImpl();
219-
Atom ac2 = getAtom("CA", 3, 3, 3);
220-
gc2.addAtom(ac2);
221-
Group altLocGc2 = new AminoAcidImpl();
222-
Atom ac22 = getAtom("CA", 4, 4, 4);
223-
altLocGc2.addAtom(ac22);
224-
225-
gc2.addAltLoc(altLocGc2);
226-
227-
Chain c2 = new ChainImpl();
228-
c2.addGroup(gc2);
229-
c2.setId("B");
230-
231-
Structure s = new StructureImpl();
232-
s.addChain(c1);
233-
s.addChain(c2);
234197

198+
Structure s = createDummyStructure();
235199

236200
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
237201
Calc.transform(s, m);
@@ -272,6 +236,72 @@ public void testStructureTransform() {
272236

273237

274238
}
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+
}
275305

276306
private static Atom getAtom(String name, double x, double y, double z) {
277307
Atom a = new AtomImpl();
@@ -294,5 +324,53 @@ private static Matrix4d getSampleTransform(){
294324
0.0,0.0,0.0,1.0});
295325
return sample;
296326
}
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+
}
297375

298376
}

0 commit comments

Comments
 (0)