Skip to content

Commit fdb5cb0

Browse files
committed
Calc.transform() documentation and testing
1 parent 38c1e33 commit fdb5cb0

File tree

5 files changed

+130
-51
lines changed

5 files changed

+130
-51
lines changed

biojava-structure-gui/src/main/java/demo/AlignmentGuiDemo.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@
2424

2525

2626
import org.biojava.nbio.structure.align.gui.AlignmentGui;
27+
import org.biojava.nbio.structure.align.gui.MultipleAlignmentGUI;
2728

2829
/**
30+
* Get an instance of the two alignment GUIs.
31+
* Try to align 2hyn vs. 1zll, for example.
2932
*
3033
* @author Andreas Prlic
3134
*
3235
*/
3336
public class AlignmentGuiDemo {
3437

35-
/** e.g. try to align 2hyn vs 1zll
36-
*
37-
*/
3838
public static void main(String[] args) {
39-
39+
4040
AlignmentGui.getInstance();
41-
41+
MultipleAlignmentGUI.getInstance();
4242
}
4343
}

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

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
* Created on 08.05.2004
2121
*
2222
*/
23-
24-
2523
package org.biojava.nbio.structure ;
2624

2725
import org.biojava.nbio.structure.jama.Matrix;
@@ -33,17 +31,15 @@
3331
import javax.vecmath.Point3d;
3432
import javax.vecmath.Vector3d;
3533

36-
37-
38-
/** utility operations on Atoms, AminoAcids, etc.
34+
/**
35+
* Utility operations on Atoms, AminoAcids, etc.
3936
* <p>
40-
* Currently the
41-
* coordinates of an Atom are stored as an array of size 3
42-
* (double[3]). It would be more powerful to use Point3D from
43-
* javax.vecmath. but unfortunately this is not a part of standard
44-
* java installations, since it comes with java3d . So to keep things
37+
* Currently the coordinates of an Atom are stored as an array
38+
* of size 3 (double[3]). It would be more powerful to use Point3D from
39+
* javax.vecmath, but unfortunately this is not a part of standard
40+
* java installations, since it comes with java3d. So to keep things
4541
* simple at the moment biojava does not depend on java3d.
46-
* </p>
42+
*
4743
* @author Andreas Prlic
4844
* @since 1.4
4945
* @version %I% %G%
@@ -53,7 +49,6 @@ public class Calc {
5349

5450
private final static Logger logger = LoggerFactory.getLogger(Calc.class);
5551

56-
5752
/**
5853
* calculate distance between two atoms.
5954
*
@@ -73,7 +68,7 @@ public static final double getDistance(Atom a, Atom b) {
7368

7469

7570
/**
76-
* Will calculate the *square* of distances between two atoms. This will be
71+
* Will calculate the square of distances between two atoms. This will be
7772
* faster as it will not perform the final square root to get the actual
7873
* distance. Use this if doing large numbers of distance comparisons - it is
7974
* marginally faster than getDistance().
@@ -314,10 +309,9 @@ public static final boolean isConnected(AminoAcid a, AminoAcid b) {
314309
return distance < 2.5;
315310
}
316311

317-
318-
319-
/** rotate a single atom aroud a rotation matrix.
320-
* matrix must be a 3x3 matrix.
312+
/**
313+
* Rotate a single Atom aroud a rotation matrix.
314+
* The rotation Matrix must be a pre-multiplication 3x3 Matrix.
321315
*
322316
* If the matrix is indexed m[row][col], then the matrix will be
323317
* pre-multiplied (y=atom*M)
@@ -340,15 +334,18 @@ public static final void rotate(Atom atom, double[][] m){
340334
atom.setZ(nz);
341335
}
342336

343-
/** Rotate a structure.
337+
/**
338+
* Rotate a structure.
339+
* The rotation Matrix must be a pre-multiplication Matrix.
344340
*
345341
* @param structure a Structure object
346-
* @param rotationmatrix an array (3x3) of double representing the rotation matrix.
342+
* @param rotationmatrix an array (3x3) of double
343+
* representing the rotation matrix.
347344
* @throws StructureException ...
348345
*/
349-
public static final void rotate(Structure structure, double[][] rotationmatrix)
350-
throws StructureException
351-
{
346+
public static final void rotate(Structure structure,
347+
double[][] rotationmatrix) throws StructureException {
348+
352349
if ( rotationmatrix.length != 3 ) {
353350
throw new StructureException ("matrix does not have size 3x3 !");
354351
}
@@ -359,13 +356,17 @@ public static final void rotate(Structure structure, double[][] rotationmatrix)
359356
}
360357
}
361358

