2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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.getAlignment().findAllGroups(seq);
66 if (inCurrentSequenceGroup(i))
68 if (currentSequenceGroup.getDisplayBoxes())
70 getBoxColour(currentSequenceGroup.cs, seq, i);
73 else if (av.getShowBoxes())
75 getBoxColour(av.getGlobalColourScheme(), 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.getAlignment().findAllGroups(seq);
102 drawBoxes(seq, i, i, 0);
106 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
116 drawBoxes(seq, start, end, y1);
118 if (av.validCharWidth)
120 drawText(seq, start, end, y1);
124 public void drawBoxes(SequenceI seq, int start, int end, int y1)
127 int length = seq.getLength();
130 int curWidth = av.charWidth;
132 Color tempColour = null;
135 resBoxColour = Color.white;
138 if (inCurrentSequenceGroup(i))
140 if (currentSequenceGroup.getDisplayBoxes())
142 getBoxColour(currentSequenceGroup.cs, seq, i);
145 else if (av.getShowBoxes())
147 getBoxColour(av.getGlobalColourScheme(), seq, i);
151 if (resBoxColour != tempColour)
153 if (tempColour != null)
155 graphics.fillRect(av.charWidth * (curStart - start), y1,
156 curWidth, av.charHeight);
158 graphics.setColor(resBoxColour);
161 curWidth = av.charWidth;
162 tempColour = resBoxColour;
167 curWidth += av.charWidth;
173 graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,
177 public void drawText(SequenceI seq, int start, int end, int y1)
179 Font boldFont = null;
180 boolean bold = false;
181 if (av.upperCasebold)
183 boldFont = new Font(av.getFont().getName(), Font.BOLD, av.charHeight);
185 graphics.setFont(av.getFont());
188 y1 += av.charHeight - av.charHeight / 5; // height/5 replaces pady
192 // Need to find the sequence position here.
193 if (end + 1 >= seq.getLength())
195 end = seq.getLength() - 1;
200 for (int i = start; i <= end; i++)
202 graphics.setColor(Color.black);
204 s = seq.getCharAt(i);
205 if (!renderGaps && jalview.util.Comparison.isGap(s))
210 if (inCurrentSequenceGroup(i))
212 if (!currentSequenceGroup.getDisplayText())
217 if (currentSequenceGroup.getColourText())
219 getBoxColour(currentSequenceGroup.cs, seq, i);
220 graphics.setColor(resBoxColour.darker());
222 if (currentSequenceGroup.getShowNonconserved())
224 // cheat - use this if we have a consensus for each group: s =
225 // getDisplayChar(currentSequenceGroup.getConsensus(), i, s, '.');
226 s = getDisplayChar(av.getAlignmentConsensusAnnotation(), 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.getAlignmentConsensusAnnotation(), 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
282 private char getDisplayChar(AlignmentAnnotation consensus, int position,
285 char conschar = consensus.annotations[position].displayCharacter
287 if (conschar != '-' && s == conschar)
294 boolean inCurrentSequenceGroup(int res)
296 if (allGroups == null)
301 for (int i = 0; i < allGroups.length; i++)
303 if (allGroups[i].getStartRes() <= res
304 && allGroups[i].getEndRes() >= res)
306 currentSequenceGroup = allGroups[i];
314 public void drawHighlightedText(SequenceI seq, int start, int end,
317 int pady = av.charHeight / 5;
319 graphics.setColor(Color.black);
320 graphics.fillRect(x1, y1, av.charWidth * (end - start + 1),
322 graphics.setColor(Color.white);
325 // Need to find the sequence position here.
326 if (av.validCharWidth)
328 for (int i = start; i <= end; i++)
330 if (i < seq.getLength())
332 s = seq.getCharAt(i);
335 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
336 graphics.drawString(String.valueOf(s), charOffset + x1
337 + av.charWidth * (i - start), y1 + av.charHeight - pady);
342 public void drawCursor(SequenceI seq, int res, int x1, int y1)
344 int pady = av.charHeight / 5;
346 graphics.setColor(Color.black);
347 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
348 graphics.setColor(Color.white);
350 graphics.setColor(Color.white);
352 char s = seq.getCharAt(res);
353 if (av.validCharWidth)
356 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
357 graphics.drawString(String.valueOf(s), charOffset + x1,
358 (y1 + av.charHeight) - pady);