X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=test%2Fjalview%2Fanalysis%2Fscoremodels%2FScoreMatrixTest.java;h=01de7415bd806e0c032b87af95c4bcaeec701d88;hb=7f5ab7d1f58d870622968e0e6a430f33403b8e4f;hp=0a9dd25668112998c583c97a4b362b70bb534833;hpb=7c5c70f4bbe69a722dd6e8bd269d7d01e1089034;p=jalview.git diff --git a/test/jalview/analysis/scoremodels/ScoreMatrixTest.java b/test/jalview/analysis/scoremodels/ScoreMatrixTest.java index 0a9dd25..01de741 100644 --- a/test/jalview/analysis/scoremodels/ScoreMatrixTest.java +++ b/test/jalview/analysis/scoremodels/ScoreMatrixTest.java @@ -1,7 +1,20 @@ package jalview.analysis.scoremodels; + import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertNotSame; +import static org.testng.Assert.assertTrue; import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; +import jalview.io.DataSourceType; +import jalview.io.FileParse; +import jalview.io.ScoreMatrixFile; +import jalview.math.MatrixI; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.util.Arrays; + import org.testng.annotations.Test; public class ScoreMatrixTest @@ -110,6 +123,19 @@ public class ScoreMatrixTest } @Test(groups = "Functional") + public void testGetMatrix() + { + ScoreMatrix sm = ScoreModels.getInstance().getBlosum62(); + float[][] m = sm.getMatrix(); + assertEquals(m.length, sm.getSize()); + assertEquals(m[2][4], -3f); + // verify a defensive copy is returned + float[][] m2 = sm.getMatrix(); + assertNotSame(m, m2); + assertTrue(Arrays.deepEquals(m, m2)); + } + + @Test(groups = "Functional") public void testGetMatrixIndex() { ScoreMatrix sm = ScoreModels.getInstance().getBlosum62(); @@ -134,4 +160,88 @@ public class ScoreMatrixTest ScoreMatrix sm = ScoreModels.getInstance().getBlosum62(); assertEquals(sm.getMatrix().length, sm.getSize()); } + + @Test(groups = "Functional") + public void testComputePairwiseScores() + { + /* + * NB score matrix assumes space for gap - Jalview converts + * space to gap before computing PCA or Tree + */ + String[] seqs = new String[] { "FKL", "R D", "QIA", "GWC" }; + ScoreMatrix sm = ScoreModels.getInstance().getBlosum62(); + + MatrixI pairwise = sm.findSimilarities(seqs); + + /* + * should be NxN where N = number of sequences + */ + assertEquals(pairwise.height(), 4); + assertEquals(pairwise.width(), 4); + + /* + * should be symmetrical (because BLOSUM62 is) + */ + for (int i = 0; i < pairwise.height(); i++) + { + for (int j = i + 1; j < pairwise.width(); j++) + { + assertEquals(pairwise.getValue(i, j), pairwise.getValue(j, i), + String.format("Not symmetric at [%d, %d]", i, j)); + } + } + /* + * verify expected BLOSUM dot product scores + */ + // F.F + K.K + L.L = 6 + 5 + 4 = 15 + assertEquals(pairwise.getValue(0, 0), 15d); + // R.R + -.- + D.D = 5 + 1 + 6 = 12 + assertEquals(pairwise.getValue(1, 1), 12d); + // Q.Q + I.I + A.A = 5 + 4 + 4 = 13 + assertEquals(pairwise.getValue(2, 2), 13d); + // G.G + W.W + C.C = 6 + 11 + 9 = 26 + assertEquals(pairwise.getValue(3, 3), 26d); + // F.R + K.- + L.D = -3 + -4 + -4 = -11 + assertEquals(pairwise.getValue(0, 1), -11d); + // F.Q + K.I + L.A = -3 + -3 + -1 = -7 + assertEquals(pairwise.getValue(0, 2), -7d); + // F.G + K.W + L.C = -3 + -3 + -1 = -7 + assertEquals(pairwise.getValue(0, 3), -7d); + // R.Q + -.I + D.A = 1 + -4 + -2 = -5 + assertEquals(pairwise.getValue(1, 2), -5d); + // R.G + -.W + D.C = -2 + -4 + -3 = -9 + assertEquals(pairwise.getValue(1, 3), -9d); + // Q.G + I.W + A.C = -2 + -3 + 0 = -5 + assertEquals(pairwise.getValue(2, 3), -5d); + } + + /** + * Test that the result of outputMatrix can be reparsed to give an identical + * ScoreMatrix + * + * @throws IOException + * @throws MalformedURLException + */ + @Test(groups = "Functional") + public void testOutputMatrix_roundTrip() throws MalformedURLException, + IOException + { + ScoreMatrix sm = ScoreModels.getInstance().getBlosum62(); + String output = sm.outputMatrix(false); + FileParse fp = new FileParse(output, DataSourceType.PASTE); + ScoreMatrixFile parser = new ScoreMatrixFile(fp); + ScoreMatrix sm2 = parser.parseMatrix(); + assertNotNull(sm2); + assertTrue(sm2.equals(sm)); + } + + @Test(groups = "Functional") + public void testEqualsAndHashCode() + { + ScoreMatrix sm = ScoreModels.getInstance().getBlosum62(); + ScoreMatrix sm2 = new ScoreMatrix(sm.getName(), sm.getSymbols() + .toCharArray(), sm.getMatrix()); + assertTrue(sm.equals(sm2)); + assertEquals(sm.hashCode(), sm2.hashCode()); + } }