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)))
229 Color renderedColour = null;
230 if (transparency == 1.0f)
233 * simple case - just find the topmost rendered visible feature colour
235 renderedColour = findFeatureColour(seq, seq.findPosition(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,
272 SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
273 if (sequenceFeatures == null || sequenceFeatures.length == 0)
280 if (transparency != 1f && g != null)
282 Graphics2D g2 = (Graphics2D) g;
283 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
287 int startPos = seq.findPosition(start);
288 int endPos = seq.findPosition(end);
290 int sfSize = sequenceFeatures.length;
291 Color drawnColour = null;
294 * iterate over features in ordering of their rendering (last is on top)
296 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
298 String type = renderOrder[renderIndex];
299 if (!showFeatureOfType(type))
304 // loop through all features in sequence to find
305 // current feature to render
306 for (int sfindex = 0; sfindex < sfSize; sfindex++)
308 final SequenceFeature sequenceFeature = sequenceFeatures[sfindex];
309 if (!sequenceFeature.type.equals(type))
315 * a feature type may be flagged as shown but the group
316 * an instance of it belongs to may be hidden
318 if (featureGroupNotShown(sequenceFeature))
324 * check feature overlaps the target range
325 * TODO: efficient retrieval of features overlapping a range
327 if (sequenceFeature.getBegin() > endPos
328 || sequenceFeature.getEnd() < startPos)
333 Color featureColour = getColour(sequenceFeature);
334 boolean isContactFeature = sequenceFeature.isContactFeature();
336 if (isContactFeature)
338 boolean drawn = renderFeature(g, seq,
339 seq.findIndex(sequenceFeature.begin) - 1,
340 seq.findIndex(sequenceFeature.begin) - 1, featureColour,
341 start, end, y1, colourOnly);
342 drawn |= renderFeature(g, seq,
343 seq.findIndex(sequenceFeature.end) - 1,
344 seq.findIndex(sequenceFeature.end) - 1, featureColour,
345 start, end, y1, colourOnly);
348 drawnColour = featureColour;
351 else if (showFeature(sequenceFeature))
353 if (av.isShowSequenceFeaturesHeight()
354 && !Float.isNaN(sequenceFeature.score))
356 boolean drawn = renderScoreFeature(g, seq,
357 seq.findIndex(sequenceFeature.begin) - 1,
358 seq.findIndex(sequenceFeature.end) - 1, featureColour,
359 start, end, y1, normaliseScore(sequenceFeature),
363 drawnColour = featureColour;
368 boolean drawn = renderFeature(g, seq,
369 seq.findIndex(sequenceFeature.begin) - 1,
370 seq.findIndex(sequenceFeature.end) - 1, featureColour,
371 start, end, y1, colourOnly);
374 drawnColour = featureColour;
381 if (transparency != 1.0f && g != null)
386 Graphics2D g2 = (Graphics2D) g;
387 g2.setComposite(NO_TRANSPARENCY);
394 * Answers true if the feature belongs to a feature group which is not
395 * currently displayed, else false
397 * @param sequenceFeature
400 protected boolean featureGroupNotShown(
401 final SequenceFeature sequenceFeature)
403 return featureGroups != null
404 && sequenceFeature.featureGroup != null
405 && sequenceFeature.featureGroup.length() != 0
406 && featureGroups.containsKey(sequenceFeature.featureGroup)
407 && !featureGroups.get(sequenceFeature.featureGroup)
412 * Called when alignment in associated view has new/modified features to
413 * discover and display.
417 public void featuresAdded()
423 * Returns the sequence feature colour rendered at the given sequence
424 * position, or null if none found. The feature of highest render order (i.e.
425 * on top) is found, subject to both feature type and feature group being
426 * visible, and its colour returned.
432 Color findFeatureColour(SequenceI seq, int pos)
434 SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
435 if (sequenceFeatures == null || sequenceFeatures.length == 0)
441 * check for new feature added while processing
446 * inspect features in reverse renderOrder (the last in the array is
447 * displayed on top) until we find one that is rendered at the position
449 for (int renderIndex = renderOrder.length - 1; renderIndex >= 0; renderIndex--)
451 String type = renderOrder[renderIndex];
452 if (!showFeatureOfType(type))
457 for (int sfindex = 0; sfindex < sequenceFeatures.length; sfindex++)
459 SequenceFeature sequenceFeature = sequenceFeatures[sfindex];
460 if (!sequenceFeature.type.equals(type))
465 if (featureGroupNotShown(sequenceFeature))
471 * check the column position is within the feature range
472 * (or is one of the two contact positions for a contact feature)
474 boolean featureIsAtPosition = sequenceFeature.begin <= pos
475 && sequenceFeature.end >= pos;
476 if (sequenceFeature.isContactFeature())
478 featureIsAtPosition = sequenceFeature.begin == pos
479 || sequenceFeature.end == pos;
481 if (featureIsAtPosition)
483 return getColour(sequenceFeature);
489 * no displayed feature found at position