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;
23 import jalview.api.AlignmentColsCollectionI;
24 import jalview.api.AlignmentRowsCollectionI;
25 import jalview.api.RendererListenerI;
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.Annotation;
29 import jalview.datamodel.SequenceGroup;
30 import jalview.datamodel.SequenceI;
31 import jalview.renderer.seqfeatures.FeatureColourFinder;
32 import jalview.renderer.seqfeatures.FeatureRenderer;
33 import jalview.viewmodel.OverviewDimensions;
35 import java.awt.AlphaComposite;
36 import java.awt.Color;
37 import java.awt.Graphics;
38 import java.awt.Graphics2D;
39 import java.awt.image.BufferedImage;
40 import java.beans.PropertyChangeSupport;
42 public class OverviewRenderer
44 // transparency of hidden cols/seqs overlay
45 private final float TRANSPARENCY = 0.5f;
47 public static final String UPDATE = "OverviewUpdate";
49 private static final int MAX_PROGRESS = 100;
51 private PropertyChangeSupport changeSupport = new PropertyChangeSupport(
54 private FeatureColourFinder finder;
57 private BufferedImage miniMe;
59 // raw number of pixels to allocate to each column
60 private float pixelsPerCol;
62 // raw number of pixels to allocate to each row
63 private float pixelsPerSeq;
65 // height in pixels of graph
66 private int graphHeight;
68 // flag to indicate whether to halt drawing
69 private volatile boolean redraw = false;
71 // reference to alignment, needed to get sequence groups
72 private AlignmentI al;
74 private ResidueShaderI shader;
76 private OverviewResColourFinder resColFinder;
78 public OverviewRenderer(FeatureRenderer fr, OverviewDimensions od,
80 ResidueShaderI resshader, OverviewResColourFinder colFinder)
82 finder = fr.getViewport().isShowSequenceFeatures() ?
83 new FeatureColourFinder(fr) : null;
84 resColFinder = colFinder;
89 pixelsPerCol = od.getPixelsPerCol();
90 pixelsPerSeq = od.getPixelsPerSeq();
91 graphHeight = od.getGraphHeight();
92 miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
93 BufferedImage.TYPE_INT_RGB);
97 * Draw alignment rows and columns onto an image
100 * Iterator over rows to be drawn
102 * Iterator over columns to be drawn
103 * @return image containing the drawing
105 public BufferedImage draw(AlignmentRowsCollectionI rows,
106 AlignmentColsCollectionI cols)
108 int rgbcolor = Color.white.getRGB();
111 int alignmentHeight = miniMe.getHeight() - graphHeight;
112 int totalPixels = miniMe.getWidth() * alignmentHeight;
114 int lastRowUpdate = 0;
116 changeSupport.firePropertyChange(UPDATE, -1, 0);
118 for (int alignmentRow : rows)
125 // get details of this alignment row
126 SequenceI seq = rows.getSequence(alignmentRow);
128 // rate limiting step when rendering overview for lots of groups
129 SequenceGroup[] allGroups = al.findAllGroups(seq);
131 // calculate where this row extends to in pixels
132 int endRow = Math.min(Math.round((seqIndex + 1) * pixelsPerSeq) - 1,
133 miniMe.getHeight() - 1);
137 for (int alignmentCol : cols)
144 // calculate where this column extends to in pixels
145 int endCol = Math.min(Math.round((colIndex + 1) * pixelsPerCol) - 1,
146 miniMe.getWidth() - 1);
148 // don't do expensive colour determination if we're not going to use it
149 // NB this is important to avoid performance issues in the overview
151 if (pixelCol <= endCol)
153 rgbcolor = getColumnColourFromSequence(allGroups, seq,
156 // fill in the appropriate number of pixels
157 for (int row = pixelRow; row <= endRow; ++row)
159 for (int col = pixelCol; col <= endCol; ++col)
161 miniMe.setRGB(col, row, rgbcolor);
165 // store last update value
166 lastUpdate = sendProgressUpdate(
167 (pixelCol + 1) * (endRow - pixelRow), totalPixels,
168 lastRowUpdate, lastUpdate);
170 pixelCol = endCol + 1;
175 if (pixelRow != endRow + 1)
177 // store row offset and last update value
178 lastRowUpdate = sendProgressUpdate(endRow + 1, alignmentHeight, 0,
180 lastUpdate = lastRowUpdate;
181 pixelRow = endRow + 1;
186 overlayHiddenRegions(rows, cols);
187 // final update to progress bar if present
190 sendProgressUpdate(pixelRow - 1, alignmentHeight, 0, 0);
194 sendProgressUpdate(alignmentHeight, miniMe.getHeight(), 0, 0);
200 * Calculate progress update value and fire event
201 * @param rowOffset number of rows to offset calculation by
202 * @return new rowOffset - return value only to be used when at end of a row
204 private int sendProgressUpdate(int position, int maximum, int rowOffset,
207 int newUpdate = rowOffset
208 + Math.round(MAX_PROGRESS * ((float) position / maximum));
209 if (newUpdate > lastUpdate)
211 changeSupport.firePropertyChange(UPDATE, rowOffset, newUpdate);
218 * Find the RGB value of the colour of a sequence at a specified column position
221 * sequence to get colour for
223 * column position to get colour for
224 * @return colour of sequence at this position, as RGB
226 int getColumnColourFromSequence(SequenceGroup[] allGroups,
227 SequenceI seq, int lastcol)
229 Color color = resColFinder.GAP_COLOUR;
231 if ((seq != null) && (seq.getLength() > lastcol))
233 color = resColFinder.getResidueColour(true, shader, allGroups, seq,
237 return color.getRGB();
241 * Overlay the hidden regions on the overview image
244 * collection of rows the overview is built over
246 * collection of columns the overview is built over
248 private void overlayHiddenRegions(AlignmentRowsCollectionI rows,
249 AlignmentColsCollectionI cols)
251 if (cols.hasHidden() || rows.hasHidden())
253 BufferedImage mask = buildHiddenImage(rows, cols, miniMe.getWidth(),
256 Graphics2D g = (Graphics2D) miniMe.getGraphics();
257 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
259 g.drawImage(mask, 0, 0, miniMe.getWidth(), miniMe.getHeight(), null);
264 * Build a masking image of hidden columns and rows to be applied on top of
265 * the main overview image.
268 * collection of rows the overview is built over
270 * collection of columns the overview is built over
272 * width of overview in pixels
274 * height of overview in pixels
275 * @return BufferedImage containing mask of hidden regions
277 private BufferedImage buildHiddenImage(AlignmentRowsCollectionI rows,
278 AlignmentColsCollectionI cols, int width, int height)
281 BufferedImage hiddenImage = new BufferedImage(width, height,
282 BufferedImage.TYPE_INT_ARGB);
287 Color hidden = resColFinder.getHiddenColour();
289 Graphics2D g2d = (Graphics2D) hiddenImage.getGraphics();
291 // set background to transparent
292 g2d.setComposite(AlphaComposite.Clear);
293 g2d.fillRect(0, 0, width, height);
295 // set next colour to opaque
296 g2d.setComposite(AlphaComposite.Src);
298 for (int alignmentCol : cols)
305 // calculate where this column extends to in pixels
306 int endCol = Math.min(Math.round((colIndex + 1) * pixelsPerCol) - 1,
307 hiddenImage.getWidth() - 1);
309 if (pixelCol <= endCol)
311 // determine the colour based on the sequence and column position
312 if (cols.isHidden(alignmentCol))
314 g2d.setColor(hidden);
315 g2d.fillRect(pixelCol, 0, endCol - pixelCol + 1, height);
318 pixelCol = endCol + 1;
326 for (int alignmentRow : rows)
333 // calculate where this row extends to in pixels
334 int endRow = Math.min(Math.round((seqIndex + 1) * pixelsPerSeq) - 1,
335 miniMe.getHeight() - 1);
337 // get details of this alignment row
338 if (rows.isHidden(alignmentRow))
340 g2d.setColor(hidden);
341 g2d.fillRect(0, pixelRow, width, endRow - pixelRow + 1);
343 pixelRow = endRow + 1;
351 * Draw the alignment annotation in the overview panel
354 * the graphics object to draw on
356 * alignment annotation information
358 * y-position for the annotation graph
360 * the collection of columns used in the overview panel
362 public void drawGraph(Graphics g, AlignmentAnnotation anno, int y,
363 AlignmentColsCollectionI cols)
365 Annotation[] annotations = anno.annotations;
366 g.setColor(Color.white);
367 g.fillRect(0, 0, miniMe.getWidth(), y);
372 for (int alignmentCol : cols)
376 changeSupport.firePropertyChange(UPDATE, MAX_PROGRESS - 1, 0);
380 if (alignmentCol >= annotations.length)
382 break; // no more annotations to draw here
386 int endCol = Math.min(Math.round((colIndex + 1) * pixelsPerCol) - 1,
387 miniMe.getWidth() - 1);
389 if (annotations[alignmentCol] != null)
391 if (annotations[alignmentCol].colour == null)
393 g.setColor(Color.black);
397 g.setColor(annotations[alignmentCol].colour);
400 height = (int) ((annotations[alignmentCol].value / anno.graphMax)
407 g.fillRect(pixelCol, y - height, endCol - pixelCol + 1, height);
410 pixelCol = endCol + 1;
414 changeSupport.firePropertyChange(UPDATE, MAX_PROGRESS - 1,
419 * Allows redraw flag to be set
422 * value to set redraw to: true = redraw is occurring, false = no
425 public void setRedraw(boolean b)
433 public void addPropertyChangeListener(RendererListenerI listener)
435 changeSupport.addPropertyChangeListener(listener);
438 public void removePropertyChangeListener(RendererListenerI listener)
440 changeSupport.removePropertyChangeListener(listener);