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.
23 import jalview.api.AlignViewportI;
24 import jalview.datamodel.SequenceGroup;
25 import jalview.datamodel.SequenceI;
26 import jalview.renderer.ResidueColourFinder;
27 import jalview.renderer.seqfeatures.FeatureColourFinder;
28 import jalview.schemes.ColourSchemeI;
29 import jalview.util.Comparison;
31 import java.awt.Color;
32 import java.awt.FontMetrics;
33 import java.awt.Graphics;
35 public class SequenceRenderer implements jalview.api.SequenceRendererI
37 final static int CHAR_TO_UPPER = 'A' - 'a';
39 private AlignViewportI av;
41 private FontMetrics fm;
43 private SequenceGroup[] allGroups = null;
45 private Graphics graphics;
47 private boolean monospacedFont;
49 private ResidueColourFinder resColourFinder;
52 * Creates a new SequenceRenderer object
56 public SequenceRenderer(AlignViewportI viewport)
59 resColourFinder = new ResidueColourFinder();
63 * Constructor given a choice of colour scheme. May be used to find colours
64 * for a structure that has a viewport colour scheme other than 'by sequence'
68 public SequenceRenderer(AlignViewportI viewport, ColourSchemeI cs)
71 resColourFinder = new ResidueColourFinder(viewport, cs);
75 * Sets the Graphics context to draw on, and also guesses whether we are using a monospaced font
79 public void setGraphics(Graphics g)
82 fm = g.getFontMetrics();
84 // If EPS graphics, stringWidth will be a double, not an int
85 double dwidth = fm.getStringBounds("M", g).getWidth();
87 monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth()
88 && av.getCharWidth() == dwidth);
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 allGroups = av.getAlignment().findAllGroups(seq);
106 return resColourFinder.getResidueColour(av.getShowBoxes(),
107 av.getResidueShading(),
108 allGroups, seq, position,
113 * Draws the sequence (box colour and residues) over the given range, at the
114 * specified y-offset on the Graphics context
117 * @param sg groups (if any) that the sequence is a member of (may have
118 * distinct group colouring)
124 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
125 int end, int y1, boolean drawGaps)
129 drawBoxes(seq, start, end, y1);
131 if (av.isValidCharWidth())
133 drawText(seq, start, end, y1, drawGaps);
138 * Draws box colours on the given sequence residue range, at the specified
139 * y-offset on the Graphics context
146 public synchronized void drawBoxes(SequenceI seq, int start, int end,
149 Color resBoxColour = Color.white;
153 return; // fix for racecondition
156 int length = seq.getLength();
159 int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(),
160 avHeight = av.getCharHeight();
162 Color tempColour = null;
166 resBoxColour = Color.white;
170 SequenceGroup currentSequenceGroup = resColourFinder
171 .getCurrentSequenceGroup(
173 if (currentSequenceGroup != null)
175 if (currentSequenceGroup.getDisplayBoxes())
177 resBoxColour = resColourFinder.getBoxColour(
178 currentSequenceGroup.getGroupColourScheme(), seq,
182 else if (av.getShowBoxes())
184 resBoxColour = resColourFinder
185 .getBoxColour(av.getResidueShading(), seq, i);
189 if (resBoxColour != tempColour)
191 if (tempColour != null)
193 graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
197 graphics.setColor(resBoxColour);
201 tempColour = resBoxColour;
211 graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
216 * Draws residue letters on the given sequence residue range, at the specified
217 * y-offset on the Graphics context
225 public void drawText(SequenceI seq, int start, int end, int y1, boolean drawGaps)
227 y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
231 if (end + 1 >= seq.getLength())
233 end = seq.getLength() - 1;
235 graphics.setColor(av.getTextColour());
237 if (monospacedFont && av.getShowText() && allGroups.length == 0
238 && !av.getColourText() && av.getThresholdTextColour() == 0)
240 if (av.isRenderGaps())
242 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
246 char gap = av.getGapCharacter();
248 seq.getSequenceAsString(start, end + 1).replace(gap, ' '),
254 boolean srep = av.isDisplayReferenceSeq();
255 boolean getboxColour = false;
256 boolean isarep = av.getAlignment().getSeqrep() == seq;
257 Color resBoxColour = Color.white;
259 for (int i = start; i <= end; i++)
262 graphics.setColor(av.getTextColour());
263 getboxColour = false;
264 s = seq.getCharAt(i);
266 if (!drawGaps && Comparison.isGap(s))
271 SequenceGroup currentSequenceGroup = resColourFinder
272 .getCurrentSequenceGroup(
274 if (currentSequenceGroup != null)
276 if (!currentSequenceGroup.getDisplayText())
281 if (currentSequenceGroup.thresholdTextColour > 0
282 || currentSequenceGroup.getColourText())
285 resBoxColour = resColourFinder.getBoxColour(
286 currentSequenceGroup.getGroupColourScheme(), seq,
289 if (currentSequenceGroup.getColourText())
291 graphics.setColor(resBoxColour.darker());
294 if (currentSequenceGroup.thresholdTextColour > 0)
296 if (resBoxColour.getRed() + resBoxColour.getBlue()
298 .getGreen() < currentSequenceGroup.thresholdTextColour)
300 graphics.setColor(currentSequenceGroup.textColour2);
306 graphics.setColor(currentSequenceGroup.textColour);
308 boolean isgrep = currentSequenceGroup != null
309 ? currentSequenceGroup.getSeqrep() == seq : false;
310 if (!isarep && !isgrep
311 && currentSequenceGroup.getShowNonconserved()) // todo
314 // todo - use sequence group consensus
315 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
320 if (!av.getShowText())
325 if (av.getColourText())
328 resBoxColour = resColourFinder
329 .getBoxColour(av.getResidueShading(), seq, i);
331 if (av.getShowBoxes())
333 graphics.setColor(resBoxColour.darker());
337 graphics.setColor(resBoxColour);
341 if (av.getThresholdTextColour() > 0)
345 resBoxColour = resColourFinder
346 .getBoxColour(av.getResidueShading(), seq, i);
349 if (resBoxColour.getRed() + resBoxColour.getBlue()
350 + resBoxColour.getGreen() < av.getThresholdTextColour())
352 graphics.setColor(av.getTextColour2());
355 if (!isarep && av.getShowUnconserved())
357 s = getDisplayChar(srep, i, s, '.', null);
361 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
362 graphics.drawString(String.valueOf(s),
363 charOffset + av.getCharWidth() * (i - start), y1);
369 * Returns 'conservedChar' to represent the given position if the sequence
370 * character at that position is equal to the consensus (ignoring case), else
371 * returns the sequence character
375 * @param sequenceChar
376 * @param conservedChar
379 private char getDisplayChar(final boolean usesrep, int position,
380 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
382 // TODO - use currentSequenceGroup rather than alignment
383 // currentSequenceGroup.getConsensus()
384 char conschar = (usesrep) ? (currentGroup == null
385 || position < currentGroup.getStartRes()
386 || position > currentGroup.getEndRes()
387 ? av.getAlignment().getSeqrep().getCharAt(position)
388 : (currentGroup.getSeqrep() != null
389 ? currentGroup.getSeqrep().getCharAt(position)
390 : av.getAlignment().getSeqrep()
391 .getCharAt(position)))
392 : (currentGroup != null && currentGroup.getConsensus() != null
393 && position >= currentGroup.getStartRes()
394 && position <= currentGroup.getEndRes()
396 .getConsensus().annotations.length > position)
398 .getConsensus().annotations[position].displayCharacter
400 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
402 if (!jalview.util.Comparison.isGap(conschar)
403 && (sequenceChar == conschar
404 || sequenceChar + CHAR_TO_UPPER == conschar))
406 sequenceChar = conservedChar;
429 public void drawHighlightedText(SequenceI seq, int start, int end, int x1,
432 int pady = av.getCharHeight() / 5;
434 graphics.setColor(Color.BLACK);
435 graphics.fillRect(x1, y1, av.getCharWidth() * (end - start + 1),
437 graphics.setColor(Color.white);
441 // Need to find the sequence position here.
442 if (av.isValidCharWidth())
444 for (int i = start; i <= end; i++)
446 if (i < seq.getLength())
448 s = seq.getCharAt(i);
451 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
452 graphics.drawString(String.valueOf(s),
453 charOffset + x1 + (av.getCharWidth() * (i - start)),
454 (y1 + av.getCharHeight()) - pady);
460 * Draw a sequence canvas cursor
463 * graphics context to draw on
465 * character to draw at cursor
467 * x position of cursor in graphics context
469 * y position of cursor in graphics context
471 public void drawCursor(Graphics g, char s, int x1, int y1)
473 int pady = av.getCharHeight() / 5;
475 g.setColor(Color.black);
476 g.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
478 if (av.isValidCharWidth())
480 g.setColor(Color.white);
481 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
482 g.drawString(String.valueOf(s), charOffset + x1,
483 (y1 + av.getCharHeight()) - pady);