/*
* Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
* Copyright (C) $$Year-Rel$$ The Jalview Authors
*
* This file is part of Jalview.
*
* Jalview is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* Jalview is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Jalview. If not, see .
* The Jalview Authors are detailed in the 'AUTHORS' file.
*/
package jalview.renderer;
import jalview.api.AlignmentColsCollectionI;
import jalview.api.AlignmentRowsCollectionI;
import jalview.api.AlignmentViewPanel;
import jalview.api.RendererListenerI;
import jalview.datamodel.AlignmentAnnotation;
import jalview.datamodel.AlignmentI;
import jalview.datamodel.Annotation;
import jalview.datamodel.SequenceGroup;
import jalview.datamodel.SequenceI;
import jalview.renderer.seqfeatures.FeatureColourFinder;
import jalview.renderer.seqfeatures.FeatureRenderer;
import jalview.util.Platform;
import jalview.viewmodel.OverviewDimensions;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.awt.image.WritableRaster;
import java.beans.PropertyChangeSupport;
import java.util.BitSet;
import java.util.Iterator;
import javax.swing.Timer;
public class OverviewRenderer
{
// transparency of hidden cols/seqs overlay
private final float TRANSPARENCY = 0.5f;
public static final String UPDATE = "OverviewUpdate";
private static final int MAX_PROGRESS = 100;
private PropertyChangeSupport changeSupport = new PropertyChangeSupport(
this);
private FeatureColourFinder finder;
// image to render on
private BufferedImage miniMe;
// raw number of pixels to allocate to each column
private float pixelsPerCol;
// raw number of pixels to allocate to each row
private float pixelsPerSeq;
// height in pixels of graph
private int graphHeight;
// flag to indicate whether to halt drawing
private volatile boolean redraw = false;
// reference to alignment, needed to get sequence groups
private AlignmentI al;
private ResidueShaderI shader;
private OverviewResColourFinder resColFinder;
private boolean showProgress;
private AlignmentViewPanel panel;
public OverviewRenderer(AlignmentViewPanel panel, FeatureRenderer fr,
OverviewDimensions od,
AlignmentI alignment,
ResidueShaderI resshader, OverviewResColourFinder colFinder)
{
this(panel, fr, od, alignment, resshader, colFinder, true);
}
public OverviewRenderer(AlignmentViewPanel panel,
jalview.api.FeatureRenderer fr,
OverviewDimensions od,
AlignmentI alignment, ResidueShaderI resshader,
OverviewResColourFinder colFinder, boolean showProgress)
{
this.panel = panel;
finder = new FeatureColourFinder(fr);
resColFinder = colFinder;
al = alignment;
shader = resshader;
pixelsPerCol = od.getPixelsPerCol();
pixelsPerSeq = od.getPixelsPerSeq();
graphHeight = od.getGraphHeight();
miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
BufferedImage.TYPE_INT_RGB);
this.showProgress = showProgress;
}
final static int STATE_INIT = 0;
final static int STATE_NEXT = 1;
final static int STATE_DONE = 2;
int state;
boolean isJS = Platform.isJS();
Timer timer;
int delay = (isJS ? 1 : 0);
int seqIndex;
int pixelRow;
private Integer row;
void mainLoop()
{
while (!redraw)
{
switch (state)
{
case STATE_INIT:
seqIndex = 0;
pixelRow = 0;
state = STATE_NEXT;
continue;
case STATE_NEXT:
if (iter.hasNext())
{
nextRow();
}
else
{
state = STATE_DONE;
}
break;
case STATE_DONE:
done();
return;
}
if (delay > 0)
{
jsloop();
return;
}
}
done();
}
private void jsloop()
{
if (timer == null)
{
timer = new Timer(delay, new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
mainLoop();
}
});
timer.setRepeats(false);
timer.start();
}
else
{
timer.restart();
}
}
private void nextRow()
{
row = iter.next();
// System.out.println("OR row " + r);
// get details of this alignment row
SequenceI seq = rows.getSequence(row);
// rate limiting step when rendering overview for lots of groups
SequenceGroup[] allGroups = al.findAllGroups(seq);
// calculate where this row extends to in pixels
int endRow = Math.min(Math.round((++seqIndex) * pixelsPerSeq), h);
for (int pixelCol = 0, colIndex = 0, c = bscol
.nextSetBit(0); c >= 0; c = bscol.nextSetBit(c + 1))
{
if (redraw)
{
break;
}
// calculate where this column extends to in pixels
int endCol = Math.min(Math.round((++colIndex) * pixelsPerCol), w);
// don't do expensive colour determination if we're not going to use it
// NB this is important to avoid performance issues in the overview
// panel
if (pixelCol < endCol)
{
// System.out.println("OR pc ec " + pixelCol + " " + endCol);
int rgb = getColumnColourFromSequence(allGroups, seq, c);
// fill in the appropriate number of pixels
for (int row = pixelRow; row < endRow; ++row)
{
for (int col = pixelCol; col < endCol; ++col)
{
// BH 2019.07.27 was:
//
// miniMe.setRGB(col, row, rgbcolor);
//
// but just directly writing to the int[] pixel buffer
// is three times faster by my experimentation
pixels[row * w + col] = rgb;
ndone++;
}
}
// }
pixelCol = endCol;
// store last update value
if (showProgress)
{
lastUpdate = sendProgressUpdate(endCol * (endRow - 1 - pixelRow),
totalPixels, lastRowUpdate, lastUpdate);
}
}
}
if (pixelRow < endRow)
{
pixelRow = endRow;
// store row offset and last update value
if (showProgress)
{
// BH 2019.07.29 was (old) endRow + 1 (now endRow), but should be
// pixelRow + 1, surely
lastRowUpdate = sendProgressUpdate(endRow, alignmentHeight, 0,
lastUpdate);
lastUpdate = lastRowUpdate;
}
}
}
private void done()
{
Platform.timeCheck(
"overviewrender " + ndone + " pixels row:" + row + " redraw:"
+ redraw,
Platform.TIME_MARK);
overlayHiddenRegions(rows, cols);
if (showProgress)
{
// final update to progress bar if present
if (redraw)
{
// aborted in Java
// BH was pixelRow - 1, but that could go negative
sendProgressUpdate(pixelRow, alignmentHeight, 0, 0);
}
else
{
// sendProgressUpdate(alignmentHeight, miniMe.getHeight(), 0, 0);
sendProgressUpdate(1, 1, 0, 0);
}
}
panel.overviewDone(miniMe);
}
int ndone = 0;
private AlignmentRowsCollectionI rows;
private AlignmentColsCollectionI cols;
Iterator iter;
int alignmentHeight;
int totalPixels;
int lastRowUpdate;
int lastUpdate;
int[] pixels;
BitSet bscol = new BitSet();
int w, h;
/**
* Draw alignment rows and columns onto an image
*
* @param rit
* Iterator over rows to be drawn
* @param cit
* Iterator over columns to be drawn
* @return image containing the drawing
*/
public BufferedImage draw(AlignmentRowsCollectionI rows,
AlignmentColsCollectionI cols)
{
this.rows = rows;
this.cols = cols;
iter = rows.iterator();
w = miniMe.getWidth();
h = miniMe.getHeight();
alignmentHeight = h - graphHeight;
totalPixels = w * alignmentHeight;
lastRowUpdate = 0;
lastUpdate = 0;
if (showProgress)
{
changeSupport.firePropertyChange(UPDATE, -1, 0);
}
WritableRaster raster = miniMe.getRaster();
DataBufferInt db = (DataBufferInt) raster.getDataBuffer();
Platform.timeCheck(null, Platform.TIME_MARK);
pixels = db.getBankData()[0];
bscol.clear();
for (int c : cols)
{
bscol.set(c);
}
state = STATE_INIT;
mainLoop();
return miniMe;
}
/*
* Calculate progress update value and fire event
* @param rowOffset number of rows to offset calculation by
* @return new rowOffset - return value only to be used when at end of a row
*/
private int sendProgressUpdate(int position, int maximum, int rowOffset,
int lastUpdate)
{
int newUpdate = rowOffset
+ Math.round(MAX_PROGRESS * ((float) position / maximum));
if (newUpdate > lastUpdate)
{
changeSupport.firePropertyChange(UPDATE, rowOffset, newUpdate);
return newUpdate;
}
return newUpdate;
}
/*
* Find the RGB value of the colour of a sequence at a specified column position
*
* @param seq
* sequence to get colour for
* @param lastcol
* column position to get colour for
* @return colour of sequence at this position, as RGB
*/
int getColumnColourFromSequence(SequenceGroup[] allGroups, SequenceI seq,
int icol)
{
return (seq == null || icol >= seq.getLength()
? resColFinder.GAP_COLOUR
: resColFinder.getResidueColour(true, shader, allGroups, seq,
icol, finder)).getRGB();
}
/**
* Overlay the hidden regions on the overview image
*
* @param rows
* collection of rows the overview is built over
* @param cols
* collection of columns the overview is built over
*/
private void overlayHiddenRegions(AlignmentRowsCollectionI rows,
AlignmentColsCollectionI cols)
{
if (cols.hasHidden() || rows.hasHidden())
{
BufferedImage mask = buildHiddenImage(rows, cols, miniMe.getWidth(),
miniMe.getHeight());
Graphics2D g = (Graphics2D) miniMe.getGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
TRANSPARENCY));
g.drawImage(mask, 0, 0, miniMe.getWidth(), miniMe.getHeight(), null);
g.dispose();
}
}
/**
* Build a masking image of hidden columns and rows to be applied on top of
* the main overview image.
*
* @param rows
* collection of rows the overview is built over
* @param cols
* collection of columns the overview is built over
* @param width
* width of overview in pixels
* @param height
* height of overview in pixels
* @return BufferedImage containing mask of hidden regions
*/
private BufferedImage buildHiddenImage(AlignmentRowsCollectionI rows,
AlignmentColsCollectionI cols, int width, int height)
{
// new masking image
BufferedImage hiddenImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
Color hidden = resColFinder.getHiddenColour();
Graphics2D g2d = (Graphics2D) hiddenImage.getGraphics();
// set background to transparent
// g2d.setComposite(AlphaComposite.Clear);
// g2d.fillRect(0, 0, width, height);
// set next colour to opaque
g2d.setComposite(AlphaComposite.Src);
if (cols.hasHidden())
{
int colIndex = 0;
int pixelCol = 0;
for (int alignmentCol : cols)
{
if (redraw)
{
break;
}
// calculate where this column extends to in pixels
int endCol = Math.min(Math.round((++colIndex) * pixelsPerCol),
width);
// endCol is one more than old endCol
if (pixelCol < endCol)
{
// determine the colour based on the sequence and column position
if (cols.isHidden(alignmentCol))
{
g2d.setColor(hidden);
g2d.fillRect(pixelCol, 0, endCol - pixelCol, height);
}
pixelCol = endCol;
}
}
}
if (rows.hasHidden())
{
int seqIndex = 0;
int pixelRow = 0;
for (int alignmentRow : rows)
{
if (redraw)
{
break;
}
// calculate where this row extends to in pixels
int endRow = Math.min(Math.round((++seqIndex) * pixelsPerSeq),
height);
// get details of this alignment row
if (rows.isHidden(alignmentRow))
{
g2d.setColor(hidden);
g2d.fillRect(0, pixelRow, width, endRow - 1 - pixelRow);
}
pixelRow = endRow;
}
}
g2d.dispose();
return hiddenImage;
}
/**
* Draw the alignment annotation in the overview panel
*
* @param g
* the graphics object to draw on
* @param anno
* alignment annotation information
* @param y
* y-position for the annotation graph
* @param cols
* the collection of columns used in the overview panel
*/
public void drawGraph(Graphics g, AlignmentAnnotation anno, int y,
AlignmentColsCollectionI cols)
{
Annotation[] annotations = anno.annotations;
float max = anno.graphMax;
g.setColor(Color.white);
int width = miniMe.getWidth();
g.fillRect(0, 0, width, y);
int colIndex = 0;
int pixelCol = 0;
for (int icol : cols)
{
if (redraw)
{
if (showProgress)
{
changeSupport.firePropertyChange(UPDATE, MAX_PROGRESS - 1, 0);
}
break;
}
if (icol >= annotations.length)
{
break; // no more annotations to draw here
}
int endCol = Math.min(Math.round((++colIndex) * pixelsPerCol), width);
Annotation ann = annotations[icol];
if (ann != null)
{
Color color = ann.colour;
g.setColor(color == null ? Color.black : color);
int height = Math.min(y, (int) ((ann.value / max) * y));
g.fillRect(pixelCol, y - height, endCol - pixelCol, height);
}
pixelCol = endCol;
}
if (showProgress)
{
changeSupport.firePropertyChange(UPDATE, MAX_PROGRESS - 1,
MAX_PROGRESS);
}
}
/**
* Allows redraw flag to be set
*
* @param b
* value to set redraw to: true = redraw is occurring, false = no
* redraw
*/
public void setRedraw(boolean b)
{
synchronized (this)
{
redraw = b;
}
}
public void addPropertyChangeListener(RendererListenerI listener)
{
changeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(RendererListenerI listener)
{
changeSupport.removePropertyChangeListener(listener);
}
}