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