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.api.FeatureRenderer;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.schemes.ColourSchemeI;
29 import java.awt.Color;
31 import java.awt.FontMetrics;
32 import java.awt.Graphics;
34 public class SequenceRenderer implements jalview.api.SequenceRenderer
40 boolean renderGaps = true;
42 SequenceGroup currentSequenceGroup = null;
44 SequenceGroup[] allGroups = null;
50 boolean forOverview = false;
52 public SequenceRenderer(AlignViewport av)
63 public void prepare(Graphics g, boolean renderGaps)
66 fm = g.getFontMetrics();
68 this.renderGaps = renderGaps;
71 public Color getResidueBoxColour(SequenceI seq, int i)
73 allGroups = av.getAlignment().findAllGroups(seq);
75 if (inCurrentSequenceGroup(i))
77 if (currentSequenceGroup.getDisplayBoxes())
79 getBoxColour(currentSequenceGroup.cs, seq, i);
82 else if (av.getShowBoxes())
84 getBoxColour(av.getGlobalColourScheme(), seq, i);
91 * Get the residue colour at the given sequence position - as determined by
92 * the sequence group colour (if any), else the colour scheme, possibly
93 * overridden by a feature colour.
101 public Color getResidueColour(final SequenceI seq, int position,
104 // TODO replace 8 or so code duplications with calls to this method
105 // (refactored as needed)
106 Color col = getResidueBoxColour(seq, position);
110 col = fr.findFeatureColour(col, seq, position);
115 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
119 resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
122 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
124 resBoxColour = Color.lightGray;
128 resBoxColour = Color.white;
133 public Color findSequenceColour(SequenceI seq, int i)
135 allGroups = av.getAlignment().findAllGroups(seq);
136 drawBoxes(seq, i, i, 0);
140 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
150 drawBoxes(seq, start, end, y1);
152 if (av.validCharWidth)
154 drawText(seq, start, end, y1);
158 public void drawBoxes(SequenceI seq, int start, int end, int y1)
161 int length = seq.getLength();
164 int curWidth = av.charWidth;
166 Color tempColour = null;
169 resBoxColour = Color.white;
172 if (inCurrentSequenceGroup(i))
174 if (currentSequenceGroup.getDisplayBoxes())
176 getBoxColour(currentSequenceGroup.cs, seq, i);
179 else if (av.getShowBoxes())
181 getBoxColour(av.getGlobalColourScheme(), seq, i);
185 if (resBoxColour != tempColour)
187 if (tempColour != null)
189 graphics.fillRect(av.charWidth * (curStart - start), y1,
190 curWidth, av.charHeight);
192 graphics.setColor(resBoxColour);
195 curWidth = av.charWidth;
196 tempColour = resBoxColour;
201 curWidth += av.charWidth;
207 graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,
211 public void drawText(SequenceI seq, int start, int end, int y1)
213 Font boldFont = null;
214 boolean bold = false;
215 if (av.upperCasebold)
217 boldFont = new Font(av.getFont().getName(), Font.BOLD, av.charHeight);
219 graphics.setFont(av.getFont());
222 y1 += av.charHeight - av.charHeight / 5; // height/5 replaces pady
226 // Need to find the sequence position here.
227 if (end + 1 >= seq.getLength())
229 end = seq.getLength() - 1;
233 boolean srep = av.isDisplayReferenceSeq();
234 for (int i = start; i <= end; i++)
236 graphics.setColor(Color.black);
238 s = seq.getCharAt(i);
239 if (!renderGaps && jalview.util.Comparison.isGap(s))
244 if (inCurrentSequenceGroup(i))
246 if (!currentSequenceGroup.getDisplayText())
251 if (currentSequenceGroup.getColourText())
253 getBoxColour(currentSequenceGroup.cs, seq, i);
254 graphics.setColor(resBoxColour.darker());
256 if (currentSequenceGroup.getShowNonconserved())
258 s = getDisplayChar(srep, i, s,
264 if (!av.getShowText())
269 if (av.getColourText())
271 getBoxColour(av.getGlobalColourScheme(), seq, i);
272 if (av.getShowBoxes())
274 graphics.setColor(resBoxColour.darker());
278 graphics.setColor(resBoxColour);
281 if (av.getShowUnconserved())
283 s = getDisplayChar(srep, i, s,
289 if (av.upperCasebold)
291 fm = graphics.getFontMetrics();
292 if ('A' <= s && s <= 'Z')
297 graphics.setFont(boldFont);
303 graphics.setFont(av.font);
309 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
310 graphics.drawString(String.valueOf(s), charOffset + av.charWidth
316 private char getDisplayChar(final boolean usesrep, int position,
319 // TODO - use currentSequenceGroup rather than alignemnt
320 // currentSequenceGroup.getConsensus()
321 char conschar = (usesrep) ? av.getAlignment().getSeqrep().getCharAt(position) : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
323 if (conschar != '-' && s == conschar)
330 boolean inCurrentSequenceGroup(int res)
332 if (allGroups == null)
337 for (int i = 0; i < allGroups.length; i++)
339 if (allGroups[i].getStartRes() <= res
340 && allGroups[i].getEndRes() >= res)
342 currentSequenceGroup = allGroups[i];
350 public void drawHighlightedText(SequenceI seq, int start, int end,
353 int pady = av.charHeight / 5;
355 graphics.setColor(Color.black);
356 graphics.fillRect(x1, y1, av.charWidth * (end - start + 1),
358 graphics.setColor(Color.white);
361 // Need to find the sequence position here.
362 if (av.validCharWidth)
364 for (int i = start; i <= end; i++)
366 if (i < seq.getLength())
368 s = seq.getCharAt(i);
371 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
372 graphics.drawString(String.valueOf(s), charOffset + x1
373 + av.charWidth * (i - start), y1 + av.charHeight - pady);
378 public void drawCursor(SequenceI seq, int res, int x1, int y1)
380 int pady = av.charHeight / 5;
382 graphics.setColor(Color.black);
383 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
384 graphics.setColor(Color.white);
386 graphics.setColor(Color.white);
388 char s = seq.getCharAt(res);
389 if (av.validCharWidth)
392 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
393 graphics.drawString(String.valueOf(s), charOffset + x1,
394 (y1 + av.charHeight) - pady);