From: gmungoc Date: Mon, 25 Mar 2019 15:22:55 +0000 (+0000) Subject: JAL-3215 refactored ScoreColourScheme construction for scriptability X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=a689267090c39792d83ceb5d8f88c539a085fea6;p=jalview.git JAL-3215 refactored ScoreColourScheme construction for scriptability --- diff --git a/src/jalview/schemes/BuriedColourScheme.java b/src/jalview/schemes/BuriedColourScheme.java index 938417d..b2ef14f 100755 --- a/src/jalview/schemes/BuriedColourScheme.java +++ b/src/jalview/schemes/BuriedColourScheme.java @@ -26,33 +26,21 @@ import jalview.datamodel.AnnotatedCollectionI; import java.awt.Color; /** - * DOCUMENT ME! - * - * @author $author$ - * @version $Revision$ + * A graduated colour scheme based on residue buried index score */ public class BuriedColourScheme extends ScoreColourScheme { - /** - * Creates a new BuriedColourScheme object. - */ - public BuriedColourScheme() - { - super(ResidueProperties.aaIndex, ResidueProperties.buried); - } + private static final Color minScoreColour = new Color(0, 255, 0); + + private static final Color maxScoreColour = new Color(0, 0, 255); /** - * DOCUMENT ME! - * - * @param c - * DOCUMENT ME! - * - * @return DOCUMENT ME! + * Constructor */ - @Override - public Color makeColour(float c) + public BuriedColourScheme() { - return new Color(0, (float) (1.0 - c), c); + super(JalviewColourScheme.Buried.toString(), ResidueProperties.aaIndex, + ResidueProperties.buried, minScoreColour, maxScoreColour); } @Override @@ -61,12 +49,6 @@ public class BuriedColourScheme extends ScoreColourScheme return true; } - @Override - public String getSchemeName() - { - return JalviewColourScheme.Buried.toString(); - } - /** * Returns a new instance of this colour scheme with which the given data may * be coloured diff --git a/src/jalview/schemes/ColourSchemes.java b/src/jalview/schemes/ColourSchemes.java index 42465f2..8b70fb9 100644 --- a/src/jalview/schemes/ColourSchemes.java +++ b/src/jalview/schemes/ColourSchemes.java @@ -28,7 +28,7 @@ import jalview.datamodel.SequenceI; import java.util.LinkedHashMap; import java.util.Map; -public class ColourSchemes +public final class ColourSchemes { /* * singleton instance of this class diff --git a/src/jalview/schemes/HelixColourScheme.java b/src/jalview/schemes/HelixColourScheme.java index 10a7ce5..ee7fbe4 100755 --- a/src/jalview/schemes/HelixColourScheme.java +++ b/src/jalview/schemes/HelixColourScheme.java @@ -25,17 +25,19 @@ import jalview.datamodel.AnnotatedCollectionI; import java.awt.Color; +/** + * A graduated colour scheme based on residue helix propensity score + */ public class HelixColourScheme extends ScoreColourScheme { - public HelixColourScheme() - { - super(ResidueProperties.aaIndex, ResidueProperties.helix); - } + private static final Color minScoreColour = new Color(0, 255, 0); - @Override - public Color makeColour(float c) + private static final Color maxScoreColour = new Color(255, 0, 255); + + public HelixColourScheme() { - return new Color(c, (float) 1.0 - c, c); + super(JalviewColourScheme.Helix.toString(), ResidueProperties.aaIndex, + ResidueProperties.helix, minScoreColour, maxScoreColour); } @Override @@ -44,12 +46,6 @@ public class HelixColourScheme extends ScoreColourScheme return true; } - @Override - public String getSchemeName() - { - return JalviewColourScheme.Helix.toString(); - } - /** * Returns a new instance of this colour scheme with which the given data may * be coloured diff --git a/src/jalview/schemes/HydrophobicColourScheme.java b/src/jalview/schemes/HydrophobicColourScheme.java index c8cf4c7..e91f469 100755 --- a/src/jalview/schemes/HydrophobicColourScheme.java +++ b/src/jalview/schemes/HydrophobicColourScheme.java @@ -26,33 +26,26 @@ import jalview.datamodel.AnnotatedCollectionI; import java.awt.Color; /** - * DOCUMENT ME! + * A hydrophobicity colour scheme based on * - * @author $author$ - * @version $Revision$ + *
+ * Kyte, J., and Doolittle, R.F., J. Mol. Biol. 1157, 105-132, 1982
+ * 
*/ public class HydrophobicColourScheme extends ScoreColourScheme { - /** - * Creates a new HydrophobicColourScheme object. - */ - public HydrophobicColourScheme() - { - super(ResidueProperties.aaIndex, ResidueProperties.hyd); - } + private static final Color minScoreColour = new Color(0, 0, 255); + + private static final Color maxScoreColour = new Color(255, 0, 0); /** - * DOCUMENT ME! - * - * @param c - * DOCUMENT ME! - * - * @return DOCUMENT ME! + * Constructor */ - @Override - public Color makeColour(float c) + public HydrophobicColourScheme() { - return new Color(c, (float) 0.0, (float) 1.0 - c); + super(JalviewColourScheme.Hydrophobic.toString(), + ResidueProperties.aaIndex, ResidueProperties.hyd, + minScoreColour, maxScoreColour); } @Override @@ -61,12 +54,6 @@ public class HydrophobicColourScheme extends ScoreColourScheme return true; } - @Override - public String getSchemeName() - { - return JalviewColourScheme.Hydrophobic.toString(); - } - /** * Returns a new instance of this colour scheme with which the given data may * be coloured diff --git a/src/jalview/schemes/ScoreColourScheme.java b/src/jalview/schemes/ScoreColourScheme.java index 442306d..66bb5b4 100755 --- a/src/jalview/schemes/ScoreColourScheme.java +++ b/src/jalview/schemes/ScoreColourScheme.java @@ -38,52 +38,76 @@ import java.awt.Color; */ public class ScoreColourScheme extends ResidueColourScheme { - public double min; + String schemeName; - public double max; + double min; - public double[] scores; + double max; + + Color minColour; + + Color maxColour; + + double[] scores; /** * Constructor * * @param symbolIndex - * a lookup where the index is a character e.g. 'R' or 'r', and the - * value is its position in the colour table lookup + * 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 - * per residue, indices matching colors lookup + * per residue, with indices corresponding to those for colour lookup */ - public ScoreColourScheme(int symbolIndex[], double[] scores) + public ScoreColourScheme(String name, int[] symbolIndex, double[] scores, + Color minColour, Color maxColour) { super(symbolIndex); + this.schemeName = name; + this.minColour = minColour; + this.maxColour = maxColour; + setMinMax(scores); this.scores = scores; - // Make colours in constructor - // Why wasn't this done earlier? int iSize = scores.length; colors = new Color[scores.length]; for (int i = 0; i < iSize; i++) { - /* - * scale score between min and max to the range 0.0 - 1.0 - */ - float score = (float) (scores[i] - (float) min) / (float) (max - min); - - if (score > 1.0f) - { - score = 1.0f; - } - - if (score < 0.0f) - { - score = 0.0f; - } - colors[i] = makeColour(score); + colors[i] = getScoreColour(scores[i]); } } /** + * Computes a colour for a score by + * + * This method is used to make colours for residue scores, but may also be + * called to generate a colour for an intermediate score value (for example, a + * column average). + */ + public Color getScoreColour(double rawScore) + { + float score = (float) (rawScore - (float) min) / (float) (max - min); + score = Math.max(score, 0f); + score = Math.min(score, 1f); + + int r = minColour.getRed() + + Math.round((maxColour.getRed() - minColour.getRed()) * score); + int g = minColour.getGreen() + + Math.round( + (maxColour.getGreen() - minColour.getGreen()) * score); + int b = minColour.getBlue() + + Math.round( + (maxColour.getBlue() - minColour.getBlue()) * score); + + Color c = new Color(r, g, b); + return c; + } + + /** * Inspects score values and saves the minimum and maximum * * @param vals @@ -113,23 +137,10 @@ public class ScoreColourScheme extends ResidueColourScheme return super.findColour(c); } - /** - * DOCUMENT ME! - * - * @param c - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public Color makeColour(float c) - { - return new Color(c, (float) 0.0, (float) 1.0 - c); - } - @Override public String getSchemeName() { - return "Score"; + return schemeName; } /** @@ -140,6 +151,7 @@ public class ScoreColourScheme extends ResidueColourScheme public ColourSchemeI getInstance(AlignViewportI view, AnnotatedCollectionI coll) { - return new ScoreColourScheme(symbolIndex, scores); + return new ScoreColourScheme(schemeName, symbolIndex, scores, minColour, + maxColour); } } diff --git a/src/jalview/schemes/StrandColourScheme.java b/src/jalview/schemes/StrandColourScheme.java index c1b15e4..46d2c14 100755 --- a/src/jalview/schemes/StrandColourScheme.java +++ b/src/jalview/schemes/StrandColourScheme.java @@ -26,33 +26,21 @@ import jalview.datamodel.AnnotatedCollectionI; import java.awt.Color; /** - * DOCUMENT ME! - * - * @author $author$ - * @version $Revision$ + * A graduated colour scheme based on residue strand propensity */ public class StrandColourScheme extends ScoreColourScheme { - /** - * Creates a new StrandColourScheme object. - */ - public StrandColourScheme() - { - super(ResidueProperties.aaIndex, ResidueProperties.strand); - } + private static final Color minScoreColour = new Color(0, 0, 255); + + private static final Color maxScoreColour = new Color(255, 255, 0); /** - * DOCUMENT ME! - * - * @param c - * DOCUMENT ME! - * - * @return DOCUMENT ME! + * Constructor */ - @Override - public Color makeColour(float c) + public StrandColourScheme() { - return new Color(c, c, (float) 1.0 - c); + super(JalviewColourScheme.Strand.toString(), ResidueProperties.aaIndex, + ResidueProperties.strand, minScoreColour, maxScoreColour); } @Override @@ -61,12 +49,6 @@ public class StrandColourScheme extends ScoreColourScheme return true; } - @Override - public String getSchemeName() - { - return JalviewColourScheme.Strand.toString(); - } - /** * Returns a new instance of this colour scheme with which the given data may * be coloured diff --git a/src/jalview/schemes/TurnColourScheme.java b/src/jalview/schemes/TurnColourScheme.java index b8b2d90..4f354a5 100755 --- a/src/jalview/schemes/TurnColourScheme.java +++ b/src/jalview/schemes/TurnColourScheme.java @@ -26,33 +26,21 @@ import jalview.datamodel.AnnotatedCollectionI; import java.awt.Color; /** - * DOCUMENT ME! - * - * @author $author$ - * @version $Revision$ + * A graduated colour scheme based on residue turn propensity */ public class TurnColourScheme extends ScoreColourScheme { - /** - * Creates a new TurnColourScheme object. - */ - public TurnColourScheme() - { - super(ResidueProperties.aaIndex, ResidueProperties.turn); - } + private static final Color minScoreColour = new Color(0, 255, 255); + + private static final Color maxScoreColour = new Color(255, 0, 0); /** - * DOCUMENT ME! - * - * @param c - * DOCUMENT ME! - * - * @return DOCUMENT ME! + * Constructor */ - @Override - public Color makeColour(float c) + public TurnColourScheme() { - return new Color(c, 1 - c, 1 - c); + super(JalviewColourScheme.Turn.toString(), ResidueProperties.aaIndex, + ResidueProperties.turn, minScoreColour, maxScoreColour); } @Override @@ -61,12 +49,6 @@ public class TurnColourScheme extends ScoreColourScheme return true; } - @Override - public String getSchemeName() - { - return JalviewColourScheme.Turn.toString(); - } - /** * Returns a new instance of this colour scheme with which the given data may * be coloured diff --git a/test/jalview/schemes/ResidueColourSchemeTest.java b/test/jalview/schemes/ResidueColourSchemeTest.java index decd9dd..2c9f62c 100644 --- a/test/jalview/schemes/ResidueColourSchemeTest.java +++ b/test/jalview/schemes/ResidueColourSchemeTest.java @@ -102,9 +102,13 @@ public class ResidueColourSchemeTest */ assertTrue(new UserColourScheme().isApplicableTo(peptide)); assertTrue(new UserColourScheme().isApplicableTo(nucleotide)); - assertTrue(new ScoreColourScheme(new int[] {}, new double[] {}) + assertTrue( + new ScoreColourScheme("george", new int[] {}, new double[] {}, + null, null) .isApplicableTo(peptide)); - assertTrue(new ScoreColourScheme(new int[] {}, new double[] {}) + assertTrue( + new ScoreColourScheme("jane", new int[] {}, new double[] {}, + null, null) .isApplicableTo(nucleotide)); ResidueColourScheme rcs = new PIDColourScheme(); assertTrue(rcs.isApplicableTo(peptide)); @@ -189,8 +193,8 @@ public class ResidueColourSchemeTest assertEquals("RNA Interaction type", new RNAInteractionColourScheme().getSchemeName()); assertEquals("User Defined", new UserColourScheme().getSchemeName()); - assertEquals("Score", new ScoreColourScheme(new int[] {}, - new double[] {}).getSchemeName()); + assertEquals("Score", new ScoreColourScheme("Score", new int[] {}, + new double[] {}, null, null).getSchemeName()); assertEquals("% Identity", new PIDColourScheme().getSchemeName()); assertEquals("Follower", new FollowerColourScheme().getSchemeName()); assertEquals("T-Coffee Scores",