Class |
Line # |
Actions |
|||
---|---|---|---|---|---|
ColourSchemeLoader | 37 | 33 | 7 |
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 | 0 | public static UserColourScheme loadColourScheme(String filePath) |
50 | { | |
51 | 0 | UserColourScheme ucs = null; |
52 | 0 | Color[] newColours = null; |
53 | 0 | File file = new File(filePath); |
54 | 0 | try |
55 | { | |
56 | 0 | InputStreamReader in = new InputStreamReader( |
57 | new FileInputStream(file), "UTF-8"); | |
58 | ||
59 | 0 | JAXBContext jc = JAXBContext |
60 | .newInstance("jalview.xml.binding.jalview"); | |
61 | 0 | javax.xml.bind.Unmarshaller um = jc.createUnmarshaller(); |
62 | 0 | XMLStreamReader streamReader = XMLInputFactory.newInstance() |
63 | .createXMLStreamReader(in); | |
64 | 0 | JAXBElement<JalviewUserColours> jbe = um.unmarshal(streamReader, |
65 | JalviewUserColours.class); | |
66 | 0 | 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 | 0 | newColours = new Color[24]; |
74 | 0 | Color[] lowerCase = new Color[23]; |
75 | 0 | boolean caseSensitive = false; |
76 | ||
77 | 0 | String name; |
78 | 0 | int index; |
79 | 0 | for (int i = 0; i < jucs.getColour().size(); i++) |
80 | { | |
81 | 0 | name = jucs.getColour().get(i).getName(); |
82 | 0 | if (ResidueProperties.aa3Hash.containsKey(name)) |
83 | { | |
84 | 0 | index = ResidueProperties.aa3Hash.get(name).intValue(); |
85 | } | |
86 | else | |
87 | { | |
88 | 0 | index = ResidueProperties.aaIndex[name.charAt(0)]; |
89 | } | |
90 | 0 | if (index == -1) |
91 | { | |
92 | 0 | continue; |
93 | } | |
94 | ||
95 | 0 | Color color = new Color( |
96 | Integer.parseInt(jucs.getColour().get(i).getRGB(), 16)); | |
97 | 0 | if (name.toLowerCase(Locale.ROOT).equals(name)) |
98 | { | |
99 | 0 | caseSensitive = true; |
100 | 0 | lowerCase[index] = color; |
101 | } | |
102 | else | |
103 | { | |
104 | 0 | newColours[index] = color; |
105 | } | |
106 | } | |
107 | ||
108 | /* | |
109 | * instantiate the colour scheme | |
110 | */ | |
111 | 0 | ucs = new UserColourScheme(newColours); |
112 | 0 | ucs.setName(jucs.getSchemeName()); |
113 | 0 | if (caseSensitive) |
114 | { | |
115 | 0 | ucs.setLowerCaseColours(lowerCase); |
116 | } | |
117 | } catch (Exception ex) | |
118 | { | |
119 | // used to try to parse a V1 Castor generated colours file | |
120 | 0 | jalview.bin.Console.errPrintln("Failed to read colour scheme from " |
121 | + filePath + " : " + ex.toString()); | |
122 | } | |
123 | ||
124 | 0 | return ucs; |
125 | } | |
126 | ||
127 | } |