JAL-3063 Save/Load user defined colours using JAXB
[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 jalview.xml.binding.jalview.JalviewUserColours;
24
25 import java.awt.Color;
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.InputStreamReader;
29
30 import javax.xml.bind.JAXBContext;
31 import javax.xml.bind.JAXBElement;
32 import javax.xml.stream.XMLInputFactory;
33 import javax.xml.stream.XMLStreamReader;
34
35 public class ColourSchemeLoader
36 {
37
38   /**
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.
42    * 
43    * @param filePath
44    * 
45    * @return
46    */
47   public static UserColourScheme loadColourScheme(String filePath)
48   {
49     UserColourScheme ucs = null;
50     Color[] newColours = null;
51     File file = new File(filePath);
52     try
53     {
54       InputStreamReader in = new InputStreamReader(
55               new FileInputStream(file), "UTF-8");
56
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();
65
66       /*
67        * non-case-sensitive colours are for 20 amino acid codes,
68        * B, Z, X and Gap
69        * optionally, lower-case alternatives for all except Gap
70        */
71       newColours = new Color[24];
72       Color[] lowerCase = new Color[23];
73       boolean caseSensitive = false;
74
75       String name;
76       int index;
77       for (int i = 0; i < jucs.getColour().size(); i++)
78       {
79         name = jucs.getColour().get(i).getName();
80         if (ResidueProperties.aa3Hash.containsKey(name))
81         {
82           index = ResidueProperties.aa3Hash.get(name).intValue();
83         }
84         else
85         {
86           index = ResidueProperties.aaIndex[name.charAt(0)];
87         }
88         if (index == -1)
89         {
90           continue;
91         }
92
93         Color color = new Color(
94                 Integer.parseInt(jucs.getColour().get(i).getRGB(), 16));
95         if (name.toLowerCase().equals(name))
96         {
97           caseSensitive = true;
98           lowerCase[index] = color;
99         }
100         else
101         {
102           newColours[index] = color;
103         }
104       }
105
106       /*
107        * instantiate the colour scheme
108        */
109       ucs = new UserColourScheme(newColours);
110       ucs.setName(jucs.getSchemeName());
111       if (caseSensitive)
112       {
113         ucs.setLowerCaseColours(lowerCase);
114       }
115     } catch (Exception ex)
116     {
117       // Could be old Jalview Archive format
118       try
119       {
120         InputStreamReader in = new InputStreamReader(
121                 new FileInputStream(file), "UTF-8");
122
123         jalview.binding.JalviewUserColours jucs = new jalview.binding.JalviewUserColours();
124
125         jucs = jalview.binding.JalviewUserColours.unmarshal(in);
126
127         newColours = new Color[jucs.getColourCount()];
128
129         for (int i = 0; i < 24; i++)
130         {
131           newColours[i] = new Color(
132                   Integer.parseInt(jucs.getColour(i).getRGB(), 16));
133         }
134         ucs = new UserColourScheme(newColours);
135         ucs.setName(jucs.getSchemeName());
136       } catch (Exception ex2)
137       {
138         ex2.printStackTrace();
139       }
140
141       if (newColours == null)
142       {
143         System.out.println("Error loading User ColourFile\n" + ex);
144       }
145     }
146
147     return ucs;
148   }
149
150 }