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;
29 import java.awt.Color;
30 import java.awt.FontMetrics;
31 import java.awt.Graphics;
33 import org.jfree.graphics2d.svg.SVGGraphics2D;
34 import org.jibble.epsgraphics.EpsGraphics2D;
36 public class SequenceRenderer implements jalview.api.SequenceRenderer
38 final static int CHAR_TO_UPPER = 'A' - 'a';
44 boolean renderGaps = true;
46 SequenceGroup[] allGroups = null;
48 // Color resBoxColour;
52 boolean monospacedFont;
54 ResidueColourFinder resColourFinder;
57 * Creates a new SequenceRenderer object
61 public SequenceRenderer(AlignViewportI viewport)
64 resColourFinder = new ResidueColourFinder();
73 public void prepare(Graphics g, boolean renderGaps)
76 fm = g.getFontMetrics();
78 // If EPS graphics, stringWidth will be a double, not an int
79 double dwidth = fm.getStringBounds("M", g).getWidth();
81 monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth()
82 && av.getCharWidth() == dwidth);
84 this.renderGaps = renderGaps;
88 * Get the residue colour at the given sequence position - as determined by
89 * the sequence group colour (if any), else the colour scheme, possibly
90 * overridden by a feature colour.
98 public Color getResidueColour(final SequenceI seq, int position,
99 FeatureColourFinder finder)
101 allGroups = av.getAlignment().findAllGroups(seq);
102 return resColourFinder.getResidueColour(av.getShowBoxes(),
103 av.getResidueShading(),
104 allGroups, seq, position,
130 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
135 drawBoxes(seq, start, end, y1);
137 if (av.isValidCharWidth())
139 drawText(seq, start, end, y1);
161 public synchronized void drawBoxes(SequenceI seq, int start, int end,
164 Color resBoxColour = Color.white;
168 return; // fix for racecondition
171 int length = seq.getLength();
174 int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(),
175 avHeight = av.getCharHeight();
177 Color tempColour = null;
181 resBoxColour = Color.white;
185 SequenceGroup currentSequenceGroup = resColourFinder
186 .getCurrentSequenceGroup(
188 if (currentSequenceGroup != null)
190 if (currentSequenceGroup.getDisplayBoxes())
192 resBoxColour = resColourFinder.getBoxColour(
193 currentSequenceGroup.getGroupColourScheme(), seq,
197 else if (av.getShowBoxes())
199 resBoxColour = resColourFinder
200 .getBoxColour(av.getResidueShading(), seq, i);
204 if (resBoxColour != tempColour)
206 if (tempColour != null)
208 graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
212 graphics.setColor(resBoxColour);
216 tempColour = resBoxColour;
226 graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
248 public void drawText(SequenceI seq, int start, int end, int y1)
250 y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
254 if (end + 1 >= seq.getLength())
256 end = seq.getLength() - 1;
258 graphics.setColor(av.getTextColour());
260 boolean drawAllText = monospacedFont && av.getShowText() && allGroups.length == 0
261 && !av.getColourText() && av.getThresholdTextColour() == 0;
264 * EPS or SVG misaligns monospaced strings (JAL-3239)
265 * so always draw these one character at a time
267 if (graphics instanceof EpsGraphics2D
268 || graphics instanceof SVGGraphics2D)
274 if (av.isRenderGaps())
276 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
280 char gap = av.getGapCharacter();
282 seq.getSequenceAsString(start, end + 1).replace(gap, ' '),
288 boolean srep = av.isDisplayReferenceSeq();
289 boolean getboxColour = false;
290 boolean isarep = av.getAlignment().getSeqrep() == seq;
291 Color resBoxColour = Color.white;
293 for (int i = start; i <= end; i++)
296 graphics.setColor(av.getTextColour());
297 getboxColour = false;
298 s = seq.getCharAt(i);
300 if (!renderGaps && jalview.util.Comparison.isGap(s))
305 SequenceGroup currentSequenceGroup = resColourFinder
306 .getCurrentSequenceGroup(
308 if (currentSequenceGroup != null)
310 if (!currentSequenceGroup.getDisplayText())
315 if (currentSequenceGroup.thresholdTextColour > 0
316 || currentSequenceGroup.getColourText())
319 resBoxColour = resColourFinder.getBoxColour(
320 currentSequenceGroup.getGroupColourScheme(), seq,
323 if (currentSequenceGroup.getColourText())
325 graphics.setColor(resBoxColour.darker());
328 if (currentSequenceGroup.thresholdTextColour > 0)
330 if (resBoxColour.getRed() + resBoxColour.getBlue()
332 .getGreen() < currentSequenceGroup.thresholdTextColour)
334 graphics.setColor(currentSequenceGroup.textColour2);
340 graphics.setColor(currentSequenceGroup.textColour);
342 boolean isgrep = currentSequenceGroup != null
343 ? currentSequenceGroup.getSeqrep() == seq : false;
344 if (!isarep && !isgrep
345 && currentSequenceGroup.getShowNonconserved()) // todo
348 // todo - use sequence group consensus
349 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
356 if (!av.getShowText())
361 if (av.getColourText())
364 resBoxColour = resColourFinder
365 .getBoxColour(av.getResidueShading(), seq, i);
367 if (av.getShowBoxes())
369 graphics.setColor(resBoxColour.darker());
373 graphics.setColor(resBoxColour);
377 if (av.getThresholdTextColour() > 0)
381 resBoxColour = resColourFinder
382 .getBoxColour(av.getResidueShading(), seq, i);
385 if (resBoxColour.getRed() + resBoxColour.getBlue()
386 + resBoxColour.getGreen() < av.getThresholdTextColour())
388 graphics.setColor(av.getTextColour2());
391 if (!isarep && av.getShowUnconserved())
393 s = getDisplayChar(srep, i, s, '.', null);
399 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
400 graphics.drawString(String.valueOf(s),
401 charOffset + av.getCharWidth() * (i - start), y1);
408 * Returns 'conservedChar' to represent the given position if the sequence
409 * character at that position is equal to the consensus (ignoring case), else
410 * returns the sequence character
414 * @param sequenceChar
415 * @param conservedChar
418 private char getDisplayChar(final boolean usesrep, int position,
419 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
421 // TODO - use currentSequenceGroup rather than alignment
422 // currentSequenceGroup.getConsensus()
423 char conschar = (usesrep) ? (currentGroup == null
424 || position < currentGroup.getStartRes()
425 || position > currentGroup.getEndRes()
426 ? av.getAlignment().getSeqrep().getCharAt(position)
427 : (currentGroup.getSeqrep() != null
428 ? currentGroup.getSeqrep().getCharAt(position)
429 : av.getAlignment().getSeqrep()
430 .getCharAt(position)))
431 : (currentGroup != null && currentGroup.getConsensus() != null
432 && position >= currentGroup.getStartRes()
433 && position <= currentGroup.getEndRes()
435 .getConsensus().annotations.length > position)
437 .getConsensus().annotations[position].displayCharacter
439 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
441 if (!jalview.util.Comparison.isGap(conschar)
442 && (sequenceChar == conschar
443 || sequenceChar + CHAR_TO_UPPER == conschar))
445 sequenceChar = conservedChar;
468 public void drawHighlightedText(SequenceI seq, int start, int end, int x1,
471 int pady = av.getCharHeight() / 5;
473 graphics.setColor(Color.BLACK);
474 graphics.fillRect(x1, y1, av.getCharWidth() * (end - start + 1),
476 graphics.setColor(Color.white);
480 // Need to find the sequence position here.
481 if (av.isValidCharWidth())
483 for (int i = start; i <= end; i++)
485 if (i < seq.getLength())
487 s = seq.getCharAt(i);
490 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
491 graphics.drawString(String.valueOf(s),
492 charOffset + x1 + (av.getCharWidth() * (i - start)),
493 (y1 + av.getCharHeight()) - pady);
499 * Draw a sequence canvas cursor
502 * graphics context to draw on
504 * character to draw at cursor
506 * x position of cursor in graphics context
508 * y position of cursor in graphics context
510 public void drawCursor(Graphics g, char s, int x1, int y1)
512 int pady = av.getCharHeight() / 5;
514 g.setColor(Color.black);
515 g.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
517 if (av.isValidCharWidth())
519 g.setColor(Color.white);
520 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
521 g.drawString(String.valueOf(s), charOffset + x1,
522 (y1 + av.getCharHeight()) - pady);