Merge branch 'feature/2588' into merge/develop_feature/2588_JAL-2588
[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.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.Annotation;
28 import jalview.datamodel.SequenceGroup;
29 import jalview.datamodel.SequenceI;
30 import jalview.renderer.seqfeatures.FeatureColourFinder;
31 import jalview.renderer.seqfeatures.FeatureRenderer;
32 import jalview.viewmodel.OverviewDimensions;
33
34 import java.awt.Color;
35 import java.awt.Graphics;
36 import java.awt.image.BufferedImage;
37
38 public class OverviewRenderer
39 {
40   private FeatureColourFinder finder;
41
42   // image to render on
43   private BufferedImage miniMe;
44
45   // raw number of pixels to allocate to each column
46   private float pixelsPerCol;
47
48   // raw number of pixels to allocate to each row
49   private float pixelsPerSeq;
50
51   // flag to indicate whether to halt drawing
52   private volatile boolean redraw = false;
53
54   // reference to alignment, needed to get sequence groups
55   private AlignmentI al;
56
57   private ResidueShaderI shader;
58
59   private ResidueColourFinder resColFinder;
60
61   public OverviewRenderer(FeatureRenderer fr, OverviewDimensions od,
62           AlignmentI alignment,
63           ResidueShaderI resshader)
64   {
65     finder = new FeatureColourFinder(fr);
66     resColFinder = new OverviewResColourFinder();
67
68     al = alignment;
69     shader = resshader;
70
71     pixelsPerCol = od.getPixelsPerCol();
72     pixelsPerSeq = od.getPixelsPerSeq();
73     miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
74             BufferedImage.TYPE_INT_RGB);
75   }
76
77   /**
78    * Draw alignment rows and columns onto an image
79    * 
80    * @param rit
81    *          Iterator over rows to be drawn
82    * @param cit
83    *          Iterator over columns to be drawn
84    * @return image containing the drawing
85    */
86   public BufferedImage draw(AlignmentRowsCollectionI rows,
87           AlignmentColsCollectionI cols)
88   {
89     int rgbcolor = Color.white.getRGB();
90     int seqIndex = 0;
91     int pixelRow = 0;
92
93     for (int alignmentRow : rows)
94     {
95       if (redraw)
96       {
97         break;
98       }
99
100       // get details of this alignment row
101       boolean hidden = rows.isHidden(alignmentRow);
102       SequenceI seq = rows.getSequence(alignmentRow);
103       // rate limiting step when rendering overview for lots of groups
104       SequenceGroup[] allGroups = al.findAllGroups(seq);
105
106       // calculate where this row extends to in pixels
107       int endRow = Math.min(Math.round((seqIndex + 1) * pixelsPerSeq) - 1,
108               miniMe.getHeight() - 1);
109
110       int colIndex = 0;
111       int pixelCol = 0;
112       for (int alignmentCol : cols)
113       {
114         if (redraw)
115         {
116           break;
117         }
118
119         // calculate where this column extends to in pixels
120         int endCol = Math.min(Math.round((colIndex + 1) * pixelsPerCol) - 1,
121                 miniMe.getWidth() - 1);
122
123         // don't do expensive colour determination if we're not going to use it
124         // NB this is important to avoid performance issues in the overview
125         // panel
126         if (pixelCol <= endCol)
127         {
128           // determine the colour based on the sequence and column position
129           rgbcolor = getColumnColourFromSequence(allGroups, seq,
130                   hidden || cols.isHidden(alignmentCol), alignmentCol,
131                   finder);
132
133           // fill in the appropriate number of pixels
134           for (int row = pixelRow; row <= endRow; ++row)
135           {
136             for (int col = pixelCol; col <= endCol; ++col)
137             {
138               miniMe.setRGB(col, row, rgbcolor);
139             }
140           }
141
142           pixelCol = endCol + 1;
143         }
144         colIndex++;
145       }
146       pixelRow = endRow + 1;
147       seqIndex++;
148     }
149     return miniMe;
150   }
151
152   /*
153    * Find the colour of a sequence at a specified column position
154    */
155   private int getColumnColourFromSequence(SequenceGroup[] allGroups,
156           jalview.datamodel.SequenceI seq,
157           boolean isHidden, int lastcol, FeatureColourFinder fcfinder)
158   {
159     Color color = Color.white;
160
161     if ((seq != null) && (seq.getLength() > lastcol))
162     {
163       color = resColFinder.getResidueColour(true, shader, allGroups, seq,
164               lastcol,
165               fcfinder);
166     }
167
168     if (isHidden)
169     {
170       color = color.darker().darker();
171     }
172
173     return color.getRGB();
174   }
175
176   /**
177    * Draw the alignment annotation in the overview panel
178    * 
179    * @param g
180    *          the graphics object to draw on
181    * @param anno
182    *          alignment annotation information
183    * @param charWidth
184    *          alignment character width value
185    * @param y
186    *          y-position for the annotation graph
187    * @param cols
188    *          the collection of columns used in the overview panel
189    */
190   public void drawGraph(Graphics g, AlignmentAnnotation anno, int charWidth,
191           int y, AlignmentColsCollectionI cols)
192   {
193     Annotation[] annotations = anno.annotations;
194     g.setColor(Color.white);
195     g.fillRect(0, 0, miniMe.getWidth(), y);
196
197     int height;
198     int colIndex = 0;
199     int pixelCol = 0;
200     for (int alignmentCol : cols)
201     {
202       if (redraw)
203       {
204         break;
205       }
206       if (alignmentCol >= annotations.length)
207       {
208         break; // no more annotations to draw here
209       }
210       else
211       {
212         int endCol = Math.min(Math.round((colIndex + 1) * pixelsPerCol) - 1,
213                 miniMe.getWidth() - 1);
214
215         if (annotations[alignmentCol] != null)
216         {
217           if (annotations[alignmentCol].colour == null)
218           {
219             g.setColor(Color.black);
220           }
221           else
222           {
223             g.setColor(annotations[alignmentCol].colour);
224           }
225
226           height = (int) ((annotations[alignmentCol].value / anno.graphMax)
227                   * y);
228           if (height > y)
229           {
230             height = y;
231           }
232
233           g.fillRect(pixelCol, y - height, endCol - pixelCol + 1, height);
234         }
235         pixelCol = endCol + 1;
236         colIndex++;
237       }
238     }
239   }
240
241   public void setRedraw(boolean b)
242   {
243     synchronized (this)
244     {
245       redraw = b;
246     }
247   }
248 }