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.ColorI;
24 import jalview.datamodel.SequenceFeature;
25 import jalview.datamodel.SequenceI;
26 import jalview.schemes.Colour;
27 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
29 import java.awt.AlphaComposite;
30 import java.awt.Color;
31 import java.awt.FontMetrics;
32 import java.awt.Graphics;
33 import java.awt.Graphics2D;
34 import java.awt.image.BufferedImage;
36 public class FeatureRenderer extends FeatureRendererModel
43 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);
171 BufferedImage offscreenImage;
174 public ColorI findFeatureColour(ColorI initialCol, SequenceI seq, int res)
176 return new Colour(findFeatureColour(initialCol.getRGB(), seq, res));
179 public ColorI findFeatureColour(Color initialCol, SequenceI seq, int res)
181 return findFeatureColour(new Colour(initialCol), seq, res);
185 * This is used by the Molecule Viewer and Overview to get the accurate colour
186 * of the rendered sequence
188 public synchronized int findFeatureColour(int initialCol,
189 final SequenceI seq, int column)
191 if (!av.isShowSequenceFeatures())
196 SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
200 lastSequenceFeatures = sequenceFeatures;
201 if (lastSequenceFeatures != null)
203 sfSize = lastSequenceFeatures.length;
208 if (lastSequenceFeatures != sequenceFeatures)
210 lastSequenceFeatures = sequenceFeatures;
211 if (lastSequenceFeatures != null)
213 sfSize = lastSequenceFeatures.length;
218 if (lastSequenceFeatures == null || sfSize == 0)
223 if (jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))
225 return Color.white.getRGB();
228 // Only bother making an offscreen image if transparency is applied
229 if (transparency != 1.0f && offscreenImage == null)
231 offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
234 currentColour = null;
235 // TODO: non-threadsafe - each rendering thread needs its own instance of
236 // the feature renderer - or this should be synchronized.
237 offscreenRender = true;
239 if (offscreenImage != null)
241 offscreenImage.setRGB(0, 0, initialCol);
242 drawSequence(offscreenImage.getGraphics(), lastSeq, column, column, 0);
244 return offscreenImage.getRGB(0, 0);
248 drawSequence(null, lastSeq, lastSeq.findPosition(column), -1, -1);
250 if (currentColour == null)
256 return currentColour.intValue();
262 private volatile SequenceFeature[] lastSequenceFeatures;
272 private Integer currentColour;
274 public synchronized void drawSequence(Graphics g, final SequenceI seq,
275 int start, int end, int y1)
277 SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
278 if (sequenceFeatures == null || sequenceFeatures.length == 0)
285 fm = g.getFontMetrics();
290 if (lastSeq == null || seq != lastSeq
291 || sequenceFeatures != lastSequenceFeatures)
294 lastSequenceFeatures = sequenceFeatures;
297 if (transparency != 1 && g != null)
299 Graphics2D g2 = (Graphics2D) g;
300 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
304 if (!offscreenRender)
306 spos = lastSeq.findPosition(start);
307 epos = lastSeq.findPosition(end);
310 sfSize = lastSequenceFeatures.length;
312 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
314 type = renderOrder[renderIndex];
316 if (type == null || !showFeatureOfType(type))
321 // loop through all features in sequence to find
322 // current feature to render
323 for (sfindex = 0; sfindex < sfSize; sfindex++)
325 final SequenceFeature sequenceFeature = lastSequenceFeatures[sfindex];
326 if (!sequenceFeature.type.equals(type))
331 if (featureGroups != null
332 && sequenceFeature.featureGroup != null
333 && sequenceFeature.featureGroup.length() != 0
334 && featureGroups.containsKey(sequenceFeature.featureGroup)
335 && !featureGroups.get(sequenceFeature.featureGroup)
342 && (sequenceFeature.getBegin() > epos || sequenceFeature
348 if (offscreenRender && offscreenImage == null)
350 if (sequenceFeature.begin <= start
351 && sequenceFeature.end >= start)
353 // this is passed out to the overview and other sequence renderers
354 // (e.g. molecule viewer) to get displayed colour for rendered
356 currentColour = new Integer(getColour(sequenceFeature).getRGB());
357 // used to be retreived from av.featuresDisplayed
358 // currentColour = av.featuresDisplayed
359 // .get(sequenceFeatures[sfindex].type);
363 else if (sequenceFeature.type.equals("disulfide bond"))
365 renderFeature(g, seq, seq.findIndex(sequenceFeature.begin) - 1,
366 seq.findIndex(sequenceFeature.begin) - 1,
367 getColour(sequenceFeature)
368 // new Color(((Integer) av.featuresDisplayed
369 // .get(sequenceFeatures[sfindex].type)).intValue())
371 renderFeature(g, seq, seq.findIndex(sequenceFeature.end) - 1,
372 seq.findIndex(sequenceFeature.end) - 1,
373 getColour(sequenceFeature)
374 // new Color(((Integer) av.featuresDisplayed
375 // .get(sequenceFeatures[sfindex].type)).intValue())
379 else if (showFeature(sequenceFeature))
381 if (av_isShowSeqFeatureHeight
382 && !Float.isNaN(sequenceFeature.score))
384 renderScoreFeature(g, seq,
385 seq.findIndex(sequenceFeature.begin) - 1,
386 seq.findIndex(sequenceFeature.end) - 1,
387 getColour(sequenceFeature), start, end, y1,
388 normaliseScore(sequenceFeature));
392 renderFeature(g, seq, seq.findIndex(sequenceFeature.begin) - 1,
393 seq.findIndex(sequenceFeature.end) - 1,
394 getColour(sequenceFeature), start, end, y1);
402 if (transparency != 1.0f && g != null)
404 Graphics2D g2 = (Graphics2D) g;
405 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
411 * Called when alignment in associated view has new/modified features to
412 * discover and display.
416 public void featuresAdded()