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.binding.JalviewUserColours;
25 import java.awt.Color;
27 import java.io.FileInputStream;
28 import java.io.InputStreamReader;
30 import org.exolab.castor.xml.Unmarshaller;
32 public class ColourSchemeLoader
36 * Loads a user defined colour scheme from file. The file should contain a
37 * definition of residue colours in XML format as defined in
38 * JalviewUserColours.xsd.
44 public static UserColourScheme loadColourScheme(String filePath)
46 UserColourScheme ucs = null;
47 Color[] newColours = null;
48 File file = new File(filePath);
51 InputStreamReader in = new InputStreamReader(
52 new FileInputStream(file), "UTF-8");
54 jalview.schemabinding.version2.JalviewUserColours jucs = new jalview.schemabinding.version2.JalviewUserColours();
56 org.exolab.castor.xml.Unmarshaller unmar = new org.exolab.castor.xml.Unmarshaller(
58 jucs = (jalview.schemabinding.version2.JalviewUserColours) unmar
62 * non-case-sensitive colours are for 20 amino acid codes,
64 * optionally, lower-case alternatives for all except Gap
66 newColours = new Color[24];
67 Color[] lowerCase = new Color[23];
68 boolean caseSensitive = false;
72 for (int i = 0; i < jucs.getColourCount(); i++)
74 name = jucs.getColour(i).getName();
75 if (ResidueProperties.aa3Hash.containsKey(name))
77 index = ResidueProperties.aa3Hash.get(name).intValue();
81 index = ResidueProperties.aaIndex[name.charAt(0)];
88 Color color = new Color(
89 Integer.parseInt(jucs.getColour(i).getRGB(), 16));
90 if (name.toLowerCase().equals(name))
93 lowerCase[index] = color;
97 newColours[index] = color;
102 * instantiate the colour scheme
104 ucs = new UserColourScheme(newColours);
105 ucs.setName(jucs.getSchemeName());
108 ucs.setLowerCaseColours(lowerCase);
110 } catch (Exception ex)
112 // Could be old Jalview Archive format
115 InputStreamReader in = new InputStreamReader(
116 new FileInputStream(file), "UTF-8");
118 jalview.binding.JalviewUserColours jucs = new jalview.binding.JalviewUserColours();
120 jucs = JalviewUserColours.unmarshal(in);
122 newColours = new Color[jucs.getColourCount()];
124 for (int i = 0; i < 24; i++)
126 newColours[i] = new Color(
127 Integer.parseInt(jucs.getColour(i).getRGB(), 16));
129 ucs = new UserColourScheme(newColours);
130 ucs.setName(jucs.getSchemeName());
131 } catch (Exception ex2)
133 ex2.printStackTrace();
136 if (newColours == null)
138 System.out.println("Error loading User ColourFile\n" + ex);