66b3a13a4058ceff86f567bf673846a33adc8ae0
[jalview.git] / src / jalview / schemes / AnnotationColourGradient.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18 */
19 package jalview.schemes;
20
21 import java.awt.*;
22
23 import jalview.datamodel.*;
24
25 public class AnnotationColourGradient extends ResidueColourScheme
26 {
27     public static int NO_THRESHOLD = -1;
28     public static int BELOW_THRESHOLD = 0;
29     public static int ABOVE_THRESHOLD = 1;
30
31     AlignmentAnnotation annotation;
32     int aboveAnnotationThreshold = -1;
33
34     GraphLine annotationThreshold;
35
36     float r1, g1, b1, rr, gg, bb, dr, dg, db;
37     float range = 0;
38
39     ColourSchemeI colourScheme;
40
41     /**
42      * Creates a new AnnotationColourGradient object.
43      */
44     public AnnotationColourGradient(AlignmentAnnotation annotation,
45                                     ColourSchemeI originalColour,
46                                     int aboveThreshold)
47     {
48       if(originalColour instanceof AnnotationColourGradient)
49       {
50         colourScheme = ((AnnotationColourGradient)originalColour).colourScheme;
51       }
52       else
53         colourScheme = originalColour;
54
55       this.annotation = annotation;
56
57       aboveAnnotationThreshold = aboveThreshold;
58
59       if(aboveThreshold!=NO_THRESHOLD && annotation.threshold!=null)
60         annotationThreshold = annotation.threshold;
61     }
62
63     /**
64      * Creates a new AnnotationColourGradient object.
65      */
66     public AnnotationColourGradient(AlignmentAnnotation annotation,
67         Color minColour, Color maxColour, int aboveThreshold)
68     {
69       this.annotation = annotation;
70
71       aboveAnnotationThreshold = aboveThreshold;
72
73       if(aboveThreshold!=NO_THRESHOLD && annotation.threshold!=null)
74         annotationThreshold = annotation.threshold;
75
76       r1 = minColour.getRed();
77       g1 = minColour.getGreen();
78       b1 = minColour.getBlue();
79
80       rr = maxColour.getRed() - r1;
81       gg = maxColour.getGreen() - g1;
82       bb = maxColour.getBlue() - b1;
83
84       range = annotation.graphMax - annotation.graphMin;
85
86     }
87
88
89     public String getAnnotation()
90     {
91       return annotation.label;
92     }
93
94     public int getAboveThreshold()
95     {
96       return aboveAnnotationThreshold;
97     }
98
99     public float getAnnotationThreshold()
100     {
101       if(annotationThreshold==null)
102         return 0;
103       else
104         return annotationThreshold.value;
105     }
106
107     public ColourSchemeI getBaseColour()
108     {
109       return colourScheme;
110     }
111
112     public Color getMinColour()
113     {
114       return new Color( (int) r1, (int) g1, (int) b1);
115     }
116
117     public Color getMaxColour()
118     {
119       return new Color( (int) (r1 + rr), (int) (g1 + gg), (int) (b1 + bb));
120     }
121
122     /**
123      * DOCUMENT ME!
124      *
125      * @param n DOCUMENT ME!
126      *
127      * @return DOCUMENT ME!
128      */
129     public Color findColour(String n)
130     {
131         return Color.red;
132     }
133
134     /**
135      * DOCUMENT ME!
136      *
137      * @param n DOCUMENT ME!
138      * @param j DOCUMENT ME!
139      *
140      * @return DOCUMENT ME!
141      */
142     public Color findColour(String n, int j)
143     {
144         currentColour = Color.white;
145
146         if ((threshold == 0) || aboveThreshold(n, j))
147         {
148           if( j<annotation.annotations.length
149               && annotation.annotations[j]!=null
150               && !jalview.util.Comparison.isGap(n.charAt(0)))
151           {
152             if(  aboveAnnotationThreshold==NO_THRESHOLD
153                || (annotationThreshold!=null && aboveAnnotationThreshold==ABOVE_THRESHOLD && annotation.annotations[j].value>=annotationThreshold.value)
154                || (annotationThreshold!=null && aboveAnnotationThreshold==BELOW_THRESHOLD && annotation.annotations[j].value<=annotationThreshold.value))
155             {
156               if(colourScheme!=null)
157               {
158                 currentColour = colourScheme.findColour(n, j);
159               }
160               else if(range!=0)
161               {
162                 dr = rr *
163                     ( (annotation.annotations[j].value - annotation.graphMin) /
164                      range)
165                     + r1;
166                 dg = gg *
167                     ( (annotation.annotations[j].value - annotation.graphMin) /
168                      range)
169                     + g1;
170                 db = bb *
171                     ( (annotation.annotations[j].value - annotation.graphMin) /
172                      range)
173                     + b1;
174
175                 currentColour = new Color( (int) dr, (int) dg, (int) db);
176               }
177             }
178           }
179         }
180
181         if(conservationColouring)
182          applyConservation(j);
183
184        return currentColour;
185     }
186 }