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