2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.appletgui;
22 import jalview.datamodel.*;
23 import jalview.schemes.*;
25 public class SequenceRenderer implements jalview.api.SequenceRenderer
31 boolean renderGaps = true;
33 SequenceGroup currentSequenceGroup = null;
35 SequenceGroup[] allGroups = null;
41 boolean forOverview = false;
43 public SequenceRenderer(AlignViewport av)
54 public void prepare(Graphics g, boolean renderGaps)
57 fm = g.getFontMetrics();
59 this.renderGaps = renderGaps;
62 public Color getResidueBoxColour(SequenceI seq, int i)
64 allGroups = av.alignment.findAllGroups(seq);
66 if (inCurrentSequenceGroup(i))
68 if (currentSequenceGroup.getDisplayBoxes())
70 getBoxColour(currentSequenceGroup.cs, seq, i);
73 else if (av.getShowBoxes())
75 getBoxColour(av.globalColourScheme, seq, i);
81 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
85 resBoxColour = cs.findColour(seq.getCharAt(i), i);
88 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
90 resBoxColour = Color.lightGray;
94 resBoxColour = Color.white;
99 public Color findSequenceColour(SequenceI seq, int i)
101 allGroups = av.alignment.findAllGroups(seq);
102 drawBoxes(seq, i, i, 0);
107 * draw coloured sequence to the current graphics cursor
113 * @param drawBox - draw background
114 * @param drawText - draw text (if shading style permits)
117 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
118 int end, int y1, boolean drawBox, boolean drawText)
127 drawBoxes(seq, start, end, y1);
130 if (av.validCharWidth && drawText)
132 drawText(seq, start, end, y1);
136 public void drawBoxes(SequenceI seq, int start, int end, int y1)
139 int length = seq.getLength();
142 int curWidth = av.charWidth;
144 Color tempColour = null;
147 resBoxColour = Color.white;
150 if (inCurrentSequenceGroup(i))
152 if (currentSequenceGroup.getDisplayBoxes())
154 getBoxColour(currentSequenceGroup.cs, seq, i);
157 else if (av.getShowBoxes())
159 getBoxColour(av.getGlobalColourScheme(), seq, i);
163 if (resBoxColour != tempColour)
165 if (tempColour != null)
167 graphics.fillRect(av.charWidth * (curStart - start), y1,
168 curWidth, av.charHeight);
170 graphics.setColor(resBoxColour);
173 curWidth = av.charWidth;
174 tempColour = resBoxColour;
179 curWidth += av.charWidth;
185 graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,
189 public void drawText(SequenceI seq, int start, int end, int y1)
191 Font boldFont = null;
192 boolean bold = false;
193 if (av.upperCasebold)
195 boldFont = new Font(av.getFont().getName(), Font.BOLD, av.charHeight);
197 graphics.setFont(av.getFont());
200 y1 += av.charHeight - av.charHeight / 5; // height/5 replaces pady
204 // Need to find the sequence position here.
205 if (end + 1 >= seq.getLength())
207 end = seq.getLength() - 1;
212 for (int i = start; i <= end; i++)
214 graphics.setColor(Color.black);
216 s = seq.getCharAt(i);
217 if (!renderGaps && jalview.util.Comparison.isGap(s))
222 if (inCurrentSequenceGroup(i))
224 if (!currentSequenceGroup.getDisplayText())
229 if (currentSequenceGroup.getColourText())
231 getBoxColour(currentSequenceGroup.cs, seq, i);
232 graphics.setColor(resBoxColour.darker());
234 if (currentSequenceGroup.getShowNonconserved())
236 // cheat - use this if we have a consensus for each group: s =
237 // getDisplayChar(currentSequenceGroup.getConsensus(), i, s, '.');
238 s = getDisplayChar(av.consensus, i, s, '.');
243 if (!av.getShowText())
248 if (av.getColourText())
250 getBoxColour(av.getGlobalColourScheme(), seq, i);
251 if (av.getShowBoxes())
253 graphics.setColor(resBoxColour.darker());
257 graphics.setColor(resBoxColour);
260 if (av.getShowunconserved())
262 s = getDisplayChar(av.consensus, i, s, '.');
267 if (av.upperCasebold)
269 fm = graphics.getFontMetrics();
270 if ('A' <= s && s <= 'Z')
275 graphics.setFont(boldFont);
281 graphics.setFont(av.font);
287 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
288 graphics.drawString(String.valueOf(s), charOffset + av.charWidth
294 private char getDisplayChar(AlignmentAnnotation consensus, int position,
297 char conschar = consensus.annotations[position].displayCharacter
299 if (conschar != '-' && s == conschar)
306 boolean inCurrentSequenceGroup(int res)
308 if (allGroups == null)
313 for (int i = 0; i < allGroups.length; i++)
315 if (allGroups[i].getStartRes() <= res
316 && allGroups[i].getEndRes() >= res)
318 currentSequenceGroup = allGroups[i];
326 public void drawHighlightedText(SequenceI seq, int start, int end,
329 int pady = av.charHeight / 5;
331 graphics.setColor(Color.black);
332 graphics.fillRect(x1, y1, av.charWidth * (end - start + 1),
334 graphics.setColor(Color.white);
337 // Need to find the sequence position here.
338 if (av.validCharWidth)
340 for (int i = start; i <= end; i++)
342 if (i < seq.getLength())
344 s = seq.getCharAt(i);
347 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
348 graphics.drawString(String.valueOf(s), charOffset + x1
349 + av.charWidth * (i - start), y1 + av.charHeight - pady);
354 public void drawCursor(SequenceI seq, int res, int x1, int y1)
356 int pady = av.charHeight / 5;
358 graphics.setColor(Color.black);
359 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
360 graphics.setColor(Color.white);
362 graphics.setColor(Color.white);
364 char s = seq.getCharAt(res);
365 if (av.validCharWidth)
368 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
369 graphics.drawString(String.valueOf(s), charOffset + x1,
370 (y1 + av.charHeight) - pady);