3 * Jalview - A Sequence Alignment Editor and Viewer
\r
4 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
6 * This program is free software; you can redistribute it and/or
\r
7 * modify it under the terms of the GNU General Public License
\r
8 * as published by the Free Software Foundation; either version 2
\r
9 * of the License, or (at your option) any later version.
\r
11 * This program is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with this program; if not, write to the Free Software
\r
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
20 package jalview.appletgui;
\r
22 import jalview.datamodel.*;
\r
32 * @version $Revision$
\r
34 public class FeatureRenderer
\r
38 Hashtable featureColours = new Hashtable();
\r
40 // A higher level for grouping features of a
\r
42 Hashtable featureGroups = null;
\r
44 // Holds web links for feature groups and feature types
\r
45 // in the form label|link
\r
46 Hashtable featureLinks = null;
\r
49 // This is actually an Integer held in the hashtable,
\r
50 // Retrieved using the key feature type
\r
51 Object currentColour;
\r
53 String [] renderOrder;
\r
58 float transparency = 1f;
\r
60 TransparencySetter transparencySetter = null;
\r
63 * Creates a new FeatureRenderer object.
\r
65 * @param av DOCUMENT ME!
\r
67 public FeatureRenderer(AlignViewport av)
\r
71 if(!System.getProperty("java.version").startsWith("1.1"))
\r
72 transparencySetter = new TransparencySetter();
\r
76 public void transferSettings(FeatureRenderer fr)
\r
78 renderOrder = fr.renderOrder;
\r
79 featureGroups = fr.featureGroups;
\r
80 featureColours = fr.featureColours;
\r
84 public Color findFeatureColour(Color initialCol, SequenceI seq, int i)
\r
87 if(!av.showSequenceFeatures)
\r
91 sequenceFeatures = lastSequence.getSequenceFeatures();
\r
92 if(sequenceFeatures==null)
\r
95 sfSize = sequenceFeatures.length;
\r
97 if(jalview.util.Comparison.isGap(lastSequence.getCharAt(i)))
\r
100 currentColour = null;
\r
102 drawSequence(null, lastSequence, lastSequence.findPosition(i), -1,-1, -1, -1);
\r
104 if(currentColour==null)
\r
107 return new Color( ((Integer)currentColour).intValue() );
\r
111 * This is used by the Molecule Viewer to get the accurate colour
\r
112 * of the rendered sequence
\r
114 boolean overview = false;
\r
116 int white = Color.white.getRGB();
\r
117 public int findFeatureColour(int initialCol, int seqIndex, int column)
\r
119 if(!av.showSequenceFeatures)
\r
122 if(seqIndex!=lastSequenceIndex)
\r
124 lastSequence = av.alignment.getSequenceAt(seqIndex);
\r
125 lastSequenceIndex = seqIndex;
\r
126 sequenceFeatures = lastSequence.getSequenceFeatures();
\r
127 if(sequenceFeatures==null)
\r
130 sfSize = sequenceFeatures.length;
\r
134 if(jalview.util.Comparison.isGap(lastSequence.getCharAt(column)))
\r
135 return Color.white.getRGB();
\r
137 currentColour = null;
\r
139 drawSequence(null, lastSequence, lastSequence.findPosition(column), -1,-1, -1, -1);
\r
141 if(currentColour==null)
\r
144 return ((Integer)currentColour).intValue();
\r
151 * @param g DOCUMENT ME!
\r
152 * @param seq DOCUMENT ME!
\r
153 * @param sg DOCUMENT ME!
\r
154 * @param start DOCUMENT ME!
\r
155 * @param end DOCUMENT ME!
\r
156 * @param x1 DOCUMENT ME!
\r
157 * @param y1 DOCUMENT ME!
\r
158 * @param width DOCUMENT ME!
\r
159 * @param height DOCUMENT ME!
\r
162 // SequenceFeature sf;
\r
163 int lastSequenceIndex=-1;
\r
164 SequenceI lastSequence;
\r
165 SequenceFeature [] sequenceFeatures;
\r
166 int sfSize, sfindex, spos, epos;
\r
168 public void drawSequence(Graphics g, SequenceI seq,
\r
169 int start, int end, int y1, int width, int height)
\r
171 if ( seq.getSequenceFeatures() == null
\r
172 || seq.getSequenceFeatures().length==0)
\r
175 if(transparencySetter!=null && g!=null)
\r
177 transparencySetter.setTransparency(g, transparency);
\r
180 if (av.featuresDisplayed == null || renderOrder==null)
\r
183 if(av.featuresDisplayed.size()<1)
\r
186 sequenceFeatures = seq.getSequenceFeatures();
\r
187 sfSize = sequenceFeatures.length;
\r
189 if(lastSequence==null || seq!=lastSequence)
\r
191 lastSequence = seq;
\r
192 sequenceFeatures = seq.getSequenceFeatures();
\r
193 sfSize = sequenceFeatures.length;
\r
197 spos = lastSequence.findPosition(start);
\r
198 epos = lastSequence.findPosition(end);
\r
200 fm = g.getFontMetrics();
\r
203 for(int renderIndex=0; renderIndex<renderOrder.length; renderIndex++)
\r
205 type = renderOrder[renderIndex];
\r
206 if(!av.featuresDisplayed.containsKey(type))
\r
209 // loop through all features in sequence to find
\r
210 // current feature to render
\r
211 for (sfindex = 0; sfindex < sfSize; sfindex++)
\r
213 if (!sequenceFeatures[sfindex].type.equals(type))
\r
216 if (featureGroups != null
\r
217 && sequenceFeatures[sfindex].featureGroup != null
\r
219 featureGroups.containsKey(sequenceFeatures[sfindex].featureGroup)
\r
221 ! ( (Boolean) featureGroups.get(sequenceFeatures[sfindex].featureGroup)).
\r
227 if (!overview && (sequenceFeatures[sfindex].getBegin() > epos
\r
228 || sequenceFeatures[sfindex].getEnd() < spos))
\r
233 if (sequenceFeatures[sfindex].begin <= start &&
\r
234 sequenceFeatures[sfindex].end >= start)
\r
236 currentColour = av.featuresDisplayed.get(sequenceFeatures[sfindex].
\r
241 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
\r
244 renderFeature(g, seq,
\r
245 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
\r
246 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
\r
247 new Color( ( (Integer) av.featuresDisplayed.get(
\r
248 sequenceFeatures[sfindex].type)).intValue()),
\r
249 start, end, y1, width, height);
\r
250 renderFeature(g, seq,
\r
251 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
\r
252 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
\r
253 new Color( ( (Integer) av.featuresDisplayed.get(
\r
254 sequenceFeatures[sfindex].type)).intValue()),
\r
255 start, end, y1, width, height);
\r
259 renderFeature(g, seq,
\r
260 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
\r
261 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
\r
262 getColour(sequenceFeatures[sfindex].type),
\r
263 start, end, y1, width, height);
\r
268 if(transparencySetter!=null && g!=null)
\r
270 transparencySetter.setTransparency(g, 1.0f);
\r
277 void renderFeature(Graphics g, SequenceI seq,
\r
278 int fstart, int fend, Color featureColour, int start, int end, int y1, int width, int height)
\r
281 if (((fstart <= end) && (fend >= start)))
\r
283 if (fstart < start)
\r
284 { // fix for if the feature we have starts before the sequence start,
\r
285 fstart = start; // but the feature end is still valid!!
\r
293 for (i = fstart; i <= fend; i++)
\r
295 s = seq.getSequence().charAt(i);
\r
297 if (jalview.util.Comparison.isGap(s))
\r
302 g.setColor(featureColour);
\r
304 g.fillRect( (i - start) * width, y1, width, height);
\r
306 if(!av.validCharWidth)
\r
309 g.setColor(Color.white);
\r
310 charOffset = (width - fm.charWidth(s)) / 2;
\r
311 g.drawString(String.valueOf(s),
\r
312 charOffset + (width * (i - start)),
\r
313 (y1 + height) - height / 5); //pady = height / 5;
\r
319 void findAllFeatures()
\r
321 av.featuresDisplayed = new Hashtable();
\r
322 Vector allfeatures = new Vector();
\r
323 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
325 SequenceFeature [] features = av.alignment.getSequenceAt(i).getSequenceFeatures();
\r
327 if (features == null)
\r
331 while (index < features.length)
\r
333 if (!av.featuresDisplayed.containsKey(features[index].getType()))
\r
335 av.featuresDisplayed.put(features[index].getType(),
\r
336 new Integer( getColour(features[index].getType()).getRGB()) );
\r
337 allfeatures.addElement(features[index].getType());
\r
343 renderOrder = new String[allfeatures.size()];
\r
344 Enumeration en = allfeatures.elements();
\r
345 int i = allfeatures.size()-1;
\r
346 while(en.hasMoreElements())
\r
348 renderOrder[i] = en.nextElement().toString();
\r
353 public Color getColour(String featureType)
\r
355 return (Color)featureColours.get(featureType);
\r
358 public void addNewFeature(String name, Color col)
\r
361 setColour(name, col);
\r
362 if(av.featuresDisplayed==null)
\r
363 av.featuresDisplayed = new Hashtable();
\r
366 av.featuresDisplayed.put(name, "NOGROUP");
\r
369 public void setColour(String featureType, Color col)
\r
371 featureColours.put(featureType, col);
\r
374 public void setFeaturePriority(Object [][] data)
\r
376 // The feature table will display high priority
\r
377 // features at the top, but theses are the ones
\r
378 // we need to render last, so invert the data
\r
379 if(av.featuresDisplayed!=null)
\r
380 av.featuresDisplayed.clear();
\r
382 renderOrder = new String[data.length];
\r
384 if (data.length > 0)
\r
385 for (int i = 0; i < data.length; i++)
\r
387 String type = data[i][0].toString();
\r
388 setColour(type, (Color) data[i][1]);
\r
389 if ( ( (Boolean) data[i][2]).booleanValue())
\r
391 av.featuresDisplayed.put(type, new Integer(getColour(type).getRGB()));
\r
394 renderOrder[data.length - i - 1] = type;
\r
399 class TransparencySetter
\r
401 void setTransparency(Graphics g, float value)
\r
403 // Graphics2D g2 = (Graphics2D) g;
\r
404 // g2.setComposite(
\r
405 // AlphaComposite.getInstance(
\r
406 // AlphaComposite.SRC_OVER, value));
\r