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 private Integer currentColour;
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 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 colour
181 * of the rendered sequence
183 public synchronized int findFeatureColour(int initialCol,
184 final SequenceI seq, int column)
186 if (!av.isShowSequenceFeatures())
191 SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
195 lastSequenceFeatures = sequenceFeatures;
196 if (lastSequenceFeatures != null)
198 sfSize = lastSequenceFeatures.length;
203 if (lastSequenceFeatures != sequenceFeatures)
205 lastSequenceFeatures = sequenceFeatures;
206 if (lastSequenceFeatures != null)
208 sfSize = lastSequenceFeatures.length;
213 if (lastSequenceFeatures == 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 currentColour.intValue();
257 private volatile SequenceFeature[] lastSequenceFeatures;
267 public synchronized void drawSequence(Graphics g, final SequenceI seq,
268 int start, int end, int y1)
270 SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
271 if (sequenceFeatures == null || sequenceFeatures.length == 0)
278 fm = g.getFontMetrics();
283 if (lastSeq == null || seq != lastSeq
284 || sequenceFeatures != lastSequenceFeatures)
287 lastSequenceFeatures = sequenceFeatures;
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 = lastSequenceFeatures.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 final SequenceFeature sequenceFeature = lastSequenceFeatures[sfindex];
319 if (!sequenceFeature.type.equals(type))
324 if (featureGroups != null
325 && sequenceFeature.featureGroup != null
326 && sequenceFeature.featureGroup.length() != 0
327 && featureGroups.containsKey(sequenceFeature.featureGroup)
328 && !featureGroups.get(sequenceFeature.featureGroup)
335 && (sequenceFeature.getBegin() > epos || sequenceFeature
341 if (offscreenRender && offscreenImage == null)
343 if (sequenceFeature.begin <= start
344 && sequenceFeature.end >= start)
346 // this is passed out to the overview and other sequence renderers
347 // (e.g. molecule viewer) to get displayed colour for rendered
349 currentColour = new Integer(getColour(sequenceFeature).getRGB());
350 // used to be retreived from av.featuresDisplayed
351 // currentColour = av.featuresDisplayed
352 // .get(sequenceFeatures[sfindex].type);
356 else if (sequenceFeature.type.equals("disulfide bond"))
358 renderFeature(g, seq, seq.findIndex(sequenceFeature.begin) - 1,
359 seq.findIndex(sequenceFeature.begin) - 1,
360 getColour(sequenceFeature)
361 // new Color(((Integer) av.featuresDisplayed
362 // .get(sequenceFeatures[sfindex].type)).intValue())
364 renderFeature(g, seq, seq.findIndex(sequenceFeature.end) - 1,
365 seq.findIndex(sequenceFeature.end) - 1,
366 getColour(sequenceFeature)
367 // new Color(((Integer) av.featuresDisplayed
368 // .get(sequenceFeatures[sfindex].type)).intValue())
372 else if (showFeature(sequenceFeature))
374 if (av_isShowSeqFeatureHeight
375 && !Float.isNaN(sequenceFeature.score))
377 renderScoreFeature(g, seq,
378 seq.findIndex(sequenceFeature.begin) - 1,
379 seq.findIndex(sequenceFeature.end) - 1,
380 getColour(sequenceFeature), start, end, y1,
381 normaliseScore(sequenceFeature));
385 renderFeature(g, seq, seq.findIndex(sequenceFeature.begin) - 1,
386 seq.findIndex(sequenceFeature.end) - 1,
387 getColour(sequenceFeature), start, end, y1);
395 if (transparency != 1.0f && g != null)
397 Graphics2D g2 = (Graphics2D) g;
398 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
404 * Called when alignment in associated view has new/modified features to
405 * discover and display.
409 public void featuresAdded()