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