Highlight from alignment
[jalview.git] / src / jalview / appletgui / SequenceRenderer.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 \r
20 package jalview.appletgui;\r
21 \r
22 import java.awt.*;\r
23 \r
24 import jalview.datamodel.*;\r
25 import jalview.schemes.*;\r
26 \r
27 public class SequenceRenderer\r
28 {\r
29   AlignViewport av;\r
30   FontMetrics fm;\r
31   boolean renderGaps = true;\r
32   SequenceGroup currentSequenceGroup = null;\r
33   SequenceGroup[] allGroups = null;\r
34   Color resBoxColour;\r
35   Graphics graphics;\r
36 \r
37   public SequenceRenderer(AlignViewport av)\r
38   {\r
39     this.av = av;\r
40   }\r
41 \r
42   public void renderGaps(boolean b)\r
43   {\r
44     renderGaps = b;\r
45   }\r
46 \r
47   public Color getResidueBoxColour(ColourSchemeI cs, SequenceI seq, int i)\r
48   {\r
49     getBoxColour(cs, seq, i);\r
50     return resBoxColour;\r
51   }\r
52 \r
53   void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)\r
54   {\r
55     if (cs != null)\r
56     {\r
57       resBoxColour = cs.findColour(seq.getSequence(i, i + 1), i);\r
58     }\r
59     else\r
60     {\r
61       resBoxColour = Color.white;\r
62     }\r
63   }\r
64 \r
65   public Color findSequenceColour(SequenceI seq, int i)\r
66   {\r
67     allGroups = av.alignment.findAllGroups(seq);\r
68     drawBoxes(seq, i,i, 0, 0, 1,1);\r
69     return resBoxColour;\r
70   }\r
71 \r
72   public void drawSequence(Graphics g, SequenceI seq, SequenceGroup[] sg,\r
73                            int start, int end, int x1, int y1, int width,\r
74                            int height)\r
75   {\r
76     allGroups = sg;\r
77 \r
78     graphics = g;\r
79 \r
80     drawBoxes(seq, start, end, x1, y1, (int) width, height);\r
81 \r
82     fm = g.getFontMetrics();\r
83     drawText(seq, start, end, x1, y1, (int) width, height);\r
84 \r
85   }\r
86 \r
87   public void drawBoxes(SequenceI seq, int start, int end, int x1, int y1,\r
88                         int width, int height)\r
89   {\r
90     int i = start;\r
91     int length = seq.getLength();\r
92 \r
93     int curStart = -1;\r
94     int curWidth = width;\r
95 \r
96     Color tempColour = null;\r
97     while (i <= end)\r
98     {\r
99       resBoxColour = Color.white;\r
100       if(i < length)\r
101       {\r
102         if (inCurrentSequenceGroup(i))\r
103         {\r
104           if (currentSequenceGroup.getDisplayBoxes())\r
105           {\r
106             getBoxColour(currentSequenceGroup.cs, seq, i);\r
107           }\r
108         }\r
109         else if (av.getShowBoxes())\r
110         {\r
111           getBoxColour(av.getGlobalColourScheme(), seq, i);\r
112         }\r
113       }\r
114 \r
115 \r
116       if (resBoxColour != tempColour)\r
117       {\r
118         if (tempColour != null)\r
119         {\r
120           graphics.fillRect(x1 + width * (curStart - start), y1, curWidth,\r
121                             height);\r
122         }\r
123         graphics.setColor(resBoxColour);\r
124 \r
125         curStart = i;\r
126         curWidth = width;\r
127         tempColour = resBoxColour;\r
128 \r
129       }\r
130       else\r
131       {\r
132         curWidth += width;\r
133       }\r
134 \r
135       i++;\r
136     }\r
137 \r
138     graphics.fillRect(x1 + width * (curStart - start), y1, curWidth, height);\r
139   }\r
140 \r
141   public void drawText(SequenceI seq, int start, int end, int x1, int y1,\r
142                        int width, int height)\r
143   {\r
144     int pady = height / 5;\r
145     int charOffset = 0;\r
146     char s=' ';\r
147     // Need to find the sequence position here.\r
148 \r
149     String sequence = seq.getSequence();\r
150 \r
151     if(end+1>=seq.getLength())\r
152           end = seq.getLength()-1;\r
153 \r
154     for (int i = start; i <= end; i++)\r
155     {\r
156       graphics.setColor(Color.black);\r
157 \r
158       s = sequence.charAt(i);\r
159 \r
160       if (!renderGaps && jalview.util.Comparison.isGap(s))\r
161       {\r
162         continue;\r
163       }\r
164 \r
165       if (inCurrentSequenceGroup(i))\r
166       {\r
167         if (!currentSequenceGroup.getDisplayText())\r
168         {\r
169           continue;\r
170         }\r
171 \r
172         if (currentSequenceGroup.getColourText())\r
173         {\r
174           getBoxColour(currentSequenceGroup.cs, seq, i);\r
175           graphics.setColor(resBoxColour.darker());\r
176         }\r
177       }\r
178       else\r
179       {\r
180         if (!av.getShowText())\r
181         {\r
182           continue;\r
183         }\r
184 \r
185         if (av.getColourText())\r
186         {\r
187           getBoxColour(av.getGlobalColourScheme(), seq, i);\r
188           if (av.getShowBoxes())\r
189           {\r
190             graphics.setColor(resBoxColour.darker());\r
191           }\r
192           else\r
193           {\r
194             graphics.setColor(resBoxColour);\r
195           }\r
196         }\r
197       }\r
198 \r
199       charOffset = (width - fm.charWidth(s)) / 2;\r
200       graphics.drawString(String.valueOf(s),\r
201                           charOffset + x1 + width * (i - start),\r
202                           y1 + height - pady);\r
203     }\r
204 \r
205   }\r
206 \r
207   boolean inCurrentSequenceGroup(int res)\r
208   {\r
209     if (allGroups == null)\r
210     {\r
211       return false;\r
212     }\r
213 \r
214     for (int i = 0; i < allGroups.length; i++)\r
215     {\r
216       if (allGroups[i].getStartRes() <= res && allGroups[i].getEndRes() >= res)\r
217       {\r
218         currentSequenceGroup = allGroups[i];\r
219         return true;\r
220       }\r
221     }\r
222 \r
223     return false;\r
224   }\r
225 \r
226   public void drawHighlightedText(SequenceI seq, int start, int end, int x1,\r
227                                   int y1, int width, int height)\r
228   {\r
229     int pady = height / 5;\r
230     int charOffset = 0;\r
231     graphics.setColor(Color.black);\r
232     graphics.fillRect(x1, y1, width * (end - start + 1), height);\r
233     graphics.setColor(Color.white);\r
234 \r
235     char s = '~';\r
236     // Need to find the sequence position here.\r
237     for (int i = start; i <= end; i++)\r
238     {\r
239       if (i < seq.getLength())\r
240       {\r
241         s = seq.getSequence().charAt(i);\r
242       }\r
243 \r
244       charOffset = (width - fm.charWidth(s)) / 2;\r
245       graphics.drawString(String.valueOf(s),\r
246                           charOffset + x1 + width * (i - start),\r
247                           y1 + height - pady);\r
248     }\r
249   }\r
250 \r
251 }\r