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);
40 protected SequenceI lastSeq;
42 private volatile SequenceFeature[] lastSequenceFeatures;
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,
110 if (colourOnly || !validCharWidth)
115 g.setColor(Color.white);
116 int charOffset = (charWidth - fm.charWidth(s)) / 2;
117 g.drawString(String.valueOf(s), charOffset
118 + (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), charOffset
207 + (charWidth * (i - start)), pady);
216 public Color findFeatureColour(SequenceI seq, int column, Graphics g)
218 if (!av.isShowSequenceFeatures())
223 SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
227 lastSequenceFeatures = sequenceFeatures;
228 if (lastSequenceFeatures != null)
230 sfSize = lastSequenceFeatures.length;
235 if (lastSequenceFeatures != sequenceFeatures)
237 lastSequenceFeatures = sequenceFeatures;
238 if (lastSequenceFeatures != null)
240 sfSize = lastSequenceFeatures.length;
245 if (lastSequenceFeatures == null || sfSize == 0)
250 if (Comparison.isGap(lastSeq.getCharAt(column)))
255 Color renderedColour = null;
256 if (transparency == 1.0f)
259 * simple case - just find the topmost rendered visible feature colour
261 renderedColour = findFeatureColour(seq, seq.findPosition(column));
266 * transparency case - draw all visible features in render order to
267 * build up a composite colour on the graphics context
269 renderedColour = drawSequence(g, lastSeq, column, column, 0, true);
271 return renderedColour;
275 * Draws the sequence features on the graphics context, or just determines the
276 * colour that would be drawn (if flag colourOnly is true). Returns the last
277 * colour drawn (which may not be the effective colour if transparency
278 * applies), or null if no feature is drawn in the range given.
281 * the graphics context to draw on (may be null if colourOnly==true)
288 * vertical offset at which to draw on the graphics
290 * if true, only do enough to determine the colour for the position,
291 * do not draw the character
294 public synchronized Color drawSequence(final Graphics g,
295 final SequenceI seq, int start, int end, int y1,
298 SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
299 if (sequenceFeatures == null || sequenceFeatures.length == 0)
306 if (lastSeq == null || seq != lastSeq
307 || sequenceFeatures != lastSequenceFeatures)
310 lastSequenceFeatures = sequenceFeatures;
313 if (transparency != 1f && g != null)
315 Graphics2D g2 = (Graphics2D) g;
316 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
320 int startPos = lastSeq.findPosition(start);
321 int endPos = lastSeq.findPosition(end);
323 sfSize = lastSequenceFeatures.length;
324 Color drawnColour = null;
327 * iterate over features in ordering of their rendering (last is on top)
329 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
331 String type = renderOrder[renderIndex];
332 if (!showFeatureOfType(type))
337 // loop through all features in sequence to find
338 // current feature to render
339 for (int sfindex = 0; sfindex < sfSize; sfindex++)
341 final SequenceFeature sequenceFeature = lastSequenceFeatures[sfindex];
342 if (!sequenceFeature.type.equals(type))
348 * a feature type may be flagged as shown but the group
349 * an instance of it belongs to may be hidden
351 if (featureGroupNotShown(sequenceFeature))
357 * check feature overlaps the target range
358 * TODO: efficient retrieval of features overlapping a range
360 if (sequenceFeature.getBegin() > endPos
361 || sequenceFeature.getEnd() < startPos)
366 Color featureColour = getColour(sequenceFeature);
367 boolean isContactFeature = sequenceFeature.isContactFeature();
369 if (isContactFeature)
371 boolean drawn = renderFeature(g, seq,
372 seq.findIndex(sequenceFeature.begin) - 1,
373 seq.findIndex(sequenceFeature.begin) - 1, featureColour,
374 start, end, y1, colourOnly);
375 drawn |= renderFeature(g, seq,
376 seq.findIndex(sequenceFeature.end) - 1,
377 seq.findIndex(sequenceFeature.end) - 1, featureColour,
378 start, end, y1, colourOnly);
381 drawnColour = featureColour;
384 else if (showFeature(sequenceFeature))
386 if (av.isShowSequenceFeaturesHeight()
387 && !Float.isNaN(sequenceFeature.score))
389 boolean drawn = renderScoreFeature(g, seq,
390 seq.findIndex(sequenceFeature.begin) - 1,
391 seq.findIndex(sequenceFeature.end) - 1, featureColour,
392 start, end, y1, normaliseScore(sequenceFeature),
396 drawnColour = featureColour;
401 boolean drawn = renderFeature(g, seq,
402 seq.findIndex(sequenceFeature.begin) - 1,
403 seq.findIndex(sequenceFeature.end) - 1, featureColour,
404 start, end, y1, colourOnly);
407 drawnColour = featureColour;
414 if (transparency != 1.0f && g != null)
419 Graphics2D g2 = (Graphics2D) g;
420 g2.setComposite(NO_TRANSPARENCY);
427 * Answers true if the feature belongs to a feature group which is not
428 * currently displayed, else false
430 * @param sequenceFeature
433 protected boolean featureGroupNotShown(
434 final SequenceFeature sequenceFeature)
436 return featureGroups != null
437 && sequenceFeature.featureGroup != null
438 && sequenceFeature.featureGroup.length() != 0
439 && featureGroups.containsKey(sequenceFeature.featureGroup)
440 && !featureGroups.get(sequenceFeature.featureGroup)
445 * Called when alignment in associated view has new/modified features to
446 * discover and display.
450 public void featuresAdded()
457 * Returns the sequence feature colour rendered at the given sequence
458 * position, or null if none found. The feature of highest render order (i.e.
459 * on top) is found, subject to both feature type and feature group being
460 * visible, and its colour returned.
466 Color findFeatureColour(SequenceI seq, int pos)
468 SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
469 if (sequenceFeatures == null || sequenceFeatures.length == 0)
475 * check for new feature added while processing
480 * inspect features in reverse renderOrder (the last in the array is
481 * displayed on top) until we find one that is rendered at the position
483 for (int renderIndex = renderOrder.length - 1; renderIndex >= 0; renderIndex--)
485 String type = renderOrder[renderIndex];
486 if (!showFeatureOfType(type))
491 for (int sfindex = 0; sfindex < sequenceFeatures.length; sfindex++)
493 SequenceFeature sequenceFeature = sequenceFeatures[sfindex];
494 if (!sequenceFeature.type.equals(type))
499 if (featureGroupNotShown(sequenceFeature))
505 * check the column position is within the feature range
506 * (or is one of the two contact positions for a contact feature)
508 boolean featureIsAtPosition = sequenceFeature.begin <= pos
509 && sequenceFeature.end >= pos;
510 if (sequenceFeature.isContactFeature())
512 featureIsAtPosition = sequenceFeature.begin == pos
513 || sequenceFeature.end == pos;
515 if (featureIsAtPosition)
517 return getColour(sequenceFeature);
523 * no displayed feature found at position