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.api.FeatureColourI;
25 import jalview.datamodel.SequenceFeature;
26 import jalview.datamodel.SequenceI;
27 import jalview.util.Comparison;
28 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
30 import java.awt.AlphaComposite;
31 import java.awt.Color;
32 import java.awt.FontMetrics;
33 import java.awt.Graphics;
34 import java.awt.Graphics2D;
35 import java.util.List;
37 public class FeatureRenderer extends FeatureRendererModel
39 private static final AlphaComposite NO_TRANSPARENCY = AlphaComposite
40 .getInstance(AlphaComposite.SRC_OVER, 1.0f);
43 * Constructor given a viewport
47 public FeatureRenderer(AlignViewportI viewport)
53 * Renders the sequence using the given feature colour between the given start
54 * and end columns. Returns true if at least one column is drawn, else false
55 * (the feature range does not overlap the start and end positions).
61 * @param featureColour
68 boolean renderFeature(Graphics g, SequenceI seq, int featureStart,
69 int featureEnd, Color featureColour, int start, int end, int y1,
72 int charHeight = av.getCharHeight();
73 int charWidth = av.getCharWidth();
74 boolean validCharWidth = av.isValidCharWidth();
76 if (featureStart > end || featureEnd < start)
81 if (featureStart < start)
85 if (featureEnd >= end)
89 int pady = (y1 + charHeight) - charHeight / 5;
91 FontMetrics fm = g.getFontMetrics();
92 for (int i = featureStart; i <= featureEnd; i++)
94 char s = seq.getCharAt(i);
96 if (Comparison.isGap(s))
101 g.setColor(featureColour);
103 g.fillRect((i - start) * charWidth, y1, charWidth,
106 if (colourOnly || !validCharWidth)
111 g.setColor(Color.white);
112 int charOffset = (charWidth - fm.charWidth(s)) / 2;
113 g.drawString(String.valueOf(s), charOffset
114 + (charWidth * (i - start)), pady);
120 * Renders the sequence using the given SCORE feature colour between the given
121 * start and end columns. Returns true if at least one column is drawn, else
122 * false (the feature range does not overlap the start and end positions).
128 * @param featureColour
136 boolean renderScoreFeature(Graphics g, SequenceI seq, int fstart,
137 int fend, Color featureColour, int start, int end, int y1,
138 byte[] bs, boolean colourOnly)
140 if (fstart > end || fend < start)
146 { // fix for if the feature we have starts before the sequence start,
147 fstart = start; // but the feature end is still valid!!
154 int charHeight = av.getCharHeight();
155 int pady = (y1 + charHeight) - charHeight / 5;
156 int ystrt = 0, yend = charHeight;
159 // signed - zero is always middle of residue line.
162 yend = charHeight * (128 - bs[1]) / 512;
163 ystrt = charHeight - yend / 2;
167 ystrt = charHeight / 2;
168 yend = charHeight * (bs[1] - 128) / 512;
173 yend = charHeight * bs[1] / 255;
174 ystrt = charHeight - yend;
178 FontMetrics fm = g.getFontMetrics();
179 int charWidth = av.getCharWidth();
181 for (int i = fstart; i <= fend; i++)
183 char s = seq.getCharAt(i);
185 if (Comparison.isGap(s))
190 g.setColor(featureColour);
191 int x = (i - start) * charWidth;
192 g.drawRect(x, y1, charWidth, charHeight);
193 g.fillRect(x, y1 + ystrt, charWidth, yend);
195 if (colourOnly || !av.isValidCharWidth())
200 g.setColor(Color.black);
201 int charOffset = (charWidth - fm.charWidth(s)) / 2;
202 g.drawString(String.valueOf(s), charOffset
203 + (charWidth * (i - start)), pady);
212 public Color findFeatureColour(SequenceI seq, int column, Graphics g)
214 if (!av.isShowSequenceFeatures())
219 if (Comparison.isGap(seq.getCharAt(column)))
224 Color renderedColour = null;
225 if (transparency == 1.0f)
228 * simple case - just find the topmost rendered visible feature colour
230 renderedColour = findFeatureColour(seq, column);
235 * transparency case - draw all visible features in render order to
236 * build up a composite colour on the graphics context
238 renderedColour = drawSequence(g, seq, column, column, 0, true);
240 return renderedColour;
244 * Draws the sequence features on the graphics context, or just determines the
245 * colour that would be drawn (if flag colourOnly is true). Returns the last
246 * colour drawn (which may not be the effective colour if transparency
247 * applies), or null if no feature is drawn in the range given.
250 * the graphics context to draw on (may be null if colourOnly==true)
257 * vertical offset at which to draw on the graphics
259 * if true, only do enough to determine the colour for the position,
260 * do not draw the character
263 public synchronized Color drawSequence(final Graphics g,
264 final SequenceI seq, int start, int end, int y1,
267 if (!seq.getFeatures().hasFeatures())
274 if (transparency != 1f && g != null)
276 Graphics2D g2 = (Graphics2D) g;
277 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
281 Color drawnColour = null;
284 * iterate over features in ordering of their rendering (last is on top)
286 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
288 String type = renderOrder[renderIndex];
289 if (!showFeatureOfType(type))
294 FeatureColourI fc = getFeatureStyle(type);
295 List<SequenceFeature> overlaps = seq.findFeatures(start + 1, end + 1,
298 filterFeaturesForDisplay(overlaps, fc);
300 for (SequenceFeature sf : overlaps)
303 * a feature type may be flagged as shown but the group
304 * an instance of it belongs to may be hidden
306 if (featureGroupNotShown(sf))
311 Color featureColour = fc.getColor(sf);
312 boolean isContactFeature = sf.isContactFeature();
314 int featureStartCol = seq.findIndex(sf.begin);
315 int featureEndCol = sf.begin == sf.end ? featureStartCol : seq
317 if (isContactFeature)
319 boolean drawn = renderFeature(g, seq, featureStartCol - 1,
320 featureStartCol - 1, featureColour, start, end, y1,
322 drawn |= renderFeature(g, seq, featureEndCol - 1,
323 featureEndCol - 1, featureColour, start, end, y1,
327 drawnColour = featureColour;
330 else if (showFeature(sf))
333 * showing feature score by height of colour
334 * is not implemented as a selectable option
336 if (av.isShowSequenceFeaturesHeight()
337 && !Float.isNaN(sequenceFeature.score))
339 boolean drawn = renderScoreFeature(g, seq,
340 seq.findIndex(sequenceFeature.begin) - 1,
341 seq.findIndex(sequenceFeature.end) - 1, featureColour,
342 start, end, y1, normaliseScore(sequenceFeature),
346 drawnColour = featureColour;
352 boolean drawn = renderFeature(g, seq,
354 featureEndCol - 1, featureColour,
355 start, end, y1, colourOnly);
358 drawnColour = featureColour;
365 if (transparency != 1.0f && g != null)
370 Graphics2D g2 = (Graphics2D) g;
371 g2.setComposite(NO_TRANSPARENCY);
378 * Called when alignment in associated view has new/modified features to
379 * discover and display.
383 public void featuresAdded()
389 * Returns the sequence feature colour rendered at the given column position,
390 * or null if none found. The feature of highest render order (i.e. on top) is
391 * found, subject to both feature type and feature group being visible, and
392 * its colour returned.
394 * Note this method does not check for a gap in the column so would return the
395 * colour for features enclosing a gapped column. Check for gap before calling
396 * if different behaviour is wanted.
403 Color findFeatureColour(SequenceI seq, int column)
406 * check for new feature added while processing
411 * inspect features in reverse renderOrder (the last in the array is
412 * displayed on top) until we find one that is rendered at the position
414 for (int renderIndex = renderOrder.length - 1; renderIndex >= 0; renderIndex--)
416 String type = renderOrder[renderIndex];
417 if (!showFeatureOfType(type))
422 List<SequenceFeature> overlaps = seq.findFeatures(column, column,
424 for (SequenceFeature sequenceFeature : overlaps)
426 if (!featureGroupNotShown(sequenceFeature))
428 return getColour(sequenceFeature);
434 * no displayed feature found at position