2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.renderer.seqfeatures;
23 import jalview.api.AlignViewportI;
24 import jalview.datamodel.SequenceFeature;
25 import jalview.datamodel.SequenceI;
26 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
28 import java.awt.AlphaComposite;
29 import java.awt.Color;
30 import java.awt.FontMetrics;
31 import java.awt.Graphics;
32 import java.awt.Graphics2D;
33 import java.awt.image.BufferedImage;
35 public class FeatureRenderer extends FeatureRendererModel
42 boolean offscreenRender = false;
44 protected SequenceI lastSeq;
50 int av_charHeight, av_charWidth;
52 boolean av_validCharWidth, av_isShowSeqFeatureHeight;
55 * Constructor given a viewport
59 public FeatureRenderer(AlignViewportI viewport)
64 protected void updateAvConfig()
66 av_charHeight = av.getCharHeight();
67 av_charWidth = av.getCharWidth();
68 av_validCharWidth = av.isValidCharWidth();
69 av_isShowSeqFeatureHeight = av.isShowSequenceFeaturesHeight();
72 void renderFeature(Graphics g, SequenceI seq, int fstart, int fend,
73 Color featureColour, int start, int end, int y1)
76 if (((fstart <= end) && (fend >= start)))
79 { // fix for if the feature we have starts before the sequence start,
80 fstart = start; // but the feature end is still valid!!
87 int pady = (y1 + av_charHeight) - av_charHeight / 5;
88 for (i = fstart; i <= fend; i++)
92 if (jalview.util.Comparison.isGap(s))
97 g.setColor(featureColour);
99 g.fillRect((i - start) * av_charWidth, y1, av_charWidth,
102 if (offscreenRender || !av_validCharWidth)
107 g.setColor(Color.white);
108 charOffset = (av_charWidth - fm.charWidth(s)) / 2;
109 g.drawString(String.valueOf(s), charOffset
110 + (av_charWidth * (i - start)), pady);
116 void renderScoreFeature(Graphics g, SequenceI seq, int fstart, int fend,
117 Color featureColour, int start, int end, int y1, byte[] bs)
120 if (((fstart <= end) && (fend >= start)))
123 { // fix for if the feature we have starts before the sequence start,
124 fstart = start; // but the feature end is still valid!!
131 int pady = (y1 + av_charHeight) - av_charHeight / 5;
132 int ystrt = 0, yend = av_charHeight;
135 // signed - zero is always middle of residue line.
138 yend = av_charHeight * (128 - bs[1]) / 512;
139 ystrt = av_charHeight - yend / 2;
143 ystrt = av_charHeight / 2;
144 yend = av_charHeight * (bs[1] - 128) / 512;
149 yend = av_charHeight * bs[1] / 255;
150 ystrt = av_charHeight - yend;
153 for (i = fstart; i <= fend; i++)
155 s = seq.getCharAt(i);
157 if (jalview.util.Comparison.isGap(s))
162 g.setColor(featureColour);
163 int x = (i - start) * av_charWidth;
164 g.drawRect(x, y1, av_charWidth, av_charHeight);
165 g.fillRect(x, y1 + ystrt, av_charWidth, yend);
167 if (offscreenRender || !av_validCharWidth)
172 g.setColor(Color.black);
173 charOffset = (av_charWidth - fm.charWidth(s)) / 2;
174 g.drawString(String.valueOf(s), charOffset
175 + (av_charWidth * (i - start)), pady);
180 BufferedImage offscreenImage;
183 public Color findFeatureColour(Color initialCol, SequenceI seq, int res)
185 return new Color(findFeatureColour(initialCol.getRGB(), seq, res));
189 * This is used by the Molecule Viewer and Overview to get the accurate
190 * colourof the rendered sequence
192 public synchronized int findFeatureColour(int initialCol,
193 final SequenceI seq, int column)
195 if (!av.isShowSequenceFeatures())
200 SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
204 lastSequenceFeatures = sequenceFeatures;
205 if (lastSequenceFeatures != null)
207 sfSize = lastSequenceFeatures.length;
212 if (lastSequenceFeatures != sequenceFeatures)
214 lastSequenceFeatures = sequenceFeatures;
215 if (lastSequenceFeatures != null)
217 sfSize = lastSequenceFeatures.length;
222 if (lastSequenceFeatures == null || sfSize == 0)
227 if (jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))
229 return Color.white.getRGB();
232 // Only bother making an offscreen image if transparency is applied
233 if (transparency != 1.0f && offscreenImage == null)
235 offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
238 currentColour = null;
239 // TODO: non-threadsafe - each rendering thread needs its own instance of
240 // the feature renderer - or this should be synchronized.
241 offscreenRender = true;
243 if (offscreenImage != null)
245 offscreenImage.setRGB(0, 0, initialCol);
246 drawSequence(offscreenImage.getGraphics(), lastSeq, column, column, 0);
248 return offscreenImage.getRGB(0, 0);
252 drawSequence(null, lastSeq, lastSeq.findPosition(column), -1, -1);
254 if (currentColour == null)
260 return ((Integer) currentColour).intValue();
266 private volatile SequenceFeature[] lastSequenceFeatures;
276 public synchronized void drawSequence(Graphics g, final SequenceI seq,
277 int start, int end, int y1)
279 SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
280 if (sequenceFeatures == null || sequenceFeatures.length == 0)
287 fm = g.getFontMetrics();
292 if (lastSeq == null || seq != lastSeq
293 || sequenceFeatures != lastSequenceFeatures)
296 lastSequenceFeatures = sequenceFeatures;
299 if (transparency != 1 && g != null)
301 Graphics2D g2 = (Graphics2D) g;
302 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
306 if (!offscreenRender)
308 spos = lastSeq.findPosition(start);
309 epos = lastSeq.findPosition(end);
312 sfSize = lastSequenceFeatures.length;
314 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
316 type = renderOrder[renderIndex];
318 if (type == null || !showFeatureOfType(type))
323 // loop through all features in sequence to find
324 // current feature to render
325 for (sfindex = 0; sfindex < sfSize; sfindex++)
327 final SequenceFeature sequenceFeature = lastSequenceFeatures[sfindex];
328 if (!sequenceFeature.type.equals(type))
333 if (featureGroups != null
334 && sequenceFeature.featureGroup != null
335 && sequenceFeature.featureGroup.length() != 0
336 && featureGroups.containsKey(sequenceFeature.featureGroup)
337 && !featureGroups.get(sequenceFeature.featureGroup)
344 && (sequenceFeature.getBegin() > epos || sequenceFeature
350 if (offscreenRender && offscreenImage == null)
352 if (sequenceFeature.begin <= start
353 && sequenceFeature.end >= start)
355 // this is passed out to the overview and other sequence renderers
356 // (e.g. molecule viewer) to get displayed colour for rendered
358 currentColour = new Integer(getColour(sequenceFeature).getRGB());
359 // used to be retreived from av.featuresDisplayed
360 // currentColour = av.featuresDisplayed
361 // .get(sequenceFeatures[sfindex].type);
365 else if (sequenceFeature.type.equals("disulfide bond"))
367 renderFeature(g, seq, seq.findIndex(sequenceFeature.begin) - 1,
368 seq.findIndex(sequenceFeature.begin) - 1,
369 getColour(sequenceFeature)
370 // new Color(((Integer) av.featuresDisplayed
371 // .get(sequenceFeatures[sfindex].type)).intValue())
373 renderFeature(g, seq, seq.findIndex(sequenceFeature.end) - 1,
374 seq.findIndex(sequenceFeature.end) - 1,
375 getColour(sequenceFeature)
376 // new Color(((Integer) av.featuresDisplayed
377 // .get(sequenceFeatures[sfindex].type)).intValue())
381 else if (showFeature(sequenceFeature))
383 if (av_isShowSeqFeatureHeight
384 && !Float.isNaN(sequenceFeature.score))
386 renderScoreFeature(g, seq,
387 seq.findIndex(sequenceFeature.begin) - 1,
388 seq.findIndex(sequenceFeature.end) - 1,
389 getColour(sequenceFeature), start, end, y1,
390 normaliseScore(sequenceFeature));
394 renderFeature(g, seq, seq.findIndex(sequenceFeature.begin) - 1,
395 seq.findIndex(sequenceFeature.end) - 1,
396 getColour(sequenceFeature), start, end, y1);
404 if (transparency != 1.0f && g != null)
406 Graphics2D g2 = (Graphics2D) g;
407 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
413 * Called when alignment in associated view has new/modified features to
414 * discover and display.
418 public void featuresAdded()