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.datamodel.SequenceFeature;
24 import jalview.datamodel.SequenceI;
26 import java.awt.AlphaComposite;
27 import java.awt.Color;
28 import java.awt.FontMetrics;
29 import java.awt.Graphics;
30 import java.awt.Graphics2D;
31 import java.awt.image.BufferedImage;
33 public class FeatureRenderer extends
34 jalview.viewmodel.seqfeatures.FeatureRendererModel
41 boolean offscreenRender = false;
43 protected SequenceI lastSeq;
49 int av_charHeight, av_charWidth;
51 boolean av_validCharWidth, av_isShowSeqFeatureHeight;
53 protected void updateAvConfig()
55 av_charHeight = av.getCharHeight();
56 av_charWidth = av.getCharWidth();
57 av_validCharWidth = av.isValidCharWidth();
58 av_isShowSeqFeatureHeight = av.isShowSequenceFeaturesHeight();
61 void renderFeature(Graphics g, SequenceI seq, int fstart, int fend,
62 Color featureColour, int start, int end, int y1)
65 if (((fstart <= end) && (fend >= start)))
68 { // fix for if the feature we have starts before the sequence start,
69 fstart = start; // but the feature end is still valid!!
76 int pady = (y1 + av_charHeight) - av_charHeight / 5;
77 for (i = fstart; i <= fend; i++)
81 if (jalview.util.Comparison.isGap(s))
86 g.setColor(featureColour);
88 g.fillRect((i - start) * av_charWidth, y1, av_charWidth,
91 if (offscreenRender || !av_validCharWidth)
96 g.setColor(Color.white);
97 charOffset = (av_charWidth - fm.charWidth(s)) / 2;
98 g.drawString(String.valueOf(s), charOffset
99 + (av_charWidth * (i - start)), pady);
105 void renderScoreFeature(Graphics g, SequenceI seq, int fstart, int fend,
106 Color featureColour, int start, int end, int y1, byte[] bs)
109 if (((fstart <= end) && (fend >= start)))
112 { // fix for if the feature we have starts before the sequence start,
113 fstart = start; // but the feature end is still valid!!
120 int pady = (y1 + av_charHeight) - av_charHeight / 5;
121 int ystrt = 0, yend = av_charHeight;
124 // signed - zero is always middle of residue line.
127 yend = av_charHeight * (128 - bs[1]) / 512;
128 ystrt = av_charHeight - yend / 2;
132 ystrt = av_charHeight / 2;
133 yend = av_charHeight * (bs[1] - 128) / 512;
138 yend = av_charHeight * bs[1] / 255;
139 ystrt = av_charHeight - yend;
142 for (i = fstart; i <= fend; i++)
144 s = seq.getCharAt(i);
146 if (jalview.util.Comparison.isGap(s))
151 g.setColor(featureColour);
152 int x = (i - start) * av_charWidth;
153 g.drawRect(x, y1, av_charWidth, av_charHeight);
154 g.fillRect(x, y1 + ystrt, av_charWidth, yend);
156 if (offscreenRender || !av_validCharWidth)
161 g.setColor(Color.black);
162 charOffset = (av_charWidth - fm.charWidth(s)) / 2;
163 g.drawString(String.valueOf(s), charOffset
164 + (av_charWidth * (i - start)), pady);
169 BufferedImage offscreenImage;
171 public Color findFeatureColour(Color initialCol, SequenceI seq, int res)
173 return new Color(findFeatureColour(initialCol.getRGB(), seq, res));
177 * This is used by the Molecule Viewer and Overview to get the accurate
178 * colourof the rendered sequence
180 public synchronized int findFeatureColour(int initialCol,
181 final SequenceI seq, int column)
183 if (!av.isShowSequenceFeatures())
188 SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
192 lastSequenceFeatures = sequenceFeatures;
193 if (lastSequenceFeatures != null)
195 sfSize = lastSequenceFeatures.length;
200 if (lastSequenceFeatures != sequenceFeatures)
202 lastSequenceFeatures = sequenceFeatures;
203 if (lastSequenceFeatures != null)
205 sfSize = lastSequenceFeatures.length;
210 if (lastSequenceFeatures == null || sfSize == 0)
215 if (jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))
217 return Color.white.getRGB();
220 // Only bother making an offscreen image if transparency is applied
221 if (transparency != 1.0f && offscreenImage == null)
223 offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
226 currentColour = null;
227 // TODO: non-threadsafe - each rendering thread needs its own instance of
228 // the feature renderer - or this should be synchronized.
229 offscreenRender = true;
231 if (offscreenImage != null)
233 offscreenImage.setRGB(0, 0, initialCol);
234 drawSequence(offscreenImage.getGraphics(), lastSeq, column, column, 0);
236 return offscreenImage.getRGB(0, 0);
240 drawSequence(null, lastSeq, lastSeq.findPosition(column), -1, -1);
242 if (currentColour == null)
248 return ((Integer) currentColour).intValue();
254 private volatile SequenceFeature[] lastSequenceFeatures;
264 public synchronized void drawSequence(Graphics g, final SequenceI seq,
265 int start, int end, int y1)
267 SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
268 if (sequenceFeatures == null || sequenceFeatures.length == 0)
275 fm = g.getFontMetrics();
280 if (lastSeq == null || seq != lastSeq
281 || sequenceFeatures != lastSequenceFeatures)
284 lastSequenceFeatures = sequenceFeatures;
287 if (transparency != 1 && g != null)
289 Graphics2D g2 = (Graphics2D) g;
290 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
294 if (!offscreenRender)
296 spos = lastSeq.findPosition(start);
297 epos = lastSeq.findPosition(end);
300 sfSize = lastSequenceFeatures.length;
302 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
304 type = renderOrder[renderIndex];
306 if (type == null || !showFeatureOfType(type))
311 // loop through all features in sequence to find
312 // current feature to render
313 for (sfindex = 0; sfindex < sfSize; sfindex++)
315 final SequenceFeature sequenceFeature = lastSequenceFeatures[sfindex];
316 if (!sequenceFeature.type.equals(type))
321 if (featureGroups != null
322 && sequenceFeature.featureGroup != null
323 && sequenceFeature.featureGroup.length() != 0
324 && featureGroups.containsKey(sequenceFeature.featureGroup)
325 && !featureGroups.get(sequenceFeature.featureGroup)
332 && (sequenceFeature.getBegin() > epos || sequenceFeature
338 if (offscreenRender && offscreenImage == null)
340 if (sequenceFeature.begin <= start
341 && sequenceFeature.end >= start)
343 // this is passed out to the overview and other sequence renderers
344 // (e.g. molecule viewer) to get displayed colour for rendered
346 currentColour = new Integer(getColour(sequenceFeature).getRGB());
347 // used to be retreived from av.featuresDisplayed
348 // currentColour = av.featuresDisplayed
349 // .get(sequenceFeatures[sfindex].type);
353 else if (sequenceFeature.type.equals("disulfide bond"))
355 renderFeature(g, seq, seq.findIndex(sequenceFeature.begin) - 1,
356 seq.findIndex(sequenceFeature.begin) - 1,
357 getColour(sequenceFeature)
358 // new Color(((Integer) av.featuresDisplayed
359 // .get(sequenceFeatures[sfindex].type)).intValue())
361 renderFeature(g, seq, seq.findIndex(sequenceFeature.end) - 1,
362 seq.findIndex(sequenceFeature.end) - 1,
363 getColour(sequenceFeature)
364 // new Color(((Integer) av.featuresDisplayed
365 // .get(sequenceFeatures[sfindex].type)).intValue())
369 else if (showFeature(sequenceFeature))
371 if (av_isShowSeqFeatureHeight
372 && !Float.isNaN(sequenceFeature.score))
374 renderScoreFeature(g, seq,
375 seq.findIndex(sequenceFeature.begin) - 1,
376 seq.findIndex(sequenceFeature.end) - 1,
377 getColour(sequenceFeature), start, end, y1,
378 normaliseScore(sequenceFeature));
382 renderFeature(g, seq, seq.findIndex(sequenceFeature.begin) - 1,
383 seq.findIndex(sequenceFeature.end) - 1,
384 getColour(sequenceFeature), start, end, y1);
392 if (transparency != 1.0f && g != null && transparencyAvailable)
394 Graphics2D g2 = (Graphics2D) g;
395 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
400 boolean transparencyAvailable = true;
402 protected void setTransparencyAvailable(boolean isTransparencyAvailable)
404 transparencyAvailable = isTransparencyAvailable;
408 public boolean isTransparencyAvailable()
410 return transparencyAvailable;
414 * Called when alignment in associated view has new/modified features to
415 * discover and display.
418 public void featuresAdded()