X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fschemes%2FUserColourScheme.java;h=85bf54ee400542998240b6f3e4cdc64fd4baa1a6;hb=400b50efffaa43ae2c3b4d3f653bf8215c5d1edc;hp=1865518d45ce34ba1892e14c30e7b26e3c744e2e;hpb=b0cee3aaf7d8873910939f97b6acb217d518968d;p=jalview.git diff --git a/src/jalview/schemes/UserColourScheme.java b/src/jalview/schemes/UserColourScheme.java index 1865518..85bf54e 100755 --- a/src/jalview/schemes/UserColourScheme.java +++ b/src/jalview/schemes/UserColourScheme.java @@ -24,13 +24,22 @@ import jalview.datamodel.AnnotatedCollectionI; import jalview.datamodel.SequenceCollectionI; import jalview.datamodel.SequenceI; import jalview.util.ColorUtils; +import jalview.util.StringUtils; import java.awt.Color; +import java.util.ArrayList; +import java.util.Collections; +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; @@ -47,23 +56,53 @@ public class UserColourScheme extends ResidueColourScheme } @Override - public ColourSchemeI applyTo(AnnotatedCollectionI sg, + public ColourSchemeI getInstance(AnnotatedCollectionI sg, Map hiddenRepSequences) { - UserColourScheme usc = new UserColourScheme(colors); - if (lowerCaseColours != null) + return new UserColourScheme(this); + } + + /** + * Copy constructor + * + * @return + */ + protected UserColourScheme(UserColourScheme from) + { + this(from.colors); + schemeName = from.schemeName; + if (from.lowerCaseColours != null) { - usc.schemeName = new String(schemeName); - usc.lowerCaseColours = new Color[lowerCaseColours.length]; - System.arraycopy(lowerCaseColours, 0, usc.lowerCaseColours, 0, - lowerCaseColours.length); + lowerCaseColours = new Color[lowerCaseColours.length]; + System.arraycopy(from.lowerCaseColours, 0, lowerCaseColours, 0, + from.lowerCaseColours.length); } - 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 +111,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 +173,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 +203,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 +220,7 @@ public class UserColourScheme extends ResidueColourScheme { if (lowerCaseColours == null) { - lowerCaseColours = new Color[23]; + lowerCaseColours = new Color[colors.length]; } lowerCaseColours[colIndex] = ColorUtils.parseColourString(colour); } @@ -180,36 +238,6 @@ public class UserColourScheme extends ResidueColourScheme } - @Override - public Color findColour(char c, int j, SequenceI seq) - { - Color currentColour; - int index = ResidueProperties.aaIndex[c]; - - if ((threshold == 0) || aboveThreshold(c, j)) - { - if (lowerCaseColours != null && 'a' <= c && c <= 'z') - { - currentColour = lowerCaseColours[index]; - } - else - { - currentColour = colors[index]; - } - } - else - { - currentColour = Color.white; - } - - if (conservationColouring) - { - currentColour = applyConservation(currentColour, j); - } - - return currentColour; - } - public void setLowerCaseColours(Color[] lcolours) { lowerCaseColours = lcolours; @@ -245,7 +273,80 @@ public class UserColourScheme extends ResidueColourScheme { return schemeName; } - return JalviewColourScheme.UserDefined.toString(); + return "User Defined"; } + /** + * Generate an applet colour parameter like A,C,D=12ffe9;Q,W=2393fd;w=9178dd + * + * @return + */ + public String toAppletParameter() + { + /* + * step 1: build a map from colours to the symbol(s) that have the colour + */ + 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); + } + } + } + + /* + * step 2: make a list of { A,G,R=12f9d6 } residues/colour specs + */ + List residueColours = new ArrayList(); + for (Entry> cols : colours.entrySet()) + { + boolean first = true; + StringBuilder sb = new StringBuilder(); + 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); + residueColours.add(sb.toString()); + } + + /* + * sort and output + */ + Collections.sort(residueColours); + return StringUtils.listToDelimitedString(residueColours, ";"); + } }