2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.appletgui;
23 import jalview.datamodel.*;
24 import jalview.schemes.*;
26 public class SequenceRenderer
32 boolean renderGaps = true;
34 SequenceGroup currentSequenceGroup = null;
36 SequenceGroup[] allGroups = null;
42 boolean forOverview = false;
44 public SequenceRenderer(AlignViewport av)
55 public void prepare(Graphics g, boolean renderGaps)
58 fm = g.getFontMetrics();
60 this.renderGaps = renderGaps;
63 public Color getResidueBoxColour(SequenceI seq, int i)
65 allGroups = av.alignment.findAllGroups(seq);
67 if (inCurrentSequenceGroup(i))
69 if (currentSequenceGroup.getDisplayBoxes())
71 getBoxColour(currentSequenceGroup.cs, seq, i);
74 else if (av.getShowBoxes())
76 getBoxColour(av.globalColourScheme, seq, i);
82 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
86 resBoxColour = cs.findColour(seq.getCharAt(i), i);
89 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
91 resBoxColour = Color.lightGray;
95 resBoxColour = Color.white;
100 public Color findSequenceColour(SequenceI seq, int i)
102 allGroups = av.alignment.findAllGroups(seq);
103 drawBoxes(seq, i, i, 0);
107 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
117 drawBoxes(seq, start, end, y1);
119 if (av.validCharWidth)
121 drawText(seq, start, end, y1);
125 public void drawBoxes(SequenceI seq, int start, int end, int y1)
128 int length = seq.getLength();
131 int curWidth = av.charWidth;
133 Color tempColour = null;
136 resBoxColour = Color.white;
139 if (inCurrentSequenceGroup(i))
141 if (currentSequenceGroup.getDisplayBoxes())
143 getBoxColour(currentSequenceGroup.cs, seq, i);
146 else if (av.getShowBoxes())
148 getBoxColour(av.getGlobalColourScheme(), seq, i);
152 if (resBoxColour != tempColour)
154 if (tempColour != null)
156 graphics.fillRect(av.charWidth * (curStart - start), y1,
157 curWidth, av.charHeight);
159 graphics.setColor(resBoxColour);
162 curWidth = av.charWidth;
163 tempColour = resBoxColour;
168 curWidth += av.charWidth;
174 graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,
178 public void drawText(SequenceI seq, int start, int end, int y1)
180 Font boldFont = null;
181 boolean bold = false;
182 if (av.upperCasebold)
184 boldFont = new Font(av.getFont().getName(), Font.BOLD, av.charHeight);
186 graphics.setFont(av.getFont());
189 y1 += av.charHeight - av.charHeight / 5; // height/5 replaces pady
193 // Need to find the sequence position here.
194 if (end + 1 >= seq.getLength())
196 end = seq.getLength() - 1;
201 for (int i = start; i <= end; i++)
203 graphics.setColor(Color.black);
205 s = seq.getCharAt(i);
206 if (!renderGaps && jalview.util.Comparison.isGap(s))
211 if (inCurrentSequenceGroup(i))
213 if (!currentSequenceGroup.getDisplayText())
218 if (currentSequenceGroup.getColourText())
220 getBoxColour(currentSequenceGroup.cs, seq, i);
221 graphics.setColor(resBoxColour.darker());
223 if (currentSequenceGroup.getShowunconserved())
225 // cheat - use this if we have a consensus for each group: s = getDisplayChar(currentSequenceGroup.getConsensus(), i, s, '.');
226 s = getDisplayChar(av.consensus, i, s, '.');
231 if (!av.getShowText())
236 if (av.getColourText())
238 getBoxColour(av.getGlobalColourScheme(), seq, i);
239 if (av.getShowBoxes())
241 graphics.setColor(resBoxColour.darker());
245 graphics.setColor(resBoxColour);
248 if (av.getShowunconserved())
250 s = getDisplayChar(av.consensus, i, s, '.');
255 if (av.upperCasebold)
257 fm = graphics.getFontMetrics();
258 if ('A' <= s && s <= 'Z')
263 graphics.setFont(boldFont);
269 graphics.setFont(av.font);
275 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
276 graphics.drawString(String.valueOf(s), charOffset + av.charWidth
283 private char getDisplayChar(AlignmentAnnotation consensus, int position, char s, char c)
285 char conschar = consensus.annotations[position].displayCharacter.charAt(0);
286 if (conschar!='-' && s==conschar)
293 boolean inCurrentSequenceGroup(int res)
295 if (allGroups == null)
300 for (int i = 0; i < allGroups.length; i++)
302 if (allGroups[i].getStartRes() <= res
303 && allGroups[i].getEndRes() >= res)
305 currentSequenceGroup = allGroups[i];
313 public void drawHighlightedText(SequenceI seq, int start, int end,
316 int pady = av.charHeight / 5;
318 graphics.setColor(Color.black);
319 graphics.fillRect(x1, y1, av.charWidth * (end - start + 1),
321 graphics.setColor(Color.white);
324 // Need to find the sequence position here.
325 if (av.validCharWidth)
327 for (int i = start; i <= end; i++)
329 if (i < seq.getLength())
331 s = seq.getCharAt(i);
334 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
335 graphics.drawString(String.valueOf(s), charOffset + x1
336 + av.charWidth * (i - start), y1 + av.charHeight - pady);
341 public void drawCursor(SequenceI seq, int res, int x1, int y1)
343 int pady = av.charHeight / 5;
345 graphics.setColor(Color.black);
346 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
347 graphics.setColor(Color.white);
349 graphics.setColor(Color.white);
351 char s = seq.getCharAt(res);
352 if (av.validCharWidth)
355 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
356 graphics.drawString(String.valueOf(s), charOffset + x1,
357 (y1 + av.charHeight) - pady);