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.bin.Cache;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.renderer.ResidueColourFinder;
28 import jalview.renderer.seqfeatures.FeatureColourFinder;
30 import java.awt.Color;
31 import java.awt.FontMetrics;
32 import java.awt.Graphics;
34 import org.jfree.graphics2d.svg.SVGGraphics2D;
35 import org.jibble.epsgraphics.EpsGraphics2D;
37 public class SequenceRenderer implements jalview.api.SequenceRenderer
39 final static int CHAR_TO_UPPER = 'A' - 'a';
45 boolean renderGaps = true;
47 SequenceGroup[] allGroups = null;
49 // Color resBoxColour;
53 boolean monospacedFont;
55 ResidueColourFinder resColourFinder;
57 private boolean colourGaps;
58 private Color gapColour=Color.white;
61 * Creates a new SequenceRenderer object
65 public SequenceRenderer(AlignViewportI viewport)
68 resColourFinder = new ResidueColourFinder();
72 * initialise state for a render
73 * @param renderGaps - whether gap symbols are shown
74 * @param colourGaps - whether boxes for gaps are to be coloured
76 public void prepare(Graphics g, boolean renderGaps, boolean colourGaps)
79 fm = g.getFontMetrics();
81 // If EPS graphics, stringWidth will be a double, not an int
82 double dwidth = fm.getStringBounds("M", g).getWidth();
84 monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth()
85 && av.getCharWidth() == dwidth);
87 this.renderGaps = renderGaps;
88 this.colourGaps = colourGaps;
89 this.gapColour = Color.white;
92 this.gapColour = Cache.getDefaultColour(Preferences.GAP_COLOUR,
93 jalview.renderer.OverviewResColourFinder.OVERVIEW_DEFAULT_GAP);
95 resColourFinder = new ResidueColourFinder(gapColour);
99 * Get the residue colour at the given sequence position - as determined by
100 * the sequence group colour (if any), else the colour scheme, possibly
101 * overridden by a feature colour.
109 public Color getResidueColour(final SequenceI seq, int position,
110 FeatureColourFinder finder)
112 allGroups = av.getAlignment().findAllGroups(seq);
113 return resColourFinder.getResidueColour(av.getShowBoxes(),
114 av.getResidueShading(),
115 allGroups, seq, position,
141 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
146 drawBoxes(seq, start, end, y1);
148 if (av.isValidCharWidth())
150 drawText(seq, start, end, y1);
172 public synchronized void drawBoxes(SequenceI seq, int start, int end,
175 Color resBoxColour = gapColour;
179 return; // fix for racecondition
182 int length = seq.getLength();
185 int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(),
186 avHeight = av.getCharHeight();
188 Color tempColour = null;
192 resBoxColour = Color.white;
196 SequenceGroup currentSequenceGroup = resColourFinder
197 .getCurrentSequenceGroup(
199 if (currentSequenceGroup != null)
201 if (currentSequenceGroup.getDisplayBoxes())
203 resBoxColour = resColourFinder.getBoxColour(
204 currentSequenceGroup.getGroupColourScheme(), seq,
208 else if (av.getShowBoxes())
210 resBoxColour = resColourFinder
211 .getBoxColour(av.getResidueShading(), seq, i);
214 resBoxColour = gapColour;
217 if (resBoxColour != tempColour)
219 if (tempColour != null)
221 graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
225 graphics.setColor(resBoxColour);
229 tempColour = resBoxColour;
239 graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
261 public void drawText(SequenceI seq, int start, int end, int y1)
263 y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
267 if (end + 1 >= seq.getLength())
269 end = seq.getLength() - 1;
271 graphics.setColor(av.getTextColour());
273 boolean drawAllText = monospacedFont && av.getShowText() && allGroups.length == 0
274 && !av.getColourText() && av.getThresholdTextColour() == 0;
277 * EPS or SVG misaligns monospaced strings (JAL-3239)
278 * so always draw these one character at a time
280 if (graphics instanceof EpsGraphics2D
281 || graphics instanceof SVGGraphics2D)
287 if (av.isRenderGaps())
289 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
293 char gap = av.getGapCharacter();
295 seq.getSequenceAsString(start, end + 1).replace(gap, ' '),
301 boolean srep = av.isDisplayReferenceSeq();
302 boolean getboxColour = false;
303 boolean isarep = av.getAlignment().getSeqrep() == seq;
304 Color resBoxColour = Color.white;
306 for (int i = start; i <= end; i++)
309 graphics.setColor(av.getTextColour());
310 getboxColour = false;
311 s = seq.getCharAt(i);
313 if (!renderGaps && jalview.util.Comparison.isGap(s))
318 SequenceGroup currentSequenceGroup = resColourFinder
319 .getCurrentSequenceGroup(
321 if (currentSequenceGroup != null)
323 if (!currentSequenceGroup.getDisplayText())
328 if (currentSequenceGroup.thresholdTextColour > 0
329 || currentSequenceGroup.getColourText())
332 resBoxColour = resColourFinder.getBoxColour(
333 currentSequenceGroup.getGroupColourScheme(), seq,
336 if (currentSequenceGroup.getColourText())
338 graphics.setColor(resBoxColour.darker());
341 if (currentSequenceGroup.thresholdTextColour > 0)
343 if (resBoxColour.getRed() + resBoxColour.getBlue()
345 .getGreen() < currentSequenceGroup.thresholdTextColour)
347 graphics.setColor(currentSequenceGroup.textColour2);
353 graphics.setColor(currentSequenceGroup.textColour);
355 boolean isgrep = currentSequenceGroup != null
356 ? currentSequenceGroup.getSeqrep() == seq : false;
357 if (!isarep && !isgrep
358 && currentSequenceGroup.getShowNonconserved()) // todo
361 // todo - use sequence group consensus
362 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
369 if (!av.getShowText())
374 if (av.getColourText())
377 resBoxColour = resColourFinder
378 .getBoxColour(av.getResidueShading(), seq, i);
380 if (av.getShowBoxes())
382 graphics.setColor(resBoxColour.darker());
386 graphics.setColor(resBoxColour);
390 if (av.getThresholdTextColour() > 0)
394 resBoxColour = resColourFinder
395 .getBoxColour(av.getResidueShading(), seq, i);
398 if (resBoxColour.getRed() + resBoxColour.getBlue()
399 + resBoxColour.getGreen() < av.getThresholdTextColour())
401 graphics.setColor(av.getTextColour2());
404 if (!isarep && av.getShowUnconserved())
406 s = getDisplayChar(srep, i, s, '.', null);
412 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
413 graphics.drawString(String.valueOf(s),
414 charOffset + av.getCharWidth() * (i - start), y1);
421 * Returns 'conservedChar' to represent the given position if the sequence
422 * character at that position is equal to the consensus (ignoring case), else
423 * returns the sequence character
427 * @param sequenceChar
428 * @param conservedChar
431 private char getDisplayChar(final boolean usesrep, int position,
432 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
434 // TODO - use currentSequenceGroup rather than alignment
435 // currentSequenceGroup.getConsensus()
436 char conschar = (usesrep) ? (currentGroup == null
437 || position < currentGroup.getStartRes()
438 || position > currentGroup.getEndRes()
439 ? av.getAlignment().getSeqrep().getCharAt(position)
440 : (currentGroup.getSeqrep() != null
441 ? currentGroup.getSeqrep().getCharAt(position)
442 : av.getAlignment().getSeqrep()
443 .getCharAt(position)))
444 : (currentGroup != null && currentGroup.getConsensus() != null
445 && position >= currentGroup.getStartRes()
446 && position <= currentGroup.getEndRes()
448 .getConsensus().annotations.length > position)
450 .getConsensus().annotations[position].displayCharacter
452 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
454 if (!jalview.util.Comparison.isGap(conschar)
455 && (sequenceChar == conschar
456 || sequenceChar + CHAR_TO_UPPER == conschar))
458 sequenceChar = conservedChar;
481 public void drawHighlightedText(SequenceI seq, int start, int end, int x1,
484 int pady = av.getCharHeight() / 5;
486 graphics.setColor(Color.BLACK);
487 graphics.fillRect(x1, y1, av.getCharWidth() * (end - start + 1),
489 graphics.setColor(Color.white);
493 // Need to find the sequence position here.
494 if (av.isValidCharWidth())
496 for (int i = start; i <= end; i++)
498 if (i < seq.getLength())
500 s = seq.getCharAt(i);
503 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
504 graphics.drawString(String.valueOf(s),
505 charOffset + x1 + (av.getCharWidth() * (i - start)),
506 (y1 + av.getCharHeight()) - pady);
512 * Draw a sequence canvas cursor
515 * graphics context to draw on
517 * character to draw at cursor
519 * x position of cursor in graphics context
521 * y position of cursor in graphics context
523 public void drawCursor(Graphics g, char s, int x1, int y1)
525 int pady = av.getCharHeight() / 5;
527 g.setColor(Color.black);
528 g.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
530 if (av.isValidCharWidth())
532 g.setColor(Color.white);
533 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
534 g.drawString(String.valueOf(s), charOffset + x1,
535 (y1 + av.getCharHeight()) - pady);