X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=test%2Fjalview%2Fanalysis%2Fscoremodels%2FScoreMatrixTest.java;h=1a5d43c98f91459a5effca9382d25029f9d2716f;hb=bd7b3138379c2db8507fe7e8d25f5a921e2d9df7;hp=5051bf625bc2a3ddfbf2bf0d439ce5fa856daab2;hpb=8333f7266bd69f387a8c9c09cb844b615856a626;p=jalview.git diff --git a/test/jalview/analysis/scoremodels/ScoreMatrixTest.java b/test/jalview/analysis/scoremodels/ScoreMatrixTest.java index 5051bf6..1a5d43c 100644 --- a/test/jalview/analysis/scoremodels/ScoreMatrixTest.java +++ b/test/jalview/analysis/scoremodels/ScoreMatrixTest.java @@ -1,8 +1,11 @@ package jalview.analysis.scoremodels; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNotEquals; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNotSame; +import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; @@ -27,7 +30,7 @@ public class ScoreMatrixTest // note score matrix does not have to be symmetric (though it should be!) float[][] scores = new float[3][]; scores[0] = new float[] { 1f, 2f, 3f }; - scores[1] = new float[] { 4f, 5f, 6f }; + scores[1] = new float[] { -4f, 5f, 6f }; scores[2] = new float[] { 7f, 8f, 9f }; ScoreMatrix sm = new ScoreMatrix("Test", "ABC".toCharArray(), scores); assertEquals(sm.getSize(), 3); @@ -35,9 +38,14 @@ public class ScoreMatrixTest assertEquals(sm.getPairwiseScore('A', 'a'), 1f); assertEquals(sm.getPairwiseScore('b', 'c'), 6f); assertEquals(sm.getPairwiseScore('c', 'b'), 8f); - assertEquals(sm.getPairwiseScore('A', 'D'), 0f); assertEquals(sm.getMatrixIndex('c'), 2); assertEquals(sm.getMatrixIndex(' '), -1); + + // substitution to or from unknown symbol gets minimum score + assertEquals(sm.getPairwiseScore('A', 'D'), -4f); + assertEquals(sm.getPairwiseScore('D', 'A'), -4f); + // unknown-to-self gets a score of 1 + assertEquals(sm.getPairwiseScore('D', 'D'), 1f); } @Test( @@ -76,7 +84,12 @@ public class ScoreMatrixTest @Test(groups = "Functional") public void testBuildSymbolIndex() { - short[] index = ScoreMatrix.buildSymbolIndex("AX-. yxYp".toCharArray()); + float[][] scores = new float[2][]; + scores[0] = new float[] { 1f, 2f }; + scores[1] = new float[] { 3f, 4f }; + ScoreMatrix sm = new ScoreMatrix("Test", new char[] { 'A', '.' }, + scores); + short[] index = sm.buildSymbolIndex("AX-yxYp".toCharArray()); assertEquals(index.length, 128); // ASCII character set size @@ -84,12 +97,10 @@ public class ScoreMatrixTest assertEquals(index['a'], 0); // lower-case mapping added assertEquals(index['X'], 1); assertEquals(index['-'], 2); - assertEquals(index['.'], 3); - assertEquals(index[' '], 4); - assertEquals(index['y'], 5); // lower-case override - assertEquals(index['x'], 6); // lower-case override - assertEquals(index['Y'], 7); - assertEquals(index['p'], 8); + assertEquals(index['y'], 3); // lower-case override + assertEquals(index['x'], 4); // lower-case override + assertEquals(index['Y'], 5); + assertEquals(index['p'], 6); assertEquals(index['P'], -1); // lower-case doesn't map upper-case /* @@ -110,8 +121,13 @@ public class ScoreMatrixTest @Test(groups = "Functional") public void testBuildSymbolIndex_nonAscii() { + float[][] scores = new float[2][]; + scores[0] = new float[] { 1f, 2f }; + scores[1] = new float[] { 3f, 4f }; + ScoreMatrix sm = new ScoreMatrix("Test", new char[] { 'A', '.' }, + scores); char[] weird = new char[] { 128, 245, 'P' }; - short[] index = ScoreMatrix.buildSymbolIndex(weird); + short[] index = sm.buildSymbolIndex(weird); assertEquals(index.length, 128); assertEquals(index['P'], 2); assertEquals(index['p'], 2); @@ -148,10 +164,10 @@ public class ScoreMatrixTest assertEquals(sm.getMatrixIndex('D'), 3); assertEquals(sm.getMatrixIndex('X'), 22); assertEquals(sm.getMatrixIndex('x'), 22); - assertEquals(sm.getMatrixIndex(' '), 23); - assertEquals(sm.getMatrixIndex('*'), 24); - assertEquals(sm.getMatrixIndex('.'), -1); assertEquals(sm.getMatrixIndex('-'), -1); + assertEquals(sm.getMatrixIndex('*'), 23); + assertEquals(sm.getMatrixIndex('.'), -1); + assertEquals(sm.getMatrixIndex(' '), -1); assertEquals(sm.getMatrixIndex('?'), -1); assertEquals(sm.getMatrixIndex((char) 128), -1); } @@ -167,10 +183,9 @@ public class ScoreMatrixTest public void testComputePairwiseScores() { /* - * NB score matrix assumes space for gap - Jalview converts - * space to gap before computing PCA or Tree + * NB score matrix expects '-' for gap */ - String[] seqs = new String[] { "FKL", "R D", "QIA", "GWC" }; + String[] seqs = new String[] { "FKL", "R-D", "QIA", "GWC" }; ScoreMatrix sm = ScoreModels.getInstance().getBlosum62(); MatrixI pairwise = sm.findSimilarities(seqs, SimilarityParams.Jalview); @@ -245,6 +260,12 @@ public class ScoreMatrixTest .toCharArray(), sm.getMatrix()); assertTrue(sm.equals(sm2)); assertEquals(sm.hashCode(), sm2.hashCode()); + + sm2 = ScoreModels.getInstance().getPam250(); + assertFalse(sm.equals(sm2)); + assertNotEquals(sm.hashCode(), sm2.hashCode()); + + assertFalse(sm.equals("hello")); } /** @@ -253,16 +274,11 @@ public class ScoreMatrixTest @Test(groups = "Functional") public void testcomputeSimilarity_matchLongestSequence() { - // TODO params.matchGaps() is not used for ScoreMatrix - // - includeGaps is sufficient (there is no denominator) - // ==> bespoke parameters only 3 booleans? /* - * for now, using space for gap to match callers of - * AlignmentView.getSequenceStrings() - * may change this to '-' (with corresponding change to matrices) + * ScoreMatrix expects '-' for gaps */ - String s1 = "FR K S"; - String s2 = "FS L"; + String s1 = "FR-K-S"; + String s2 = "FS--L"; ScoreMatrix blosum = ScoreModels.getInstance().getBlosum62(); /* @@ -318,16 +334,11 @@ public class ScoreMatrixTest @Test(groups = "Functional") public void testcomputeSimilarity_matchShortestSequence() { - // TODO params.matchGaps() is not used for ScoreMatrix - // - includeGaps is sufficient (there is no denominator) - // ==> bespoke parameters only 3 booleans? /* - * for now, using space for gap to match callers of - * AlignmentView.getSequenceStrings() - * may change this to '-' (with corresponding change to matrices) + * ScoreMatrix expects '-' for gaps */ - String s1 = "FR K S"; - String s2 = "FS L"; + String s1 = "FR-K-S"; + String s2 = "FS--L"; ScoreMatrix blosum = ScoreModels.getInstance().getBlosum62(); /* @@ -408,6 +419,10 @@ public class ScoreMatrixTest { ScoreMatrix sm = ScoreModels.getInstance().getBlosum62(); + assertTrue(sm.isProtein()); + assertFalse(sm.isDNA()); + assertNull(sm.getDescription()); + /* * verify expected scores against ARNDCQEGHILKMFPSTWYVBZX * scraped from https://www.ncbi.nlm.nih.gov/Class/FieldGuide/BLOSUM62.txt @@ -488,4 +503,86 @@ public class ScoreMatrixTest String.format("%s->%s", res, c2)); } } + + @Test(groups = "Functional") + public void testConstructor_gapDash() + { + float[][] scores = new float[2][]; + scores[0] = new float[] { 1f, 2f }; + scores[1] = new float[] { 4f, 5f }; + ScoreMatrix sm = new ScoreMatrix("Test", new char[] { 'A', '-' }, + scores); + assertEquals(sm.getSize(), 2); + assertArrayEquals(scores, sm.getMatrix()); + assertEquals(sm.getPairwiseScore('A', 'a'), 1f); + assertEquals(sm.getPairwiseScore('A', 'A'), 1f); + assertEquals(sm.getPairwiseScore('a', '-'), 2f); + assertEquals(sm.getPairwiseScore('-', 'A'), 4f); + assertEquals(sm.getMatrixIndex('a'), 0); + assertEquals(sm.getMatrixIndex('A'), 0); + assertEquals(sm.getMatrixIndex('-'), 1); + assertEquals(sm.getMatrixIndex(' '), -1); + assertEquals(sm.getMatrixIndex('.'), -1); + } + + @Test(groups = "Functional") + public void testGetPairwiseScore() + { + float[][] scores = new float[2][]; + scores[0] = new float[] { 1f, 2f }; + scores[1] = new float[] { -4f, 5f }; + ScoreMatrix sm = new ScoreMatrix("Test", new char[] { 'A', 'B' }, + scores); + assertEquals(sm.getPairwiseScore('A', 'A'), 1f); + assertEquals(sm.getPairwiseScore('A', 'a'), 1f); + assertEquals(sm.getPairwiseScore('A', 'B'), 2f); + assertEquals(sm.getPairwiseScore('b', 'a'), -4f); + assertEquals(sm.getPairwiseScore('B', 'b'), 5f); + + /* + * unknown symbols currently score minimum score + * or 1 for identity with self + */ + assertEquals(sm.getPairwiseScore('A', '-'), -4f); + assertEquals(sm.getPairwiseScore('-', 'A'), -4f); + assertEquals(sm.getPairwiseScore('-', '-'), 1f); + assertEquals(sm.getPairwiseScore('Q', 'W'), -4f); + assertEquals(sm.getPairwiseScore('Q', 'Q'), 1f); + + /* + * symbols not in basic ASCII set score zero + */ + char c = (char) 200; + assertEquals(sm.getPairwiseScore('Q', c), 0f); + assertEquals(sm.getPairwiseScore(c, 'Q'), 0f); + } + + @Test(groups = "Functional") + public void testGetMinimumScore() + { + ScoreMatrix sm = ScoreModels.getInstance().getBlosum62(); + assertEquals(sm.getMinimumScore(), -4f); + } + + @Test(groups = "Functional") + public void testGetMaximumScore() + { + ScoreMatrix sm = ScoreModels.getInstance().getBlosum62(); + assertEquals(sm.getMaximumScore(), 11f); + } + + @Test(groups = "Functional") + public void testOutputMatrix_html() + { + float[][] scores = new float[2][]; + scores[0] = new float[] { 1f, 2f }; + scores[1] = new float[] { 4f, -5.3E-10f }; + ScoreMatrix sm = new ScoreMatrix("Test", "AB".toCharArray(), scores); + String html = sm.outputMatrix(true); + String expected = "\n" + + "\n" + + "\n" + + "
 A  B 
A1.02.0
B4.0-5.3E-10
"; + assertEquals(html, expected); + } }