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.util.Comparison;
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;
35 public class FeatureRenderer extends FeatureRendererModel
37 private static final AlphaComposite NO_TRANSPARENCY = AlphaComposite
38 .getInstance(AlphaComposite.SRC_OVER, 1.0f);
40 protected SequenceI lastSeq;
42 private volatile SequenceFeature[] lastSequenceFeatures;
47 * Constructor given a viewport
51 public FeatureRenderer(AlignViewportI viewport)
57 * Renders the sequence using the given feature colour between the given start
58 * and end columns. Returns true if at least one column is drawn, else false
59 * (the feature range does not overlap the start and end positions).
65 * @param featureColour
72 boolean renderFeature(Graphics g, SequenceI seq, int featureStart,
73 int featureEnd, Color featureColour, int start, int end, int y1,
76 int charHeight = av.getCharHeight();
77 int charWidth = av.getCharWidth();
78 boolean validCharWidth = av.isValidCharWidth();
80 if (featureStart > end || featureEnd < start)
85 if (featureStart < start)
89 if (featureEnd >= end)
93 int pady = (y1 + charHeight) - charHeight / 5;
95 FontMetrics fm = g.getFontMetrics();
96 for (int i = featureStart; i <= featureEnd; i++)
98 char s = seq.getCharAt(i);
100 if (Comparison.isGap(s))
105 g.setColor(featureColour);
107 g.fillRect((i - start) * charWidth, y1, charWidth,
110 if (colourOnly || !validCharWidth)
115 g.setColor(Color.white);
116 int charOffset = (charWidth - fm.charWidth(s)) / 2;
117 g.drawString(String.valueOf(s), charOffset
118 + (charWidth * (i - start)), pady);
124 * Renders the sequence using the given SCORE feature colour between the given
125 * start and end columns. Returns true if at least one column is drawn, else
126 * false (the feature range does not overlap the start and end positions).
132 * @param featureColour
140 boolean renderScoreFeature(Graphics g, SequenceI seq, int fstart,
141 int fend, Color featureColour, int start, int end, int y1,
142 byte[] bs, boolean colourOnly)
144 if (fstart > end || fend < start)
150 { // fix for if the feature we have starts before the sequence start,
151 fstart = start; // but the feature end is still valid!!
158 int charHeight = av.getCharHeight();
159 int pady = (y1 + charHeight) - charHeight / 5;
160 int ystrt = 0, yend = charHeight;
163 // signed - zero is always middle of residue line.
166 yend = charHeight * (128 - bs[1]) / 512;
167 ystrt = charHeight - yend / 2;
171 ystrt = charHeight / 2;
172 yend = charHeight * (bs[1] - 128) / 512;
177 yend = charHeight * bs[1] / 255;
178 ystrt = charHeight - yend;
182 FontMetrics fm = g.getFontMetrics();
183 int charWidth = av.getCharWidth();
185 for (int i = fstart; i <= fend; i++)
187 char s = seq.getCharAt(i);
189 if (Comparison.isGap(s))
194 g.setColor(featureColour);
195 int x = (i - start) * charWidth;
196 g.drawRect(x, y1, charWidth, charHeight);
197 g.fillRect(x, y1 + ystrt, charWidth, yend);
199 if (colourOnly || !av.isValidCharWidth())
204 g.setColor(Color.black);
205 int charOffset = (charWidth - fm.charWidth(s)) / 2;
206 g.drawString(String.valueOf(s), charOffset
207 + (charWidth * (i - start)), pady);
213 * This is used by Structure Viewers and the Overview Window to get the
214 * feature colour of the rendered sequence
221 public Color findFeatureColour(SequenceI seq, int column, Graphics g)
223 if (!av.isShowSequenceFeatures())
228 SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
232 lastSequenceFeatures = sequenceFeatures;
233 if (lastSequenceFeatures != null)
235 sfSize = lastSequenceFeatures.length;
240 if (lastSequenceFeatures != sequenceFeatures)
242 lastSequenceFeatures = sequenceFeatures;
243 if (lastSequenceFeatures != null)
245 sfSize = lastSequenceFeatures.length;
250 if (lastSequenceFeatures == null || sfSize == 0)
255 if (Comparison.isGap(lastSeq.getCharAt(column)))
260 Color renderedColour = null;
261 if (transparency == 1.0f)
263 renderedColour = findFeatureColour(seq, seq.findPosition(column));
267 renderedColour = drawSequence(g, lastSeq, column, column, 0, true);
269 return renderedColour;
273 * Draws the sequence features on the graphics context, or just determines the
274 * colour that would be drawn (if flag offscreenrender is true).
277 * the graphics context to draw on (may be null if colourOnly==true)
280 * start column (or sequence position in offscreenrender mode)
282 * end column (not used in offscreenrender mode)
284 * vertical offset at which to draw on the graphics
286 * if true, only do enough to determine the colour for the position,
287 * do not draw the character
290 public synchronized Color drawSequence(final Graphics g,
291 final SequenceI seq, int start, int end, int y1,
294 SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
295 if (sequenceFeatures == null || sequenceFeatures.length == 0)
302 if (lastSeq == null || seq != lastSeq
303 || sequenceFeatures != lastSequenceFeatures)
306 lastSequenceFeatures = sequenceFeatures;
309 if (transparency != 1f && g != null)
311 Graphics2D g2 = (Graphics2D) g;
312 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
316 int startPos = lastSeq.findPosition(start);
317 int endPos = lastSeq.findPosition(end);
319 sfSize = lastSequenceFeatures.length;
320 Color drawnColour = null;
323 * iterate over features in ordering of their rendering;
324 * if drawing a range of columns, use render order to ensure last is on top
325 * if drawing a single column (as in findFeatureColour), with no
326 * transparency, work backwards to find the topmost rendered feature colour
328 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
330 String type = renderOrder[renderIndex];
331 if (!showFeatureOfType(type))
336 // loop through all features in sequence to find
337 // current feature to render
338 for (int sfindex = 0; sfindex < sfSize; sfindex++)
340 final SequenceFeature sequenceFeature = lastSequenceFeatures[sfindex];
341 if (!sequenceFeature.type.equals(type))
346 if (featureGroupNotShown(sequenceFeature))
352 * check feature overlaps the visible part of the alignment,
353 * unless doing offscreenRender (to the Overview window or a
354 * structure viewer) which is not limited
356 if (sequenceFeature.getBegin() > endPos
357 || sequenceFeature.getEnd() < startPos)
362 Color featureColour = getColour(sequenceFeature);
363 boolean isContactFeature = sequenceFeature.isContactFeature();
365 if (isContactFeature)
367 boolean drawn = renderFeature(g, seq,
368 seq.findIndex(sequenceFeature.begin) - 1,
369 seq.findIndex(sequenceFeature.begin) - 1, featureColour,
370 start, end, y1, colourOnly);
371 drawn |= renderFeature(g, seq,
372 seq.findIndex(sequenceFeature.end) - 1,
373 seq.findIndex(sequenceFeature.end) - 1, featureColour,
374 start, end, y1, colourOnly);
377 drawnColour = featureColour;
380 else if (showFeature(sequenceFeature))
382 if (av.isShowSequenceFeaturesHeight()
383 && !Float.isNaN(sequenceFeature.score))
385 boolean drawn = renderScoreFeature(g, seq,
386 seq.findIndex(sequenceFeature.begin) - 1,
387 seq.findIndex(sequenceFeature.end) - 1, featureColour,
388 start, end, y1, normaliseScore(sequenceFeature),
392 drawnColour = featureColour;
397 boolean drawn = renderFeature(g, seq,
398 seq.findIndex(sequenceFeature.begin) - 1,
399 seq.findIndex(sequenceFeature.end) - 1, featureColour,
400 start, end, y1, colourOnly);
403 drawnColour = featureColour;
410 if (transparency != 1.0f && g != null)
415 Graphics2D g2 = (Graphics2D) g;
416 g2.setComposite(NO_TRANSPARENCY);
423 * Answers true if the feature belongs to a feature group which is not
424 * currently displayed, else false
426 * @param sequenceFeature
429 protected boolean featureGroupNotShown(
430 final SequenceFeature sequenceFeature)
432 return featureGroups != null
433 && sequenceFeature.featureGroup != null
434 && sequenceFeature.featureGroup.length() != 0
435 && featureGroups.containsKey(sequenceFeature.featureGroup)
436 && !featureGroups.get(sequenceFeature.featureGroup)
441 * Called when alignment in associated view has new/modified features to
442 * discover and display.
446 public void featuresAdded()
453 * Returns the sequence feature colour rendered at the given sequence
454 * position, or null if none found. The feature of highest render order (i.e.
455 * on top) is found, subject to both feature type and feature group being
456 * visible, and its colour returned.
462 Color findFeatureColour(SequenceI seq, int pos)
464 SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
465 if (sequenceFeatures == null || sequenceFeatures.length == 0)
473 * inspect features in reverse renderOrder (the last in the array is
474 * displayed on top) until we find one that is rendered at the position
476 for (int renderIndex = renderOrder.length - 1; renderIndex >= 0; renderIndex--)
478 String type = renderOrder[renderIndex];
479 if (!showFeatureOfType(type))
484 for (int sfindex = 0; sfindex < sequenceFeatures.length; sfindex++)
486 SequenceFeature sequenceFeature = sequenceFeatures[sfindex];
487 if (!sequenceFeature.type.equals(type))
492 if (featureGroupNotShown(sequenceFeature))
497 boolean featureIsAtPosition = sequenceFeature.begin <= pos
498 && sequenceFeature.end >= pos;
499 if (sequenceFeature.isContactFeature())
501 featureIsAtPosition = sequenceFeature.begin == pos
502 || sequenceFeature.end == pos;
504 if (featureIsAtPosition)
506 return getColour(sequenceFeature);
512 * no displayed feature found at position