Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.schemes

File ColourSchemes.java

 

Coverage histogram

../../img/srcFileCovDistChart8.png
19% of files have more coverage

Code metrics

12
26
9
1
190
85
16
0.62
2.89
9
1.78

Classes

Class Line # Actions
ColourSchemes 30 26 16 13
0.723404272.3%
 

Contributing tests

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