1 package jalview.schemes;
3 import jalview.binding.JalviewUserColours;
4 import jalview.datamodel.AnnotatedCollectionI;
5 import jalview.datamodel.SequenceCollectionI;
6 import jalview.datamodel.SequenceI;
9 import java.io.FileInputStream;
10 import java.io.InputStreamReader;
11 import java.util.LinkedHashMap;
14 public class ColourSchemes
17 * singleton instance of this class
19 private static ColourSchemes instance = new ColourSchemes();
22 * a map from scheme name to an instance of it
24 private Map<String, ColourSchemeI> schemes;
27 * Returns the singleton instance of this class
31 public static ColourSchemes getInstance()
36 private ColourSchemes()
42 * Loads an instance of each standard or user-defined colour scheme
46 void loadColourSchemes()
49 * store in an order-preserving map, so items can be added to menus
50 * in the order in which they are 'discovered'
52 schemes = new LinkedHashMap<String, ColourSchemeI>();
54 for (JalviewColourScheme cs : JalviewColourScheme.values())
58 registerColourScheme(cs.getSchemeClass().newInstance());
59 } catch (InstantiationException | IllegalAccessException e)
61 System.err.println("Error instantiating colour scheme for "
62 + cs.toString() + " " + e.getMessage());
68 * Registers a colour scheme
72 public void registerColourScheme(ColourSchemeI cs)
74 String name = cs.getSchemeName();
77 System.err.println("ColourScheme name may not be null");
82 * name is lower-case for non-case-sensitive lookup
83 * (name in the colour keeps its true case)
85 String lower = name.toLowerCase();
86 if (schemes.containsKey(lower))
89 .println("Warning: overwriting colour scheme named " + name);
91 schemes.put(lower, cs);
95 * Removes a colour scheme by name
99 public void removeColourScheme(String name)
101 schemes.remove(name);
105 * Returns an instance of the colour scheme with which the given view may be
109 * name of the colour scheme
111 * the data to be coloured
113 * map from hidden representative sequences to the sequences they
117 public ColourSchemeI getColourScheme(String name,
118 AnnotatedCollectionI forData,
119 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
125 ColourSchemeI cs = schemes.get(name.toLowerCase());
126 return cs == null ? null : cs.getInstance(forData, hiddenRepSequences);
130 * Returns an instance of the colour scheme with which the given view may be
134 * name of the colour scheme
136 * the data to be coloured
139 public ColourSchemeI getColourScheme(String name,
140 AnnotatedCollectionI forData)
142 return getColourScheme(name, forData, null);
146 * Returns an iterable set of the colour schemes, in the order in which they
151 public Iterable<ColourSchemeI> getColourSchemes()
153 return schemes.values();
157 * Answers true if there is a scheme with the given name, else false. The test
158 * is not case-sensitive.
163 public boolean nameExists(String name)
169 name = name.toLowerCase();
170 for (ColourSchemeI scheme : getColourSchemes())
172 if (name.equals(scheme.getSchemeName().toLowerCase()))
181 * Loads a user defined colour scheme from file. The file should contain a
182 * definition of residue colours in XML format as defined in
183 * JalviewUserColours.xsd.
189 public static UserColourScheme loadColourScheme(String file)
191 UserColourScheme ucs = null;
192 Color[] newColours = null;
195 InputStreamReader in = new InputStreamReader(
196 new FileInputStream(file), "UTF-8");
198 jalview.schemabinding.version2.JalviewUserColours jucs = new jalview.schemabinding.version2.JalviewUserColours();
200 org.exolab.castor.xml.Unmarshaller unmar = new org.exolab.castor.xml.Unmarshaller(
202 jucs = (jalview.schemabinding.version2.JalviewUserColours) unmar
206 * non-case-sensitive colours are for 20 amino acid codes,
208 * optionally, lower-case alternatives for all except Gap
210 newColours = new Color[24];
211 Color[] lowerCase = new Color[23];
212 boolean caseSensitive = false;
216 for (int i = 0; i < jucs.getColourCount(); i++)
218 name = jucs.getColour(i).getName();
219 if (ResidueProperties.aa3Hash.containsKey(name))
221 index = ResidueProperties.aa3Hash.get(name).intValue();
225 index = ResidueProperties.aaIndex[name.charAt(0)];
232 Color color = new Color(Integer.parseInt(jucs.getColour(i)
234 if (name.toLowerCase().equals(name))
236 caseSensitive = true;
237 lowerCase[index] = color;
241 newColours[index] = color;
246 * instantiate the colour scheme
248 ucs = new UserColourScheme(newColours);
249 ucs.setName(jucs.getSchemeName());
252 ucs.setLowerCaseColours(lowerCase);
254 } catch (Exception ex)
256 // Could be old Jalview Archive format
259 InputStreamReader in = new InputStreamReader(new FileInputStream(
262 jalview.binding.JalviewUserColours jucs = new jalview.binding.JalviewUserColours();
264 jucs = JalviewUserColours.unmarshal(in);
266 newColours = new Color[jucs.getColourCount()];
268 for (int i = 0; i < 24; i++)
270 newColours[i] = new Color(Integer.parseInt(jucs.getColour(i)
273 ucs = new UserColourScheme(newColours);
274 ucs.setName(jucs.getSchemeName());
275 } catch (Exception ex2)
277 ex2.printStackTrace();
280 if (newColours == null)
282 System.out.println("Error loading User ColourFile\n" + ex);