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(), allGroups, seq, position, finder);
128 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
133 drawBoxes(seq, start, end, y1);
135 if (av.isValidCharWidth())
137 drawText(seq, start, end, y1);
159 public synchronized void drawBoxes(SequenceI seq, int start, int end,
162 Color resBoxColour = Color.white;
166 return; // fix for racecondition
169 int length = seq.getLength();
172 int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(),
173 avHeight = av.getCharHeight();
175 Color tempColour = null;
179 resBoxColour = Color.white;
183 SequenceGroup currentSequenceGroup = resColourFinder
184 .getCurrentSequenceGroup(allGroups, i);
185 if (currentSequenceGroup != null)
187 if (currentSequenceGroup.getDisplayBoxes())
189 resBoxColour = resColourFinder.getBoxColour(
190 currentSequenceGroup.getGroupColourScheme(), seq, i);
193 else if (av.getShowBoxes())
195 resBoxColour = resColourFinder
196 .getBoxColour(av.getResidueShading(), seq, i);
200 if (resBoxColour != tempColour)
202 if (tempColour != null)
204 graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
208 graphics.setColor(resBoxColour);
212 tempColour = resBoxColour;
222 graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
244 public void drawText(SequenceI seq, int start, int end, int y1)
246 y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
250 if (end + 1 >= seq.getLength())
252 end = seq.getLength() - 1;
254 graphics.setColor(av.getTextColour());
256 boolean drawAllText = monospacedFont && av.getShowText()
257 && allGroups.length == 0 && !av.getColourText()
258 && av.getThresholdTextColour() == 0;
261 * EPS or SVG misaligns monospaced strings (JAL-3239)
262 * so always draw these one character at a time
264 if (graphics instanceof EpsGraphics2D
265 || graphics instanceof SVGGraphics2D)
271 if (av.isRenderGaps())
273 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
277 char gap = av.getGapCharacter();
279 seq.getSequenceAsString(start, end + 1).replace(gap, ' '),
285 boolean srep = av.isDisplayReferenceSeq();
286 boolean getboxColour = false;
287 boolean isarep = av.getAlignment().getSeqrep() == seq;
288 Color resBoxColour = Color.white;
290 for (int i = start; i <= end; i++)
293 graphics.setColor(av.getTextColour());
294 getboxColour = false;
295 s = seq.getCharAt(i);
297 if (!renderGaps && jalview.util.Comparison.isGap(s))
302 SequenceGroup currentSequenceGroup = resColourFinder
303 .getCurrentSequenceGroup(allGroups, i);
304 if (currentSequenceGroup != null)
306 if (!currentSequenceGroup.getDisplayText())
311 if (currentSequenceGroup.thresholdTextColour > 0
312 || currentSequenceGroup.getColourText())
315 resBoxColour = resColourFinder.getBoxColour(
316 currentSequenceGroup.getGroupColourScheme(), seq, i);
318 if (currentSequenceGroup.getColourText())
320 graphics.setColor(resBoxColour.darker());
323 if (currentSequenceGroup.thresholdTextColour > 0)
325 if (resBoxColour.getRed() + resBoxColour.getBlue()
327 .getGreen() < currentSequenceGroup.thresholdTextColour)
329 graphics.setColor(currentSequenceGroup.textColour2);
335 graphics.setColor(currentSequenceGroup.textColour);
337 boolean isgrep = currentSequenceGroup != null
338 ? currentSequenceGroup.getSeqrep() == seq
340 if (!isarep && !isgrep
341 && currentSequenceGroup.getShowNonconserved()) // todo
344 // todo - use sequence group consensus
345 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
352 if (!av.getShowText())
357 if (av.getColourText())
360 resBoxColour = resColourFinder
361 .getBoxColour(av.getResidueShading(), seq, i);
363 if (av.getShowBoxes())
365 graphics.setColor(resBoxColour.darker());
369 graphics.setColor(resBoxColour);
373 if (av.getThresholdTextColour() > 0)
377 resBoxColour = resColourFinder
378 .getBoxColour(av.getResidueShading(), seq, i);
381 if (resBoxColour.getRed() + resBoxColour.getBlue()
382 + resBoxColour.getGreen() < av.getThresholdTextColour())
384 graphics.setColor(av.getTextColour2());
387 if (!isarep && av.getShowUnconserved())
389 s = getDisplayChar(srep, i, s, '.', null);
395 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
396 graphics.drawString(String.valueOf(s),
397 charOffset + av.getCharWidth() * (i - start), y1);
404 * Returns 'conservedChar' to represent the given position if the sequence
405 * character at that position is equal to the consensus (ignoring case), else
406 * returns the sequence character
410 * @param sequenceChar
411 * @param conservedChar
414 private char getDisplayChar(final boolean usesrep, int position,
415 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
417 // TODO - use currentSequenceGroup rather than alignment
418 // currentSequenceGroup.getConsensus()
419 char conschar = (usesrep) ? (currentGroup == null
420 || position < currentGroup.getStartRes()
421 || position > currentGroup.getEndRes()
422 ? av.getAlignment().getSeqrep().getCharAt(position)
423 : (currentGroup.getSeqrep() != null
424 ? currentGroup.getSeqrep().getCharAt(position)
425 : av.getAlignment().getSeqrep()
426 .getCharAt(position)))
427 : (currentGroup != null && currentGroup.getConsensus() != null
428 && position >= currentGroup.getStartRes()
429 && position <= currentGroup.getEndRes()
431 .getConsensus().annotations.length > position)
433 .getConsensus().annotations[position].displayCharacter
435 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
437 if (!jalview.util.Comparison.isGap(conschar)
438 && (sequenceChar == conschar
439 || sequenceChar + CHAR_TO_UPPER == conschar))
441 sequenceChar = conservedChar;
464 public void drawHighlightedText(SequenceI seq, int start, int end, int x1,
467 int pady = av.getCharHeight() / 5;
469 graphics.setColor(Color.BLACK);
470 graphics.fillRect(x1, y1, av.getCharWidth() * (end - start + 1),
472 graphics.setColor(Color.white);
476 // Need to find the sequence position here.
477 if (av.isValidCharWidth())
479 for (int i = start; i <= end; i++)
481 if (i < seq.getLength())
483 s = seq.getCharAt(i);
486 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
487 graphics.drawString(String.valueOf(s),
488 charOffset + x1 + (av.getCharWidth() * (i - start)),
489 (y1 + av.getCharHeight()) - pady);
495 * Draw a sequence canvas cursor
498 * graphics context to draw on
500 * character to draw at cursor
502 * x position of cursor in graphics context
504 * y position of cursor in graphics context
506 public void drawCursor(Graphics g, char s, int x1, int y1)
508 int pady = av.getCharHeight() / 5;
510 g.setColor(Color.black);
511 g.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
513 if (av.isValidCharWidth())
515 g.setColor(Color.white);
516 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
517 g.drawString(String.valueOf(s), charOffset + x1,
518 (y1 + av.getCharHeight()) - pady);