Clover icon

Coverage Report

  1. Project Clover database Thu Jan 15 2026 16:11:02 GMT
  2. Package jalview.schemes

File ColourSchemeLoader.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
62% of files have more coverage

Code metrics

10
33
1
1
127
77
7
0.21
33
1
7

Classes

Class Line # Actions
ColourSchemeLoader 36 33 7
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

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.awt.Color;
24    import java.io.File;
25    import java.io.FileInputStream;
26    import java.io.InputStreamReader;
27    import java.util.Locale;
28   
29    import javax.xml.bind.JAXBContext;
30    import javax.xml.bind.JAXBElement;
31    import javax.xml.stream.XMLInputFactory;
32    import javax.xml.stream.XMLStreamReader;
33   
34    import jalview.xml.binding.jalview.JalviewUserColours;
35   
 
36    public class ColourSchemeLoader
37    {
38   
39    /**
40    * Loads a user defined colour scheme from file. The file should contain a
41    * definition of residue colours in XML format as defined in
42    * JalviewUserColours.xsd.
43    *
44    * @param filePath
45    *
46    * @return
47    */
 
48  0 toggle public static UserColourScheme loadColourScheme(String filePath)
49    {
50  0 UserColourScheme ucs = null;
51  0 Color[] newColours = null;
52  0 File file = new File(filePath);
53  0 try
54    {
55  0 InputStreamReader in = new InputStreamReader(
56    new FileInputStream(file), "UTF-8");
57   
58  0 JAXBContext jc = JAXBContext.class.getDeclaredConstructor()
59    .newInstance("jalview.xml.binding.jalview");
60  0 javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
61  0 XMLStreamReader streamReader = XMLInputFactory.class
62    .getDeclaredConstructor().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    }