From: gmungoc Date: Fri, 16 Dec 2016 13:13:30 +0000 (+0000) Subject: JAL-2360 added UserColourScheme.toAppletParameter+test, hid X-Git-Tag: Release_2_10_3b1~357^2~49 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=a09d6f5c16b0e222806e035cd38bfb4c4eb92c75;p=jalview.git JAL-2360 added UserColourScheme.toAppletParameter+test, hid parseAppletParameter() --- diff --git a/src/jalview/appletgui/AlignViewport.java b/src/jalview/appletgui/AlignViewport.java index b025f64..9e82ae5 100644 --- a/src/jalview/appletgui/AlignViewport.java +++ b/src/jalview/appletgui/AlignViewport.java @@ -218,8 +218,8 @@ public class AlignViewport extends AlignmentViewport implements if (applet.getParameter("userDefinedColour") != null) { - ((UserColourScheme) globalColourScheme).parseAppletParameter(applet - .getParameter("userDefinedColour")); + globalColourScheme = new UserColourScheme( + applet.getParameter("userDefinedColour")); } } initAutoAnnotation(); diff --git a/src/jalview/bin/Jalview.java b/src/jalview/bin/Jalview.java index c52b562..966e952 100755 --- a/src/jalview/bin/Jalview.java +++ b/src/jalview/bin/Jalview.java @@ -40,7 +40,6 @@ import jalview.io.NewickFile; import jalview.io.gff.SequenceOntologyFactory; import jalview.schemes.ColourSchemeI; import jalview.schemes.ColourSchemeProperty; -import jalview.schemes.UserColourScheme; import jalview.util.MessageManager; import jalview.util.Platform; import jalview.ws.jws2.Jws2Discoverer; @@ -514,13 +513,7 @@ public class Jalview ColourSchemeI cs = ColourSchemeProperty.getColourScheme(af .getViewport().getAlignment(), data); - if (cs == null) - { - UserColourScheme ucs = new UserColourScheme("white"); - ucs.parseAppletParameter(data); - cs = ucs; - } - else + if (cs != null) { System.out.println("CMD [-color " + data + "] executed successfully!"); diff --git a/src/jalview/schemes/ColourSchemeProperty.java b/src/jalview/schemes/ColourSchemeProperty.java index 425b3a1..cee2cae 100755 --- a/src/jalview/schemes/ColourSchemeProperty.java +++ b/src/jalview/schemes/ColourSchemeProperty.java @@ -60,6 +60,7 @@ public class ColourSchemeProperty *
  • RNA Helices
  • *
  • User Defined
  • *
  • an AWT colour name e.g. red
  • + *
  • an AWT hex rgb colour e.g. ff2288
  • *
  • residue colours list e.g. D,E=red;K,R,H=0022FF;c=yellow
  • * * If none of these formats is matched, the string is converted to a colour @@ -83,26 +84,17 @@ public class ColourSchemeProperty return scheme.getColourScheme(forData); } - if (name.indexOf('=') == -1) - { - /* - * parse the name as a colour specification - * e.g. "red" or "ff00ed", - * or failing that hash the name to a colour - */ - return new UserColourScheme(name); - } - /* * try to parse the string as a residues colour scheme * e.g. A=red;T,G=blue etc + * else parse the name as a colour specification + * e.g. "red" or "ff00ed", + * or failing that hash the name to a colour */ UserColourScheme ucs = null; try { - // fix the launchApp user defined colourscheme transfer bug - ucs = new UserColourScheme("white"); - ucs.parseAppletParameter(name); + ucs = new UserColourScheme(name); } catch (Exception e) { // System.err.println("Ignoring exception when parsing colourscheme as applet-parameter"); diff --git a/src/jalview/schemes/JalviewColourScheme.java b/src/jalview/schemes/JalviewColourScheme.java index 355ff83..7044a10 100644 --- a/src/jalview/schemes/JalviewColourScheme.java +++ b/src/jalview/schemes/JalviewColourScheme.java @@ -2,7 +2,6 @@ package jalview.schemes; import jalview.datamodel.AnnotatedCollectionI; -import java.awt.Color; import java.util.HashMap; import java.util.Map; @@ -136,11 +135,6 @@ public enum JalviewColourScheme @Override public ColourSchemeI getColourScheme(AnnotatedCollectionI coll) { - Color[] col = new Color[24]; - for (int i = 0; i < 24; i++) - { - col[i] = Color.white; - } return new UserColourScheme("white"); } }; diff --git a/src/jalview/schemes/ResidueColourScheme.java b/src/jalview/schemes/ResidueColourScheme.java index c1eeafa..38ab622 100755 --- a/src/jalview/schemes/ResidueColourScheme.java +++ b/src/jalview/schemes/ResidueColourScheme.java @@ -41,10 +41,17 @@ public class ResidueColourScheme implements ColourSchemeI { public static final String NONE = "None"; + /* + * lookup up by character value e.g. 'G' to the colors array index + * e.g. if symbolIndex['K'] = 11 then colors[11] is the colour for K + */ final int[] symbolIndex; boolean conservationColouring = false; + /* + * colour for residue characters as indexed by symbolIndex + */ Color[] colors = null; int threshold = 0; diff --git a/src/jalview/schemes/UserColourScheme.java b/src/jalview/schemes/UserColourScheme.java index 1865518..7bf02c1 100755 --- a/src/jalview/schemes/UserColourScheme.java +++ b/src/jalview/schemes/UserColourScheme.java @@ -26,11 +26,18 @@ import jalview.datamodel.SequenceI; import jalview.util.ColorUtils; import java.awt.Color; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.StringTokenizer; public class UserColourScheme extends ResidueColourScheme { + /* + * lookup (by symbol index) of lower case colours (if configured) + */ Color[] lowerCaseColours; protected String schemeName; @@ -53,7 +60,7 @@ public class UserColourScheme extends ResidueColourScheme UserColourScheme usc = new UserColourScheme(colors); if (lowerCaseColours != null) { - usc.schemeName = new String(schemeName); + usc.schemeName = schemeName; usc.lowerCaseColours = new Color[lowerCaseColours.length]; System.arraycopy(lowerCaseColours, 0, usc.lowerCaseColours, 0, lowerCaseColours.length); @@ -61,9 +68,30 @@ public class UserColourScheme extends ResidueColourScheme return usc; } + /** + * Constructor for an animino acid colour scheme. The colour specification may + * be one of + * + * + * @param colour + */ public UserColourScheme(String colour) { super(ResidueProperties.aaIndex); + + if (colour.contains("=")) + { + /* + * a list of colours per residue(s) + */ + parseAppletParameter(colour); + return; + } + Color col = ColorUtils.parseColourString(colour); if (col == null) @@ -72,12 +100,31 @@ public class UserColourScheme extends ResidueColourScheme col = ColorUtils.createColourFromName(colour); } - colors = new Color[24]; - for (int i = 0; i < 24; i++) + setAll(col); + schemeName = colour; + } + + /** + * Sets all symbols to the specified colour + * + * @param col + */ + protected void setAll(Color col) + { + if (symbolIndex == null) + { + return; + } + int max = 0; + for (int index : symbolIndex) + { + max = Math.max(max, index); + } + colors = new Color[max + 1]; + for (int i = 0; i <= max; i++) { colors[i] = col; } - schemeName = colour; } public Color[] getColours() @@ -115,10 +162,10 @@ public class UserColourScheme extends ResidueColourScheme * * @param paramValue */ - public void parseAppletParameter(String paramValue) + void parseAppletParameter(String paramValue) { - // TODO: need a function to generate appletParameter colour string from a - // UCS + setAll(Color.white); + StringTokenizer st = new StringTokenizer(paramValue, ";"); StringTokenizer st2; String token = null, colour, residues; @@ -145,9 +192,9 @@ public class UserColourScheme extends ResidueColourScheme { if (lowerCaseColours == null) { - lowerCaseColours = new Color[23]; + lowerCaseColours = new Color[colors.length]; } - for (int i = 0; i < 23; i++) + for (int i = 0; i < lowerCaseColours.length; i++) { if (lowerCaseColours[i] == null) { @@ -162,7 +209,7 @@ public class UserColourScheme extends ResidueColourScheme { if (lowerCaseColours == null) { - lowerCaseColours = new Color[23]; + lowerCaseColours = new Color[colors.length]; } lowerCaseColours[colIndex] = ColorUtils.parseColourString(colour); } @@ -248,4 +295,68 @@ public class UserColourScheme extends ResidueColourScheme return JalviewColourScheme.UserDefined.toString(); } + /** + * Generate an applet colour parameter like A,C,D=12ffe9;Q,W=2393fd;w=9178dd + * + * @return + */ + public String toAppletParameter() + { + Map> colours = new HashMap>(); + + for (char symbol = 'A'; symbol <= 'Z'; symbol++) + { + String residue = String.valueOf(symbol); + int index = symbolIndex[symbol]; + Color c = colors[index]; + if (c != null && !c.equals(Color.white)) + { + if (colours.get(c) == null) + { + colours.put(c, new ArrayList()); + } + colours.get(c).add(residue); + } + if (lowerCaseColours != null) + { + c = lowerCaseColours[index]; + if (c != null && !c.equals(Color.white)) + { + residue = residue.toLowerCase(); + if (colours.get(c) == null) + { + colours.put(c, new ArrayList()); + } + colours.get(c).add(residue); + } + } + } + StringBuilder sb = new StringBuilder(); + for (Entry> cols : colours.entrySet()) + { + if (sb.length() > 0) + { + sb.append(";"); + } + boolean first = true; + for (String residue : cols.getValue()) + { + if (!first) + { + sb.append(","); + } + sb.append(residue); + first = false; + } + sb.append("="); + /* + * get color as hex value, dropping the alpha (ff) part + */ + String hexString = Integer.toHexString(cols.getKey().getRGB()) + .substring(2); + sb.append(hexString); + } + + return sb.toString(); + } } diff --git a/test/jalview/schemes/ClustalxColourSchemeTest.java b/test/jalview/schemes/ClustalxColourSchemeTest.java index 16317d9..fda7d27 100644 --- a/test/jalview/schemes/ClustalxColourSchemeTest.java +++ b/test/jalview/schemes/ClustalxColourSchemeTest.java @@ -27,7 +27,7 @@ public class ClustalxColourSchemeTest ">seq0\nKKKWWWQW\n"; // @formatter:on - @Test + @Test(groups = "Functional") public void testFindColour() { AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(FASTA, diff --git a/test/jalview/schemes/ColourSchemePropertyTest.java b/test/jalview/schemes/ColourSchemePropertyTest.java index 6fd830a..c63f04d 100644 --- a/test/jalview/schemes/ColourSchemePropertyTest.java +++ b/test/jalview/schemes/ColourSchemePropertyTest.java @@ -9,11 +9,13 @@ import jalview.datamodel.AlignmentI; import jalview.datamodel.Sequence; import jalview.datamodel.SequenceI; +import java.awt.Color; + import org.testng.annotations.Test; public class ColourSchemePropertyTest { - @Test + @Test(groups = "Functional") public void testGetColourName() { SequenceI seq = new Sequence("Seq1", "abcd"); @@ -68,7 +70,7 @@ public class ColourSchemePropertyTest assertEquals(ColourSchemeProperty.getColourName(null), "None"); } - @Test + @Test(groups = "Functional") public void testGetColourScheme() { SequenceI seq = new Sequence("Seq1", "abcd"); @@ -99,5 +101,20 @@ public class ColourSchemePropertyTest assertNull(ColourSchemeProperty.getColourScheme(al, "none")); // default is to convert the name into a fixed coloour assertTrue(ColourSchemeProperty.getColourScheme(al, "elephants") instanceof UserColourScheme); + + /* + * explicit aa colours + */ + ColourSchemeI cs = 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); + assertEquals(cs.findColour('r'), Color.red); + assertEquals(cs.findColour('G'), Color.red); + assertEquals(cs.findColour('C'), Color.blue); + assertEquals(cs.findColour('c'), Color.green); + assertEquals(cs.findColour('Q'), new Color(10, 20, 30)); + assertEquals(cs.findColour('S'), new Color(0x11ffdd)); + assertEquals(cs.findColour('T'), new Color(0x11ffdd)); } } diff --git a/test/jalview/schemes/JalviewColourSchemeTest.java b/test/jalview/schemes/JalviewColourSchemeTest.java index 8bc0893..b5246ba 100644 --- a/test/jalview/schemes/JalviewColourSchemeTest.java +++ b/test/jalview/schemes/JalviewColourSchemeTest.java @@ -13,7 +13,7 @@ import org.testng.annotations.Test; public class JalviewColourSchemeTest { - @Test + @Test(groups = "Functional") public void testForName() { assertSame(JalviewColourScheme.Clustal, @@ -56,7 +56,7 @@ public class JalviewColourSchemeTest assertNull(JalviewColourScheme.forName(null)); } - @Test + @Test(groups = "Functional") public void testGetColourScheme() { SequenceI seq = new Sequence("Seq1", "abcd"); diff --git a/test/jalview/schemes/ResidueColourSchemeTest.java b/test/jalview/schemes/ResidueColourSchemeTest.java index 809dce3..4e8ea52 100644 --- a/test/jalview/schemes/ResidueColourSchemeTest.java +++ b/test/jalview/schemes/ResidueColourSchemeTest.java @@ -185,7 +185,7 @@ public class ResidueColourSchemeTest assertEquals(Color.WHITE, rcs.applyConservation(colour, 12)); } - @Test + @Test(groups = "Functional") public void testIsApplicableTo() { SequenceI pep1 = new Sequence("pep1", "APQTWLS"); @@ -275,7 +275,7 @@ public class ResidueColourSchemeTest assertTrue(new RNAHelicesColour(nucleotide).isApplicableTo(nucleotide)); } - @Test + @Test(groups = "Functional") public void testIsApplicableTo_dynamicColourScheme() { SequenceI pep1 = new Sequence("pep1", "APQTWLS"); diff --git a/test/jalview/schemes/UserColourSchemeTest.java b/test/jalview/schemes/UserColourSchemeTest.java index f89568b..151462b 100644 --- a/test/jalview/schemes/UserColourSchemeTest.java +++ b/test/jalview/schemes/UserColourSchemeTest.java @@ -56,7 +56,7 @@ public class UserColourSchemeTest assertEquals(c2, cs.findColour('c')); cs = new UserColourScheme("white"); - cs.parseAppletParameter("D,E=red; K,R,H=0022FF; c=10 , 20,30;lowercase=blue;s=pink"); + cs.parseAppletParameter("D,E=red; K,R,H=0022FF; c=10 , 20,30;t=orange;lowercase=blue;s=pink"); assertEquals(Color.RED, cs.findColour('D')); assertEquals(Color.blue, cs.findColour('d')); assertEquals(Color.RED, cs.findColour('E')); @@ -66,8 +66,18 @@ public class UserColourSchemeTest assertEquals(Color.blue, cs.findColour('h')); assertEquals(c2, cs.findColour('c')); // 'lowercase' sets all lower-case not already set to the given colour + assertEquals(Color.orange, cs.findColour('t')); assertEquals(Color.blue, cs.findColour('k')); assertEquals(Color.blue, cs.findColour('a')); assertEquals(Color.pink, cs.findColour('s')); } + + @Test(groups = "Functional") + public void testToAppletParameter() + { + UserColourScheme cs = new UserColourScheme( + "E,D=red; K,R,H=0022FF; c=10 , 20,30"); + String param = cs.toAppletParameter(); + assertEquals("H,K,R=0022ff;c=0a141e;D,E=ff0000", param); + } } diff --git a/test/jalview/util/ColorUtilsTest.java b/test/jalview/util/ColorUtilsTest.java index 77a03d6..77eab16 100644 --- a/test/jalview/util/ColorUtilsTest.java +++ b/test/jalview/util/ColorUtilsTest.java @@ -214,7 +214,7 @@ public class ColorUtilsTest assertNull(ColorUtils.parseColourString("100,200,100,200")); // too many } - @Test + @Test(groups = "Functional") public void testGetAWTColorFromName() { assertEquals(Color.white, ColorUtils.getAWTColorFromName("white")); assertEquals(Color.white, ColorUtils.getAWTColorFromName("White"));