From: gmungoc Date: Tue, 6 Jun 2017 14:00:25 +0000 (+0100) Subject: JAL-2589 correct colour for Gap in Score and User Defined schemes X-Git-Tag: Release_2_10_3b1~132^2~11^2^2~2 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=201706071c8ec2ca6c8a6002db68a4f949f2279c;p=jalview.git JAL-2589 correct colour for Gap in Score and User Defined schemes --- diff --git a/src/jalview/schemes/ResidueColourScheme.java b/src/jalview/schemes/ResidueColourScheme.java index 03fc129..b47b82e 100755 --- a/src/jalview/schemes/ResidueColourScheme.java +++ b/src/jalview/schemes/ResidueColourScheme.java @@ -24,7 +24,6 @@ import jalview.datamodel.AnnotatedCollectionI; import jalview.datamodel.SequenceCollectionI; import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; -import jalview.util.Comparison; import java.awt.Color; import java.util.Map; @@ -91,7 +90,7 @@ public abstract class ResidueColourScheme implements ColourSchemeI { Color colour = Color.white; - if (!Comparison.isGap(c) && colors != null && symbolIndex != null + if (colors != null && symbolIndex != null && c < symbolIndex.length && symbolIndex[c] < colors.length) { diff --git a/src/jalview/schemes/ScoreColourScheme.java b/src/jalview/schemes/ScoreColourScheme.java index aa20121..e1b60ca 100755 --- a/src/jalview/schemes/ScoreColourScheme.java +++ b/src/jalview/schemes/ScoreColourScheme.java @@ -23,6 +23,7 @@ package jalview.schemes; import jalview.datamodel.AnnotatedCollectionI; import jalview.datamodel.SequenceCollectionI; import jalview.datamodel.SequenceI; +import jalview.util.Comparison; import java.awt.Color; import java.util.Map; @@ -35,13 +36,10 @@ import java.util.Map; */ public class ScoreColourScheme extends ResidueColourScheme { - /** DOCUMENT ME!! */ public double min; - /** DOCUMENT ME!! */ public double max; - /** DOCUMENT ME!! */ public double[] scores; /** @@ -65,25 +63,38 @@ public class ScoreColourScheme extends ResidueColourScheme // Make colours in constructor // Why wasn't this done earlier? - int i, iSize = scores.length; + int iSize = scores.length; colors = new Color[scores.length]; - for (i = 0; i < iSize; i++) + for (int i = 0; i < iSize; i++) { - float red = (float) (scores[i] - (float) min) / (float) (max - min); + /* + * scale score between min and max to the range 0.0 - 1.0 + */ + float score = (float) (scores[i] - (float) min) / (float) (max - min); - if (red > 1.0f) + if (score > 1.0f) { - red = 1.0f; + score = 1.0f; } - if (red < 0.0f) + if (score < 0.0f) { - red = 0.0f; + score = 0.0f; } - colors[i] = makeColour(red); + colors[i] = makeColour(score); } } + @Override + public Color findColour(char c, int j, SequenceI seq) + { + if (Comparison.isGap(c)) + { + return Color.white; + } + return super.findColour(c); + } + /** * DOCUMENT ME! * diff --git a/test/jalview/schemes/BuriedColourSchemeTest.java b/test/jalview/schemes/BuriedColourSchemeTest.java new file mode 100644 index 0000000..cdb0e80 --- /dev/null +++ b/test/jalview/schemes/BuriedColourSchemeTest.java @@ -0,0 +1,33 @@ +package jalview.schemes; + +import static org.testng.Assert.assertEquals; + +import java.awt.Color; + +import org.testng.annotations.Test; + +public class BuriedColourSchemeTest +{ + /** + * Turn colours are based on the scores in ResidueProperties.buried A = 1.7, R + * = 0.1, N = 0.4, D = 0.4... min = 0.05 max = 4.6 + *

+ * scores are scaled to c 0-1 between min and max and colour is (0, 1-c, c) + */ + @Test(groups = "Functional") + public void testFindColour() + { + ScoreColourScheme scheme = new BuriedColourScheme(); + + float min = 0.05f; + float max = 4.6f; + float a = (1.7f - min) / (max - min); + assertEquals(scheme.findColour('A', 0, null), new Color(0, 1 - a, a)); + + float d = (0.4f - min) / (max - min); + assertEquals(scheme.findColour('D', 0, null), new Color(0, 1 - d, d)); + + assertEquals(scheme.findColour('-', 0, null), Color.WHITE); + } + +} diff --git a/test/jalview/schemes/HelixColourSchemeTest.java b/test/jalview/schemes/HelixColourSchemeTest.java new file mode 100644 index 0000000..ed46419 --- /dev/null +++ b/test/jalview/schemes/HelixColourSchemeTest.java @@ -0,0 +1,33 @@ +package jalview.schemes; + +import static org.testng.Assert.assertEquals; + +import java.awt.Color; + +import org.testng.annotations.Test; + +public class HelixColourSchemeTest +{ + /** + * Turn colours are based on the scores in ResidueProperties.helix A = 1.42, R + * = 0.98, N = 0.67, D = 1.01... min = 0.57 max = 1.51 + *

+ * scores are scaled to c 0-1 between min and max and colour is (c, 1-c, c) + */ + @Test(groups = "Functional") + public void testFindColour() + { + ScoreColourScheme scheme = new HelixColourScheme(); + + float min = 0.57f; + float max = 1.51f; + float a = (1.42f - min) / (max - min); + assertEquals(scheme.findColour('A', 0, null), new Color(a, 1 - a, a)); + + float d = (1.01f - min) / (max - min); + assertEquals(scheme.findColour('D', 0, null), new Color(d, 1 - d, d)); + + assertEquals(scheme.findColour('-', 0, null), Color.WHITE); + } + +} diff --git a/test/jalview/schemes/HydrophobicColourSchemeTest.java b/test/jalview/schemes/HydrophobicColourSchemeTest.java new file mode 100644 index 0000000..f3b93a9 --- /dev/null +++ b/test/jalview/schemes/HydrophobicColourSchemeTest.java @@ -0,0 +1,35 @@ +package jalview.schemes; + +import static org.testng.Assert.assertEquals; + +import java.awt.Color; + +import org.testng.annotations.Test; + +public class HydrophobicColourSchemeTest +{ + /** + * Turn colours are based on the scores in ResidueProperties.hyd A = 1.8, R = + * -4.5, N = -3.5, D = -3.5... min = -3.9 max = 4.5 + *

+ * scores are scaled to c 0-1 between min and max and colour is (c, 0, 1-c) + */ + @Test(groups = "Functional") + public void testFindColour() + { + ScoreColourScheme scheme = new HydrophobicColourScheme(); + + float min = -3.9f; + float max = 4.5f; + float a = (1.8f - min) / (max - min); + assertEquals(scheme.findColour('A', 0, null), + new Color(a, 0, 1 - a)); + + float d = (-3.5f - min) / (max - min); + assertEquals(scheme.findColour('D', 0, null), + new Color(d, 0, 1 - d)); + + assertEquals(scheme.findColour('-', 0, null), Color.WHITE); + } + +} diff --git a/test/jalview/schemes/StrandColourSchemeTest.java b/test/jalview/schemes/StrandColourSchemeTest.java new file mode 100644 index 0000000..4a8d0e9 --- /dev/null +++ b/test/jalview/schemes/StrandColourSchemeTest.java @@ -0,0 +1,35 @@ +package jalview.schemes; + +import static org.testng.Assert.assertEquals; + +import java.awt.Color; + +import org.testng.annotations.Test; + +public class StrandColourSchemeTest +{ + /** + * Turn colours are based on the scores in ResidueProperties.strand A = 0.83, + * R = 0.93, N = 0.89, D = 0.54... min = 0.37 max = 1.7 + *

+ * scores are scaled to c 0-1 between min and max and colour is (c, c, 1-c) + */ + @Test(groups = "Functional") + public void testFindColour() + { + ScoreColourScheme scheme = new StrandColourScheme(); + + float min = 0.37f; + float max = 1.7f; + float a = (0.83f - min) / (max - min); + assertEquals(scheme.findColour('A', 0, null), + new Color(a, a, 1 - a)); + + float d = (0.54f - min) / (max - min); + assertEquals(scheme.findColour('D', 0, null), + new Color(d, d, 1 - d)); + + assertEquals(scheme.findColour('-', 0, null), Color.WHITE); + } + +} diff --git a/test/jalview/schemes/TurnColourSchemeTest.java b/test/jalview/schemes/TurnColourSchemeTest.java new file mode 100644 index 0000000..5e6baf0 --- /dev/null +++ b/test/jalview/schemes/TurnColourSchemeTest.java @@ -0,0 +1,35 @@ +package jalview.schemes; + +import static org.testng.Assert.assertEquals; + +import java.awt.Color; + +import org.testng.annotations.Test; + +public class TurnColourSchemeTest +{ + /** + * Turn colours are based on the scores in ResidueProperties.turn A = 0.66, R + * = 0.95, N = 1.56, D = 1.46... min = 0.47 max = 1.56 + *

+ * scores are scaled to c 0-1 between min and max and colour is (c, 1-c, 1-c) + */ + @Test(groups = "Functional") + public void testFindColour() + { + ScoreColourScheme scheme = new TurnColourScheme(); + + float min = 0.47f; + float max = 1.56f; + float a = (0.66f - min) / (max - min); + assertEquals(scheme.findColour('A', 0, null), + new Color(a, 1 - a, 1 - a)); + + float d = (1.46f - min) / (max - min); + assertEquals(scheme.findColour('D', 0, null), + new Color(d, 1 - d, 1 - d)); + + assertEquals(scheme.findColour('-', 0, null), Color.WHITE); + } + +} diff --git a/test/jalview/schemes/UserColourSchemeTest.java b/test/jalview/schemes/UserColourSchemeTest.java index 497014e..2a482ee 100644 --- a/test/jalview/schemes/UserColourSchemeTest.java +++ b/test/jalview/schemes/UserColourSchemeTest.java @@ -54,6 +54,10 @@ public class UserColourSchemeTest assertEquals(c1, cs.findColour('h')); Color c2 = new Color(10, 20, 30); assertEquals(c2, cs.findColour('c')); + assertEquals(Color.WHITE, cs.findColour('G')); + assertEquals(Color.WHITE, cs.findColour('-')); + assertEquals(Color.WHITE, cs.findColour('.')); + assertEquals(Color.WHITE, cs.findColour(' ')); cs = new UserColourScheme("white"); cs.parseAppletParameter("D,E=red; K,R,H=0022FF; c=10 , 20,30;t=orange;lowercase=blue;s=pink"); @@ -80,4 +84,29 @@ public class UserColourSchemeTest String param = cs.toAppletParameter(); assertEquals("D,E=ff0000;H,K,R=0022ff;c=0a141e", param); } + + /** + * Test for user colour scheme constructed with a colour per residue, + * including gap. Note this can currently be done from the User Defined + * Colours dialog, but not by parsing a colours parameter, as + * parseAppletParameter only recognises amino acid codes. + */ + @Test(groups = "Functional") + public void testConstructor_coloursArray() + { + Color g = Color.green; + Color y = Color.yellow; + Color b = Color.blue; + Color r = Color.red; + // colours for ARNDCQEGHILKMFPSTWYVBZ and gap + Color[] colours = new Color[] { g, y, b, r, g, y, r, b, g, y, r, b, g, + y, r, b, g, y, r, b, g, y, r, g }; + UserColourScheme cs = new UserColourScheme(colours); + + assertEquals(g, cs.findColour('A')); + assertEquals(b, cs.findColour('n')); + assertEquals(g, cs.findColour('-')); + assertEquals(g, cs.findColour('.')); + assertEquals(g, cs.findColour(' ')); + } }