87937bacbc965bb21782fe9fdd4a38cb484c341c
[jalview.git] / src / jalview / gui / 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.gui;
22
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.AlignmentColsCollection;
25 import jalview.datamodel.AlignmentRowsCollection;
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 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(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(AlignmentRowsCollection rows,
72           AlignmentColsCollection 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 finder)
123   {
124     Color color = Color.white;
125
126     if ((seq != null) && (seq.getLength() > lastcol))
127     {
128       color = sr.getResidueColour(seq, lastcol, finder);
129     }
130
131     if (isHidden)
132     {
133       color = color.darker().darker();
134     }
135
136     return color.getRGB();
137   }
138
139   public void drawGraph(Graphics g, AlignmentAnnotation _aa, int charWidth,
140           int y, AlignmentColsCollection cols)
141   {
142     Annotation[] aa_annotations = _aa.annotations;
143     g.setColor(Color.white);
144     g.fillRect(0, 0, miniMe.getWidth(), y);
145     // g.setColor(new Color(0, 0, 180));
146
147     int height;
148
149     int colIndex = 0;
150     int pixelCol = 0;
151     for (int alignmentCol : cols)
152     {
153       int endCol = Math.min(Math.round((colIndex + 1) * pixelsPerCol) - 1,
154               miniMe.getWidth() - 1);
155
156       if (alignmentCol < aa_annotations.length
157               && aa_annotations[alignmentCol] != null)
158       {
159         if (aa_annotations[alignmentCol].colour == null)
160         {
161           g.setColor(Color.black);
162         }
163         else
164         {
165           g.setColor(aa_annotations[alignmentCol].colour);
166         }
167
168         height = (int) ((aa_annotations[alignmentCol].value / _aa.graphMax) * y);
169         if (height > y)
170         {
171           height = y;
172         }
173
174         g.fillRect(pixelCol, y - height, endCol - pixelCol + 1, height);
175       }
176       pixelCol = endCol + 1;
177       colIndex++;
178     }
179   }
180 }