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.Range;
26 import jalview.datamodel.SequenceFeature;
27 import jalview.datamodel.SequenceI;
28 import jalview.util.Comparison;
29 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
31 import java.awt.AlphaComposite;
32 import java.awt.Color;
33 import java.awt.FontMetrics;
34 import java.awt.Graphics;
35 import java.awt.Graphics2D;
36 import java.util.List;
38 public class FeatureRenderer extends FeatureRendererModel
40 private static final AlphaComposite NO_TRANSPARENCY = AlphaComposite
41 .getInstance(AlphaComposite.SRC_OVER, 1.0f);
44 * Constructor given a viewport
48 public FeatureRenderer(AlignViewportI viewport)
54 * Renders the sequence using the given feature colour between the given start
55 * and end columns. Returns true if at least one column is drawn, else false
56 * (the feature range does not overlap the start and end positions).
62 * @param featureColour
69 boolean renderFeature(Graphics g, SequenceI seq, int featureStart,
70 int featureEnd, Color featureColour, int start, int end, int y1,
73 int charHeight = av.getCharHeight();
74 int charWidth = av.getCharWidth();
75 boolean validCharWidth = av.isValidCharWidth();
77 if (featureStart > end || featureEnd < start)
82 if (featureStart < start)
86 if (featureEnd >= end)
90 int pady = (y1 + charHeight) - charHeight / 5;
92 FontMetrics fm = g.getFontMetrics();
93 for (int i = featureStart; i <= featureEnd; i++)
95 char s = seq.getCharAt(i);
97 if (Comparison.isGap(s))
102 g.setColor(featureColour);
104 g.fillRect((i - start) * charWidth, y1, charWidth, charHeight);
106 if (colourOnly || !validCharWidth)
111 g.setColor(Color.white);
112 int charOffset = (charWidth - fm.charWidth(s)) / 2;
113 g.drawString(String.valueOf(s),
114 charOffset + (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),
203 charOffset + (charWidth * (i - start)), pady);
212 public Color findFeatureColour(SequenceI seq, int column, Graphics g)
214 if (!av.isShowSequenceFeatures())
219 // column is 'base 1' but getCharAt is an array index (ie from 0)
220 if (Comparison.isGap(seq.getCharAt(column - 1)))
223 * returning null allows the colour scheme to provide gap colour
224 * - normally white, but can be customised
229 Color renderedColour = null;
230 if (transparency == 1.0f)
233 * simple case - just find the topmost rendered visible feature colour
235 renderedColour = findFeatureColour(seq, column);
240 * transparency case - draw all visible features in render order to
241 * build up a composite colour on the graphics context
243 renderedColour = drawSequence(g, seq, column, column, 0, true);
245 return renderedColour;
249 * Draws the sequence features on the graphics context, or just determines the
250 * colour that would be drawn (if flag colourOnly is true). Returns the last
251 * colour drawn (which may not be the effective colour if transparency
252 * applies), or null if no feature is drawn in the range given.
255 * the graphics context to draw on (may be null if colourOnly==true)
262 * vertical offset at which to draw on the graphics
264 * if true, only do enough to determine the colour for the position,
265 * do not draw the character
268 public synchronized Color drawSequence(final Graphics g,
269 final SequenceI seq, int start, int end, int y1,
273 * if columns are all gapped, or sequence has no features, nothing to do
275 Range visiblePositions = seq.findPositions(start+1, end+1);
276 if (visiblePositions == null || !seq.getFeatures().hasFeatures())
283 if (transparency != 1f && g != null)
285 Graphics2D g2 = (Graphics2D) g;
286 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
290 Color drawnColour = null;
293 * iterate over features in ordering of their rendering (last is on top)
295 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
297 String type = renderOrder[renderIndex];
298 if (!showFeatureOfType(type))
303 FeatureColourI fc = getFeatureStyle(type);
304 List<SequenceFeature> overlaps = seq.getFeatures().findFeatures(
305 visiblePositions.getBegin(), visiblePositions.getEnd(), type);
307 if (fc.isSimpleColour())
309 filterFeaturesForDisplay(overlaps);
312 for (SequenceFeature sf : overlaps)
314 Color featureColour = getColor(sf, fc);
315 if (featureColour == null)
318 * feature excluded by visibility settings, filters, or colour threshold
324 * if feature starts/ends outside the visible range,
325 * restrict to visible positions (or if a contact feature,
326 * to a single position)
328 int visibleStart = sf.getBegin();
329 if (visibleStart < visiblePositions.getBegin())
331 visibleStart = sf.isContactFeature() ? sf.getEnd()
332 : visiblePositions.getBegin();
334 int visibleEnd = sf.getEnd();
335 if (visibleEnd > visiblePositions.getEnd())
337 visibleEnd = sf.isContactFeature() ? sf.getBegin()
338 : visiblePositions.getEnd();
341 int featureStartCol = seq.findIndex(visibleStart);
342 int featureEndCol = sf.begin == sf.end ? featureStartCol : seq
343 .findIndex(visibleEnd);
345 // Color featureColour = getColour(sequenceFeature);
347 boolean isContactFeature = sf.isContactFeature();
349 if (isContactFeature)
351 boolean drawn = renderFeature(g, seq, featureStartCol - 1,
352 featureStartCol - 1, featureColour, start, end, y1,
354 drawn |= renderFeature(g, seq, featureEndCol - 1,
355 featureEndCol - 1, featureColour, start, end, y1,
359 drawnColour = featureColour;
365 * showing feature score by height of colour
366 * is not implemented as a selectable option
368 if (av.isShowSequenceFeaturesHeight()
369 && !Float.isNaN(sequenceFeature.score))
371 boolean drawn = renderScoreFeature(g, seq,
372 seq.findIndex(sequenceFeature.begin) - 1,
373 seq.findIndex(sequenceFeature.end) - 1, featureColour,
374 start, end, y1, normaliseScore(sequenceFeature),
378 drawnColour = featureColour;
384 boolean drawn = renderFeature(g, seq,
386 featureEndCol - 1, featureColour,
387 start, end, y1, colourOnly);
390 drawnColour = featureColour;
397 if (transparency != 1.0f && g != null)
402 Graphics2D g2 = (Graphics2D) g;
403 g2.setComposite(NO_TRANSPARENCY);
410 * Called when alignment in associated view has new/modified features to
411 * discover and display.
415 public void featuresAdded()
421 * Returns the sequence feature colour rendered at the given column position,
422 * or null if none found. The feature of highest render order (i.e. on top) is
423 * found, subject to both feature type and feature group being visible, and
424 * its colour returned. This method is suitable when no feature transparency
425 * applied (only the topmost visible feature colour is rendered).
427 * Note this method does not check for a gap in the column so would return the
428 * colour for features enclosing a gapped column. Check for gap before calling
429 * if different behaviour is wanted.
436 Color findFeatureColour(SequenceI seq, int column)
439 * check for new feature added while processing
444 * inspect features in reverse renderOrder (the last in the array is
445 * displayed on top) until we find one that is rendered at the position
447 for (int renderIndex = renderOrder.length
448 - 1; renderIndex >= 0; renderIndex--)
450 String type = renderOrder[renderIndex];
451 if (!showFeatureOfType(type))
456 List<SequenceFeature> overlaps = seq.findFeatures(column, column,
458 for (SequenceFeature sequenceFeature : overlaps)
460 if (!featureGroupNotShown(sequenceFeature))
462 Color col = getColour(sequenceFeature);
472 * no displayed feature found at position