add76a0f883aaba155fe21c16c311929646eadda
[jalview.git] / src / jalview / schemes / ResidueColourScheme.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 jalview.analysis.*;
22
23 import java.awt.*;
24
25 import java.util.*;
26
27
28 /**
29  * DOCUMENT ME!
30  *
31  * @author $author$
32  * @version $Revision$
33  */
34 public class ResidueColourScheme implements ColourSchemeI
35 {
36
37     boolean conservationColouring = false;
38     boolean consensusColouring = false;
39
40     Color[] colors;
41     int threshold = 0;
42
43     /* Set when threshold colouring to either pid_gaps or pid_nogaps*/
44     protected String ignoreGaps = "pid_gaps";
45
46     /** Consenus as a hashtable array */
47     Hashtable [] consensus;
48
49     /** Conservation string as a char array */
50    char [] conservation;
51
52    /** DOCUMENT ME!! */
53    int inc = 30;
54
55    /**
56     * The colour to be calculated, manipulated and returned
57     */
58    Color currentColour = null;
59
60
61
62     /**
63      * Creates a new ResidueColourScheme object.
64      *
65      * @param colors DOCUMENT ME!
66      * @param threshold DOCUMENT ME!
67      */
68     public ResidueColourScheme(Color[] colours, int threshold)
69     {
70         this.colors = colours;
71         this.threshold = threshold;
72     }
73
74     /**
75      * Creates a new ResidueColourScheme object.
76      */
77     public ResidueColourScheme()
78     {
79     }
80
81     /**
82     * Find a colour without an index in a sequence
83     */
84    public Color findColour(String aa)
85    {
86        return colors[((Integer) (ResidueProperties.aaHash.get(aa))).intValue()];
87    }
88
89
90
91    public Color findColour(String s, int j)
92    {
93
94        int index = ((Integer) (ResidueProperties.aaHash.get(s))).intValue();
95
96        if ((threshold == 0) || aboveThreshold(ResidueProperties.aa[index], j))
97        {
98            currentColour = colors[index];
99        }
100        else
101        {
102            currentColour = Color.white;
103        }
104
105        if(conservationColouring)
106          applyConservation(j);
107
108
109        return currentColour;
110    }
111
112
113     /**
114      * Get the percentage threshold for this colour scheme
115      *
116      * @return Returns the percentage threshold
117      */
118     public int getThreshold()
119     {
120         return threshold;
121     }
122
123     /**
124      * DOCUMENT ME!
125      *
126      * @param ct DOCUMENT ME!
127      */
128     public void setThreshold(int ct, boolean ignoreGaps)
129     {
130         threshold = ct;
131         if(ignoreGaps)
132           this.ignoreGaps = "pid_nogaps";
133         else
134           this.ignoreGaps = "pid_gaps";
135     }
136
137     /**
138      * DOCUMENT ME!
139      *
140      * @param s DOCUMENT ME!
141      * @param j DOCUMENT ME!
142      *
143      * @return DOCUMENT ME!
144      */
145     public boolean aboveThreshold(String s, int j)
146     {
147         if ((((Integer) consensus[j].get("maxCount")).intValue() != -1) &&
148                 consensus[j].contains(s))
149         {
150             if (((Float)consensus[j].get(ignoreGaps)).floatValue() >= threshold)
151             {
152                 return true;
153             }
154         }
155
156         return false;
157     }
158
159
160     public boolean conservationApplied()
161     {
162       return conservationColouring;
163     }
164
165     public void setConservationInc(int i)
166     {
167       inc = i;
168     }
169
170     public int getConservationInc()
171     {
172       return inc;
173     }
174
175     /**
176      * DOCUMENT ME!
177      *
178      * @param consensus DOCUMENT ME!
179      */
180     public void setConsensus(Vector vconsensus)
181     {
182       if(vconsensus==null)
183         return;
184
185        int i, iSize=vconsensus.size();
186        consensus = new Hashtable[iSize];
187        for(i=0; i<iSize; i++)
188         consensus[i] = (Hashtable)vconsensus.elementAt(i);
189     }
190
191
192     public void setConservation(Conservation cons)
193     {
194       if(cons==null)
195       {
196         conservationColouring = false;
197         conservation = null;
198       }
199       else
200       {
201         conservationColouring = true;
202         int i, iSize = cons.getConsSequence().getLength();
203         conservation = new char[iSize];
204         for (i = 0; i < iSize; i++)
205           conservation[i] = cons.getConsSequence().getCharAt(i);
206       }
207
208     }
209
210
211     /**
212     * DOCUMENT ME!
213     *
214     * @param s DOCUMENT ME!
215     * @param i DOCUMENT ME!
216     *
217     * @return DOCUMENT ME!
218     */
219
220    void applyConservation(int i)
221    {
222
223      if ((conservation[i] != '*') && (conservation[i] != '+'))
224      {
225        if(jalview.util.Comparison.isGap(conservation[i]))
226        {
227          currentColour = Color.white;
228        }
229        else
230        {
231          float t = 11 - (conservation[i] - '0');
232          if(t==0)
233          {
234            currentColour = Color.white;
235            return;
236          }
237
238          int red = currentColour.getRed();
239          int green = currentColour.getGreen();
240          int blue = currentColour.getBlue();
241
242          int dr = 255 - red;
243          int dg = 255 - green;
244          int db = 255 - blue;
245
246          dr *= t / 10f;
247          dg *= t / 10f;
248          db *= t / 10f;
249
250          red += (inc / 20f) * dr;
251          green += (inc / 20f) * dg;
252          blue += (inc / 20f) * db;
253
254          if (red > 255 || green > 255 || blue > 255)
255            currentColour = Color.white;
256          else
257            currentColour = new Color(red, green, blue);
258        }
259        }
260    }
261
262
263 }