JAL-1066 - T-Coffee color scheme + signature change on findColour method
[jalview.git] / src / jalview / schemes / AnnotationColourGradient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.schemes;
19
20 import jalview.datamodel.AlignmentAnnotation;
21 import jalview.datamodel.GraphLine;
22
23 import java.awt.Color;
24
25 public class AnnotationColourGradient extends ResidueColourScheme
26 {
27   public static final int NO_THRESHOLD = -1;
28
29   public static final int BELOW_THRESHOLD = 0;
30
31   public static final int ABOVE_THRESHOLD = 1;
32
33   public AlignmentAnnotation annotation;
34
35   int aboveAnnotationThreshold = -1;
36
37   public boolean thresholdIsMinMax = false;
38
39   GraphLine annotationThreshold;
40
41   float r1, g1, b1, rr, gg, bb, dr, dg, db;
42
43   ColourSchemeI colourScheme;
44
45   public boolean predefinedColours = false;
46
47   /**
48    * Creates a new AnnotationColourGradient object.
49    */
50   public AnnotationColourGradient(AlignmentAnnotation annotation,
51           ColourSchemeI originalColour, int aboveThreshold)
52   {
53     if (originalColour instanceof AnnotationColourGradient)
54     {
55       colourScheme = ((AnnotationColourGradient) originalColour).colourScheme;
56     }
57     else
58     {
59       colourScheme = originalColour;
60     }
61
62     this.annotation = annotation;
63
64     aboveAnnotationThreshold = aboveThreshold;
65
66     if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
67     {
68       annotationThreshold = annotation.threshold;
69     }
70   }
71
72   /**
73    * Creates a new AnnotationColourGradient object.
74    */
75   public AnnotationColourGradient(AlignmentAnnotation annotation,
76           Color minColour, Color maxColour, int aboveThreshold)
77   {
78     this.annotation = annotation;
79
80     aboveAnnotationThreshold = aboveThreshold;
81
82     if (aboveThreshold != NO_THRESHOLD && annotation.threshold != null)
83     {
84       annotationThreshold = annotation.threshold;
85     }
86
87     r1 = minColour.getRed();
88     g1 = minColour.getGreen();
89     b1 = minColour.getBlue();
90
91     rr = maxColour.getRed() - r1;
92     gg = maxColour.getGreen() - g1;
93     bb = maxColour.getBlue() - b1;
94   }
95
96   public String getAnnotation()
97   {
98     return annotation.label;
99   }
100
101   public int getAboveThreshold()
102   {
103     return aboveAnnotationThreshold;
104   }
105
106   public float getAnnotationThreshold()
107   {
108     if (annotationThreshold == null)
109     {
110       return 0;
111     }
112     else
113     {
114       return annotationThreshold.value;
115     }
116   }
117
118   public ColourSchemeI getBaseColour()
119   {
120     return colourScheme;
121   }
122
123   public Color getMinColour()
124   {
125     return new Color((int) r1, (int) g1, (int) b1);
126   }
127
128   public Color getMaxColour()
129   {
130     return new Color((int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
131   }
132
133   /**
134    * DOCUMENT ME!
135    * 
136    * @param n
137    *          DOCUMENT ME!
138    * 
139    * @return DOCUMENT ME!
140    */
141   public Color findColour(char c)
142   {
143     return Color.red;
144   }
145
146   /**
147    * DOCUMENT ME!
148    * 
149    * @param n
150    *          DOCUMENT ME!
151    * @param j
152    *          DOCUMENT ME!
153    * 
154    * @return DOCUMENT ME!
155    */
156   @Override
157   public Color findColour(char c, int j, int sequenceIndex)
158   {
159     Color currentColour = Color.white;
160
161     if ((threshold == 0) || aboveThreshold(c, j))
162     {
163       if (j < annotation.annotations.length
164               && annotation.annotations[j] != null
165               && !jalview.util.Comparison.isGap(c))
166       {
167
168         if (predefinedColours)
169         {
170           if (annotation.annotations[j].colour != null)
171             return annotation.annotations[j].colour;
172           else
173             return currentColour;
174         }
175
176         if (aboveAnnotationThreshold == NO_THRESHOLD
177                 || (annotationThreshold != null
178                         && aboveAnnotationThreshold == ABOVE_THRESHOLD && annotation.annotations[j].value >= annotationThreshold.value)
179                 || (annotationThreshold != null
180                         && aboveAnnotationThreshold == BELOW_THRESHOLD && annotation.annotations[j].value <= annotationThreshold.value))
181         {
182
183           float range = 1f;
184           if (thresholdIsMinMax
185                   && annotation.threshold != null
186                   && aboveAnnotationThreshold == ABOVE_THRESHOLD
187                   && annotation.annotations[j].value > annotation.threshold.value)
188           {
189             range = (annotation.annotations[j].value - annotation.threshold.value)
190                     / (annotation.graphMax - annotation.threshold.value);
191           }
192           else if (thresholdIsMinMax && annotation.threshold != null
193                   && aboveAnnotationThreshold == BELOW_THRESHOLD
194                   && annotation.annotations[j].value > annotation.graphMin)
195           {
196             range = (annotation.annotations[j].value - annotation.graphMin)
197                     / (annotation.threshold.value - annotation.graphMin);
198           }
199           else
200           {
201             range = (annotation.annotations[j].value - annotation.graphMin)
202                     / (annotation.graphMax - annotation.graphMin);
203           }
204
205           if (colourScheme != null)
206           {
207             currentColour = colourScheme.findColour(c, j, sequenceIndex);
208           }
209           else if (range != 0)
210           {
211             dr = rr * range + r1;
212             dg = gg * range + g1;
213             db = bb * range + b1;
214
215             currentColour = new Color((int) dr, (int) dg, (int) db);
216           }
217         }
218       }
219     }
220
221     if (conservationColouring)
222     {
223       currentColour = applyConservation(currentColour, j);
224     }
225
226     return currentColour;
227   }
228 }