2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
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.
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.
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
21 import jalview.datamodel.*;
27 import java.awt.image.*;
36 public class FeatureRenderer
40 float transparency = 1.0f;
44 Hashtable featureColours = new Hashtable();
47 // A higher level for grouping features of a
49 Hashtable featureGroups = null;
51 // This is actually an Integer held in the hashtable,
52 // Retrieved using the key feature type
55 String [] renderOrder;
58 * Creates a new FeatureRenderer object.
60 * @param av DOCUMENT ME!
62 public FeatureRenderer(AlignViewport av)
67 public void transferSettings(FeatureRenderer fr)
69 renderOrder = fr.renderOrder;
70 featureGroups = fr.featureGroups;
71 featureColours = fr.featureColours;
72 transparency = fr.transparency;
75 BufferedImage offscreenImage;
76 boolean offscreenRender = false;
77 public Color findFeatureColour(Color initialCol, SequenceI seq, int res)
79 return new Color( findFeatureColour (initialCol.getRGB(),
84 * This is used by the Molecule Viewer and Overview to
85 * get the accurate colourof the rendered sequence
87 public int findFeatureColour(int initialCol, SequenceI seq, int column)
89 if(!av.showSequenceFeatures)
95 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
96 if(sequenceFeatures==null)
99 sfSize = sequenceFeatures.length;
102 if(jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))
103 return Color.white.getRGB();
106 //Only bother making an offscreen image if transparency is applied
107 if(transparency!=1.0f && offscreenImage==null)
109 offscreenImage = new BufferedImage(1,1,BufferedImage.TYPE_INT_ARGB);
112 currentColour = null;
114 offscreenRender = true;
116 if(offscreenImage!=null)
118 offscreenImage.setRGB(0,0,initialCol);
119 drawSequence(offscreenImage.getGraphics(),
123 return offscreenImage.getRGB(0,0);
129 lastSeq.findPosition(column),
132 if (currentColour == null)
135 return ((Integer)currentColour).intValue();
145 * @param g DOCUMENT ME!
146 * @param seq DOCUMENT ME!
147 * @param sg DOCUMENT ME!
148 * @param start DOCUMENT ME!
149 * @param end DOCUMENT ME!
150 * @param x1 DOCUMENT ME!
151 * @param y1 DOCUMENT ME!
152 * @param width DOCUMENT ME!
153 * @param height DOCUMENT ME!
156 // SequenceFeature sf;
158 SequenceFeature [] sequenceFeatures;
159 int sfSize, sfindex, spos, epos;
161 public void drawSequence(Graphics g, SequenceI seq,
162 int start, int end, int y1)
164 if ( seq.getDatasetSequence().getSequenceFeatures() == null
165 || seq.getDatasetSequence().getSequenceFeatures().length==0)
169 fm = g.getFontMetrics();
172 if (av.featuresDisplayed == null
177 if(av.featuresDisplayed.size()<1)
180 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
181 sfSize = sequenceFeatures.length;
184 if(lastSeq==null || seq!=lastSeq)
187 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
188 sfSize = sequenceFeatures.length;
192 if (transparency != 1 && g!=null)
194 Graphics2D g2 = (Graphics2D) g;
196 AlphaComposite.getInstance(
197 AlphaComposite.SRC_OVER, transparency));
202 spos = lastSeq.findPosition(start);
203 epos = lastSeq.findPosition(end);
208 for(int renderIndex=0; renderIndex<renderOrder.length; renderIndex++)
210 type = renderOrder[renderIndex];
212 if(!av.featuresDisplayed.containsKey(type))
215 // loop through all features in sequence to find
216 // current feature to render
217 for (sfindex = 0; sfindex < sfSize; sfindex++)
219 if(sequenceFeatures.length<=sfindex)
223 if (!sequenceFeatures[sfindex].type.equals(type))
226 if (featureGroups != null
227 && sequenceFeatures[sfindex].featureGroup != null
229 featureGroups.containsKey(sequenceFeatures[sfindex].featureGroup)
231 ! ( (Boolean) featureGroups.get(sequenceFeatures[sfindex].featureGroup)).
237 if (!offscreenRender && (sequenceFeatures[sfindex].getBegin() > epos
238 || sequenceFeatures[sfindex].getEnd() < spos))
241 if (offscreenRender && offscreenImage==null)
243 if (sequenceFeatures[sfindex].begin <= start &&
244 sequenceFeatures[sfindex].end >= start)
246 currentColour = av.featuresDisplayed.get(sequenceFeatures[sfindex].
250 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
253 renderFeature(g, seq,
254 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
255 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
256 new Color( ( (Integer) av.featuresDisplayed.get(
257 sequenceFeatures[sfindex].type)).intValue()),
259 renderFeature(g, seq,
260 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
261 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
262 new Color( ( (Integer) av.featuresDisplayed.get(
263 sequenceFeatures[sfindex].type)).intValue()),
268 renderFeature(g, seq,
269 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
270 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
271 getColour(sequenceFeatures[sfindex].type),
279 if(transparency!=1.0f && g!=null)
281 Graphics2D g2 = (Graphics2D) g;
283 AlphaComposite.getInstance(
284 AlphaComposite.SRC_OVER, 1.0f));
291 void renderFeature(Graphics g, SequenceI seq,
292 int fstart, int fend, Color featureColour, int start, int end, int y1)
295 if (((fstart <= end) && (fend >= start)))
298 { // fix for if the feature we have starts before the sequence start,
299 fstart = start; // but the feature end is still valid!!
306 int pady = (y1 + av.charHeight) - av.charHeight / 5;
307 for (i = fstart; i <= fend; i++)
309 s = seq.getSequence().charAt(i);
311 if (jalview.util.Comparison.isGap(s))
316 g.setColor(featureColour);
318 g.fillRect( (i - start) * av.charWidth, y1, av.charWidth,av.charHeight);
320 if(offscreenRender || !av.validCharWidth)
323 g.setColor(Color.white);
324 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
325 g.drawString(String.valueOf(s),
326 charOffset + (av.charWidth * (i - start)),
334 boolean newFeatureAdded = false;
336 public void featuresAdded()
341 boolean findingFeatures = false;
342 synchronized void findAllFeatures()
344 newFeatureAdded = false;
348 newFeatureAdded = true;
352 findingFeatures = true;
353 jalview.schemes.UserColourScheme ucs = new
354 jalview.schemes.UserColourScheme();
356 if(av.featuresDisplayed==null)
357 av.featuresDisplayed = new Hashtable();
359 av.featuresDisplayed.clear();
361 Vector allfeatures = new Vector();
362 for (int i = 0; i < av.alignment.getHeight(); i++)
364 SequenceFeature [] features
365 = av.alignment.getSequenceAt(i).getDatasetSequence().getSequenceFeatures();
367 if (features == null)
371 while (index < features.length)
373 if (!av.featuresDisplayed.containsKey(features[index].getType()))
375 if(!(features[index].begin == 0 && features[index].end ==0))
377 // If beginning and end are 0, the feature is for the whole sequence
378 // and we don't want to render the feature in the normal way
380 if (getColour(features[index].getType()) == null)
382 featureColours.put(features[index].getType(),
383 ucs.createColourFromName(features[index].
387 av.featuresDisplayed.put(features[index].getType(),
388 new Integer(getColour(features[index].
389 getType()).getRGB()));
390 allfeatures.addElement(features[index].getType());
397 renderOrder = new String[allfeatures.size()];
398 Enumeration en = allfeatures.elements();
399 int i = allfeatures.size()-1;
400 while(en.hasMoreElements())
402 renderOrder[i] = en.nextElement().toString();
406 findingFeatures = false;
409 public Color getColour(String featureType)
411 Color colour = (Color)featureColours.get(featureType);
416 public void addNewFeature(String name, Color col, String group)
419 setColour(name, col);
420 if(av.featuresDisplayed==null)
421 av.featuresDisplayed = new Hashtable();
426 av.featuresDisplayed.put(name, new Integer(col.getRGB()));
429 public void setColour(String featureType, Color col)
431 featureColours.put(featureType, col);
434 public void setTransparency(float value)
436 transparency = value;
439 public float getTransparency()
444 public void setFeaturePriority(Object [][] data)
446 // The feature table will display high priority
447 // features at the top, but theses are the ones
448 // we need to render last, so invert the data
449 if(av.featuresDisplayed!=null)
450 av.featuresDisplayed.clear();
452 av.featuresDisplayed = new Hashtable();
454 renderOrder = new String[data.length];
457 for (int i = 0; i < data.length; i++)
459 String type = data[i][0].toString();
460 setColour(type, (Color) data[i][1]);
461 if ( ( (Boolean) data[i][2]).booleanValue())
463 av.featuresDisplayed.put(type, new Integer(getColour(type).getRGB()));
466 renderOrder[data.length - i - 1] = type;