*/
package jalview.schemes;
+import jalview.api.AlignViewportI;
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;
}
@Override
- public ColourSchemeI applyTo(AnnotatedCollectionI sg,
- Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
+ public ColourSchemeI getInstance(AlignViewportI view,
+ AnnotatedCollectionI sg)
+ {
+ return new UserColourScheme(this);
+ }
+
+ /**
+ * Copy constructor
+ *
+ * @return
+ */
+ protected UserColourScheme(UserColourScheme from)
{
- UserColourScheme usc = new UserColourScheme(colors);
- if (lowerCaseColours != null)
+ 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[from.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
+ * <ul>
+ * <li>an AWT colour name e.g. red</li>
+ * <li>an AWT hex rgb colour e.g. ff2288</li>
+ * <li>residue colours list e.g. D,E=red;K,R,H=0022FF;c=yellow</li>
+ * </ul>
+ *
+ * @param colour
+ */
public UserColourScheme(String colour)
{
super(ResidueProperties.aaIndex);
- Color col = getColourFromString(colour);
+
+ if (colour.contains("="))
+ {
+ /*
+ * a list of colours per residue(s)
+ */
+ parseAppletParameter(colour);
+ return;
+ }
+
+ Color col = ColorUtils.parseColourString(colour);
if (col == null)
{
System.out.println("Making colour from name: " + colour);
- col = createColourFromName(colour);
+ 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()
}
/**
- * Parses a string into a Color, where the accepted formats are
- * <ul>
- * <li>an AWT colour name e.g. white</li>
- * <li>a hex colour value (without prefix) e.g. ff0000</li>
- * <li>an rgb triple e.g. 100,50,150</li>
- * </ul>
- *
- * @param colour
- * @return the parsed colour, or null if parsing fails
- */
- public static Color getColourFromString(String colour)
- {
- if (colour == null)
- {
- return null;
- }
- colour = colour.trim();
-
- Color col = null;
- try
- {
- int value = Integer.parseInt(colour, 16);
- col = new Color(value);
- } catch (NumberFormatException ex)
- {
- }
-
- if (col == null)
- {
- col = ColourSchemeProperty.getAWTColorFromName(colour);
- }
-
- if (col == null)
- {
- try
- {
- String[] tokens = colour.split(",");
- if (tokens.length == 3)
- {
- int r = Integer.parseInt(tokens[0].trim());
- int g = Integer.parseInt(tokens[1].trim());
- int b = Integer.parseInt(tokens[2].trim());
- col = new Color(r, g, b);
- }
- } catch (Exception ex)
- {
- // non-numeric token or out of 0-255 range
- }
- }
-
- return col;
- }
-
- public static Color createColourFromName(String name)
- {
- int r, g, b;
-
- int lsize = name.length();
- int start = 0, end = lsize / 3;
-
- int rgbOffset = Math.abs(name.hashCode() % 10) * 15;
-
- r = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
- start = end;
- end += lsize / 3;
- if (end > lsize)
- {
- end = lsize;
- }
-
- g = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
-
- b = Math.abs(name.substring(end).hashCode() + rgbOffset) % 210 + 20;
-
- Color color = new Color(r, g, b);
-
- return color;
- }
-
- /**
* Parse and save residue colours specified as (for example)
*
* <pre>
*
* @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;
{
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)
{
- lowerCaseColours[i] = getColourFromString(colour);
+ lowerCaseColours[i] = ColorUtils.parseColourString(colour);
}
}
{
if (lowerCaseColours == null)
{
- lowerCaseColours = new Color[23];
+ lowerCaseColours = new Color[colors.length];
}
- lowerCaseColours[colIndex] = getColourFromString(colour);
+ lowerCaseColours[colIndex] = ColorUtils
+ .parseColourString(colour);
}
else
{
- colors[colIndex] = getColourFromString(colour);
+ colors[colIndex] = ColorUtils.parseColourString(colour);
}
}
}
} catch (Exception ex)
{
- System.out.println("Error parsing userDefinedColours:\n" + token
- + "\n" + ex);
- }
-
- }
-
- @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);
+ System.out.println(
+ "Error parsing userDefinedColours:\n" + token + "\n" + ex);
}
- return currentColour;
}
public void setLowerCaseColours(Color[] lcolours)
return super.findColour(c);
}
+ /**
+ * Answers the customised name of the colour scheme, if it has one, else "User
+ * Defined"
+ */
+ @Override
+ public String getSchemeName()
+ {
+ if (schemeName != null && schemeName.length() > 0)
+ {
+ return schemeName;
+ }
+ return ResidueColourScheme.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<Color, List<String>> 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<String>());
+ }
+ 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<String>());
+ }
+ colours.get(c).add(residue);
+ }
+ }
+ }
+
+ /*
+ * step 2: make a list of { A,G,R=12f9d6 } residues/colour specs
+ */
+ List<String> residueColours = new ArrayList<>();
+ for (Entry<Color, List<String>> 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, ";");
+ }
+
+ @Override
+ public boolean hasGapColour()
+ {
+ return (findColour(' ') != null);
+ }
}