2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.schemes;
23 import java.util.Locale;
25 import jalview.xml.binding.jalview.JalviewUserColours;
27 import java.awt.Color;
29 import java.io.FileInputStream;
30 import java.io.InputStreamReader;
32 import javax.xml.bind.JAXBContext;
33 import javax.xml.bind.JAXBElement;
34 import javax.xml.stream.XMLInputFactory;
35 import javax.xml.stream.XMLStreamReader;
37 public class ColourSchemeLoader
41 * Loads a user defined colour scheme from file. The file should contain a
42 * definition of residue colours in XML format as defined in
43 * JalviewUserColours.xsd.
49 public static UserColourScheme loadColourScheme(String filePath)
51 UserColourScheme ucs = null;
52 Color[] newColours = null;
53 File file = new File(filePath);
56 InputStreamReader in = new InputStreamReader(
57 new FileInputStream(file), "UTF-8");
59 JAXBContext jc = JAXBContext
60 .newInstance("jalview.xml.binding.jalview");
61 javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
62 XMLStreamReader streamReader = XMLInputFactory.newInstance()
63 .createXMLStreamReader(in);
64 JAXBElement<JalviewUserColours> jbe = um.unmarshal(streamReader,
65 JalviewUserColours.class);
66 JalviewUserColours jucs = jbe.getValue();
69 * non-case-sensitive colours are for 20 amino acid codes,
71 * optionally, lower-case alternatives for all except Gap
73 newColours = new Color[24];
74 Color[] lowerCase = new Color[23];
75 boolean caseSensitive = false;
79 for (int i = 0; i < jucs.getColour().size(); i++)
81 name = jucs.getColour().get(i).getName();
82 if (ResidueProperties.aa3Hash.containsKey(name))
84 index = ResidueProperties.aa3Hash.get(name).intValue();
88 index = ResidueProperties.aaIndex[name.charAt(0)];
95 Color color = new Color(
96 Integer.parseInt(jucs.getColour().get(i).getRGB(), 16));
97 if (name.toLowerCase(Locale.ROOT).equals(name))
100 lowerCase[index] = color;
104 newColours[index] = color;
109 * instantiate the colour scheme
111 ucs = new UserColourScheme(newColours);
112 ucs.setName(jucs.getSchemeName());
115 ucs.setLowerCaseColours(lowerCase);
117 } catch (Exception ex)
119 // used to try to parse a V1 Castor generated colours file
120 System.err.println("Failed to read colour scheme from " + filePath
121 + " : " + ex.toString());