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);
41 * Constructor given a viewport
45 public FeatureRenderer(AlignViewportI viewport)
51 * Renders the sequence using the given feature colour between the given start
52 * and end columns. Returns true if at least one column is drawn, else false
53 * (the feature range does not overlap the start and end positions).
59 * @param featureColour
66 boolean renderFeature(Graphics g, SequenceI seq, int featureStart,
67 int featureEnd, Color featureColour, int start, int end, int y1,
70 int charHeight = av.getCharHeight();
71 int charWidth = av.getCharWidth();
72 boolean validCharWidth = av.isValidCharWidth();
74 if (featureStart > end || featureEnd < start)
79 if (featureStart < start)
83 if (featureEnd >= end)
87 int pady = (y1 + charHeight) - charHeight / 5;
89 FontMetrics fm = g.getFontMetrics();
90 for (int i = featureStart; i <= featureEnd; i++)
92 char s = seq.getCharAt(i);
94 if (Comparison.isGap(s))
99 g.setColor(featureColour);
101 g.fillRect((i - start) * charWidth, y1, charWidth,
104 if (colourOnly || !validCharWidth)
109 g.setColor(Color.white);
110 int charOffset = (charWidth - fm.charWidth(s)) / 2;
111 g.drawString(String.valueOf(s), charOffset
112 + (charWidth * (i - start)), pady);
118 * Renders the sequence using the given SCORE feature colour between the given
119 * start and end columns. Returns true if at least one column is drawn, else
120 * false (the feature range does not overlap the start and end positions).
126 * @param featureColour
134 boolean renderScoreFeature(Graphics g, SequenceI seq, int fstart,
135 int fend, Color featureColour, int start, int end, int y1,
136 byte[] bs, boolean colourOnly)
138 if (fstart > end || fend < start)
144 { // fix for if the feature we have starts before the sequence start,
145 fstart = start; // but the feature end is still valid!!
152 int charHeight = av.getCharHeight();
153 int pady = (y1 + charHeight) - charHeight / 5;
154 int ystrt = 0, yend = charHeight;
157 // signed - zero is always middle of residue line.
160 yend = charHeight * (128 - bs[1]) / 512;
161 ystrt = charHeight - yend / 2;
165 ystrt = charHeight / 2;
166 yend = charHeight * (bs[1] - 128) / 512;
171 yend = charHeight * bs[1] / 255;
172 ystrt = charHeight - yend;
176 FontMetrics fm = g.getFontMetrics();
177 int charWidth = av.getCharWidth();
179 for (int i = fstart; i <= fend; i++)
181 char s = seq.getCharAt(i);
183 if (Comparison.isGap(s))
188 g.setColor(featureColour);
189 int x = (i - start) * charWidth;
190 g.drawRect(x, y1, charWidth, charHeight);
191 g.fillRect(x, y1 + ystrt, charWidth, yend);
193 if (colourOnly || !av.isValidCharWidth())
198 g.setColor(Color.black);
199 int charOffset = (charWidth - fm.charWidth(s)) / 2;
200 g.drawString(String.valueOf(s), charOffset
201 + (charWidth * (i - start)), pady);
210 public Color findFeatureColour(SequenceI seq, int column, Graphics g)
212 if (!av.isShowSequenceFeatures())
217 SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
219 if (sequenceFeatures == null || sequenceFeatures.length == 0)
224 if (Comparison.isGap(seq.getCharAt(column)))
227 * returning null allows the colour scheme to provide gap colour
228 * - normally white, but can be customised otherwise
233 Color renderedColour = null;
234 if (transparency == 1.0f)
237 * simple case - just find the topmost rendered visible feature colour
239 renderedColour = findFeatureColour(seq, seq.findPosition(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,
276 SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
277 if (sequenceFeatures == null || sequenceFeatures.length == 0)
284 if (transparency != 1f && g != null)
286 Graphics2D g2 = (Graphics2D) g;
287 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
291 int startPos = seq.findPosition(start);
292 int endPos = seq.findPosition(end);
294 int sfSize = sequenceFeatures.length;
295 Color drawnColour = null;
298 * iterate over features in ordering of their rendering (last is on top)
300 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
302 String type = renderOrder[renderIndex];
303 if (!showFeatureOfType(type))
308 // loop through all features in sequence to find
309 // current feature to render
310 for (int sfindex = 0; sfindex < sfSize; sfindex++)
312 final SequenceFeature sequenceFeature = sequenceFeatures[sfindex];
313 if (!sequenceFeature.type.equals(type))
319 * a feature type may be flagged as shown but the group
320 * an instance of it belongs to may be hidden
322 if (featureGroupNotShown(sequenceFeature))
328 * check feature overlaps the target range
329 * TODO: efficient retrieval of features overlapping a range
331 if (sequenceFeature.getBegin() > endPos
332 || sequenceFeature.getEnd() < startPos)
337 Color featureColour = getColour(sequenceFeature);
338 if (featureColour == null)
340 // score feature outwith threshold for colouring
344 boolean isContactFeature = sequenceFeature.isContactFeature();
346 if (isContactFeature)
348 boolean drawn = renderFeature(g, seq,
349 seq.findIndex(sequenceFeature.begin) - 1,
350 seq.findIndex(sequenceFeature.begin) - 1, featureColour,
351 start, end, y1, colourOnly);
352 drawn |= renderFeature(g, seq,
353 seq.findIndex(sequenceFeature.end) - 1,
354 seq.findIndex(sequenceFeature.end) - 1, featureColour,
355 start, end, y1, colourOnly);
358 drawnColour = featureColour;
363 if (av.isShowSequenceFeaturesHeight()
364 && !Float.isNaN(sequenceFeature.score))
366 boolean drawn = renderScoreFeature(g, seq,
367 seq.findIndex(sequenceFeature.begin) - 1,
368 seq.findIndex(sequenceFeature.end) - 1, featureColour,
369 start, end, y1, normaliseScore(sequenceFeature),
373 drawnColour = featureColour;
378 boolean drawn = renderFeature(g, seq,
379 seq.findIndex(sequenceFeature.begin) - 1,
380 seq.findIndex(sequenceFeature.end) - 1, featureColour,
381 start, end, y1, colourOnly);
384 drawnColour = featureColour;
391 if (transparency != 1.0f && g != null)
396 Graphics2D g2 = (Graphics2D) g;
397 g2.setComposite(NO_TRANSPARENCY);
404 * Answers true if the feature belongs to a feature group which is not
405 * currently displayed, else false
407 * @param sequenceFeature
410 protected boolean featureGroupNotShown(
411 final SequenceFeature sequenceFeature)
413 return featureGroups != null
414 && sequenceFeature.featureGroup != null
415 && sequenceFeature.featureGroup.length() != 0
416 && featureGroups.containsKey(sequenceFeature.featureGroup)
417 && !featureGroups.get(sequenceFeature.featureGroup)
422 * Called when alignment in associated view has new/modified features to
423 * discover and display.
427 public void featuresAdded()
433 * Returns the sequence feature colour rendered at the given sequence
434 * position, or null if none found. The feature of highest render order (i.e.
435 * on top) is found, subject to both feature type and feature group being
436 * visible, and its colour returned.
442 Color findFeatureColour(SequenceI seq, int pos)
444 SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
445 if (sequenceFeatures == null || sequenceFeatures.length == 0)
451 * check for new feature added while processing
456 * inspect features in reverse renderOrder (the last in the array is
457 * displayed on top) until we find one that is rendered at the position
459 for (int renderIndex = renderOrder.length - 1; renderIndex >= 0; renderIndex--)
461 String type = renderOrder[renderIndex];
462 if (!showFeatureOfType(type))
467 for (int sfindex = 0; sfindex < sequenceFeatures.length; sfindex++)
469 SequenceFeature sequenceFeature = sequenceFeatures[sfindex];
470 if (!sequenceFeature.type.equals(type))
475 if (featureGroupNotShown(sequenceFeature))
481 * check the column position is within the feature range
482 * (or is one of the two contact positions for a contact feature)
484 boolean featureIsAtPosition = sequenceFeature.begin <= pos
485 && sequenceFeature.end >= pos;
486 if (sequenceFeature.isContactFeature())
488 featureIsAtPosition = sequenceFeature.begin == pos
489 || sequenceFeature.end == pos;
491 if (featureIsAtPosition)
493 return getColour(sequenceFeature);
499 * no displayed feature found at position