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.AlignmentViewPanel;
26 import jalview.api.RendererListenerI;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.Annotation;
30 import jalview.datamodel.SequenceGroup;
31 import jalview.datamodel.SequenceI;
32 import jalview.renderer.seqfeatures.FeatureColourFinder;
33 import jalview.renderer.seqfeatures.FeatureRenderer;
34 import jalview.util.Platform;
35 import jalview.viewmodel.OverviewDimensions;
37 import java.awt.AlphaComposite;
38 import java.awt.Color;
39 import java.awt.Graphics;
40 import java.awt.Graphics2D;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.awt.image.BufferedImage;
44 import java.awt.image.DataBufferInt;
45 import java.awt.image.WritableRaster;
46 import java.beans.PropertyChangeSupport;
47 import java.util.BitSet;
48 import java.util.Iterator;
50 import javax.swing.Timer;
52 public class OverviewRenderer
54 // transparency of hidden cols/seqs overlay
55 private final float TRANSPARENCY = 0.5f;
57 public static final String UPDATE = "OverviewUpdate";
59 private static final int MAX_PROGRESS = 100;
61 private PropertyChangeSupport changeSupport = new PropertyChangeSupport(
64 private FeatureColourFinder finder;
67 private BufferedImage miniMe;
70 * Number of pixelsPerCol;
72 private float pixelsPerCol;
75 * Number of visible columns per pixel.
78 private float colsPerPixel;
80 // raw number of pixels to allocate to each row
81 private float pixelsPerSeq;
83 // height in pixels of graph
84 private int graphHeight;
86 // flag to indicate whether to halt drawing
87 private volatile boolean redraw = false;
89 // reference to alignment, needed to get sequence groups
90 private AlignmentI al;
92 private ResidueShaderI shader;
94 private OverviewResColourFinder resColFinder;
96 private boolean showProgress;
98 private AlignmentViewPanel panel;
100 private int sequencesHeight;
102 public OverviewRenderer(AlignmentViewPanel panel, FeatureRenderer fr,
103 OverviewDimensions od,
104 AlignmentI alignment,
105 ResidueShaderI resshader, OverviewResColourFinder colFinder)
107 this(panel, fr, od, alignment, resshader, colFinder, true);
110 public OverviewRenderer(AlignmentViewPanel panel,
111 jalview.api.FeatureRenderer fr, OverviewDimensions od,
112 AlignmentI alignment, ResidueShaderI resshader,
113 OverviewResColourFinder colFinder, boolean showProgress)
116 finder = new FeatureColourFinder(fr);
119 resColFinder = colFinder;
120 this.showProgress = showProgress;
124 rows = od.getRows(alignment);
125 cols = od.getColumns(alignment);
126 graphHeight = od.getGraphHeight();
127 alignmentHeight = od.getSequencesHeight();
129 pixelsPerSeq = od.getPixelsPerSeq();
130 pixelsPerCol = od.getPixelsPerCol();
131 colsPerPixel = Math.max(1, 1f / pixelsPerCol);
135 final static int STATE_INIT = 0;
136 final static int STATE_NEXT = 1;
137 final static int STATE_DONE = 2;
141 boolean isJS = Platform.isJS();
144 int delay = (isJS ? 1 : 0);
153 * Draw alignment rows and columns onto an image. This method is asynchronous
154 * in JavaScript and interruptible in Java.
156 * Whether hidden rows or columns are drawn depends upon the type of
159 * Updated to skip through high-density sequences, where columns/pixels > 1.
161 * When the process is complete, the image is passed to the AlignmentViewPanel
162 * provided by the constructor.
165 * collection of rows to be drawn
167 * collection of columns to be drawn
168 * @return image containing the drawing
170 * @author Bob Hanson 2019.07.30
172 public void drawMiniMe()
178 protected void mainLoop()
189 if (!rowIterator.hasNext())
205 // Java will continue without a timeout
212 rowIterator = rows.iterator();
217 totalPixels = w * alignmentHeight;
221 changeSupport.firePropertyChange(UPDATE, -1, 0);
224 miniMe = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
225 WritableRaster raster = miniMe.getRaster();
226 DataBufferInt db = (DataBufferInt) raster.getDataBuffer();
227 Platform.timeCheck(null, Platform.TIME_MARK);
228 pixels = db.getBankData()[0];
229 bscol = cols.getOverviewBitSet();
232 private void nextRow()
234 row = rowIterator.next();
235 // get details of this alignment row
236 SequenceI seq = rows.getSequence(row);
238 // rate limiting step when rendering overview for lots of groups
239 SequenceGroup[] allGroups = al.findAllGroups(seq);
241 // calculate where this row extends to in pixels
242 int endRow = Math.min(Math.round((++seqIndex) * pixelsPerSeq), h);
243 for (int pixelCol = 0, colNext = 0, pixelEnd = 0, icol = bscol
244 .nextSetBit(0); icol >= 0; icol = getNextCol(icol, pixelEnd))
252 pixelEnd = getNextPixel(colNext, colNext);
254 if (pixelCol == pixelEnd)
258 else if (pixelCol < pixelEnd)
260 int rgb = getColumnColourFromSequence(allGroups, seq, icol);
261 // fill in the appropriate number of pixels
262 // System.out.println(
263 // "OR colNext=" + colNext + " " + pixelCol
264 // + "-" + pixelEnd + " icol=" + icol + " " + rgb + " "
266 for (int row = pixelRow; row < endRow; ++row)
268 for (int col = pixelCol; col < pixelEnd; ++col)
270 // BH 2019.07.27 was:
272 // miniMe.setRGB(col, row, rgbcolor);
274 // but just directly writing to the int[] pixel buffer
275 // is three times faster by my experimentation
276 pixels[row * w + col] = rgb;
281 // store last update value
284 lastUpdate = sendProgressUpdate(
285 pixelEnd * (endRow - 1 - pixelRow),
286 totalPixels, lastRowUpdate, lastUpdate);
291 if (pixelRow < endRow)
294 // store row offset and last update value
297 // BH 2019.07.29 was (old) endRow + 1 (now endRow), but should be
298 // pixelRow + 1, surely
299 lastRowUpdate = sendProgressUpdate(endRow, alignmentHeight, 0,
301 lastUpdate = lastRowUpdate;
307 * The next column is either the next set bit (when there are multiple pixels
308 * per column) or the next set bit for the column that aligns with the next
309 * pixel (when there are more columns than pixels).
315 private int getNextCol(int icol, int pixel)
317 return bscol.nextSetBit(
318 pixelsPerCol >= 1 ? icol + 1 : (int) (pixel * colsPerPixel));
321 private int getNextPixel(int icol, int pixel)
324 pixelsPerCol >= 1 || pixel == 0
325 ? Math.round(icol * pixelsPerCol)
330 private boolean loop()
338 timer = new Timer(delay, new ActionListener()
341 public void actionPerformed(ActionEvent e)
347 timer.setRepeats(false);
360 "overviewrender " + ndone + " pixels row:" + row + " redraw:"
364 overlayHiddenRegions();
367 // final update to progress bar if present
371 // BH was pixelRow - 1, but that could go negative
372 sendProgressUpdate(pixelRow, alignmentHeight, 0, 0);
376 // sendProgressUpdate(alignmentHeight, miniMe.getHeight(), 0, 0);
377 sendProgressUpdate(1, 1, 0, 0);
380 panel.overviewDone(miniMe);
385 private AlignmentRowsCollectionI rows;
387 private AlignmentColsCollectionI cols;
389 Iterator<Integer> rowIterator;
406 * Calculate progress update value and fire event
407 * @param rowOffset number of rows to offset calculation by
408 * @return new rowOffset - return value only to be used when at end of a row
410 private int sendProgressUpdate(int position, int maximum, int rowOffset,
413 int newUpdate = rowOffset
414 + Math.round(MAX_PROGRESS * ((float) position / maximum));
415 if (newUpdate > lastUpdate)
417 changeSupport.firePropertyChange(UPDATE, rowOffset, newUpdate);
424 * Find the RGB value of the colour of a sequence at a specified column position
427 * sequence to get colour for
429 * column position to get colour for
430 * @return colour of sequence at this position, as RGB
432 int getColumnColourFromSequence(SequenceGroup[] allGroups, SequenceI seq,
435 return (seq == null || icol >= seq.getLength()
436 ? resColFinder.GAP_COLOUR
437 : resColFinder.getResidueColourInt(true, shader, allGroups, seq,
442 * Overlay the hidden regions on the overview image
445 private void overlayHiddenRegions()
447 if (cols.hasHidden() || rows.hasHidden())
449 BufferedImage mask = buildHiddenImage();
451 Graphics2D g = (Graphics2D) miniMe.getGraphics();
452 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
454 g.drawImage(mask, 0, 0, miniMe.getWidth(), miniMe.getHeight(), null);
460 * Build a masking image of hidden columns and rows to be applied on top of
461 * the main overview image.
464 * collection of rows the overview is built over
466 * collection of columns the overview is built over
468 * width of overview in pixels
470 * height of overview in pixels
471 * @return BufferedImage containing mask of hidden regions
473 private BufferedImage buildHiddenImage()
476 BufferedImage hiddenImage = new BufferedImage(w, h,
477 BufferedImage.TYPE_INT_ARGB);
479 Color hidden = resColFinder.getHiddenColour();
481 Graphics2D g2d = (Graphics2D) hiddenImage.getGraphics();
483 g2d.setColor(hidden);
484 // set background to transparent
485 // g2d.setComposite(AlphaComposite.Clear);
486 // g2d.fillRect(0, 0, width, height);
488 // set next colour to opaque
489 g2d.setComposite(AlphaComposite.Src);
491 // System.out.println(cols.getClass().getName());
492 if (cols.hasHidden())
494 // AllColsCollection only
495 BitSet bs = cols.getHiddenBitSet();
496 for (int pixelCol = -1, icol2 = 0, icol = bs
497 .nextSetBit(0); icol >= 0; icol = bs.nextSetBit(icol2))
503 icol2 = bs.nextClearBit(icol + 1);
504 int pixelEnd = getNextPixel(icol2, 0);
505 if (pixelEnd > pixelCol)
507 pixelCol = getNextPixel(icol, 0);
508 g2d.fillRect(pixelCol, 0, Math.max(1, pixelEnd - pixelCol),
514 if (rows.hasHidden())
518 for (int alignmentRow : rows)
525 // calculate where this row extends to in pixels
526 int endRow = Math.min(Math.round((++seqIndex) * pixelsPerSeq),
529 // get details of this alignment row
530 if (rows.isHidden(alignmentRow))
532 g2d.fillRect(0, pixelRow, w, endRow - 1 - pixelRow);
542 * Draw the alignment annotation in the overview panel
545 * alignment annotation information
547 public void drawGraph(AlignmentAnnotation anno)
550 Graphics g = miniMe.getGraphics();
551 g.translate(0, alignmentHeight);
553 Annotation[] annotations = anno.annotations;
554 float max = anno.graphMax;
555 g.setColor(Color.white);
556 g.fillRect(0, 0, w, y);
558 for (int pixelCol = 0, colNext = 0, pixelEnd = 0, len = annotations.length, icol = bscol
559 .nextSetBit(0); icol >= 0
560 && icol < len; icol = getNextCol(icol, pixelEnd))
566 changeSupport.firePropertyChange(UPDATE, MAX_PROGRESS - 1, 0);
572 pixelEnd = getNextPixel(colNext, colNext);
573 Annotation ann = annotations[icol];
576 Color color = ann.colour;
577 g.setColor(color == null ? Color.black : color);
578 int height = Math.min(y, (int) ((ann.value / max) * y));
579 g.fillRect(pixelCol, y - height, Math.max(1, pixelEnd - pixelCol),
585 g.translate(0, -alignmentHeight);
590 changeSupport.firePropertyChange(UPDATE, MAX_PROGRESS - 1,
597 * Allows redraw flag to be set
600 * value to set redraw to: true = redraw is occurring, false = no
603 public void setRedraw(boolean b)
611 public void addPropertyChangeListener(RendererListenerI listener)
613 changeSupport.addPropertyChangeListener(listener);
616 public void removePropertyChangeListener(RendererListenerI listener)
618 changeSupport.removePropertyChangeListener(listener);