2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.appletgui;
21 import jalview.datamodel.AlignmentAnnotation;
22 import jalview.datamodel.SequenceGroup;
23 import jalview.datamodel.SequenceI;
24 import jalview.schemes.ColourSchemeI;
26 import java.awt.Color;
28 import java.awt.FontMetrics;
29 import java.awt.Graphics;
31 public class SequenceRenderer implements jalview.api.SequenceRenderer
37 boolean renderGaps = true;
39 SequenceGroup currentSequenceGroup = null;
41 SequenceGroup[] allGroups = null;
47 boolean forOverview = false;
49 public SequenceRenderer(AlignViewport av)
60 public void prepare(Graphics g, boolean renderGaps)
63 fm = g.getFontMetrics();
65 this.renderGaps = renderGaps;
68 public Color getResidueBoxColour(SequenceI seq, int i)
70 allGroups = av.getAlignment().findAllGroups(seq);
72 if (inCurrentSequenceGroup(i))
74 if (currentSequenceGroup.getDisplayBoxes())
76 getBoxColour(currentSequenceGroup.cs, seq, i);
79 else if (av.getShowBoxes())
81 getBoxColour(av.getGlobalColourScheme(), seq, i);
87 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
91 resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
94 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
96 resBoxColour = Color.lightGray;
100 resBoxColour = Color.white;
105 public Color findSequenceColour(SequenceI seq, int i)
107 allGroups = av.getAlignment().findAllGroups(seq);
108 drawBoxes(seq, i, i, 0);
112 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
122 drawBoxes(seq, start, end, y1);
124 if (av.validCharWidth)
126 drawText(seq, start, end, y1);
130 public void drawBoxes(SequenceI seq, int start, int end, int y1)
133 int length = seq.getLength();
136 int curWidth = av.charWidth;
138 Color tempColour = null;
141 resBoxColour = Color.white;
144 if (inCurrentSequenceGroup(i))
146 if (currentSequenceGroup.getDisplayBoxes())
148 getBoxColour(currentSequenceGroup.cs, seq, i);
151 else if (av.getShowBoxes())
153 getBoxColour(av.getGlobalColourScheme(), seq, i);
157 if (resBoxColour != tempColour)
159 if (tempColour != null)
161 graphics.fillRect(av.charWidth * (curStart - start), y1,
162 curWidth, av.charHeight);
164 graphics.setColor(resBoxColour);
167 curWidth = av.charWidth;
168 tempColour = resBoxColour;
173 curWidth += av.charWidth;
179 graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,
183 public void drawText(SequenceI seq, int start, int end, int y1)
185 Font boldFont = null;
186 boolean bold = false;
187 if (av.upperCasebold)
189 boldFont = new Font(av.getFont().getName(), Font.BOLD, av.charHeight);
191 graphics.setFont(av.getFont());
194 y1 += av.charHeight - av.charHeight / 5; // height/5 replaces pady
198 // Need to find the sequence position here.
199 if (end + 1 >= seq.getLength())
201 end = seq.getLength() - 1;
205 boolean srep = av.isDisplayReferenceSeq();
206 for (int i = start; i <= end; i++)
208 graphics.setColor(Color.black);
210 s = seq.getCharAt(i);
211 if (!renderGaps && jalview.util.Comparison.isGap(s))
216 if (inCurrentSequenceGroup(i))
218 if (!currentSequenceGroup.getDisplayText())
223 if (currentSequenceGroup.getColourText())
225 getBoxColour(currentSequenceGroup.cs, seq, i);
226 graphics.setColor(resBoxColour.darker());
228 if (currentSequenceGroup.getShowNonconserved())
230 s = getDisplayChar(srep, i, s,
236 if (!av.getShowText())
241 if (av.getColourText())
243 getBoxColour(av.getGlobalColourScheme(), seq, i);
244 if (av.getShowBoxes())
246 graphics.setColor(resBoxColour.darker());
250 graphics.setColor(resBoxColour);
253 if (av.getShowUnconserved())
255 s = getDisplayChar(srep, i, s,
261 if (av.upperCasebold)
263 fm = graphics.getFontMetrics();
264 if ('A' <= s && s <= 'Z')
269 graphics.setFont(boldFont);
275 graphics.setFont(av.font);
281 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
282 graphics.drawString(String.valueOf(s), charOffset + av.charWidth
288 private char getDisplayChar(final boolean usesrep, int position,
291 // TODO - use currentSequenceGroup rather than alignemnt
292 // currentSequenceGroup.getConsensus()
293 char conschar = (usesrep) ? av.getAlignment().getSeqrep().getCharAt(position) : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
295 if (conschar != '-' && s == conschar)
302 boolean inCurrentSequenceGroup(int res)
304 if (allGroups == null)
309 for (int i = 0; i < allGroups.length; i++)
311 if (allGroups[i].getStartRes() <= res
312 && allGroups[i].getEndRes() >= res)
314 currentSequenceGroup = allGroups[i];
322 public void drawHighlightedText(SequenceI seq, int start, int end,
325 int pady = av.charHeight / 5;
327 graphics.setColor(Color.black);
328 graphics.fillRect(x1, y1, av.charWidth * (end - start + 1),
330 graphics.setColor(Color.white);
333 // Need to find the sequence position here.
334 if (av.validCharWidth)
336 for (int i = start; i <= end; i++)
338 if (i < seq.getLength())
340 s = seq.getCharAt(i);
343 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
344 graphics.drawString(String.valueOf(s), charOffset + x1
345 + av.charWidth * (i - start), y1 + av.charHeight - pady);
350 public void drawCursor(SequenceI seq, int res, int x1, int y1)
352 int pady = av.charHeight / 5;
354 graphics.setColor(Color.black);
355 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
356 graphics.setColor(Color.white);
358 graphics.setColor(Color.white);
360 char s = seq.getCharAt(res);
361 if (av.validCharWidth)
364 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
365 graphics.drawString(String.valueOf(s), charOffset + x1,
366 (y1 + av.charHeight) - pady);