From b5e91918d2443923adb989782372df090206a73c Mon Sep 17 00:00:00 2001 From: gmungoc Date: Mon, 25 Mar 2019 16:10:52 +0000 Subject: [PATCH] JAL-3215 extract method, unit test, bug fix --- src/jalview/schemes/ScoreColourScheme.java | 31 ++++++++++---- test/jalview/schemes/ScoreColourSchemeTest.java | 49 +++++++++++++++++++++++ 2 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 test/jalview/schemes/ScoreColourSchemeTest.java diff --git a/src/jalview/schemes/ScoreColourScheme.java b/src/jalview/schemes/ScoreColourScheme.java index 66bb5b4..aef5f22 100755 --- a/src/jalview/schemes/ScoreColourScheme.java +++ b/src/jalview/schemes/ScoreColourScheme.java @@ -53,13 +53,18 @@ public class ScoreColourScheme extends ResidueColourScheme /** * Constructor * + * @param name * @param symbolIndex * a lookup where the index is a char e.g. 'R' or 'r', and the value * is its position in the colour table lookup - * @param scores + * @param theScores * per residue, with indices corresponding to those for colour lookup + * @param minColour + * graduated colour at minimum score value + * @param maxColour + * graduated colour at maximum score value */ - public ScoreColourScheme(String name, int[] symbolIndex, double[] scores, + public ScoreColourScheme(String name, int[] symbolIndex, double[] theScores, Color minColour, Color maxColour) { super(symbolIndex); @@ -67,14 +72,24 @@ public class ScoreColourScheme extends ResidueColourScheme this.minColour = minColour; this.maxColour = maxColour; - setMinMax(scores); - this.scores = scores; + setMinMax(theScores); + computeColours(theScores); + } + + /** + * Calculates and saves a graduated score based colour for each residue + * + * @param theScores + */ + protected void computeColours(double[] theScores) + { + this.scores = theScores; - int iSize = scores.length; - colors = new Color[scores.length]; + int iSize = theScores.length; + colors = new Color[theScores.length]; for (int i = 0; i < iSize; i++) { - colors[i] = getScoreColour(scores[i]); + colors[i] = getScoreColour(theScores[i]); } } @@ -117,7 +132,7 @@ public class ScoreColourScheme extends ResidueColourScheme double dMin = Double.MAX_VALUE; double dMax = -Double.MAX_VALUE; - for (int i = 0; i < vals.length - 1; i++) + for (int i = 0; i < vals.length; i++) { dMin = Math.min(dMin, vals[i]); dMax = Math.max(dMax, vals[i]); diff --git a/test/jalview/schemes/ScoreColourSchemeTest.java b/test/jalview/schemes/ScoreColourSchemeTest.java new file mode 100644 index 0000000..1cba878 --- /dev/null +++ b/test/jalview/schemes/ScoreColourSchemeTest.java @@ -0,0 +1,49 @@ +package jalview.schemes; + +import static org.testng.Assert.assertEquals; + +import java.awt.Color; + +import org.testng.annotations.Test; + +public class ScoreColourSchemeTest +{ + @Test + public void testGetScoreColour() + { + Color minColour = new Color(100, 150, 50); + Color maxColour = new Color(20, 220, 90); + + double[] scores = new double[] { 6.2d /* A */, -2.4d /* R */, + 7.9d /* N */ }; + ScoreColourScheme cs = new ScoreColourScheme("test", + ResidueProperties.aaIndex, scores, minColour, maxColour); + + /* + * basic sanity checks + */ + assertEquals(cs.min, -2.4d); + assertEquals(cs.max, 7.9d); + assertEquals(cs.minColour, minColour); + assertEquals(cs.maxColour, maxColour); + assertEquals(cs.findColour('R'), minColour); + assertEquals(cs.findColour('n'), maxColour); + + /* + * a score of 3 gets a graduated colour between (100, 150, 50) (-2.4) and + * (20, 220, 90) (7.9) + */ + double score = 3d; + float f = (float) ((score - cs.min)/ (cs.max - cs.min)); // 5.4 / 10.3 + assertEquals(f, 0.52427, 0.0001d); + int expectR = Math.round(100 + f * (20 - 100)); // 100 - 41.94 = 58.06 = 58 + assertEquals(expectR, 58); + int expectG = Math.round(150 + f * (220 - 150)); // 150 + 36.699 = 187 + assertEquals(expectG, 187); + int expectB = Math.round(50 + f * (90 - 50)); // 50 + 20.97 = 71 + assertEquals(expectB, 71); + + assertEquals(cs.getScoreColour(score), + new Color(expectR, expectG, expectB)); + } +} -- 1.7.10.2