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
*/
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;
}
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;
+ }
}
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());
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());
+ }
}