From 7c5c70f4bbe69a722dd6e8bd269d7d01e1089034 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Tue, 21 Feb 2017 10:13:06 +0000 Subject: [PATCH] JAL-2416 more validation in ScoreMatrix constructor --- src/jalview/analysis/scoremodels/ScoreMatrix.java | 29 +++++++++- .../analysis/scoremodels/ScoreMatrixTest.java | 61 +++++++++++++++++++- 2 files changed, 88 insertions(+), 2 deletions(-) diff --git a/src/jalview/analysis/scoremodels/ScoreMatrix.java b/src/jalview/analysis/scoremodels/ScoreMatrix.java index 51d8276..eb94817 100644 --- a/src/jalview/analysis/scoremodels/ScoreMatrix.java +++ b/src/jalview/analysis/scoremodels/ScoreMatrix.java @@ -58,7 +58,9 @@ public class ScoreMatrix implements PairwiseScoreModelI private boolean peptide; /** - * Constructor + * Constructor given a name, symbol alphabet, and matrix of scores for pairs + * of symbols. The matrix should be square and of the same size as the + * alphabet, for example 20x20 for a 20 symbol alphabet. * * @param name * Unique, human readable name for the matrix @@ -69,6 +71,20 @@ public class ScoreMatrix implements PairwiseScoreModelI */ public ScoreMatrix(String name, char[] alphabet, float[][] matrix) { + if (alphabet.length != matrix.length) + { + throw new IllegalArgumentException( + "score matrix size must match alphabet size"); + } + for (float[] row : matrix) + { + if (row.length != alphabet.length) + { + throw new IllegalArgumentException( + "score matrix size must be square"); + } + } + this.matrix = matrix; this.name = name; this.symbols = alphabet; @@ -265,4 +281,15 @@ public class ScoreMatrix implements PairwiseScoreModelI } return sb.toString(); } + + /** + * Answers the number of symbols coded for (also equal to the number of rows + * and columns of the score matrix) + * + * @return + */ + public int getSize() + { + return symbols.length; + } } diff --git a/test/jalview/analysis/scoremodels/ScoreMatrixTest.java b/test/jalview/analysis/scoremodels/ScoreMatrixTest.java index fcc0bc3..0a9dd25 100644 --- a/test/jalview/analysis/scoremodels/ScoreMatrixTest.java +++ b/test/jalview/analysis/scoremodels/ScoreMatrixTest.java @@ -1,12 +1,64 @@ package jalview.analysis.scoremodels; - import static org.testng.Assert.assertEquals; +import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; import org.testng.annotations.Test; public class ScoreMatrixTest { @Test(groups = "Functional") + public void testConstructor() + { + // 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[2] = new float[] { 7f, 8f, 9f }; + ScoreMatrix sm = new ScoreMatrix("Test", "ABC".toCharArray(), scores); + assertEquals(sm.getSize(), 3); + assertArrayEquals(scores, sm.getMatrix()); + 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); + } + + @Test( + groups = "Functional", + expectedExceptions = { IllegalArgumentException.class }) + public void testConstructor_matrixTooSmall() + { + float[][] scores = new float[2][]; + scores[0] = new float[] { 1f, 2f }; + scores[1] = new float[] { 3f, 4f }; + new ScoreMatrix("Test", "ABC".toCharArray(), scores); + } + + @Test( + groups = "Functional", + expectedExceptions = { IllegalArgumentException.class }) + public void testConstructor_matrixTooBig() + { + float[][] scores = new float[2][]; + scores[0] = new float[] { 1f, 2f }; + scores[1] = new float[] { 3f, 4f }; + new ScoreMatrix("Test", "A".toCharArray(), scores); + } + + @Test( + groups = "Functional", + expectedExceptions = { IllegalArgumentException.class }) + public void testConstructor_matrixNotSquare() + { + float[][] scores = new float[2][]; + scores[0] = new float[] { 1f, 2f }; + scores[1] = new float[] { 3f }; + new ScoreMatrix("Test", "AB".toCharArray(), scores); + } + + @Test(groups = "Functional") public void testBuildSymbolIndex() { short[] index = ScoreMatrix.buildSymbolIndex("AX-. yxYp".toCharArray()); @@ -75,4 +127,11 @@ public class ScoreMatrixTest assertEquals(sm.getMatrixIndex('?'), -1); assertEquals(sm.getMatrixIndex((char) 128), -1); } + + @Test(groups = "Functional") + public void testGetSize() + { + ScoreMatrix sm = ScoreModels.getInstance().getBlosum62(); + assertEquals(sm.getMatrix().length, sm.getSize()); + } } -- 1.7.10.2