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;
25 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
27 import java.awt.AlphaComposite;
28 import java.awt.Color;
29 import java.awt.FontMetrics;
30 import java.awt.Graphics;
31 import java.awt.Graphics2D;
32 import java.awt.image.BufferedImage;
34 public class FeatureRenderer extends 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;
172 public Color findFeatureColour(Color initialCol, SequenceI seq, int res)
174 return new Color(findFeatureColour(initialCol.getRGB(), seq, res));
178 * This is used by the Molecule Viewer and Overview to get the accurate
179 * colourof the rendered sequence
181 public synchronized int findFeatureColour(int initialCol,
182 final SequenceI seq, int column)
184 if (!av.isShowSequenceFeatures())
189 SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
193 lastSequenceFeatures = sequenceFeatures;
194 if (lastSequenceFeatures != null)
196 sfSize = lastSequenceFeatures.length;
201 if (lastSequenceFeatures != sequenceFeatures)
203 lastSequenceFeatures = sequenceFeatures;
204 if (lastSequenceFeatures != null)
206 sfSize = lastSequenceFeatures.length;
211 if (lastSequenceFeatures == null || sfSize == 0)
216 if (jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))
218 return Color.white.getRGB();
221 // Only bother making an offscreen image if transparency is applied
222 if (transparency != 1.0f && offscreenImage == null)
224 offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
227 currentColour = null;
228 // TODO: non-threadsafe - each rendering thread needs its own instance of
229 // the feature renderer - or this should be synchronized.
230 offscreenRender = true;
232 if (offscreenImage != null)
234 offscreenImage.setRGB(0, 0, initialCol);
235 drawSequence(offscreenImage.getGraphics(), lastSeq, column, column, 0);
237 return offscreenImage.getRGB(0, 0);
241 drawSequence(null, lastSeq, lastSeq.findPosition(column), -1, -1);
243 if (currentColour == null)
249 return ((Integer) currentColour).intValue();
255 private volatile SequenceFeature[] lastSequenceFeatures;
265 public synchronized void drawSequence(Graphics g, final SequenceI seq,
266 int start, int end, int y1)
268 SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
269 if (sequenceFeatures == null || sequenceFeatures.length == 0)
276 fm = g.getFontMetrics();
281 if (lastSeq == null || seq != lastSeq
282 || sequenceFeatures != lastSequenceFeatures)
285 lastSequenceFeatures = sequenceFeatures;
288 if (transparency != 1 && g != null)
290 Graphics2D g2 = (Graphics2D) g;
291 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
295 if (!offscreenRender)
297 spos = lastSeq.findPosition(start);
298 epos = lastSeq.findPosition(end);
301 sfSize = lastSequenceFeatures.length;
303 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
305 type = renderOrder[renderIndex];
307 if (type == null || !showFeatureOfType(type))
312 // loop through all features in sequence to find
313 // current feature to render
314 for (sfindex = 0; sfindex < sfSize; sfindex++)
316 final SequenceFeature sequenceFeature = lastSequenceFeatures[sfindex];
317 if (!sequenceFeature.type.equals(type))
322 if (featureGroups != null
323 && sequenceFeature.featureGroup != null
324 && sequenceFeature.featureGroup.length() != 0
325 && featureGroups.containsKey(sequenceFeature.featureGroup)
326 && !featureGroups.get(sequenceFeature.featureGroup)
333 && (sequenceFeature.getBegin() > epos || sequenceFeature
339 if (offscreenRender && offscreenImage == null)
341 if (sequenceFeature.begin <= start
342 && sequenceFeature.end >= start)
344 // this is passed out to the overview and other sequence renderers
345 // (e.g. molecule viewer) to get displayed colour for rendered
347 currentColour = new Integer(getColour(sequenceFeature).getRGB());
348 // used to be retreived from av.featuresDisplayed
349 // currentColour = av.featuresDisplayed
350 // .get(sequenceFeatures[sfindex].type);
354 else if (sequenceFeature.type.equals("disulfide bond"))
356 renderFeature(g, seq, seq.findIndex(sequenceFeature.begin) - 1,
357 seq.findIndex(sequenceFeature.begin) - 1,
358 getColour(sequenceFeature)
359 // new Color(((Integer) av.featuresDisplayed
360 // .get(sequenceFeatures[sfindex].type)).intValue())
362 renderFeature(g, seq, seq.findIndex(sequenceFeature.end) - 1,
363 seq.findIndex(sequenceFeature.end) - 1,
364 getColour(sequenceFeature)
365 // new Color(((Integer) av.featuresDisplayed
366 // .get(sequenceFeatures[sfindex].type)).intValue())
370 else if (showFeature(sequenceFeature))
372 if (av_isShowSeqFeatureHeight
373 && !Float.isNaN(sequenceFeature.score))
375 renderScoreFeature(g, seq,
376 seq.findIndex(sequenceFeature.begin) - 1,
377 seq.findIndex(sequenceFeature.end) - 1,
378 getColour(sequenceFeature), start, end, y1,
379 normaliseScore(sequenceFeature));
383 renderFeature(g, seq, seq.findIndex(sequenceFeature.begin) - 1,
384 seq.findIndex(sequenceFeature.end) - 1,
385 getColour(sequenceFeature), start, end, y1);
393 if (transparency != 1.0f && g != null && transparencyAvailable)
395 Graphics2D g2 = (Graphics2D) g;
396 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
401 boolean transparencyAvailable = true;
403 protected void setTransparencyAvailable(boolean isTransparencyAvailable)
405 transparencyAvailable = isTransparencyAvailable;
409 public boolean isTransparencyAvailable()
411 return transparencyAvailable;
415 * Called when alignment in associated view has new/modified features to
416 * discover and display.
420 public void featuresAdded()