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())
203 // Java will continue without a timeout
210 rowIterator = rows.iterator();
215 totalPixels = w * alignmentHeight;
219 changeSupport.firePropertyChange(UPDATE, -1, 0);
222 miniMe = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
223 WritableRaster raster = miniMe.getRaster();
224 DataBufferInt db = (DataBufferInt) raster.getDataBuffer();
225 Platform.timeCheck(null, Platform.TIME_MARK);
226 pixels = db.getBankData()[0];
227 bscol = cols.getOverviewBitSet();
230 private void nextRow()
232 row = rowIterator.next();
233 // System.out.println("OR row " + r);
234 // get details of this alignment row
235 SequenceI seq = rows.getSequence(row);
237 // rate limiting step when rendering overview for lots of groups
238 SequenceGroup[] allGroups = al.findAllGroups(seq);
240 // calculate where this row extends to in pixels
241 int endRow = Math.min(Math.round((++seqIndex) * pixelsPerSeq), h);
242 for (int pixelCol = 0, colNext = 0, pixelEnd = 0, icol = bscol
243 .nextSetBit(0); icol >= 0; icol = getNextCol(icol, pixelEnd))
251 pixelEnd = getNextPixel(colNext, colNext);
253 if (pixelCol == pixelEnd)
257 else if (pixelCol < pixelEnd)
259 int rgb = getColumnColourFromSequence(allGroups, seq, icol);
260 // fill in the appropriate number of pixels
261 // System.out.println(
262 // "OR colNext=" + colNext + " " + pixelCol
263 // + "-" + pixelEnd + " icol=" + icol + " " + rgb + " "
265 for (int row = pixelRow; row < endRow; ++row)
267 for (int col = pixelCol; col < pixelEnd; ++col)
269 // BH 2019.07.27 was:
271 // miniMe.setRGB(col, row, rgbcolor);
273 // but just directly writing to the int[] pixel buffer
274 // is three times faster by my experimentation
275 pixels[row * w + col] = rgb;
280 // store last update value
283 lastUpdate = sendProgressUpdate(
284 pixelEnd * (endRow - 1 - pixelRow),
285 totalPixels, lastRowUpdate, lastUpdate);
290 if (pixelRow < endRow)
293 // store row offset and last update value
296 // BH 2019.07.29 was (old) endRow + 1 (now endRow), but should be
297 // pixelRow + 1, surely
298 lastRowUpdate = sendProgressUpdate(endRow, alignmentHeight, 0,
300 lastUpdate = lastRowUpdate;
306 * The next column is either the next set bit (when there are multiple pixels
307 * per column) or the next set bit for the column that aligns with the next
308 * pixel (when there are more columns than pixels).
314 private int getNextCol(int icol, int pixel)
316 return bscol.nextSetBit(
317 pixelsPerCol >= 1 ? icol + 1 : (int) (pixel * colsPerPixel));
320 private int getNextPixel(int icol, int pixel)
323 pixelsPerCol >= 1 || pixel == 0
324 ? Math.round(icol * pixelsPerCol)
329 private boolean loop()
337 timer = new Timer(delay, new ActionListener()
340 public void actionPerformed(ActionEvent e)
346 timer.setRepeats(false);
359 "overviewrender " + ndone + " pixels row:" + row + " redraw:"
363 overlayHiddenRegions();
366 // final update to progress bar if present
370 // BH was pixelRow - 1, but that could go negative
371 sendProgressUpdate(pixelRow, alignmentHeight, 0, 0);
375 // sendProgressUpdate(alignmentHeight, miniMe.getHeight(), 0, 0);
376 sendProgressUpdate(1, 1, 0, 0);
379 panel.overviewDone(miniMe);
384 private AlignmentRowsCollectionI rows;
386 private AlignmentColsCollectionI cols;
388 Iterator<Integer> rowIterator;
405 * Calculate progress update value and fire event
406 * @param rowOffset number of rows to offset calculation by
407 * @return new rowOffset - return value only to be used when at end of a row
409 private int sendProgressUpdate(int position, int maximum, int rowOffset,
412 int newUpdate = rowOffset
413 + Math.round(MAX_PROGRESS * ((float) position / maximum));
414 if (newUpdate > lastUpdate)
416 changeSupport.firePropertyChange(UPDATE, rowOffset, newUpdate);
423 * Find the RGB value of the colour of a sequence at a specified column position
426 * sequence to get colour for
428 * column position to get colour for
429 * @return colour of sequence at this position, as RGB
431 int getColumnColourFromSequence(SequenceGroup[] allGroups, SequenceI seq,
434 return (seq == null || icol >= seq.getLength()
435 ? resColFinder.GAP_COLOUR
436 : resColFinder.getResidueColourInt(true, shader, allGroups, seq,
441 * Overlay the hidden regions on the overview image
444 private void overlayHiddenRegions()
446 if (cols.hasHidden() || rows.hasHidden())
448 BufferedImage mask = buildHiddenImage();
450 Graphics2D g = (Graphics2D) miniMe.getGraphics();
451 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
453 g.drawImage(mask, 0, 0, miniMe.getWidth(), miniMe.getHeight(), null);
459 * Build a masking image of hidden columns and rows to be applied on top of
460 * the main overview image.
463 * collection of rows the overview is built over
465 * collection of columns the overview is built over
467 * width of overview in pixels
469 * height of overview in pixels
470 * @return BufferedImage containing mask of hidden regions
472 private BufferedImage buildHiddenImage()
475 BufferedImage hiddenImage = new BufferedImage(w, h,
476 BufferedImage.TYPE_INT_ARGB);
478 Color hidden = resColFinder.getHiddenColour();
480 Graphics2D g2d = (Graphics2D) hiddenImage.getGraphics();
482 g2d.setColor(hidden);
483 // set background to transparent
484 // g2d.setComposite(AlphaComposite.Clear);
485 // g2d.fillRect(0, 0, width, height);
487 // set next colour to opaque
488 g2d.setComposite(AlphaComposite.Src);
490 // System.out.println(cols.getClass().getName());
491 if (cols.hasHidden())
493 // AllColsCollection only
494 BitSet bs = cols.getHiddenBitSet();
495 for (int pixelCol = -1, icol2 = 0, icol = bs
496 .nextSetBit(0); icol >= 0; icol = bs.nextSetBit(icol2))
502 icol2 = bs.nextClearBit(icol + 1);
503 int pixelEnd = getNextPixel(icol2, 0);
504 if (pixelEnd > pixelCol)
506 pixelCol = getNextPixel(icol, 0);
507 g2d.fillRect(pixelCol, 0, Math.max(1, pixelEnd - pixelCol),
513 if (rows.hasHidden())
517 for (int alignmentRow : rows)
524 // calculate where this row extends to in pixels
525 int endRow = Math.min(Math.round((++seqIndex) * pixelsPerSeq),
528 // get details of this alignment row
529 if (rows.isHidden(alignmentRow))
531 g2d.fillRect(0, pixelRow, w, endRow - 1 - pixelRow);
541 * Draw the alignment annotation in the overview panel
544 * alignment annotation information
546 public void drawGraph(AlignmentAnnotation anno)
549 Graphics g = miniMe.getGraphics();
550 g.translate(0, alignmentHeight);
552 Annotation[] annotations = anno.annotations;
553 float max = anno.graphMax;
554 g.setColor(Color.white);
555 g.fillRect(0, 0, w, y);
557 for (int pixelCol = 0, colNext = 0, pixelEnd = 0, len = annotations.length, icol = bscol
558 .nextSetBit(0); icol >= 0
559 && icol < len; icol = getNextCol(icol, pixelEnd))
565 changeSupport.firePropertyChange(UPDATE, MAX_PROGRESS - 1, 0);
571 pixelEnd = getNextPixel(colNext, colNext);
572 Annotation ann = annotations[icol];
575 Color color = ann.colour;
576 g.setColor(color == null ? Color.black : color);
577 int height = Math.min(y, (int) ((ann.value / max) * y));
578 g.fillRect(pixelCol, y - height, Math.max(1, pixelEnd - pixelCol),
584 g.translate(0, -alignmentHeight);
589 changeSupport.firePropertyChange(UPDATE, MAX_PROGRESS - 1,
596 * Allows redraw flag to be set
599 * value to set redraw to: true = redraw is occurring, false = no
602 public void setRedraw(boolean b)
610 public void addPropertyChangeListener(RendererListenerI listener)
612 changeSupport.addPropertyChangeListener(listener);
615 public void removePropertyChangeListener(RendererListenerI listener)
617 changeSupport.removePropertyChangeListener(listener);