2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.appletgui;
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.SequenceGroup;
25 import jalview.datamodel.SequenceI;
26 import jalview.schemes.ColourSchemeI;
28 import java.awt.Color;
30 import java.awt.FontMetrics;
31 import java.awt.Graphics;
33 public class SequenceRenderer implements jalview.api.SequenceRenderer
39 boolean renderGaps = true;
41 SequenceGroup currentSequenceGroup = null;
43 SequenceGroup[] allGroups = null;
49 boolean forOverview = false;
51 public SequenceRenderer(AlignViewport av)
62 public void prepare(Graphics g, boolean renderGaps)
65 fm = g.getFontMetrics();
67 this.renderGaps = renderGaps;
70 public Color getResidueBoxColour(SequenceI seq, int i)
72 allGroups = av.getAlignment().findAllGroups(seq);
74 if (inCurrentSequenceGroup(i))
76 if (currentSequenceGroup.getDisplayBoxes())
78 getBoxColour(currentSequenceGroup.cs, seq, i);
81 else if (av.getShowBoxes())
83 getBoxColour(av.getGlobalColourScheme(), seq, i);
89 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
93 resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
96 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
98 resBoxColour = Color.lightGray;
102 resBoxColour = Color.white;
107 public Color findSequenceColour(SequenceI seq, int i)
109 allGroups = av.getAlignment().findAllGroups(seq);
110 drawBoxes(seq, i, i, 0);
114 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
124 drawBoxes(seq, start, end, y1);
126 if (av.validCharWidth)
128 drawText(seq, start, end, y1);
132 public void drawBoxes(SequenceI seq, int start, int end, int y1)
135 int length = seq.getLength();
138 int curWidth = av.charWidth;
140 Color tempColour = null;
143 resBoxColour = Color.white;
146 if (inCurrentSequenceGroup(i))
148 if (currentSequenceGroup.getDisplayBoxes())
150 getBoxColour(currentSequenceGroup.cs, seq, i);
153 else if (av.getShowBoxes())
155 getBoxColour(av.getGlobalColourScheme(), seq, i);
159 if (resBoxColour != tempColour)
161 if (tempColour != null)
163 graphics.fillRect(av.charWidth * (curStart - start), y1,
164 curWidth, av.charHeight);
166 graphics.setColor(resBoxColour);
169 curWidth = av.charWidth;
170 tempColour = resBoxColour;
175 curWidth += av.charWidth;
181 graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,
185 public void drawText(SequenceI seq, int start, int end, int y1)
187 Font boldFont = null;
188 boolean bold = false;
189 if (av.upperCasebold)
191 boldFont = new Font(av.getFont().getName(), Font.BOLD, av.charHeight);
193 graphics.setFont(av.getFont());
196 y1 += av.charHeight - av.charHeight / 5; // height/5 replaces pady
200 // Need to find the sequence position here.
201 if (end + 1 >= seq.getLength())
203 end = seq.getLength() - 1;
208 for (int i = start; i <= end; i++)
210 graphics.setColor(Color.black);
212 s = seq.getCharAt(i);
213 if (!renderGaps && jalview.util.Comparison.isGap(s))
218 if (inCurrentSequenceGroup(i))
220 if (!currentSequenceGroup.getDisplayText())
225 if (currentSequenceGroup.getColourText())
227 getBoxColour(currentSequenceGroup.cs, seq, i);
228 graphics.setColor(resBoxColour.darker());
230 if (currentSequenceGroup.getShowNonconserved())
232 // cheat - use this if we have a consensus for each group: s =
233 // getDisplayChar(currentSequenceGroup.getConsensus(), i, s, '.');
234 s = getDisplayChar(av.getAlignmentConsensusAnnotation(), i, s,
240 if (!av.getShowText())
245 if (av.getColourText())
247 getBoxColour(av.getGlobalColourScheme(), seq, i);
248 if (av.getShowBoxes())
250 graphics.setColor(resBoxColour.darker());
254 graphics.setColor(resBoxColour);
257 if (av.getShowUnconserved())
259 s = getDisplayChar(av.getAlignmentConsensusAnnotation(), i, s,
265 if (av.upperCasebold)
267 fm = graphics.getFontMetrics();
268 if ('A' <= s && s <= 'Z')
273 graphics.setFont(boldFont);
279 graphics.setFont(av.font);
285 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
286 graphics.drawString(String.valueOf(s), charOffset + av.charWidth
292 private char getDisplayChar(AlignmentAnnotation consensus, int position,
295 char conschar = consensus.annotations[position].displayCharacter
297 if (conschar != '-' && s == conschar)
304 boolean inCurrentSequenceGroup(int res)
306 if (allGroups == null)
311 for (int i = 0; i < allGroups.length; i++)
313 if (allGroups[i].getStartRes() <= res
314 && allGroups[i].getEndRes() >= res)
316 currentSequenceGroup = allGroups[i];
324 public void drawHighlightedText(SequenceI seq, int start, int end,
327 int pady = av.charHeight / 5;
329 graphics.setColor(Color.black);
330 graphics.fillRect(x1, y1, av.charWidth * (end - start + 1),
332 graphics.setColor(Color.white);
335 // Need to find the sequence position here.
336 if (av.validCharWidth)
338 for (int i = start; i <= end; i++)
340 if (i < seq.getLength())
342 s = seq.getCharAt(i);
345 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
346 graphics.drawString(String.valueOf(s), charOffset + x1
347 + av.charWidth * (i - start), y1 + av.charHeight - pady);
352 public void drawCursor(SequenceI seq, int res, int x1, int y1)
354 int pady = av.charHeight / 5;
356 graphics.setColor(Color.black);
357 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
358 graphics.setColor(Color.white);
360 graphics.setColor(Color.white);
362 char s = seq.getCharAt(res);
363 if (av.validCharWidth)
366 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
367 graphics.drawString(String.valueOf(s), charOffset + x1,
368 (y1 + av.charHeight) - pady);