362-
/** rotate a structure .
359+
/**
360+
* Rotate a Group.
361+
* The rotation Matrix must be a pre-multiplication Matrix.
363362
*
364363
* @param group a group object
365-
* @param rotationmatrix an array (3x3) of double representing the rotation matrix.
364+
* @param rotationmatrix an array (3x3) of double
365+
* representing the rotation matrix.
366366
* @throws StructureException ...
367367
*/
368-
public static final void rotate(Group group, double[][] rotationmatrix) throws StructureException {
368+
public static final void rotate(Group group,
369+
double[][] rotationmatrix) throws StructureException {
369370

370371
if ( rotationmatrix.length != 3 ) {
371372
throw new StructureException ("matrix does not have size 3x3 !");
@@ -380,7 +381,9 @@ public static final void rotate(Group group, double[][] rotationmatrix) throws S
380381
}
381382
}
382383

383-
/** Rotate an atom around a Matrix object.
384+
/**
385+
* Rotate an Atom around a Matrix object.
386+
* The rotation Matrix must be a pre-multiplication Matrix.
384387
*
385388
* @param atom atom to be rotated
386389
* @param m rotation matrix to be applied to the atom
@@ -401,7 +404,9 @@ public static final void rotate(Atom atom, Matrix m){
401404

402405
}
403406

404-
/** Rotate a group object.
407+
/**
408+
* Rotate a group object.
409+
* The rotation Matrix must be a pre-multiplication Matrix.
405410
*
406411
* @param group a group to be rotated
407412
* @param m a Matrix object representing the rotation matrix
@@ -418,7 +423,9 @@ public static final void rotate(Group group, Matrix m){
418423

419424
}
420425

421-
/** Rotate a structure object.
426+
/**
427+
* Rotate a structure object.
428+
* The rotation Matrix must be a pre-multiplication Matrix.
422429
*
423430
* @param structure the structure to be rotated
424431
* @param m rotation matrix to be applied
@@ -437,6 +444,8 @@ public static final void rotate(Structure structure, Matrix m){
437444

438445
/**
439446
* Transform an array of atoms at once.
447+
* The transformation Matrix must be a post-multiplication Matrix.
448+
*
440449
* @param ca array of Atoms to shift
441450
* @param t transformation Matrix4d
442451
*/
@@ -446,7 +455,9 @@ public static void transform(Atom[] ca, Matrix4d t) {
446455

447456
/**
448457
* Transforms an atom object, given a Matrix4d (i.e. the vecmath library
449-
* double-precision 4x4 rotation+translation matrix)
458+
* double-precision 4x4 rotation+translation matrix).
459+
* The transformation Matrix must be a post-multiplication Matrix.
460+
*
450461
* @param atom
451462
* @param m
452463
*/
@@ -462,7 +473,9 @@ public static final void transform (Atom atom, Matrix4d m) {
462473

463474
/**
464475
* Transforms a group object, given a Matrix4d (i.e. the vecmath library
465-
* double-precision 4x4 rotation+translation matrix)
476+
* double-precision 4x4 rotation+translation matrix).
477+
* The transformation Matrix must be a post-multiplication Matrix.
478+
*
466479
* @param group
467480
* @param m
468481
*/
@@ -478,7 +491,9 @@ public static final void transform (Group group, Matrix4d m) {
478491

479492
/**
480493
* Transforms a structure object, given a Matrix4d (i.e. the vecmath library
481-
* double-precision 4x4 rotation+translation matrix)
494+
* double-precision 4x4 rotation+translation matrix).
495+
* The transformation Matrix must be a post-multiplication Matrix.
496+
*
482497
* @param structure
483498
* @param m
484499
*/
@@ -494,7 +509,9 @@ public static final void transform (Structure structure, Matrix4d m) {
494509

495510
/**
496511
* Transforms a chain object, given a Matrix4d (i.e. the vecmath library
497-
* double-precision 4x4 rotation+translation matrix)
512+
* double-precision 4x4 rotation+translation matrix).
513+
* The transformation Matrix must be a post-multiplication Matrix.
514+
*
498515
* @param chain
499516
* @param m
500517
*/

biojava-structure/src/main/java/org/biojava/nbio/structure/align/multiple/AbstractScoresCache.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,36 @@
1515
*/
1616
public abstract class AbstractScoresCache implements ScoresCache {
1717
private Map<String,Double> scores = null;
18-
18+
1919
protected AbstractScoresCache() {
2020
scores = null;
2121
}
2222
protected AbstractScoresCache(AbstractScoresCache cache) {
2323
this.scores = cache.scores;
2424
}
25-
25+
2626
@Override
2727
public void putScore(String property, Double score) {
28-
if(scores == null) {
29-
scores = new HashMap<String, Double>();
30-
}
31-
scores.put(property, score);
28+
if(scores == null) {
29+
scores = new HashMap<String, Double>();
30+
}
31+
scores.put(property, score);
3232
}
33-
33+
3434
@Override
3535
public Double getScore(String property) {
3636
if(scores != null && scores.containsKey(property)) {
3737
return scores.get(property);
3838
}
3939
return null;
4040
}
41-
41+
4242
@Override
4343
public Set<String> getScores() {
4444
if(scores == null) return Collections.emptySet();
4545
return Collections.unmodifiableSet(scores.keySet());
4646
}
47-
47+
4848
/**
4949
* Subclasses should override clone and use the copy constructor.
5050
*
@@ -55,7 +55,7 @@ public Set<String> getScores() {
5555
protected Object clone(Object e) throws CloneNotSupportedException {
5656
throw new CloneNotSupportedException("Subclasses must override clone");
5757
}
58-
58+
5959
/**
6060
* Clear the cached scores. This frees memory after the alignment changed.
6161
*/

biojava-structure/src/main/java/org/biojava/nbio/structure/align/multiple/util/MultipleAlignmentScorer.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
/**
1515
* Utility class for calculating common scores of {@link MultipleAlignment}s.
16-
* <p>
17-
*
1816
*
1917
* @author Spencer Bliven
2018
* @author Aleix Lafita

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

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
import static org.junit.Assert.*;
44

5+
import javax.vecmath.Matrix4d;
6+
import javax.vecmath.Point3d;
7+
8+
import org.biojava.nbio.structure.align.util.RotationAxis;
9+
import org.biojava.nbio.structure.jama.Matrix;
510
import org.junit.Test;
611

712
public class TestCalc {
@@ -35,7 +40,6 @@ public void testAngle() {
3540
assertTrue(Calc.angle(ref, a6)>=0 && Calc.angle(ref,a6)<=180.0);
3641
assertTrue(Calc.angle(ref, a7)>=0 && Calc.angle(ref,a7)<=180.0);
3742

38-
3943
Atom c = getAtom(0,0,0);
4044
Atom d = getAtom(0,0,0);
4145

@@ -80,7 +84,59 @@ public void testTorsionAngle() {
8084
assertEquals(0, Calc.torsionAngle(a, b, c, d),0.00001);
8185
}
8286

87+
@Test
88+
public void testJamaTransformation() {
89+
90+
Atom atom = getAtom(1.0, 1.0, 1.0);
91+
92+
//Identity transform
93+
Matrix identR = Matrix.identity(3, 3);
94+
Atom identT = getAtom(0, 0, 0);
95+
Calc.rotate(atom, identR);
96+
Calc.shift(atom, identT);
97+
98+
Point3d expected = new Point3d(1.0, 1.0, 1.0);
99+
Point3d actual = new Point3d(atom.getCoords());
100+
101+
assertEquals(expected, actual);
102+
103+
//Sample transform: calc transposes automatically the matrix
104+
//because it is a pre-multiplication rotation matrix
105+
Matrix sampleR = Calc.getRotationMatrix(getSampleTransform());
106+
Atom sampleT = Calc.getTranslationVector(getSampleTransform());
107+
Calc.rotate(atom, sampleR);
108+
Calc.shift(atom, sampleT);
109+
110+
expected = new Point3d(2.0, 7.0, -1.3);
111+
actual = new Point3d(atom.getCoords());
112+
113+
assertEquals(expected, actual);
114+
}
83115

116+
@Test
117+
public void testVecmathTransformation() {
118+
119+
Atom atom = getAtom(1.0, 1.0, 1.0);
120+
121+
//Identity transform
122+
Matrix4d ident = new Matrix4d();
123+
ident.setIdentity();
124+
Calc.transform(atom, ident);
125+
126+
Point3d expected = new Point3d(1.0, 1.0, 1.0);
127+
Point3d actual = new Point3d(atom.getCoords());
128+
129+
assertEquals(expected, actual);
130+
131+
//Sample transform
132+
Matrix4d sample = getSampleTransform();
133+
Calc.transform(atom, sample);
134+
135+
expected = new Point3d(2.0, 7.0, -1.3);
136+
actual = new Point3d(atom.getCoords());
137+
138+
assertEquals(expected, actual);
139+
}
84140

85141
private static Atom getAtom(double x, double y, double z) {
86142
Atom a = new AtomImpl();
@@ -89,6 +145,14 @@ private static Atom getAtom(double x, double y, double z) {
89145
a.setZ(z);
90146
return a;
91147
}
92-
93148

149+
private static Matrix4d getSampleTransform(){
150+
151+
Matrix4d sample = new Matrix4d(new double[] {1.0,1.5,0.5,-1.0,
152+
0.5,0.5,1.0,5.0,
153+
0.3,0.4,0.5,-2.5,
154+
0.0,0.0,0.0,1.0});
155+
return sample;
156+
}
157+
94158
}

0 commit comments

Comments
 (0)