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