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.SequenceFeature;
27 import jalview.datamodel.SequenceI;
28 import jalview.util.Comparison;
29 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
31 import java.awt.AlphaComposite;
32 import java.awt.Color;
33 import java.awt.FontMetrics;
34 import java.awt.Graphics;
35 import java.awt.Graphics2D;
36 import java.util.ArrayList;
37 import java.util.List;
39 public class FeatureRenderer extends FeatureRendererModel
41 private static final AlphaComposite NO_TRANSPARENCY = AlphaComposite
42 .getInstance(AlphaComposite.SRC_OVER, 1.0f);
45 * Constructor given a viewport
49 public FeatureRenderer(AlignViewportI viewport)
55 * Renders the sequence using the given feature colour between the given start
56 * and end columns. Returns true if at least one column is drawn, else false
57 * (the feature range does not overlap the start and end positions).
63 * @param featureColour
70 boolean renderFeature(Graphics g, SequenceI seq, int featureStart,
71 int featureEnd, Color featureColour, int start, int end, int y1,
74 int charHeight = av.getCharHeight();
75 int charWidth = av.getCharWidth();
76 boolean validCharWidth = av.isValidCharWidth();
78 if (featureStart > end || featureEnd < start)
83 if (featureStart < start)
87 if (featureEnd >= end)
91 int pady = (y1 + charHeight) - charHeight / 5;
93 FontMetrics fm = g.getFontMetrics();
95 for (int i = featureStart; i <= featureEnd; i++)
98 // colourOnly is just for Overview -- no need to check this again
100 if (!colourOnly && Comparison.isGap(s = seq.getCharAt(i)))
105 g.setColor(featureColour);
107 g.fillRect((i - start) * charWidth, y1, charWidth, charHeight);
119 g.setColor(Color.white);
120 int charOffset = (charWidth - fm.charWidth(s)) / 2;
121 g.drawString(String.valueOf(s),
122 charOffset + (charWidth * (i - start)), pady);
129 * BH - this method is never called?
131 * Renders the sequence using the given SCORE feature colour between the given
132 * start and end columns. Returns true if at least one column is drawn, else
133 * false (the feature range does not overlap the start and end positions).
139 * @param featureColour
147 boolean renderScoreFeature(Graphics g, SequenceI seq, int fstart,
148 int fend, Color featureColour, int start, int end, int y1,
149 byte[] bs, boolean colourOnly)
151 if (fstart > end || fend < start)
157 { // fix for if the feature we have starts before the sequence start,
158 fstart = start; // but the feature end is still valid!!
165 int charHeight = av.getCharHeight();
166 int pady = (y1 + charHeight) - charHeight / 5;
167 int ystrt = 0, yend = charHeight;
170 // signed - zero is always middle of residue line.
173 yend = charHeight * (128 - bs[1]) / 512;
174 ystrt = charHeight - yend / 2;
178 ystrt = charHeight / 2;
179 yend = charHeight * (bs[1] - 128) / 512;
184 yend = charHeight * bs[1] / 255;
185 ystrt = charHeight - yend;
189 FontMetrics fm = g.getFontMetrics();
190 int charWidth = av.getCharWidth();
192 for (int i = fstart; i <= fend; i++)
194 char s = seq.getCharAt(i);
196 if (Comparison.isGap(s))
201 g.setColor(featureColour);
202 int x = (i - start) * charWidth;
203 g.drawRect(x, y1, charWidth, charHeight);
204 g.fillRect(x, y1 + ystrt, charWidth, yend);
206 if (colourOnly || !av.isValidCharWidth())
211 g.setColor(Color.black);
212 int charOffset = (charWidth - fm.charWidth(s)) / 2;
213 g.drawString(String.valueOf(s),
214 charOffset + (charWidth * (i - start)), pady);
223 public Color findFeatureColour(SequenceI seq, int column, Graphics g)
226 // this is already checked in FeatureColorFinder
227 // if (!av.isShowSequenceFeatures())
232 // column is 'base 1' but getCharAt is an array index (ie from 0)
233 if (Comparison.isGap(seq.getCharAt(column - 1)))
236 * returning null allows the colour scheme to provide gap colour
237 * - normally white, but can be customised
242 Color renderedColour = null;
243 if (transparency == 1.0f)
246 * simple case - just find the topmost rendered visible feature colour
248 renderedColour = findFeatureColour(seq, column);
253 * transparency case - draw all visible features in render order to
254 * build up a composite colour on the graphics context
256 renderedColour = drawSequence(g, seq, column, column, 0, true);
258 return renderedColour;
262 * Draws the sequence features on the graphics context, or just determines the
263 * colour that would be drawn (if flag colourOnly is true). Returns the last
264 * colour drawn (which may not be the effective colour if transparency
265 * applies), or null if no feature is drawn in the range given.
268 * the graphics context to draw on (may be null only if t == 1 from
276 * vertical offset at which to draw on the graphics
278 * if true, only do enough to determine the colour for the position,
279 * do not draw the character
282 public synchronized Color drawSequence(final Graphics g,
283 final SequenceI seq, int start, int end, int y1,
286 // from SeqCanvas and OverviewRender
288 * if columns are all gapped, or sequence has no features, nothing to do
290 ContiguousI visiblePositions;
291 if (!seq.getFeatures().hasFeatures() || (visiblePositions = seq
292 .findPositions(start + 1, end + 1)) == null)
297 int vp0 = visiblePositions.getBegin();
298 int vp1 = visiblePositions.getEnd();
302 if (transparency != 1f) // g cannot be null here if trans == 1f - BH // && g
305 ((Graphics2D) g).setComposite(
306 AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
310 Color drawnColour = null;
313 * iterate over features in ordering of their rendering (last is on top)
315 for (int renderIndex = 0, n = renderOrder.length; renderIndex < n; renderIndex++)
317 String type = renderOrder[renderIndex];
318 if (!seq.hasFeatures(type) || !showFeatureOfType(type))
323 FeatureColourI fc = getFeatureStyle(type);
324 List<SequenceFeature> overlaps = seq.getFeatures().findFeatures(vp0,
327 // colourOnly (i.e. Overview) can only be here if translucent, so
328 // there is no need to check for filtering
329 if (!colourOnly && fc.isSimpleColour())
331 filterFeaturesForDisplay(overlaps);
334 for (int i = overlaps.size(); --i >= 0;)
336 SequenceFeature sf = overlaps.get(i);
337 Color featureColour = getColor(sf, fc);
338 if (featureColour == null)
341 * feature excluded by visibility settings, filters, or colour threshold
347 * if feature starts/ends outside the visible range,
348 * restrict to visible positions (or if a contact feature,
349 * to a single position)
351 int sf0 = sf.getBegin();
352 int sf1 = sf.getEnd();
353 int visibleStart = sf0;
354 if (visibleStart < vp0)
356 visibleStart = sf.isContactFeature() ? sf1 : vp0;
358 int visibleEnd = sf1;
359 if (visibleEnd > vp1)
361 visibleEnd = sf.isContactFeature() ? sf0 : vp1;
364 int featureStartCol = seq.findIndex(visibleStart);
365 int featureEndCol = (sf.begin == sf.end ? featureStartCol
366 : seq.findIndex(visibleEnd));
368 // Color featureColour = getColour(sequenceFeature);
370 boolean isContactFeature = sf.isContactFeature();
372 if (isContactFeature)
374 boolean drawn = renderFeature(g, seq, featureStartCol - 1,
375 featureStartCol - 1, featureColour, start, end, y1,
377 drawn |= renderFeature(g, seq, featureEndCol - 1,
378 featureEndCol - 1, featureColour, start, end, y1,
382 drawnColour = featureColour;
388 * showing feature score by height of colour
389 * is not implemented as a selectable option
391 if (av.isShowSequenceFeaturesHeight()
392 && !Float.isNaN(sequenceFeature.score))
394 boolean drawn = renderScoreFeature(g, seq,
395 seq.findIndex(sequenceFeature.begin) - 1,
396 seq.findIndex(sequenceFeature.end) - 1, featureColour,
397 start, end, y1, normaliseScore(sequenceFeature),
401 drawnColour = featureColour;
407 boolean drawn = renderFeature(g, seq, featureStartCol - 1,
408 featureEndCol - 1, featureColour, start, end, y1,
412 drawnColour = featureColour;
419 if (transparency != 1.0f)
424 ((Graphics2D) g).setComposite(NO_TRANSPARENCY);
431 * Called when alignment in associated view has new/modified features to
432 * discover and display.
436 public void featuresAdded()
441 @SuppressWarnings("unused")
442 private List<SequenceFeature> overlaps = (/** @j2sNative true || */
443 false ? null : new ArrayList<>());
446 * Returns the sequence feature colour rendered at the given column position,
447 * or null if none found. The feature of highest render order (i.e. on top) is
448 * found, subject to both feature type and feature group being visible, and
449 * its colour returned. This method is suitable when no feature transparency
450 * applied (only the topmost visible feature colour is rendered).
452 * Note this method does not check for a gap in the column so would return the
453 * colour for features enclosing a gapped column. Check for gap before calling
454 * if different behaviour is wanted.
458 * Adds a result ArrayList to parameters in order to avoid an unnecessary
459 * construction of that for every pixel checked.
467 private Color findFeatureColour(SequenceI seq, int column)
470 * check for new feature added while processing
475 * inspect features in reverse renderOrder (the last in the array is
476 * displayed on top) until we find one that is rendered at the position
478 for (int renderIndex = renderOrder.length; --renderIndex >= 0;)
480 String type = renderOrder[renderIndex];
481 if (!seq.hasFeatures(type) || !showFeatureOfType(type))
486 if (overlaps != null)
490 List<SequenceFeature> list = seq.findFeatures(column, type, overlaps);
493 for (int i = 0, n = list.size(); i < n; i++)
495 SequenceFeature sf = list.get(i);
496 if (featureGroupNotShown(sf))
500 Color col = getColour(sf);
510 * no displayed feature found at position