1 package jalview.schemes;
3 import jalview.binding.JalviewUserColours;
4 import jalview.datamodel.AnnotatedCollectionI;
5 import jalview.datamodel.SequenceCollectionI;
6 import jalview.datamodel.SequenceI;
10 import java.io.FileInputStream;
11 import java.io.InputStreamReader;
12 import java.util.LinkedHashMap;
15 public class ColourSchemes
18 * singleton instance of this class
20 private static ColourSchemes instance = new ColourSchemes();
23 * a map from scheme name to an instance of it
25 private Map<String, ColourSchemeI> schemes;
28 * Returns the singleton instance of this class
32 public static ColourSchemes getInstance()
37 private ColourSchemes()
43 * Loads an instance of each standard or user-defined colour scheme
47 void loadColourSchemes()
50 * store in an order-preserving map, so items can be added to menus
51 * in the order in which they are 'discovered'
53 schemes = new LinkedHashMap<String, ColourSchemeI>();
55 for (JalviewColourScheme cs : JalviewColourScheme.values())
59 registerColourScheme(cs.getSchemeClass().newInstance());
60 } catch (InstantiationException | IllegalAccessException e)
62 System.err.println("Error instantiating colour scheme for "
63 + cs.toString() + " " + e.getMessage());
69 * Registers a colour scheme
73 public void registerColourScheme(ColourSchemeI cs)
75 String name = cs.getSchemeName();
78 System.err.println("ColourScheme name may not be null");
83 * name is lower-case for non-case-sensitive lookup
84 * (name in the colour keeps its true case)
86 String lower = name.toLowerCase();
87 if (schemes.containsKey(lower))
90 .println("Warning: overwriting colour scheme named " + name);
92 schemes.put(lower, cs);
96 * Removes a colour scheme by name
100 public void removeColourScheme(String name)
102 schemes.remove(name);
106 * Returns an instance of the colour scheme with which the given view may be
110 * name of the colour scheme
112 * the data to be coloured
114 * map from hidden representative sequences to the sequences they
118 public ColourSchemeI getColourScheme(String name,
119 AnnotatedCollectionI forData,
120 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
126 ColourSchemeI cs = schemes.get(name.toLowerCase());
127 return cs == null ? null : cs.getInstance(forData, hiddenRepSequences);
131 * Returns an instance of the colour scheme with which the given view may be
135 * name of the colour scheme
137 * the data to be coloured
140 public ColourSchemeI getColourScheme(String name,
141 AnnotatedCollectionI forData)
143 return getColourScheme(name, forData, null);
147 * Returns an iterable set of the colour schemes, in the order in which they
152 public Iterable<ColourSchemeI> getColourSchemes()
154 return schemes.values();
158 * Answers true if there is a scheme with the given name, else false. The test
159 * is not case-sensitive.
164 public boolean nameExists(String name)
170 name = name.toLowerCase();
171 for (ColourSchemeI scheme : getColourSchemes())
173 if (name.equals(scheme.getSchemeName().toLowerCase()))
182 * Loads a user defined colour scheme from file. The file should contain a
183 * definition of residue colours in XML format as defined in
184 * JalviewUserColours.xsd.
190 public static UserColourScheme loadColourScheme(String filePath)
192 UserColourScheme ucs = null;
193 Color[] newColours = null;
194 File file = new File(filePath);
197 InputStreamReader in = new InputStreamReader(
198 new FileInputStream(file), "UTF-8");
200 jalview.schemabinding.version2.JalviewUserColours jucs = new jalview.schemabinding.version2.JalviewUserColours();
202 org.exolab.castor.xml.Unmarshaller unmar = new org.exolab.castor.xml.Unmarshaller(
204 jucs = (jalview.schemabinding.version2.JalviewUserColours) unmar
208 * non-case-sensitive colours are for 20 amino acid codes,
210 * optionally, lower-case alternatives for all except Gap
212 newColours = new Color[24];
213 Color[] lowerCase = new Color[23];
214 boolean caseSensitive = false;
218 for (int i = 0; i < jucs.getColourCount(); i++)
220 name = jucs.getColour(i).getName();
221 if (ResidueProperties.aa3Hash.containsKey(name))
223 index = ResidueProperties.aa3Hash.get(name).intValue();
227 index = ResidueProperties.aaIndex[name.charAt(0)];
234 Color color = new Color(Integer.parseInt(jucs.getColour(i)
236 if (name.toLowerCase().equals(name))
238 caseSensitive = true;
239 lowerCase[index] = color;
243 newColours[index] = color;
248 * instantiate the colour scheme
250 ucs = new UserColourScheme(newColours);
251 ucs.setName(jucs.getSchemeName());
254 ucs.setLowerCaseColours(lowerCase);
256 } catch (Exception ex)
258 // Could be old Jalview Archive format
261 InputStreamReader in = new InputStreamReader(new FileInputStream(
264 jalview.binding.JalviewUserColours jucs = new jalview.binding.JalviewUserColours();
266 jucs = JalviewUserColours.unmarshal(in);
268 newColours = new Color[jucs.getColourCount()];
270 for (int i = 0; i < 24; i++)
272 newColours[i] = new Color(Integer.parseInt(jucs.getColour(i)
275 ucs = new UserColourScheme(newColours);
276 ucs.setName(jucs.getSchemeName());
277 } catch (Exception ex2)
279 ex2.printStackTrace();
282 if (newColours == null)
284 System.out.println("Error loading User ColourFile\n" + ex);