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.datamodel.SequenceGroup;
24 import jalview.datamodel.SequenceI;
25 import jalview.renderer.ResidueShaderI;
26 import jalview.renderer.seqfeatures.FeatureColourFinder;
28 import java.awt.Color;
30 import java.awt.FontMetrics;
31 import java.awt.Graphics;
33 public class SequenceRenderer implements jalview.api.SequenceRenderer
35 final static int CHAR_TO_UPPER = 'A' - 'a';
41 boolean renderGaps = true;
43 SequenceGroup currentSequenceGroup = null;
45 SequenceGroup[] allGroups = null;
51 boolean forOverview = false;
53 public SequenceRenderer(AlignViewport av)
64 public void prepare(Graphics g, boolean renderGaps)
67 fm = g.getFontMetrics();
69 this.renderGaps = renderGaps;
72 protected Color getResidueBoxColour(SequenceI seq, int i)
74 allGroups = av.getAlignment().findAllGroups(seq);
76 if (inCurrentSequenceGroup(i))
78 if (currentSequenceGroup.getDisplayBoxes())
80 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq, i);
83 else if (av.getShowBoxes())
85 getBoxColour(av.getResidueShading(), seq, i);
92 * Get the residue colour at the given sequence position - as determined by
93 * the sequence group colour (if any), else the colour scheme, possibly
94 * overridden by a feature colour.
102 public Color getResidueColour(final SequenceI seq, int position,
103 FeatureColourFinder finder)
105 // TODO replace 8 or so code duplications with calls to this method
106 // (refactored as needed)
107 Color col = getResidueBoxColour(seq, position);
111 col = finder.findFeatureColour(col, seq, position);
116 void getBoxColour(ResidueShaderI shader, SequenceI seq, int i)
120 resBoxColour = shader.findColour(seq.getCharAt(i), i, seq);
123 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
125 resBoxColour = Color.lightGray;
129 resBoxColour = Color.white;
134 public Color findSequenceColour(SequenceI seq, int i)
136 allGroups = av.getAlignment().findAllGroups(seq);
137 drawBoxes(seq, i, i, 0);
141 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
151 drawBoxes(seq, start, end, y1);
153 if (av.validCharWidth)
155 drawText(seq, start, end, y1);
159 public void drawBoxes(SequenceI seq, int start, int end, int y1)
162 int length = seq.getLength();
165 int curWidth = av.getCharWidth(), avCharWidth = av.getCharWidth(), avCharHeight = av
168 Color tempColour = null;
171 resBoxColour = Color.white;
174 if (inCurrentSequenceGroup(i))
176 if (currentSequenceGroup.getDisplayBoxes())
178 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq,
182 else if (av.getShowBoxes())
184 getBoxColour(av.getResidueShading(), seq, i);
188 if (resBoxColour != tempColour)
190 if (tempColour != null)
192 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
195 graphics.setColor(resBoxColour);
198 curWidth = avCharWidth;
199 tempColour = resBoxColour;
204 curWidth += avCharWidth;
210 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
214 public void drawText(SequenceI seq, int start, int end, int y1)
216 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
217 Font boldFont = null;
218 boolean bold = false;
219 if (av.isUpperCasebold())
221 boldFont = new Font(av.getFont().getName(), Font.BOLD, avCharHeight);
223 graphics.setFont(av.getFont());
226 y1 += avCharHeight - avCharHeight / 5; // height/5 replaces pady
230 // Need to find the sequence position here.
231 if (end + 1 >= seq.getLength())
233 end = seq.getLength() - 1;
237 boolean srep = av.isDisplayReferenceSeq();
238 for (int i = start; i <= end; i++)
240 graphics.setColor(Color.black);
242 s = seq.getCharAt(i);
243 if (!renderGaps && jalview.util.Comparison.isGap(s))
248 if (inCurrentSequenceGroup(i))
250 if (!currentSequenceGroup.getDisplayText())
255 if (currentSequenceGroup.getColourText())
257 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq, i);
258 graphics.setColor(resBoxColour.darker());
260 if (currentSequenceGroup.getShowNonconserved())
262 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
267 if (!av.getShowText())
272 if (av.getColourText())
274 getBoxColour(av.getResidueShading(), seq, i);
275 if (av.getShowBoxes())
277 graphics.setColor(resBoxColour.darker());
281 graphics.setColor(resBoxColour);
284 if (av.getShowUnconserved())
286 s = getDisplayChar(srep, i, s, '.', null);
291 if (av.isUpperCasebold())
293 fm = graphics.getFontMetrics();
294 if ('A' <= s && s <= 'Z')
299 graphics.setFont(boldFont);
305 graphics.setFont(av.font);
311 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
312 graphics.drawString(String.valueOf(s), charOffset + avCharWidth
319 * Returns 'conservedChar' to represent the given position if the sequence
320 * character at that position is equal to the consensus (ignoring case), else
321 * returns the sequence character
325 * @param sequenceChar
326 * @param conservedChar
329 private char getDisplayChar(final boolean usesrep, int position,
330 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
332 // TODO - use currentSequenceGroup rather than alignment
333 // currentSequenceGroup.getConsensus()
334 char conschar = (usesrep) ? (currentGroup == null
335 || position < currentGroup.getStartRes()
336 || position > currentGroup.getEndRes() ? av.getAlignment()
337 .getSeqrep().getCharAt(position)
338 : (currentGroup.getSeqrep() != null ? currentGroup.getSeqrep()
339 .getCharAt(position) : av.getAlignment().getSeqrep()
340 .getCharAt(position)))
341 : (currentGroup != null && currentGroup.getConsensus() != null
342 && position >= currentGroup.getStartRes()
343 && position <= currentGroup.getEndRes() && currentGroup
344 .getConsensus().annotations.length > position) ? currentGroup
345 .getConsensus().annotations[position].displayCharacter
347 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
349 if (!jalview.util.Comparison.isGap(conschar)
350 && (sequenceChar == conschar || sequenceChar + CHAR_TO_UPPER == conschar))
352 sequenceChar = conservedChar;
357 boolean inCurrentSequenceGroup(int res)
359 if (allGroups == null)
364 for (int i = 0; i < allGroups.length; i++)
366 if (allGroups[i].getStartRes() <= res
367 && allGroups[i].getEndRes() >= res)
369 currentSequenceGroup = allGroups[i];
377 public void drawHighlightedText(SequenceI seq, int start, int end,
380 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
381 int pady = avCharHeight / 5;
383 graphics.setColor(Color.black);
384 graphics.fillRect(x1, y1, avCharWidth * (end - start + 1), avCharHeight);
385 graphics.setColor(Color.white);
388 // Need to find the sequence position here.
389 if (av.validCharWidth)
391 for (int i = start; i <= end; i++)
393 if (i < seq.getLength())
395 s = seq.getCharAt(i);
398 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
399 graphics.drawString(String.valueOf(s), charOffset + x1
400 + avCharWidth * (i - start), y1 + avCharHeight - pady);
405 public void drawCursor(SequenceI seq, int res, int x1, int y1)
407 int pady = av.getCharHeight() / 5;
409 graphics.setColor(Color.black);
410 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
411 graphics.setColor(Color.white);
413 graphics.setColor(Color.white);
415 char s = seq.getCharAt(res);
416 if (av.validCharWidth)
419 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
420 graphics.drawString(String.valueOf(s), charOffset + x1,
421 (y1 + av.getCharHeight()) - pady);