JAL-3691 automatic insertion of Locale.ROOT to toUpperCase() and toLowerCase() and...
[jalview.git] / src / jalview / schemes / ColourSchemeLoader.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.schemes;
22
23 import java.util.Locale;
24
25 import jalview.xml.binding.jalview.JalviewUserColours;
26
27 import java.awt.Color;
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.InputStreamReader;
31
32 import javax.xml.bind.JAXBContext;
33 import javax.xml.bind.JAXBElement;
34 import javax.xml.stream.XMLInputFactory;
35 import javax.xml.stream.XMLStreamReader;
36
37 public class ColourSchemeLoader
38 {
39
40   /**
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.
44    * 
45    * @param filePath
46    * 
47    * @return
48    */
49   public static UserColourScheme loadColourScheme(String filePath)
50   {
51     UserColourScheme ucs = null;
52     Color[] newColours = null;
53     File file = new File(filePath);
54     try
55     {
56       InputStreamReader in = new InputStreamReader(
57               new FileInputStream(file), "UTF-8");
58
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();
67
68       /*
69        * non-case-sensitive colours are for 20 amino acid codes,
70        * B, Z, X and Gap
71        * optionally, lower-case alternatives for all except Gap
72        */
73       newColours = new Color[24];
74       Color[] lowerCase = new Color[23];
75       boolean caseSensitive = false;
76
77       String name;
78       int index;
79       for (int i = 0; i < jucs.getColour().size(); i++)
80       {
81         name = jucs.getColour().get(i).getName();
82         if (ResidueProperties.aa3Hash.containsKey(name))
83         {
84           index = ResidueProperties.aa3Hash.get(name).intValue();
85         }
86         else
87         {
88           index = ResidueProperties.aaIndex[name.charAt(0)];
89         }
90         if (index == -1)
91         {
92           continue;
93         }
94
95         Color color = new Color(
96                 Integer.parseInt(jucs.getColour().get(i).getRGB(), 16));
97         if (name.toLowerCase(Locale.ROOT).equals(name))
98         {
99           caseSensitive = true;
100           lowerCase[index] = color;
101         }
102         else
103         {
104           newColours[index] = color;
105         }
106       }
107
108       /*
109        * instantiate the colour scheme
110        */
111       ucs = new UserColourScheme(newColours);
112       ucs.setName(jucs.getSchemeName());
113       if (caseSensitive)
114       {
115         ucs.setLowerCaseColours(lowerCase);
116       }
117     } catch (Exception ex)
118     {
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());
122     }
123
124     return ucs;
125   }
126
127 }