Don't parse input id, leave it as it is
[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   Image offscreen;\r
66   public Color findSequenceColour(SequenceI seq, int i)\r
67   {\r
68     allGroups = av.alignment.findAllGroups(seq);\r
69     drawBoxes(seq, i,i, 0, 0, 1,1);\r
70     return resBoxColour;\r
71   }\r
72 \r
73   public void drawSequence(Graphics g, SequenceI seq, SequenceGroup[] sg,\r
74                            int start, int end, int x1, int y1, int width,\r
75                            int height)\r
76   {\r
77     allGroups = sg;\r
78 \r
79     graphics = g;\r
80 \r
81     drawBoxes(seq, start, end, x1, y1, (int) width, height);\r
82 \r
83     fm = g.getFontMetrics();\r
84     drawText(seq, start, end, x1, y1, (int) width, height);\r
85 \r
86   }\r
87 \r
88   public void drawBoxes(SequenceI seq, int start, int end, int x1, int y1,\r
89                         int width, int height)\r
90   {\r
91     int i = start;\r
92     int length = seq.getLength();\r
93 \r
94     int curStart = -1;\r
95     int curWidth = width;\r
96 \r
97     Color tempColour = null;\r
98     while (i <= end)\r
99     {\r
100       resBoxColour = Color.white;\r
101       if(i < length)\r
102       {\r
103         if (inCurrentSequenceGroup(i))\r
104         {\r
105           if (currentSequenceGroup.getDisplayBoxes())\r
106           {\r
107             getBoxColour(currentSequenceGroup.cs, seq, i);\r
108           }\r
109         }\r
110         else if (av.getShowBoxes())\r
111         {\r
112           getBoxColour(av.getGlobalColourScheme(), seq, i);\r
113         }\r
114       }\r
115 \r
116 \r
117       if (resBoxColour != tempColour)\r
118       {\r
119         if (tempColour != null)\r
120         {\r
121           graphics.fillRect(x1 + width * (curStart - start), y1, curWidth,\r
122                             height);\r
123         }\r
124         graphics.setColor(resBoxColour);\r
125 \r
126         curStart = i;\r
127         curWidth = width;\r
128         tempColour = resBoxColour;\r
129 \r
130       }\r
131       else\r
132       {\r
133         curWidth += width;\r
134       }\r
135 \r
136       i++;\r
137     }\r
138 \r
139     graphics.fillRect(x1 + width * (curStart - start), y1, curWidth, height);\r
140   }\r
141 \r
142   public void drawText(SequenceI seq, int start, int end, int x1, int y1,\r
143                        int width, int height)\r
144   {\r
145     int pady = height / 5;\r
146     int charOffset = 0;\r
147     char s=' ';\r
148     // Need to find the sequence position here.\r
149 \r
150     String sequence = seq.getSequence();\r
151 \r
152     if(end+1>=seq.getLength())\r
153           end = seq.getLength()-1;\r
154 \r
155     for (int i = start; i <= end; i++)\r
156     {\r
157       graphics.setColor(Color.black);\r
158 \r
159       s = sequence.charAt(i);\r
160 \r
161       if (!renderGaps && jalview.util.Comparison.isGap(s))\r
162       {\r
163         continue;\r
164       }\r
165 \r
166       if (inCurrentSequenceGroup(i))\r
167       {\r
168         if (!currentSequenceGroup.getDisplayText())\r
169         {\r
170           continue;\r
171         }\r
172 \r
173         if (currentSequenceGroup.getColourText())\r
174         {\r
175           getBoxColour(currentSequenceGroup.cs, seq, i);\r
176           graphics.setColor(resBoxColour.darker());\r
177         }\r
178       }\r
179       else\r
180       {\r
181         if (!av.getShowText())\r
182         {\r
183           continue;\r
184         }\r
185 \r
186         if (av.getColourText())\r
187         {\r
188           getBoxColour(av.getGlobalColourScheme(), seq, i);\r
189           if (av.getShowBoxes())\r
190           {\r
191             graphics.setColor(resBoxColour.darker());\r
192           }\r
193           else\r
194           {\r
195             graphics.setColor(resBoxColour);\r
196           }\r
197         }\r
198       }\r
199 \r
200       charOffset = (width - fm.charWidth(s)) / 2;\r
201       graphics.drawString(String.valueOf(s),\r
202                           charOffset + x1 + width * (i - start),\r
203                           y1 + height - pady);\r
204     }\r
205 \r
206   }\r
207 \r
208   boolean inCurrentSequenceGroup(int res)\r
209   {\r
210     if (allGroups == null)\r
211     {\r
212       return false;\r
213     }\r
214 \r
215     for (int i = 0; i < allGroups.length; i++)\r
216     {\r
217       if (allGroups[i].getStartRes() <= res && allGroups[i].getEndRes() >= res)\r
218       {\r
219         currentSequenceGroup = allGroups[i];\r
220         return true;\r
221       }\r
222     }\r
223 \r
224     return false;\r
225   }\r
226 \r
227   public void drawHighlightedText(SequenceI seq, int start, int end, int x1,\r
228                                   int y1, int width, int height)\r
229   {\r
230     int pady = height / 5;\r
231     int charOffset = 0;\r
232     graphics.setColor(Color.black);\r
233     graphics.fillRect(x1, y1, width * (end - start + 1), height);\r
234     graphics.setColor(Color.white);\r
235 \r
236     char s = '~';\r
237     // Need to find the sequence position here.\r
238     for (int i = start; i <= end; i++)\r
239     {\r
240       if (i < seq.getLength())\r
241       {\r
242         s = seq.getSequence().charAt(i);\r
243       }\r
244 \r
245       charOffset = (width - fm.charWidth(s)) / 2;\r
246       graphics.drawString(String.valueOf(s),\r
247                           charOffset + x1 + width * (i - start),\r
248                           y1 + height - pady);\r
249     }\r
250   }\r
251 \r
252 }\r