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