416defbda925f1417659c77d8e51c8913f86a5cc
[jalview.git] / src / jalview / renderer / OverviewRenderer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.renderer;
22
23 import jalview.api.AlignmentColsCollectionI;
24 import jalview.api.AlignmentRowsCollectionI;
25 import jalview.api.RendererListenerI;
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.Annotation;
28 import jalview.datamodel.SequenceI;
29 import jalview.renderer.seqfeatures.FeatureColourFinder;
30 import jalview.renderer.seqfeatures.FeatureRenderer;
31 import jalview.viewmodel.OverviewDimensions;
32
33 import java.awt.Color;
34 import java.awt.Graphics;
35 import java.awt.image.BufferedImage;
36 import java.beans.PropertyChangeSupport;
37
38 public class OverviewRenderer
39 {
40   public static final String UPDATE = "OverviewUpdate";
41
42   private static final int MAX_PROGRESS = 100;
43
44   private PropertyChangeSupport changeSupport = new PropertyChangeSupport(
45           this);
46
47   private FeatureColourFinder finder;
48
49   private jalview.api.SequenceRenderer sr;
50
51   // image to render on
52   private BufferedImage miniMe;
53
54   // raw number of pixels to allocate to each column
55   private float pixelsPerCol;
56
57   // raw number of pixels to allocate to each row
58   private float pixelsPerSeq;
59
60   // height in pixels of graph
61   private int graphHeight;
62
63   // flag to indicate whether to halt drawing
64   private volatile boolean redraw = false;
65
66   public OverviewRenderer(jalview.api.SequenceRenderer seqRenderer,
67           FeatureRenderer fr, OverviewDimensions od)
68   {
69     sr = seqRenderer;
70     finder = new FeatureColourFinder(fr);
71
72     pixelsPerCol = od.getPixelsPerCol();
73     pixelsPerSeq = od.getPixelsPerSeq();
74     graphHeight = od.getGraphHeight();
75     miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
76             BufferedImage.TYPE_INT_RGB);
77   }
78
79   /**
80    * Draw alignment rows and columns onto an image
81    * 
82    * @param rit
83    *          Iterator over rows to be drawn
84    * @param cit
85    *          Iterator over columns to be drawn
86    * @return image containing the drawing
87    */
88   public BufferedImage draw(AlignmentRowsCollectionI rows,
89           AlignmentColsCollectionI cols)
90   {
91     int rgbcolor = Color.white.getRGB();
92     int seqIndex = 0;
93     int pixelRow = 0;
94     int alignmentHeight = miniMe.getHeight() - graphHeight;
95     int totalPixels = miniMe.getWidth() * alignmentHeight;
96
97     int lastRowUpdate = 0;
98     changeSupport.firePropertyChange(UPDATE, -1, 0);
99
100     for (int alignmentRow : rows)
101     {
102       if (redraw)
103       {
104         break;
105       }
106
107       // get details of this alignment row
108       boolean hidden = rows.isHidden(alignmentRow);
109       SequenceI seq = rows.getSequence(alignmentRow);
110
111       // calculate where this row extends to in pixels
112       int endRow = Math.min(Math.round((seqIndex + 1) * pixelsPerSeq) - 1,
113               miniMe.getHeight() - 1);
114
115       int colIndex = 0;
116       int pixelCol = 0;
117       for (int alignmentCol : cols)
118       {
119         if (redraw)
120         {
121           break;
122         }
123
124         // calculate where this column extends to in pixels
125         int endCol = Math.min(
126                 Math.round((colIndex + 1) * pixelsPerCol) - 1,
127                 miniMe.getWidth() - 1);
128
129         // don't do expensive colour determination if we're not going to use it
130         // NB this is important to avoid performance issues in the overview
131         // panel
132         if (pixelCol <= endCol)
133         {
134           // determine the colour based on the sequence and column position
135           rgbcolor = getColumnColourFromSequence(seq,
136                   hidden || cols.isHidden(alignmentCol), alignmentCol,
137                   finder);
138
139           // fill in the appropriate number of pixels
140           for (int row = pixelRow; row <= endRow; ++row)
141           {
142             for (int col = pixelCol; col <= endCol; ++col)
143             {
144               miniMe.setRGB(col, row, rgbcolor);
145             }
146           }
147
148           sendProgressUpdate((pixelCol + 1) * (endRow - pixelRow),
149                   totalPixels, lastRowUpdate);
150           pixelCol = endCol + 1;
151         }
152         colIndex++;
153       }
154
155       if (pixelRow != endRow + 1)
156       {
157         lastRowUpdate = sendProgressUpdate(endRow + 1, alignmentHeight, 0);
158         pixelRow = endRow + 1;
159       }
160       seqIndex++;
161     }
162
163     // final update to progress bar if present
164     if (redraw)
165     {
166       sendProgressUpdate(pixelRow - 1, alignmentHeight, 0);
167     }
168     else
169     {
170       sendProgressUpdate(alignmentHeight, miniMe.getHeight(), 0);
171     }
172     return miniMe;
173   }
174
175   /*
176    * Calculate progress update value and fire event
177    */
178   private int sendProgressUpdate(int position, int maximum, int offset)
179   {
180     int newUpdate = offset
181             + Math.round(MAX_PROGRESS * ((float) position / maximum));
182     if (newUpdate > offset)
183     {
184       changeSupport.firePropertyChange(UPDATE, offset, newUpdate);
185       return newUpdate;
186     }
187     return offset;
188   }
189
190   /*
191    * Find the colour of a sequence at a specified column position
192    */
193   private int getColumnColourFromSequence(jalview.datamodel.SequenceI seq,
194           boolean isHidden, int lastcol, FeatureColourFinder fcfinder)
195   {
196     Color color = Color.white;
197
198     if ((seq != null) && (seq.getLength() > lastcol))
199     {
200       color = sr.getResidueColour(seq, lastcol, fcfinder);
201     }
202
203     if (isHidden)
204     {
205       color = color.darker().darker();
206     }
207
208     return color.getRGB();
209   }
210
211   /**
212    * Draw the alignment annotation in the overview panel
213    * 
214    * @param g
215    *          the graphics object to draw on
216    * @param anno
217    *          alignment annotation information
218    * @param charWidth
219    *          alignment character width value
220    * @param y
221    *          y-position for the annotation graph
222    * @param cols
223    *          the collection of columns used in the overview panel
224    */
225   public void drawGraph(Graphics g, AlignmentAnnotation anno, int charWidth,
226           int y, AlignmentColsCollectionI cols)
227   {
228     Annotation[] annotations = anno.annotations;
229     g.setColor(Color.white);
230     g.fillRect(0, 0, miniMe.getWidth(), y);
231
232     int height;
233     int colIndex = 0;
234     int pixelCol = 0;
235     for (int alignmentCol : cols)
236     {
237       if (redraw)
238       {
239         changeSupport.firePropertyChange(UPDATE, MAX_PROGRESS - 1, 0);
240         break;
241       }
242
243       if (alignmentCol >= annotations.length)
244       {
245         break; // no more annotations to draw here
246       }
247       else
248       {
249         int endCol = Math.min(
250                 Math.round((colIndex + 1) * pixelsPerCol) - 1,
251                 miniMe.getWidth() - 1);
252
253         if (annotations[alignmentCol] != null)
254         {
255           if (annotations[alignmentCol].colour == null)
256           {
257             g.setColor(Color.black);
258           }
259           else
260           {
261             g.setColor(annotations[alignmentCol].colour);
262           }
263
264           height = (int) ((annotations[alignmentCol].value / anno.graphMax) * y);
265           if (height > y)
266           {
267             height = y;
268           }
269
270           g.fillRect(pixelCol, y - height, endCol - pixelCol + 1, height);
271         }
272
273         pixelCol = endCol + 1;
274         colIndex++;
275       }
276     }
277     changeSupport.firePropertyChange(UPDATE, MAX_PROGRESS - 1,
278             MAX_PROGRESS);
279   }
280
281   public void setRedraw(boolean b)
282   {
283     synchronized (this)
284     {
285       redraw = b;
286     }
287   }
288
289   public void addPropertyChangeListener(RendererListenerI listener)
290   {
291     changeSupport.addPropertyChangeListener(listener);
292   }
293
294   public void removePropertyChangeListener(RendererListenerI listener)
295   {
296     changeSupport.removePropertyChangeListener(listener);
297   }
298 }