From bffd5239316cc37765480e532ed8f153d512732b Mon Sep 17 00:00:00 2001 From: gmungoc Date: Mon, 9 Jan 2017 11:54:27 +0000 Subject: [PATCH] JAL-2371 remove ColourSchemeI.findColour(c), pure interface groovy script --- examples/groovy/colourSchemes.groovy | 105 ++++++++++++++------ src/MCview/PDBChain.java | 6 +- src/jalview/appletgui/UserDefinedColours.java | 2 +- src/jalview/ext/jmol/JalviewJmolBinding.java | 4 +- .../ext/rbvi/chimera/JalviewChimeraBinding.java | 2 +- src/jalview/gui/UserDefinedColours.java | 3 +- src/jalview/schemes/ColourSchemeI.java | 9 -- src/jalview/schemes/ResidueColourScheme.java | 1 - test/jalview/schemes/ColourSchemePropertyTest.java | 3 +- 9 files changed, 85 insertions(+), 50 deletions(-) diff --git a/examples/groovy/colourSchemes.groovy b/examples/groovy/colourSchemes.groovy index 27e179b..a5b60c8 100644 --- a/examples/groovy/colourSchemes.groovy +++ b/examples/groovy/colourSchemes.groovy @@ -1,54 +1,92 @@ import java.awt.Color; -import jalview.schemes.ResidueColourScheme; +import jalview.schemes.ColourSchemeI; import jalview.schemes.ColourSchemes; import jalview.datamodel.AnnotatedCollectionI; import jalview.datamodel.SequenceI; +import jalview.datamodel.SequenceCollectionI; /* * Example script that registers two new alignment colour schemes */ /* - * Class that defines a colour scheme where odd columns are red, - * even numbered columns are blue, and gaps are yellow + * Closure that defines a colour scheme where consensus residues are pink, + * other residues are red in odd columns and blue in even columns, and + * gaps are yellow */ -class Stripy extends ResidueColourScheme { - Stripy() { } - String getSchemeName() { "stripy" } - Stripy getInstance(AnnotatedCollectionI coll, Map map) { new Stripy() } - Color findColour(char res, int col, SequenceI seq) - { +def candy +candy = { -> + [ + /* + * name shown in the colour menu + */ + getSchemeName: { -> 'candy' }, + + /* + * to make a new instance for each alignment view + */ + getInstance: { AnnotatedCollectionI coll, Map map -> candy() }, + + /* + * method only needed if colour scheme has to recalculate + * values when an alignment is modified + */ + alignmentChanged: { AnnotatedCollectionI coll, Map map -> }, + + /* + * determine colour for a residue at an aligned position of a + * sequence, given consensus residue(s) for the column and the + * consensus percentage identity score for the column + */ + findColour: { char res, int col, SequenceI seq, String consensus, float pid -> if (res == ' ' || res == '-' || res == '.') { Color.yellow - } else if (col % 2 == 0) - { + } else if (consensus.contains(String.valueOf(res))) + { + Color.pink + } else if (col % 2 == 0) + { Color.blue - } else - { + } else + { Color.red - } - } + } + }, + + /* + * true means applicable to nucleotide or peptide data + */ + isApplicableTo: {AnnotatedCollectionI coll -> true}, + + /* + * simple colour schemes are those that depend on the residue + * only (these are also available to colour structure viewers) + */ + isSimple: { false } + ] as ColourSchemeI } /* - * Class that defines a colour scheme graduated - * (approximately) by amino acid weight + * A closure that defines a colour scheme graduated + * (approximately) by amino acid weight + * here from lightest (G) Blue, to heaviest (W) Red */ -class ByWeight extends ResidueColourScheme { - int min = 75 - int max = 204 - ByWeight() { } - boolean isPeptideSpecific() {true} - String getSchemeName() { "By Weight" } - ByWeight getInstance(AnnotatedCollectionI coll, Map map) { new ByWeight() } - Color makeColour(int weight) - { - int i = 255 * (weight - min) / (max - min); - new Color(i, 0, i); - } - Color findColour(char res, int col, SequenceI seq) - { +def makeColour = { weight -> + minWeight = 75 // Glycine + maxWeight = 204 // Tryptophan + int i = 255 * (weight - minWeight) / (maxWeight - minWeight); + new Color(i, 0, 255-i); +} +def byWeight +byWeight = { -> + [ + getSchemeName: { 'By Weight' }, + isApplicableTo: { coll -> true }, + alignmentChanged: { coll, map -> }, + getInstance: { coll, map -> byWeight() }, + isSimple: { true }, + findColour: {res, col, seq, consensus, pid -> switch (res) { case ' ': case '-': @@ -109,7 +147,8 @@ class ByWeight extends ResidueColourScheme { makeColour(150) } } + ] as ColourSchemeI } -ColourSchemes.instance.registerColourScheme(new Stripy()) -ColourSchemes.instance.registerColourScheme(new ByWeight()) +ColourSchemes.instance.registerColourScheme(candy()) +ColourSchemes.instance.registerColourScheme(byWeight()) diff --git a/src/MCview/PDBChain.java b/src/MCview/PDBChain.java index 783a4e2..c40cdda 100755 --- a/src/MCview/PDBChain.java +++ b/src/MCview/PDBChain.java @@ -516,10 +516,12 @@ public class PDBChain try { index = ResidueProperties.aa3Hash.get(b.at1.resName).intValue(); - b.startCol = cs.findColour(ResidueProperties.aa[index].charAt(0)); + b.startCol = cs.findColour(ResidueProperties.aa[index].charAt(0), + 0, null, null, 0f); index = ResidueProperties.aa3Hash.get(b.at2.resName).intValue(); - b.endCol = cs.findColour(ResidueProperties.aa[index].charAt(0)); + b.endCol = cs.findColour(ResidueProperties.aa[index].charAt(0), 0, + null, null, 0f); } catch (Exception e) { diff --git a/src/jalview/appletgui/UserDefinedColours.java b/src/jalview/appletgui/UserDefinedColours.java index c912dc3..aecc0c9 100644 --- a/src/jalview/appletgui/UserDefinedColours.java +++ b/src/jalview/appletgui/UserDefinedColours.java @@ -410,7 +410,7 @@ public class UserDefinedColours extends Panel implements ActionListener, Color col = Color.white; if (oldColourScheme != null && oldColourScheme.isSimple()) { - col = oldColourScheme.findColour(aa.charAt(0)); + col = oldColourScheme.findColour(aa.charAt(0), 0, null, null, 0f); } button.setBackground(col); oldColours.addElement(col); diff --git a/src/jalview/ext/jmol/JalviewJmolBinding.java b/src/jalview/ext/jmol/JalviewJmolBinding.java index b4586ca..bf80831 100644 --- a/src/jalview/ext/jmol/JalviewJmolBinding.java +++ b/src/jalview/ext/jmol/JalviewJmolBinding.java @@ -168,6 +168,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel releaseUIResources(); } + @Override public void colourByChain() { colourBySequence = false; @@ -177,6 +178,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel evalStateCommand("select *;color chain"); } + @Override public void colourByCharge() { colourBySequence = false; @@ -1264,7 +1266,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel { char res = resName.length() == 3 ? ResidueProperties .getSingleCharacterCode(resName) : resName.charAt(0); - Color col = cs.findColour(res); + Color col = cs.findColour(res, 0, null, null, 0f); command.append("select " + resName + ";color[" + col.getRed() + "," + col.getGreen() + "," + col.getBlue() + "];"); } diff --git a/src/jalview/ext/rbvi/chimera/JalviewChimeraBinding.java b/src/jalview/ext/rbvi/chimera/JalviewChimeraBinding.java index 2171815..b05c168 100644 --- a/src/jalview/ext/rbvi/chimera/JalviewChimeraBinding.java +++ b/src/jalview/ext/rbvi/chimera/JalviewChimeraBinding.java @@ -929,7 +929,7 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel { char res = resName.length() == 3 ? ResidueProperties .getSingleCharacterCode(resName) : resName.charAt(0); - Color col = cs.findColour(res); + Color col = cs.findColour(res, 0, null, null, 0f); command.append("color " + col.getRed() / normalise + "," + col.getGreen() / normalise + "," + col.getBlue() / normalise + " ::" + resName + ";"); diff --git a/src/jalview/gui/UserDefinedColours.java b/src/jalview/gui/UserDefinedColours.java index 8af0af0..a368e74 100755 --- a/src/jalview/gui/UserDefinedColours.java +++ b/src/jalview/gui/UserDefinedColours.java @@ -435,7 +435,8 @@ public class UserDefinedColours extends GUserDefinedColours implements col = Color.white; if (oldColourScheme != null && oldColourScheme.isSimple()) { - col = oldColourScheme.findColour(residue.charAt(0)); + col = oldColourScheme.findColour(residue.charAt(0), 0, null, null, + 0f); } } diff --git a/src/jalview/schemes/ColourSchemeI.java b/src/jalview/schemes/ColourSchemeI.java index fccc31b..f16ca21 100755 --- a/src/jalview/schemes/ColourSchemeI.java +++ b/src/jalview/schemes/ColourSchemeI.java @@ -30,15 +30,6 @@ import java.util.Map; public interface ColourSchemeI { /** - * Returns the colour for the given character. For use when the colour depends - * only on the symbol. - * - * @param c - * @return - */ - Color findColour(char c); - - /** * Returns the possibly context dependent colour for the given symbol at the * aligned position in the given sequence. For example, the colour may depend * on the symbol's relationship to the consensus residue for the column. diff --git a/src/jalview/schemes/ResidueColourScheme.java b/src/jalview/schemes/ResidueColourScheme.java index 892c3e8..358417b 100755 --- a/src/jalview/schemes/ResidueColourScheme.java +++ b/src/jalview/schemes/ResidueColourScheme.java @@ -87,7 +87,6 @@ public abstract class ResidueColourScheme implements ColourSchemeI /** * Find a colour without an index in a sequence */ - @Override public Color findColour(char c) { Color colour = Color.white; diff --git a/test/jalview/schemes/ColourSchemePropertyTest.java b/test/jalview/schemes/ColourSchemePropertyTest.java index 2854784..c1c6846 100644 --- a/test/jalview/schemes/ColourSchemePropertyTest.java +++ b/test/jalview/schemes/ColourSchemePropertyTest.java @@ -105,7 +105,8 @@ public class ColourSchemePropertyTest /* * explicit aa colours */ - ColourSchemeI cs = ColourSchemeProperty.getColourScheme(al, + UserColourScheme cs = (UserColourScheme) ColourSchemeProperty + .getColourScheme(al, "R,G=red;C=blue;c=green;Q=10,20,30;S,T=11ffdd"); assertEquals(cs.findColour('H'), Color.white); assertEquals(cs.findColour('R'), Color.red); -- 1.7.10.2