1 package jalview.renderer.seqfeatures;
3 import jalview.datamodel.SequenceFeature;
4 import jalview.datamodel.SequenceI;
6 import java.awt.AlphaComposite;
8 import java.awt.FontMetrics;
9 import java.awt.Graphics;
10 import java.awt.Graphics2D;
11 import java.awt.image.BufferedImage;
13 public class FeatureRenderer extends
14 jalview.viewmodel.seqfeatures.FeatureRendererModel
21 boolean offscreenRender = false;
45 protected SequenceI lastSeq;
51 int av_charHeight, av_charWidth;
53 boolean av_validCharWidth, av_isShowSeqFeatureHeight;
55 protected void updateAvConfig()
57 av_charHeight = av.getCharHeight();
58 av_charWidth = av.getCharWidth();
59 av_validCharWidth = av.isValidCharWidth();
60 av_isShowSeqFeatureHeight = av.isShowSequenceFeaturesHeight();
63 void renderFeature(Graphics g, SequenceI seq, int fstart, int fend,
64 Color featureColour, int start, int end, int y1)
67 if (((fstart <= end) && (fend >= start)))
70 { // fix for if the feature we have starts before the sequence start,
71 fstart = start; // but the feature end is still valid!!
78 int pady = (y1 + av_charHeight) - av_charHeight / 5;
79 for (i = fstart; i <= fend; i++)
83 if (jalview.util.Comparison.isGap(s))
88 g.setColor(featureColour);
90 g.fillRect((i - start) * av_charWidth, y1, av_charWidth,
93 if (offscreenRender || !av_validCharWidth)
98 g.setColor(Color.white);
99 charOffset = (av_charWidth - fm.charWidth(s)) / 2;
100 g.drawString(String.valueOf(s), charOffset
101 + (av_charWidth * (i - start)), pady);
107 void renderScoreFeature(Graphics g, SequenceI seq, int fstart, int fend,
108 Color featureColour, int start, int end, int y1, byte[] bs)
111 if (((fstart <= end) && (fend >= start)))
114 { // fix for if the feature we have starts before the sequence start,
115 fstart = start; // but the feature end is still valid!!
122 int pady = (y1 + av_charHeight) - av_charHeight / 5;
123 int ystrt = 0, yend = av_charHeight;
126 // signed - zero is always middle of residue line.
129 yend = av_charHeight * (128 - bs[1]) / 512;
130 ystrt = av_charHeight - yend / 2;
134 ystrt = av_charHeight / 2;
135 yend = av_charHeight * (bs[1] - 128) / 512;
140 yend = av_charHeight * bs[1] / 255;
141 ystrt = av_charHeight - yend;
144 for (i = fstart; i <= fend; i++)
146 s = seq.getCharAt(i);
148 if (jalview.util.Comparison.isGap(s))
153 g.setColor(featureColour);
154 int x = (i - start) * av_charWidth;
155 g.drawRect(x, y1, av_charWidth, av_charHeight);
156 g.fillRect(x, y1 + ystrt, av_charWidth, yend);
158 if (offscreenRender || !av_validCharWidth)
163 g.setColor(Color.black);
164 charOffset = (av_charWidth - fm.charWidth(s)) / 2;
165 g.drawString(String.valueOf(s), charOffset
166 + (av_charWidth * (i - start)), pady);
172 BufferedImage offscreenImage;
174 public Color findFeatureColour(Color initialCol, SequenceI seq, int res)
176 return new Color(findFeatureColour(initialCol.getRGB(), seq, res));
180 * This is used by the Molecule Viewer and Overview to get the accurate
181 * colourof the rendered sequence
183 public synchronized int findFeatureColour(int initialCol, final SequenceI seq,
186 if (!av.isShowSequenceFeatures())
191 SequenceFeature[] sf = seq.getSequenceFeatures();
195 sequenceFeatures = sf;
196 if (sequenceFeatures != null)
198 sfSize = sequenceFeatures.length;
203 if (sequenceFeatures != sf)
205 sequenceFeatures = sf;
206 if (sequenceFeatures != null)
208 sfSize = sequenceFeatures.length;
213 if (sequenceFeatures == null || sfSize == 0)
218 if (jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))
220 return Color.white.getRGB();
223 // Only bother making an offscreen image if transparency is applied
224 if (transparency != 1.0f && offscreenImage == null)
226 offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
229 currentColour = null;
230 // TODO: non-threadsafe - each rendering thread needs its own instance of
231 // the feature renderer - or this should be synchronized.
232 offscreenRender = true;
234 if (offscreenImage != null)
236 offscreenImage.setRGB(0, 0, initialCol);
237 drawSequence(offscreenImage.getGraphics(), lastSeq, column, column, 0);
239 return offscreenImage.getRGB(0, 0);
243 drawSequence(null, lastSeq, lastSeq.findPosition(column), -1, -1);
245 if (currentColour == null)
251 return ((Integer) currentColour).intValue();
257 private volatile SequenceFeature[] sequenceFeatures;
267 public synchronized void drawSequence(Graphics g, final SequenceI seq,
268 int start, int end, int y1)
270 SequenceFeature[] sf = seq.getSequenceFeatures();
271 if (sf == null || sf.length == 0)
278 fm = g.getFontMetrics();
283 if (lastSeq == null || seq != lastSeq
284 || sf != sequenceFeatures)
287 sequenceFeatures = sf;
290 if (transparency != 1 && g != null)
292 Graphics2D g2 = (Graphics2D) g;
293 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
297 if (!offscreenRender)
299 spos = lastSeq.findPosition(start);
300 epos = lastSeq.findPosition(end);
303 sfSize = sequenceFeatures.length;
305 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
307 type = renderOrder[renderIndex];
309 if (type == null || !showFeatureOfType(type))
314 // loop through all features in sequence to find
315 // current feature to render
316 for (sfindex = 0; sfindex < sfSize; sfindex++)
318 if (!sequenceFeatures[sfindex].type.equals(type))
323 if (featureGroups != null
324 && sequenceFeatures[sfindex].featureGroup != null
325 && sequenceFeatures[sfindex].featureGroup.length() != 0
327 .containsKey(sequenceFeatures[sfindex].featureGroup)
329 .get(sequenceFeatures[sfindex].featureGroup)
336 && (sequenceFeatures[sfindex].getBegin() > epos || sequenceFeatures[sfindex]
342 if (offscreenRender && offscreenImage == null)
344 if (sequenceFeatures[sfindex].begin <= start
345 && sequenceFeatures[sfindex].end >= start)
347 // this is passed out to the overview and other sequence renderers
348 // (e.g. molecule viewer) to get displayed colour for rendered
350 currentColour = new Integer(
351 getColour(sequenceFeatures[sfindex]).getRGB());
352 // used to be retreived from av.featuresDisplayed
353 // currentColour = av.featuresDisplayed
354 // .get(sequenceFeatures[sfindex].type);
358 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
361 renderFeature(g, seq,
362 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
363 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
364 getColour(sequenceFeatures[sfindex])
365 // new Color(((Integer) av.featuresDisplayed
366 // .get(sequenceFeatures[sfindex].type)).intValue())
368 renderFeature(g, seq,
369 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
370 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
371 getColour(sequenceFeatures[sfindex])
372 // new Color(((Integer) av.featuresDisplayed
373 // .get(sequenceFeatures[sfindex].type)).intValue())
377 else if (showFeature(sequenceFeatures[sfindex]))
379 if (av_isShowSeqFeatureHeight
380 && sequenceFeatures[sfindex].score != Float.NaN)
382 renderScoreFeature(g, seq,
383 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
384 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
385 getColour(sequenceFeatures[sfindex]), start, end, y1,
386 normaliseScore(sequenceFeatures[sfindex]));
390 renderFeature(g, seq,
391 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
392 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
393 getColour(sequenceFeatures[sfindex]), start, end, y1);
401 if (transparency != 1.0f && g != null && transparencyAvailable)
403 Graphics2D g2 = (Graphics2D) g;
404 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
409 boolean transparencyAvailable = true;
411 protected void setTransparencyAvailable(boolean isTransparencyAvailable)
413 transparencyAvailable = isTransparencyAvailable;
417 public boolean isTransparencyAvailable()
419 return transparencyAvailable;
423 * Called when alignment in associated view has new/modified features to
424 * discover and display.
427 public void featuresAdded()