2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.ColorI;
24 import jalview.api.FeatureRenderer;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.schemes.Colour;
28 import jalview.schemes.ColourSchemeI;
30 import java.awt.Color;
32 import java.awt.FontMetrics;
33 import java.awt.Graphics;
35 public class SequenceRenderer implements jalview.api.SequenceRenderer
37 final static int CHAR_TO_UPPER = 'A' - 'a';
43 boolean renderGaps = true;
45 SequenceGroup currentSequenceGroup = null;
47 SequenceGroup[] allGroups = null;
53 boolean forOverview = false;
55 public SequenceRenderer(AlignViewport av)
66 public void prepare(Graphics g, boolean renderGaps)
69 fm = g.getFontMetrics();
71 this.renderGaps = renderGaps;
75 public ColorI getResidueBoxColour(SequenceI seq, int i)
77 allGroups = av.getAlignment().findAllGroups(seq);
79 if (inCurrentSequenceGroup(i))
81 if (currentSequenceGroup.getDisplayBoxes())
83 getBoxColour(currentSequenceGroup.cs, seq, i);
86 else if (av.getShowBoxes())
88 getBoxColour(av.getGlobalColourScheme(), seq, i);
91 return new Colour(resBoxColour);
95 * Get the residue colour at the given sequence position - as determined by
96 * the sequence group colour (if any), else the colour scheme, possibly
97 * overridden by a feature colour.
105 public ColorI getResidueColour(final SequenceI seq, int position,
108 // TODO replace 8 or so code duplications with calls to this method
109 // (refactored as needed)
110 ColorI col = getResidueBoxColour(seq, position);
114 col = fr.findFeatureColour(col, seq, position);
119 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
123 resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
126 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
128 resBoxColour = Color.lightGray;
132 resBoxColour = Color.white;
137 public Color findSequenceColour(SequenceI seq, int i)
139 allGroups = av.getAlignment().findAllGroups(seq);
140 drawBoxes(seq, i, i, 0);
144 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
154 drawBoxes(seq, start, end, y1);
156 if (av.validCharWidth)
158 drawText(seq, start, end, y1);
162 public void drawBoxes(SequenceI seq, int start, int end, int y1)
165 int length = seq.getLength();
168 int curWidth = av.getCharWidth(), avCharWidth = av.getCharWidth(), avCharHeight = av
171 Color tempColour = null;
174 resBoxColour = Color.white;
177 if (inCurrentSequenceGroup(i))
179 if (currentSequenceGroup.getDisplayBoxes())
181 getBoxColour(currentSequenceGroup.cs, seq, i);
184 else if (av.getShowBoxes())
186 getBoxColour(av.getGlobalColourScheme(), seq, i);
190 if (resBoxColour != tempColour)
192 if (tempColour != null)
194 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
197 graphics.setColor(resBoxColour);
200 curWidth = avCharWidth;
201 tempColour = resBoxColour;
206 curWidth += avCharWidth;
212 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
216 public void drawText(SequenceI seq, int start, int end, int y1)
218 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
219 Font boldFont = null;
220 boolean bold = false;
221 if (av.isUpperCasebold())
223 boldFont = new Font(av.getFont().getName(), Font.BOLD, avCharHeight);
225 graphics.setFont(av.getFont());
228 y1 += avCharHeight - avCharHeight / 5; // height/5 replaces pady
232 // Need to find the sequence position here.
233 if (end + 1 >= seq.getLength())
235 end = seq.getLength() - 1;
239 boolean srep = av.isDisplayReferenceSeq();
240 for (int i = start; i <= end; i++)
242 graphics.setColor(Color.black);
244 s = seq.getCharAt(i);
245 if (!renderGaps && jalview.util.Comparison.isGap(s))
250 if (inCurrentSequenceGroup(i))
252 if (!currentSequenceGroup.getDisplayText())
257 if (currentSequenceGroup.getColourText())
259 getBoxColour(currentSequenceGroup.cs, seq, i);
260 graphics.setColor(resBoxColour.darker());
262 if (currentSequenceGroup.getShowNonconserved())
264 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
269 if (!av.getShowText())
274 if (av.getColourText())
276 getBoxColour(av.getGlobalColourScheme(), seq, i);
277 if (av.getShowBoxes())
279 graphics.setColor(resBoxColour.darker());
283 graphics.setColor(resBoxColour);
286 if (av.getShowUnconserved())
288 s = getDisplayChar(srep, i, s, '.', null);
293 if (av.isUpperCasebold())
295 fm = graphics.getFontMetrics();
296 if ('A' <= s && s <= 'Z')
301 graphics.setFont(boldFont);
307 graphics.setFont(av.font);
313 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
314 graphics.drawString(String.valueOf(s), charOffset + avCharWidth
321 * Returns 'conservedChar' to represent the given position if the sequence
322 * character at that position is equal to the consensus (ignoring case), else
323 * returns the sequence character
327 * @param sequenceChar
328 * @param conservedChar
331 private char getDisplayChar(final boolean usesrep, int position,
332 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
334 // TODO - use currentSequenceGroup rather than alignment
335 // currentSequenceGroup.getConsensus()
336 char conschar = (usesrep) ? (currentGroup == null
337 || position < currentGroup.getStartRes()
338 || position > currentGroup.getEndRes() ? av.getAlignment()
339 .getSeqrep().getCharAt(position)
340 : (currentGroup.getSeqrep() != null ? currentGroup.getSeqrep()
341 .getCharAt(position) : av.getAlignment().getSeqrep()
342 .getCharAt(position)))
343 : (currentGroup != null && currentGroup.getConsensus() != null
344 && position >= currentGroup.getStartRes()
345 && position <= currentGroup.getEndRes() && currentGroup
346 .getConsensus().annotations.length > position) ? currentGroup
347 .getConsensus().annotations[position].displayCharacter
349 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
351 if (!jalview.util.Comparison.isGap(conschar)
352 && (sequenceChar == conschar || sequenceChar + CHAR_TO_UPPER == conschar))
354 sequenceChar = conservedChar;
359 boolean inCurrentSequenceGroup(int res)
361 if (allGroups == null)
366 for (int i = 0; i < allGroups.length; i++)
368 if (allGroups[i].getStartRes() <= res
369 && allGroups[i].getEndRes() >= res)
371 currentSequenceGroup = allGroups[i];
379 public void drawHighlightedText(SequenceI seq, int start, int end,
382 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
383 int pady = avCharHeight / 5;
385 graphics.setColor(Color.black);
386 graphics.fillRect(x1, y1, avCharWidth * (end - start + 1), avCharHeight);
387 graphics.setColor(Color.white);
390 // Need to find the sequence position here.
391 if (av.validCharWidth)
393 for (int i = start; i <= end; i++)
395 if (i < seq.getLength())
397 s = seq.getCharAt(i);
400 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
401 graphics.drawString(String.valueOf(s), charOffset + x1
402 + avCharWidth * (i - start), y1 + avCharHeight - pady);
407 public void drawCursor(SequenceI seq, int res, int x1, int y1)
409 int pady = av.getCharHeight() / 5;
411 graphics.setColor(Color.black);
412 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
413 graphics.setColor(Color.white);
415 graphics.setColor(Color.white);
417 char s = seq.getCharAt(res);
418 if (av.validCharWidth)
421 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
422 graphics.drawString(String.valueOf(s), charOffset + x1,
423 (y1 + av.getCharHeight()) - pady);