2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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;
20 import jalview.datamodel.AlignmentAnnotation;
21 import jalview.datamodel.SequenceGroup;
22 import jalview.datamodel.SequenceI;
23 import jalview.schemes.ColourSchemeI;
25 import java.awt.Color;
27 import java.awt.FontMetrics;
28 import java.awt.Graphics;
30 public class SequenceRenderer implements jalview.api.SequenceRenderer
36 boolean renderGaps = true;
38 SequenceGroup currentSequenceGroup = null;
40 SequenceGroup[] allGroups = null;
46 boolean forOverview = false;
48 public SequenceRenderer(AlignViewport av)
59 public void prepare(Graphics g, boolean renderGaps)
62 fm = g.getFontMetrics();
64 this.renderGaps = renderGaps;
67 public Color getResidueBoxColour(SequenceI seq, int i)
69 allGroups = av.getAlignment().findAllGroups(seq);
71 if (inCurrentSequenceGroup(i))
73 if (currentSequenceGroup.getDisplayBoxes())
75 getBoxColour(currentSequenceGroup.cs, seq, i);
78 else if (av.getShowBoxes())
80 getBoxColour(av.getGlobalColourScheme(), seq, i);
86 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
90 resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
93 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
95 resBoxColour = Color.lightGray;
99 resBoxColour = Color.white;
104 public Color findSequenceColour(SequenceI seq, int i)
106 allGroups = av.getAlignment().findAllGroups(seq);
107 drawBoxes(seq, i, i, 0);
111 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
121 drawBoxes(seq, start, end, y1);
123 if (av.validCharWidth)
125 drawText(seq, start, end, y1);
129 public void drawBoxes(SequenceI seq, int start, int end, int y1)
132 int length = seq.getLength();
135 int curWidth = av.charWidth;
137 Color tempColour = null;
140 resBoxColour = Color.white;
143 if (inCurrentSequenceGroup(i))
145 if (currentSequenceGroup.getDisplayBoxes())
147 getBoxColour(currentSequenceGroup.cs, seq, i);
150 else if (av.getShowBoxes())
152 getBoxColour(av.getGlobalColourScheme(), seq, i);
156 if (resBoxColour != tempColour)
158 if (tempColour != null)
160 graphics.fillRect(av.charWidth * (curStart - start), y1,
161 curWidth, av.charHeight);
163 graphics.setColor(resBoxColour);
166 curWidth = av.charWidth;
167 tempColour = resBoxColour;
172 curWidth += av.charWidth;
178 graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,
182 public void drawText(SequenceI seq, int start, int end, int y1)
184 Font boldFont = null;
185 boolean bold = false;
186 if (av.upperCasebold)
188 boldFont = new Font(av.getFont().getName(), Font.BOLD, av.charHeight);
190 graphics.setFont(av.getFont());
193 y1 += av.charHeight - av.charHeight / 5; // height/5 replaces pady
197 // Need to find the sequence position here.
198 if (end + 1 >= seq.getLength())
200 end = seq.getLength() - 1;
205 for (int i = start; i <= end; i++)
207 graphics.setColor(Color.black);
209 s = seq.getCharAt(i);
210 if (!renderGaps && jalview.util.Comparison.isGap(s))
215 if (inCurrentSequenceGroup(i))
217 if (!currentSequenceGroup.getDisplayText())
222 if (currentSequenceGroup.getColourText())
224 getBoxColour(currentSequenceGroup.cs, seq, i);
225 graphics.setColor(resBoxColour.darker());
227 if (currentSequenceGroup.getShowNonconserved())
229 // cheat - use this if we have a consensus for each group: s =
230 // getDisplayChar(currentSequenceGroup.getConsensus(), i, s, '.');
231 s = getDisplayChar(av.getAlignmentConsensusAnnotation(), i, s,
237 if (!av.getShowText())
242 if (av.getColourText())
244 getBoxColour(av.getGlobalColourScheme(), seq, i);
245 if (av.getShowBoxes())
247 graphics.setColor(resBoxColour.darker());
251 graphics.setColor(resBoxColour);
254 if (av.getShowUnconserved())
256 s = getDisplayChar(av.getAlignmentConsensusAnnotation(), i, s,
262 if (av.upperCasebold)
264 fm = graphics.getFontMetrics();
265 if ('A' <= s && s <= 'Z')
270 graphics.setFont(boldFont);
276 graphics.setFont(av.font);
282 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
283 graphics.drawString(String.valueOf(s), charOffset + av.charWidth
289 private char getDisplayChar(AlignmentAnnotation consensus, int position,
292 char conschar = consensus.annotations[position].displayCharacter
294 if (conschar != '-' && s == conschar)
301 boolean inCurrentSequenceGroup(int res)
303 if (allGroups == null)
308 for (int i = 0; i < allGroups.length; i++)
310 if (allGroups[i].getStartRes() <= res
311 && allGroups[i].getEndRes() >= res)
313 currentSequenceGroup = allGroups[i];
321 public void drawHighlightedText(SequenceI seq, int start, int end,
324 int pady = av.charHeight / 5;
326 graphics.setColor(Color.black);
327 graphics.fillRect(x1, y1, av.charWidth * (end - start + 1),
329 graphics.setColor(Color.white);
332 // Need to find the sequence position here.
333 if (av.validCharWidth)
335 for (int i = start; i <= end; i++)
337 if (i < seq.getLength())
339 s = seq.getCharAt(i);
342 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
343 graphics.drawString(String.valueOf(s), charOffset + x1
344 + av.charWidth * (i - start), y1 + av.charHeight - pady);
349 public void drawCursor(SequenceI seq, int res, int x1, int y1)
351 int pady = av.charHeight / 5;
353 graphics.setColor(Color.black);
354 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
355 graphics.setColor(Color.white);
357 graphics.setColor(Color.white);
359 char s = seq.getCharAt(res);
360 if (av.validCharWidth)
363 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
364 graphics.drawString(String.valueOf(s), charOffset + x1,
365 (y1 + av.charHeight) - pady);