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.ContiguousI;
26 import jalview.datamodel.MappedFeatures;
27 import jalview.datamodel.SequenceFeature;
28 import jalview.datamodel.SequenceI;
29 import jalview.gui.AlignFrame;
30 import jalview.gui.Desktop;
31 import jalview.util.Comparison;
32 import jalview.util.ReverseListIterator;
33 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
35 import java.awt.AlphaComposite;
36 import java.awt.Color;
37 import java.awt.FontMetrics;
38 import java.awt.Graphics;
39 import java.awt.Graphics2D;
40 import java.util.List;
42 public class FeatureRenderer extends FeatureRendererModel
44 private static final AlphaComposite NO_TRANSPARENCY = AlphaComposite
45 .getInstance(AlphaComposite.SRC_OVER, 1.0f);
48 * Constructor given a viewport
52 public FeatureRenderer(AlignViewportI viewport)
58 * Renders the sequence using the given feature colour between the given start
59 * and end columns. Returns true if at least one column is drawn, else false
60 * (the feature range does not overlap the start and end positions).
66 * @param featureColour
73 boolean renderFeature(Graphics g, SequenceI seq, int featureStart,
74 int featureEnd, Color featureColour, int start, int end, int y1,
77 int charHeight = av.getCharHeight();
78 int charWidth = av.getCharWidth();
79 boolean validCharWidth = av.isValidCharWidth();
81 if (featureStart > end || featureEnd < start)
86 if (featureStart < start)
90 if (featureEnd >= end)
94 int pady = (y1 + charHeight) - charHeight / 5;
96 FontMetrics fm = g.getFontMetrics();
97 for (int i = featureStart; i <= featureEnd; i++)
99 char s = seq.getCharAt(i);
101 if (Comparison.isGap(s))
106 g.setColor(featureColour);
108 g.fillRect((i - start) * charWidth, y1, charWidth, charHeight);
110 if (colourOnly || !validCharWidth)
115 g.setColor(Color.white);
116 int charOffset = (charWidth - fm.charWidth(s)) / 2;
117 g.drawString(String.valueOf(s),
118 charOffset + (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),
207 charOffset + (charWidth * (i - start)), pady);
216 public Color findFeatureColour(SequenceI seq, int column, Graphics g)
218 if (!av.isShowSequenceFeatures())
223 // column is 'base 1' but getCharAt is an array index (ie from 0)
224 if (Comparison.isGap(seq.getCharAt(column - 1)))
227 * returning null allows the colour scheme to provide gap colour
228 * - normally white, but can be customised
233 Color renderedColour = null;
234 if (transparency == 1.0f)
237 * simple case - just find the topmost rendered visible feature colour
239 renderedColour = findFeatureColour(seq, column);
244 * transparency case - draw all visible features in render order to
245 * build up a composite colour on the graphics context
247 renderedColour = drawSequence(g, seq, column, column, 0, true);
249 return renderedColour;
253 * Draws the sequence features on the graphics context, or just determines the
254 * colour that would be drawn (if flag colourOnly is true). Returns the last
255 * colour drawn (which may not be the effective colour if transparency
256 * applies), or null if no feature is drawn in the range given.
259 * the graphics context to draw on (may be null if colourOnly==true)
266 * vertical offset at which to draw on the graphics
268 * if true, only do enough to determine the colour for the position,
269 * do not draw the character
272 public synchronized Color drawSequence(final Graphics g,
273 final SequenceI seq, int start, int end, int y1,
277 * if columns are all gapped, or sequence has no features, nothing to do
279 ContiguousI visiblePositions = seq.findPositions(start + 1, end + 1);
280 if (visiblePositions == null || !seq.getFeatures().hasFeatures()
281 && !av.isShowComplementFeatures())
288 if (transparency != 1f && g != null)
290 Graphics2D g2 = (Graphics2D) g;
291 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
295 Color drawnColour = null;
298 * draw 'complement' features below ours if configured to do so
300 if (av.isShowComplementFeatures()
301 && !av.isShowComplementFeaturesOnTop())
303 drawnColour = drawComplementFeatures(g, seq, start, end, y1,
304 colourOnly, visiblePositions, drawnColour);
308 * iterate over features in ordering of their rendering (last is on top)
310 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
312 String type = renderOrder[renderIndex];
313 if (!showFeatureOfType(type))
318 FeatureColourI fc = getFeatureStyle(type);
319 List<SequenceFeature> overlaps = seq.getFeatures().findFeatures(
320 visiblePositions.getBegin(), visiblePositions.getEnd(), type);
322 if (fc.isSimpleColour())
324 filterFeaturesForDisplay(overlaps);
327 for (SequenceFeature sf : overlaps)
329 Color featureColour = getColor(sf, fc);
330 if (featureColour == null)
333 * feature excluded by filters, or colour threshold
339 * if feature starts/ends outside the visible range,
340 * restrict to visible positions (or if a contact feature,
341 * to a single position)
343 int visibleStart = sf.getBegin();
344 if (visibleStart < visiblePositions.getBegin())
346 visibleStart = sf.isContactFeature() ? sf.getEnd()
347 : visiblePositions.getBegin();
349 int visibleEnd = sf.getEnd();
350 if (visibleEnd > visiblePositions.getEnd())
352 visibleEnd = sf.isContactFeature() ? sf.getBegin()
353 : visiblePositions.getEnd();
356 int featureStartCol = seq.findIndex(visibleStart);
357 int featureEndCol = sf.begin == sf.end ? featureStartCol : seq
358 .findIndex(visibleEnd);
360 // Color featureColour = getColour(sequenceFeature);
362 boolean isContactFeature = sf.isContactFeature();
364 if (isContactFeature)
366 boolean drawn = renderFeature(g, seq, featureStartCol - 1,
367 featureStartCol - 1, featureColour, start, end, y1,
369 drawn |= renderFeature(g, seq, featureEndCol - 1,
370 featureEndCol - 1, featureColour, start, end, y1,
374 drawnColour = featureColour;
380 * showing feature score by height of colour
381 * is not implemented as a selectable option
383 if (av.isShowSequenceFeaturesHeight()
384 && !Float.isNaN(sequenceFeature.score))
386 boolean drawn = renderScoreFeature(g, seq,
387 seq.findIndex(sequenceFeature.begin) - 1,
388 seq.findIndex(sequenceFeature.end) - 1, featureColour,
389 start, end, y1, normaliseScore(sequenceFeature),
393 drawnColour = featureColour;
399 boolean drawn = renderFeature(g, seq,
401 featureEndCol - 1, featureColour,
402 start, end, y1, colourOnly);
405 drawnColour = featureColour;
413 * draw 'complement' features above ours if configured to do so
415 if (av.isShowComplementFeatures() && av.isShowComplementFeaturesOnTop())
417 drawnColour = drawComplementFeatures(g, seq, start, end, y1,
418 colourOnly, visiblePositions, drawnColour);
421 if (transparency != 1.0f && g != null)
426 Graphics2D g2 = (Graphics2D) g;
427 g2.setComposite(NO_TRANSPARENCY);
434 * Find any features on the CDS/protein complement of the sequence region and
435 * draw them, with visibility and colouring as configured in the complementary
444 * @param visiblePositions
448 Color drawComplementFeatures(final Graphics g, final SequenceI seq,
449 int start, int end, int y1, boolean colourOnly,
450 ContiguousI visiblePositions, Color drawnColour)
452 AlignViewportI comp = av.getCodingComplement();
453 FeatureRenderer fr2 = Desktop.getAlignFrameFor(comp)
454 .getFeatureRenderer();
456 final int visibleStart = visiblePositions.getBegin();
457 final int visibleEnd = visiblePositions.getEnd();
459 for (int pos = visibleStart; pos <= visibleEnd; pos++)
461 int column = seq.findIndex(pos);
462 MappedFeatures mf = fr2
463 .findComplementFeaturesAtResidue(seq, pos);
466 for (SequenceFeature sf : mf.features)
468 FeatureColourI fc = fr2.getFeatureStyle(sf.getType());
469 Color featureColour = fr2.getColor(sf, fc);
470 renderFeature(g, seq, column - 1, column - 1, featureColour,
471 start, end, y1, colourOnly);
472 drawnColour = featureColour;
480 * Called when alignment in associated view has new/modified features to
481 * discover and display.
485 public void featuresAdded()
491 * Returns the sequence feature colour rendered at the given column position,
492 * or null if none found. The feature of highest render order (i.e. on top) is
493 * found, subject to both feature type and feature group being visible, and
494 * its colour returned. This method is suitable when no feature transparency
495 * applied (only the topmost visible feature colour is rendered).
497 * Note this method does not check for a gap in the column so would return the
498 * colour for features enclosing a gapped column. Check for gap before calling
499 * if different behaviour is wanted.
506 Color findFeatureColour(SequenceI seq, int column)
509 * check for new feature added while processing
514 * show complement features on top (if configured to show them)
516 if (av.isShowComplementFeatures() && av.isShowComplementFeaturesOnTop())
518 Color col = findComplementFeatureColour(seq, column);
526 * inspect features in reverse renderOrder (the last in the array is
527 * displayed on top) until we find one that is rendered at the position
529 for (int renderIndex = renderOrder.length
530 - 1; renderIndex >= 0; renderIndex--)
532 String type = renderOrder[renderIndex];
533 if (!showFeatureOfType(type))
538 List<SequenceFeature> overlaps = seq.findFeatures(column, column,
540 for (SequenceFeature sequenceFeature : overlaps)
542 if (!featureGroupNotShown(sequenceFeature))
544 Color col = getColour(sequenceFeature);
554 * show complement features underneath (if configured to show them)
557 if (av.isShowComplementFeatures()
558 && !av.isShowComplementFeaturesOnTop())
560 col = findComplementFeatureColour(seq, column);
566 Color findComplementFeatureColour(SequenceI seq, int column)
568 AlignViewportI complement = av.getCodingComplement();
569 AlignFrame af = Desktop.getAlignFrameFor(complement);
570 FeatureRendererModel fr2 = af.getFeatureRenderer();
571 MappedFeatures mf = fr2.findComplementFeaturesAtResidue(
572 seq, seq.findPosition(column - 1));
577 ReverseListIterator<SequenceFeature> it = new ReverseListIterator<>(
581 SequenceFeature sf = it.next();
582 if (!fr2.featureGroupNotShown(sf))
584 Color col = fr2.getColour(sf);