Merge branch 'releases/Release_2_11_3_Branch'
[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         jalview.bin.Console
81                 .errPrintln("Error instantiating colour scheme for "
82                         + cs.toString() + " " + e.getMessage());
83         e.printStackTrace();
84       } catch (ReflectiveOperationException roe)
85       {
86         roe.printStackTrace();
87       }
88     }
89   }
90
91   /**
92    * Registers a colour scheme
93    * 
94    * @param cs
95    */
96   public void registerColourScheme(ColourSchemeI cs)
97   {
98     String name = cs.getSchemeName();
99     if (name == null)
100     {
101       jalview.bin.Console.errPrintln("ColourScheme name may not be null");
102       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     String lower = getColourSchemeShortName(cs);
110     if (schemes.containsKey(lower))
111     {
112       System.err
113               .println("Warning: overwriting colour scheme named " + name);
114     }
115     schemes.put(lower, cs);
116   }
117
118   private String getColourSchemeShortName(ColourSchemeI cs)
119   {
120     return getColourSchemeShortName(cs.getSchemeName());
121   }
122
123   private String getColourSchemeShortName(String name)
124   {
125     if (name == null)
126       return null;
127     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   public void removeColourScheme(String name)
137   {
138     if (name != null)
139     {
140       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   public ColourSchemeI getColourScheme(String name, AlignViewportI viewport,
159           AnnotatedCollectionI forData,
160           Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
161   {
162     if (name == null)
163     {
164       return null;
165     }
166     ColourSchemeI cs = schemes.get(getColourSchemeShortName(name));
167     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   public ColourSchemeI getColourScheme(String name,
181           AnnotatedCollectionI forData)
182   {
183     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   public Iterable<ColourSchemeI> getColourSchemes()
193   {
194     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   public boolean nameExists(String name)
205   {
206     if (name == null)
207     {
208       return false;
209     }
210     return schemes.containsKey(getColourSchemeShortName(name));
211   }
212 }