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.ResidueShaderI;
27 import jalview.renderer.seqfeatures.FeatureColourFinder;
28 import jalview.util.Comparison;
30 import java.awt.Color;
31 import java.awt.FontMetrics;
32 import java.awt.Graphics;
34 public class SequenceRenderer implements jalview.api.SequenceRenderer
36 final static int CHAR_TO_UPPER = 'A' - 'a';
42 boolean renderGaps = true;
44 SequenceGroup currentSequenceGroup = null;
46 SequenceGroup[] allGroups = null;
52 boolean monospacedFont;
54 boolean forOverview = false;
57 * Creates a new SequenceRenderer object
61 public SequenceRenderer(AlignViewportI viewport)
72 public void prepare(Graphics g, boolean renderGaps)
75 fm = g.getFontMetrics();
77 // If EPS graphics, stringWidth will be a double, not an int
78 double dwidth = fm.getStringBounds("M", g).getWidth();
80 monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth() && av
81 .getCharWidth() == dwidth);
83 this.renderGaps = renderGaps;
86 protected Color getResidueBoxColour(SequenceI seq, int i)
88 // rate limiting step when rendering overview for lots of groups
89 allGroups = av.getAlignment().findAllGroups(seq);
91 if (inCurrentSequenceGroup(i))
93 if (currentSequenceGroup.getDisplayBoxes())
95 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq, i);
98 else if (av.getShowBoxes())
100 getBoxColour(av.getResidueShading(), seq, i);
107 * Get the residue colour at the given sequence position - as determined by
108 * the sequence group colour (if any), else the colour scheme, possibly
109 * overridden by a feature colour.
117 public Color getResidueColour(final SequenceI seq, int position,
118 FeatureColourFinder finder)
120 Color col = getResidueBoxColour(seq, position);
124 col = finder.findFeatureColour(col, seq, position);
139 void getBoxColour(ResidueShaderI shader, SequenceI seq, int i)
141 if (shader.getColourScheme() != null)
143 if (forOverview && Comparison.isGap(seq.getCharAt(i)))
145 resBoxColour = Color.lightGray;
149 resBoxColour = shader.findColour(seq.getCharAt(i),
153 else if (forOverview && !Comparison.isGap(seq.getCharAt(i)))
155 resBoxColour = Color.lightGray;
159 resBoxColour = Color.white;
185 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
190 drawBoxes(seq, start, end, y1);
192 if (av.isValidCharWidth())
194 drawText(seq, start, end, y1);
216 public synchronized void drawBoxes(SequenceI seq, int start, int end,
221 return; // fix for racecondition
224 int length = seq.getLength();
227 int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(), avHeight = av
230 Color tempColour = null;
234 resBoxColour = Color.white;
238 if (inCurrentSequenceGroup(i))
240 if (currentSequenceGroup.getDisplayBoxes())
242 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq,
246 else if (av.getShowBoxes())
248 getBoxColour(av.getResidueShading(), seq, i);
252 if (resBoxColour != tempColour)
254 if (tempColour != null)
256 graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
260 graphics.setColor(resBoxColour);
264 tempColour = resBoxColour;
274 graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
296 public void drawText(SequenceI seq, int start, int end, int y1)
298 y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
302 if (end + 1 >= seq.getLength())
304 end = seq.getLength() - 1;
306 graphics.setColor(av.getTextColour());
308 if (monospacedFont && av.getShowText() && allGroups.length == 0
309 && !av.getColourText() && av.getThresholdTextColour() == 0)
311 if (av.isRenderGaps())
313 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
317 char gap = av.getGapCharacter();
318 graphics.drawString(seq.getSequenceAsString(start, end + 1)
319 .replace(gap, ' '), 0, y1);
324 boolean srep = av.isDisplayReferenceSeq();
325 boolean getboxColour = false;
326 boolean isarep = av.getAlignment().getSeqrep() == seq;
327 boolean isgrep = currentSequenceGroup != null ? currentSequenceGroup
328 .getSeqrep() == seq : false;
330 for (int i = start; i <= end; i++)
333 graphics.setColor(av.getTextColour());
334 getboxColour = false;
335 s = seq.getCharAt(i);
337 if (!renderGaps && jalview.util.Comparison.isGap(s))
342 if (inCurrentSequenceGroup(i))
344 if (!currentSequenceGroup.getDisplayText())
349 if (currentSequenceGroup.thresholdTextColour > 0
350 || currentSequenceGroup.getColourText())
353 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq,
356 if (currentSequenceGroup.getColourText())
358 graphics.setColor(resBoxColour.darker());
361 if (currentSequenceGroup.thresholdTextColour > 0)
363 if (resBoxColour.getRed() + resBoxColour.getBlue()
364 + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
366 graphics.setColor(currentSequenceGroup.textColour2);
372 graphics.setColor(currentSequenceGroup.textColour);
374 if (!isarep && !isgrep
375 && currentSequenceGroup.getShowNonconserved()) // todo
378 // todo - use sequence group consensus
379 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
386 if (!av.getShowText())
391 if (av.getColourText())
394 getBoxColour(av.getResidueShading(), seq, i);
396 if (av.getShowBoxes())
398 graphics.setColor(resBoxColour.darker());
402 graphics.setColor(resBoxColour);
406 if (av.getThresholdTextColour() > 0)
410 getBoxColour(av.getResidueShading(), seq, i);
413 if (resBoxColour.getRed() + resBoxColour.getBlue()
414 + resBoxColour.getGreen() < av.getThresholdTextColour())
416 graphics.setColor(av.getTextColour2());
419 if (!isarep && av.getShowUnconserved())
421 s = getDisplayChar(srep, i, s, '.', null);
427 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
428 graphics.drawString(String.valueOf(s),
429 charOffset + av.getCharWidth() * (i - start), y1);
436 * Returns 'conservedChar' to represent the given position if the sequence
437 * character at that position is equal to the consensus (ignoring case), else
438 * returns the sequence character
442 * @param sequenceChar
443 * @param conservedChar
446 private char getDisplayChar(final boolean usesrep, int position,
447 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
449 // TODO - use currentSequenceGroup rather than alignment
450 // currentSequenceGroup.getConsensus()
451 char conschar = (usesrep) ? (currentGroup == null
452 || position < currentGroup.getStartRes()
453 || position > currentGroup.getEndRes() ? av.getAlignment()
454 .getSeqrep().getCharAt(position)
455 : (currentGroup.getSeqrep() != null ? currentGroup.getSeqrep()
456 .getCharAt(position) : av.getAlignment().getSeqrep()
457 .getCharAt(position)))
458 : (currentGroup != null && currentGroup.getConsensus() != null
459 && position >= currentGroup.getStartRes()
460 && position <= currentGroup.getEndRes() && currentGroup
461 .getConsensus().annotations.length > position) ? currentGroup
462 .getConsensus().annotations[position].displayCharacter
464 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
466 if (!jalview.util.Comparison.isGap(conschar)
467 && (sequenceChar == conschar || sequenceChar + CHAR_TO_UPPER == conschar))
469 sequenceChar = conservedChar;
480 * @return DOCUMENT ME!
482 boolean inCurrentSequenceGroup(int res)
484 if (allGroups == null)
489 for (int i = 0; i < allGroups.length; i++)
491 if ((allGroups[i].getStartRes() <= res)
492 && (allGroups[i].getEndRes() >= res))
494 currentSequenceGroup = allGroups[i];
521 public void drawHighlightedText(SequenceI seq, int start, int end,
524 int pady = av.getCharHeight() / 5;
526 graphics.setColor(Color.BLACK);
527 graphics.fillRect(x1, y1, av.getCharWidth() * (end - start + 1),
529 graphics.setColor(Color.white);
533 // Need to find the sequence position here.
534 if (av.isValidCharWidth())
536 for (int i = start; i <= end; i++)
538 if (i < seq.getLength())
540 s = seq.getCharAt(i);
543 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
544 graphics.drawString(String.valueOf(s),
545 charOffset + x1 + (av.getCharWidth() * (i - start)),
546 (y1 + av.getCharHeight()) - pady);
551 public void drawCursor(SequenceI seq, int res, int x1, int y1)
553 int pady = av.getCharHeight() / 5;
555 graphics.setColor(Color.black);
556 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
558 if (av.isValidCharWidth())
560 graphics.setColor(Color.white);
562 char s = seq.getCharAt(res);
564 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
565 graphics.drawString(String.valueOf(s), charOffset + x1,
566 (y1 + av.getCharHeight()) - pady);