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 jalview.xml.binding.jalview.JalviewUserColours;
25 import java.awt.Color;
27 import java.io.FileInputStream;
28 import java.io.InputStreamReader;
30 import javax.xml.bind.JAXBContext;
31 import javax.xml.bind.JAXBElement;
32 import javax.xml.stream.XMLInputFactory;
33 import javax.xml.stream.XMLStreamReader;
35 public class ColourSchemeLoader
39 * Loads a user defined colour scheme from file. The file should contain a
40 * definition of residue colours in XML format as defined in
41 * JalviewUserColours.xsd.
47 public static UserColourScheme loadColourScheme(String filePath)
49 UserColourScheme ucs = null;
50 Color[] newColours = null;
51 File file = new File(filePath);
54 InputStreamReader in = new InputStreamReader(
55 new FileInputStream(file), "UTF-8");
57 JAXBContext jc = JAXBContext
58 .newInstance("jalview.xml.binding.jalview");
59 javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
60 XMLStreamReader streamReader = XMLInputFactory.newInstance()
61 .createXMLStreamReader(in);
62 JAXBElement<JalviewUserColours> jbe = um.unmarshal(streamReader,
63 JalviewUserColours.class);
64 JalviewUserColours jucs = jbe.getValue();
67 * non-case-sensitive colours are for 20 amino acid codes,
69 * optionally, lower-case alternatives for all except Gap
71 newColours = new Color[24];
72 Color[] lowerCase = new Color[23];
73 boolean caseSensitive = false;
77 for (int i = 0; i < jucs.getColour().size(); i++)
79 name = jucs.getColour().get(i).getName();
80 if (ResidueProperties.aa3Hash.containsKey(name))
82 index = ResidueProperties.aa3Hash.get(name).intValue();
86 index = ResidueProperties.aaIndex[name.charAt(0)];
93 Color color = new Color(
94 Integer.parseInt(jucs.getColour().get(i).getRGB(), 16));
95 if (name.toLowerCase().equals(name))
98 lowerCase[index] = color;
102 newColours[index] = color;
107 * instantiate the colour scheme
109 ucs = new UserColourScheme(newColours);
110 ucs.setName(jucs.getSchemeName());
113 ucs.setLowerCaseColours(lowerCase);
115 } catch (Exception ex)
117 // used to try to parse a V1 Castor generated colours file
118 System.err.println("Failed to read colour scheme from " + filePath
119 + " : " + ex.toString());