Clover icon

Coverage Report

  1. Project Clover database Mon Nov 18 2024 09:56:54 GMT
  2. Package jalview.schemes

File ColourSchemes.java

 

Coverage histogram

../../img/srcFileCovDistChart7.png
29% of files have more coverage

Code metrics

14
32
11
1
212
104
20
0.62
2.91
11
1.82

Classes

Class Line # Actions
ColourSchemes 32 32 20
0.701754470.2%
 

Contributing tests

This file is covered by 189 tests. .

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.util.LinkedHashMap;
24    import java.util.Locale;
25    import java.util.Map;
26   
27    import jalview.api.AlignViewportI;
28    import jalview.datamodel.AnnotatedCollectionI;
29    import jalview.datamodel.SequenceCollectionI;
30    import jalview.datamodel.SequenceI;
31   
 
32    public class ColourSchemes
33    {
34    /*
35    * singleton instance of this class
36    */
37    private static ColourSchemes instance = new ColourSchemes();
38   
39    /*
40    * a map from scheme name (lower-cased) to an instance of it
41    */
42    private Map<String, ColourSchemeI> schemes;
43   
44    /**
45    * Returns the singleton instance of this class
46    *
47    * @return
48    */
 
49  596 toggle public static ColourSchemes getInstance()
50    {
51  596 return instance;
52    }
53   
 
54  50 toggle private ColourSchemes()
55    {
56  50 loadColourSchemes();
57    }
58   
59    /**
60    * Loads an instance of each standard or user-defined colour scheme
61    *
62    * @return
63    */
 
64  50 toggle void loadColourSchemes()
65    {
66    /*
67    * store in an order-preserving map, so items can be added to menus
68    * in the order in which they are 'discovered'
69    */
70  50 schemes = new LinkedHashMap<>();
71   
72  50 for (JalviewColourScheme cs : JalviewColourScheme.values())
73    {
74  1000 try
75    {
76  1000 registerColourScheme(
77    cs.getSchemeClass().getDeclaredConstructor().newInstance());
78    } catch (InstantiationException | IllegalAccessException e)
79    {
80  0 jalview.bin.Console
81    .errPrintln("Error instantiating colour scheme for "
82    + cs.toString() + " " + e.getMessage());
83  0 e.printStackTrace();
84    } catch (ReflectiveOperationException roe)
85    {
86  0 roe.printStackTrace();
87    }
88    }
89    }
90   
91    /**
92    * Registers a colour scheme
93    *
94    * @param cs
95    */
 
96  1002 toggle public void registerColourScheme(ColourSchemeI cs)
97    {
98  1002 String name = cs.getSchemeName();
99  1002 if (name == null)
100    {
101  0 jalview.bin.Console.errPrintln("ColourScheme name may not be null");
102  0 return;
103    }
104   
105    /*
106    * name is lower-case for non-case-sensitive lookup
107    * (name in the colour keeps its true case)
108    */
109  1002 String lower = getColourSchemeShortName(cs);
110  1002 if (schemes.containsKey(lower))
111    {
112  0 System.err
113    .println("Warning: overwriting colour scheme named " + name);
114    }
115  1002 schemes.put(lower, cs);
116    }
117   
 
118  1002 toggle private String getColourSchemeShortName(ColourSchemeI cs)
119    {
120  1002 return getColourSchemeShortName(cs.getSchemeName());
121    }
122   
 
123  1112 toggle private String getColourSchemeShortName(String name)
124    {
125  1112 if (name == null)
126  0 return null;
127  1112 return name.toLowerCase(Locale.ROOT).replaceAll("%", "pc")
128    .replaceAll("[^a-z0-9]", "-").replaceAll("--+", "-");
129    }
130   
131    /**
132    * Removes a colour scheme by name
133    *
134    * @param name
135    */
 
136  0 toggle public void removeColourScheme(String name)
137    {
138  0 if (name != null)
139    {
140  0 schemes.remove(getColourSchemeShortName(name));
141    }
142    }
143   
144    /**
145    * Returns an instance of the colour scheme with which the given view may be
146    * coloured
147    *
148    * @param name
149    * name of the colour scheme
150    * @param viewport
151    * @param forData
152    * the data to be coloured
153    * @param optional
154    * map from hidden representative sequences to the sequences they
155    * represent
156    * @return
157    */
 
158  104 toggle public ColourSchemeI getColourScheme(String name, AlignViewportI viewport,
159    AnnotatedCollectionI forData,
160    Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
161    {
162  104 if (name == null)
163    {
164  0 return null;
165    }
166  104 ColourSchemeI cs = schemes.get(getColourSchemeShortName(name));
167  104 return cs == null ? null : cs.getInstance(viewport, forData);
168    }
169   
170    /**
171    * Returns an instance of the colour scheme with which the given view may be
172    * coloured
173    *
174    * @param name
175    * name of the colour scheme
176    * @param forData
177    * the data to be coloured
178    * @return
179    */
 
180  22 toggle public ColourSchemeI getColourScheme(String name,
181    AnnotatedCollectionI forData)
182    {
183  22 return getColourScheme(name, null, forData, null);
184    }
185   
186    /**
187    * Returns an iterable set of the colour schemes, in the order in which they
188    * were added
189    *
190    * @return
191    */
 
192  508 toggle public Iterable<ColourSchemeI> getColourSchemes()
193    {
194  508 return schemes.values();
195    }
196   
197    /**
198    * Answers true if there is a scheme with the given name, else false. The test
199    * is not case-sensitive.
200    *
201    * @param name
202    * @return
203    */
 
204  7 toggle public boolean nameExists(String name)
205    {
206  7 if (name == null)
207    {
208  1 return false;
209    }
210  6 return schemes.containsKey(getColourSchemeShortName(name));
211    }
212    }