updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / schemes / UserColourScheme.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 import java.util.StringTokenizer;
23
24 public class UserColourScheme
25     extends ResidueColourScheme
26 {
27   Color [] lowerCaseColours;
28
29   protected String schemeName;
30
31   public UserColourScheme()
32   {  }
33
34   public UserColourScheme(Color[] newColors)
35   {
36     colors = newColors;
37   }
38
39   public UserColourScheme(String colour)
40   {
41     Color col = getColourFromString(colour);
42
43     if(col==null)
44     {
45       System.out.println("Unknown colour!! "+colour);
46       col = createColourFromName(colour);
47     }
48
49     colors = new Color[24];
50     for(int i=0; i<24; i++)
51       colors[i] = col;
52   }
53
54   public Color[] getColours()
55   {
56     return colors;
57   }
58
59   public Color[] getLowerCaseColours()
60   {
61     return lowerCaseColours;
62   }
63
64   public void setName(String name)
65   {
66     schemeName = name;
67   }
68
69   public String getName()
70   {
71     return schemeName;
72   }
73
74   public Color getColourFromString(String colour)
75   {
76     colour = colour.trim();
77
78     Color col = null;
79     try
80     {
81       int value = Integer.parseInt(colour, 16);
82       col = new Color(value);
83     }
84     catch (NumberFormatException ex)
85     {}
86
87     if (col == null)
88       col = ColourSchemeProperty.getAWTColorFromName(colour);
89
90     if (col == null)
91     {
92       try
93       {
94         java.util.StringTokenizer st = new java.util.StringTokenizer(colour,
95             ",");
96         int r = Integer.parseInt(st.nextToken());
97         int g = Integer.parseInt(st.nextToken());
98         int b = Integer.parseInt(st.nextToken());
99         col = new Color(r, g, b);
100       }
101       catch (Exception ex)
102       {}
103     }
104
105     return col;
106
107   }
108
109   public Color createColourFromName(String name)
110   {
111     int r, g, b;
112
113     int lsize = name.length();
114     int start = 0, end = lsize / 3;
115
116     int rgbOffset = Math.abs(name.hashCode() % 10) * 15;
117
118     r = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
119     start = end;
120     end += lsize / 3;
121     if (end > lsize)
122       end = lsize;
123
124     g = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
125
126     b = Math.abs(name.substring(end).hashCode() + rgbOffset) % 210 + 20;
127
128     Color color = new Color(r, g, b);
129
130     return color;
131   }
132
133   public void parseAppletParameter(String paramValue)
134   {
135     StringTokenizer st = new StringTokenizer(paramValue, ";");
136     StringTokenizer st2;
137     String token=null, colour, residues, residue;
138     try{
139       while (st.hasMoreElements())
140       {
141         token = st.nextToken().trim();
142         residues = token.substring(0, token.indexOf("="));
143         colour = token.substring(token.indexOf("=") + 1);
144
145         st2 = new StringTokenizer(residues, " ,");
146         while (st2.hasMoreTokens())
147         {
148           token = st2.nextToken();
149           if(token.equalsIgnoreCase("lowerCase"))
150           {
151             if (lowerCaseColours == null)
152               lowerCaseColours = new Color[23];
153             for (int i = 0; i < 23; i++)
154               if (lowerCaseColours[i] == null)
155                 lowerCaseColours[i] = getColourFromString(colour);
156
157               continue;
158           }
159
160           int colIndex =
161               ( (Integer) ResidueProperties.aaHash.
162                get(token)).intValue();
163
164           if(token.equals(token.toLowerCase()))
165           {
166             if(lowerCaseColours==null)
167             {
168               lowerCaseColours = new Color[23];
169             }
170             lowerCaseColours[colIndex] = getColourFromString(colour);
171           }
172           else
173             colors[colIndex] = getColourFromString(colour);
174         }
175       }
176     }
177     catch(Exception ex)
178   {
179     System.out.println("Error parsing userDefinedColours:\n"
180                        +token+"\n"+ex);
181   }
182
183   }
184
185
186
187   public Color findColour(String s, int j)
188   {
189       int index = ((Integer) (ResidueProperties.aaHash.get(s))).intValue();
190
191       if ((threshold == 0) || aboveThreshold(ResidueProperties.aa[index], j))
192       {
193         if(lowerCaseColours!=null && 'a' <= s.charAt(0) && s.charAt(0) <= 'z')
194           currentColour = lowerCaseColours[index];
195         else
196           currentColour = colors[index];
197       }
198       else
199       {
200           currentColour = Color.white;
201       }
202
203       if(conservationColouring)
204         applyConservation(j);
205
206
207       return currentColour;
208    }
209
210    public void setLowerCaseColours(Color [] lcolours)
211    {
212      lowerCaseColours = lcolours;
213    }
214
215 }