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.gui.AlignFrame;
29 import jalview.gui.Desktop;
30 import jalview.util.Comparison;
31 import jalview.util.ReverseListIterator;
32 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
34 import java.awt.AlphaComposite;
35 import java.awt.Color;
36 import java.awt.FontMetrics;
37 import java.awt.Graphics;
38 import java.awt.Graphics2D;
39 import java.util.List;
41 public class FeatureRenderer extends FeatureRendererModel
43 private static final AlphaComposite NO_TRANSPARENCY = AlphaComposite
44 .getInstance(AlphaComposite.SRC_OVER, 1.0f);
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, charHeight);
109 if (colourOnly || !validCharWidth)
114 g.setColor(Color.white);
115 int charOffset = (charWidth - fm.charWidth(s)) / 2;
116 g.drawString(String.valueOf(s),
117 charOffset + (charWidth * (i - start)), pady);
123 * Renders the sequence using the given SCORE feature colour between the given
124 * start and end columns. Returns true if at least one column is drawn, else
125 * false (the feature range does not overlap the start and end positions).
131 * @param featureColour
139 boolean renderScoreFeature(Graphics g, SequenceI seq, int fstart,
140 int fend, Color featureColour, int start, int end, int y1,
141 byte[] bs, boolean colourOnly)
143 if (fstart > end || fend < start)
149 { // fix for if the feature we have starts before the sequence start,
150 fstart = start; // but the feature end is still valid!!
157 int charHeight = av.getCharHeight();
158 int pady = (y1 + charHeight) - charHeight / 5;
159 int ystrt = 0, yend = charHeight;
162 // signed - zero is always middle of residue line.
165 yend = charHeight * (128 - bs[1]) / 512;
166 ystrt = charHeight - yend / 2;
170 ystrt = charHeight / 2;
171 yend = charHeight * (bs[1] - 128) / 512;
176 yend = charHeight * bs[1] / 255;
177 ystrt = charHeight - yend;
181 FontMetrics fm = g.getFontMetrics();
182 int charWidth = av.getCharWidth();
184 for (int i = fstart; i <= fend; i++)
186 char s = seq.getCharAt(i);
188 if (Comparison.isGap(s))
193 g.setColor(featureColour);
194 int x = (i - start) * charWidth;
195 g.drawRect(x, y1, charWidth, charHeight);
196 g.fillRect(x, y1 + ystrt, charWidth, yend);
198 if (colourOnly || !av.isValidCharWidth())
203 g.setColor(Color.black);
204 int charOffset = (charWidth - fm.charWidth(s)) / 2;
205 g.drawString(String.valueOf(s),
206 charOffset + (charWidth * (i - start)), pady);
215 public Color findFeatureColour(SequenceI seq, int column, Graphics g)
217 if (!av.isShowSequenceFeatures())
222 // column is 'base 1' but getCharAt is an array index (ie from 0)
223 if (Comparison.isGap(seq.getCharAt(column - 1)))
226 * returning null allows the colour scheme to provide gap colour
227 * - normally white, but can be customised
232 Color renderedColour = null;
233 if (transparency == 1.0f)
236 * simple case - just find the topmost rendered visible feature colour
238 renderedColour = findFeatureColour(seq, column);
243 * transparency case - draw all visible features in render order to
244 * build up a composite colour on the graphics context
246 renderedColour = drawSequence(g, seq, column, column, 0, true);
248 return renderedColour;
252 * Draws the sequence features on the graphics context, or just determines the
253 * colour that would be drawn (if flag colourOnly is true). Returns the last
254 * colour drawn (which may not be the effective colour if transparency
255 * applies), or null if no feature is drawn in the range given.
258 * the graphics context to draw on (may be null if colourOnly==true)
265 * vertical offset at which to draw on the graphics
267 * if true, only do enough to determine the colour for the position,
268 * do not draw the character
271 public synchronized Color drawSequence(final Graphics g,
272 final SequenceI seq, int start, int end, int y1,
276 * if columns are all gapped, or sequence has no features, nothing to do
278 Range visiblePositions = seq.findPositions(start+1, end+1);
279 if (visiblePositions == null || (!seq.getFeatures().hasFeatures()
280 && !av.isShowComplementFeatures()))
287 if (transparency != 1f && g != null)
289 Graphics2D g2 = (Graphics2D) g;
290 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
294 Color drawnColour = null;
297 * draw 'complement' features below ours if configured to do so
299 if (av.isShowComplementFeatures()
300 && !av.isShowComplementFeaturesOnTop())
302 drawnColour = drawComplementFeatures(g, seq, start, end, y1,
303 colourOnly, visiblePositions, drawnColour);
307 * iterate over features in ordering of their rendering (last is on top)
309 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
311 String type = renderOrder[renderIndex];
312 if (!showFeatureOfType(type))
317 FeatureColourI fc = getFeatureStyle(type);
318 List<SequenceFeature> overlaps = seq.getFeatures().findFeatures(
319 visiblePositions.getBegin(), visiblePositions.getEnd(), type);
321 if (fc.isSimpleColour())
323 filterFeaturesForDisplay(overlaps);
326 for (SequenceFeature sf : overlaps)
328 Color featureColour = getColor(sf, fc);
329 if (featureColour == null)
332 * feature excluded by filters, or colour threshold
338 * if feature starts/ends outside the visible range,
339 * restrict to visible positions (or if a contact feature,
340 * to a single position)
342 int visibleStart = sf.getBegin();
343 if (visibleStart < visiblePositions.getBegin())
345 visibleStart = sf.isContactFeature() ? sf.getEnd()
346 : visiblePositions.getBegin();
348 int visibleEnd = sf.getEnd();
349 if (visibleEnd > visiblePositions.getEnd())
351 visibleEnd = sf.isContactFeature() ? sf.getBegin()
352 : visiblePositions.getEnd();
355 int featureStartCol = seq.findIndex(visibleStart);
356 int featureEndCol = sf.begin == sf.end ? featureStartCol : seq
357 .findIndex(visibleEnd);
359 // Color featureColour = getColour(sequenceFeature);
361 boolean isContactFeature = sf.isContactFeature();
363 if (isContactFeature)
365 boolean drawn = renderFeature(g, seq, featureStartCol - 1,
366 featureStartCol - 1, featureColour, start, end, y1,
368 drawn |= renderFeature(g, seq, featureEndCol - 1,
369 featureEndCol - 1, featureColour, start, end, y1,
373 drawnColour = featureColour;
379 * showing feature score by height of colour
380 * is not implemented as a selectable option
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;
398 boolean drawn = renderFeature(g, seq,
400 featureEndCol - 1, featureColour,
401 start, end, y1, colourOnly);
404 drawnColour = featureColour;
412 * draw 'complement' features above ours if configured to do so
414 if (av.isShowComplementFeatures() && av.isShowComplementFeaturesOnTop())
416 drawnColour = drawComplementFeatures(g, seq, start, end, y1,
417 colourOnly, visiblePositions, drawnColour);
420 if (transparency != 1.0f && g != null)
425 Graphics2D g2 = (Graphics2D) g;
426 g2.setComposite(NO_TRANSPARENCY);
433 * Find any features on the CDS/protein complement of the sequence region and
434 * draw them, with visibility and colouring as configured in the complementary
443 * @param visiblePositions
447 Color drawComplementFeatures(final Graphics g, final SequenceI seq,
448 int start, int end, int y1, boolean colourOnly,
449 Range visiblePositions, Color drawnColour)
451 AlignViewportI comp = av.getCodingComplement();
452 FeatureRenderer fr2 = Desktop.getAlignFrameFor(comp)
453 .getFeatureRenderer();
454 for (int pos = visiblePositions.start; pos <= visiblePositions.end; pos++)
456 int column = seq.findIndex(pos);
457 List<SequenceFeature> features = fr2
458 .findComplementFeaturesAtResidue(seq, pos);
459 for (SequenceFeature sf : features)
461 FeatureColourI fc = fr2.getFeatureStyle(sf.getType());
462 Color featureColour = fr2.getColor(sf, fc);
463 renderFeature(g, seq, column - 1, column - 1, featureColour,
464 start, end, y1, colourOnly);
465 drawnColour = featureColour;
472 * Called when alignment in associated view has new/modified features to
473 * discover and display.
477 public void featuresAdded()
483 * Returns the sequence feature colour rendered at the given column position,
484 * or null if none found. The feature of highest render order (i.e. on top) is
485 * found, subject to both feature type and feature group being visible, and
486 * its colour returned. This method is suitable when no feature transparency
487 * applied (only the topmost visible feature colour is rendered).
489 * Note this method does not check for a gap in the column so would return the
490 * colour for features enclosing a gapped column. Check for gap before calling
491 * if different behaviour is wanted.
498 Color findFeatureColour(SequenceI seq, int column)
501 * check for new feature added while processing
506 * show complement features on top (if configured to show them)
508 if (av.isShowComplementFeatures() && av.isShowComplementFeaturesOnTop())
510 Color col = findComplementFeatureColour(seq, column);
518 * inspect features in reverse renderOrder (the last in the array is
519 * displayed on top) until we find one that is rendered at the position
521 for (int renderIndex = renderOrder.length
522 - 1; renderIndex >= 0; renderIndex--)
524 String type = renderOrder[renderIndex];
525 if (!showFeatureOfType(type))
530 List<SequenceFeature> overlaps = seq.findFeatures(column, column,
532 for (SequenceFeature sequenceFeature : overlaps)
534 if (!featureGroupNotShown(sequenceFeature))
536 Color col = getColour(sequenceFeature);
546 * show complement features underneath (if configured to show them)
549 if (av.isShowComplementFeatures()
550 && !av.isShowComplementFeaturesOnTop())
552 col = findComplementFeatureColour(seq, column);
558 Color findComplementFeatureColour(SequenceI seq, int column)
560 AlignViewportI complement = av.getCodingComplement();
561 AlignFrame af = Desktop.getAlignFrameFor(complement);
562 FeatureRendererModel fr2 = af.getFeatureRenderer();
563 List<SequenceFeature> features = fr2.findComplementFeaturesAtResidue(
564 seq, seq.findPosition(column - 1));
566 ReverseListIterator<SequenceFeature> it = new ReverseListIterator<>(
570 SequenceFeature sf = it.next();
571 if (!fr2.featureGroupNotShown(sf))
573 Color col = fr2.getColour(sf);