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