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.datamodel.SequenceGroup;
24 import jalview.datamodel.SequenceI;
25 import jalview.renderer.ResidueShaderI;
26 import jalview.renderer.seqfeatures.FeatureColourFinder;
27 import jalview.util.Comparison;
29 import java.awt.Color;
30 import java.awt.FontMetrics;
31 import java.awt.Graphics;
33 public class SequenceRenderer implements jalview.api.SequenceRenderer
35 final static int CHAR_TO_UPPER = 'A' - 'a';
41 boolean renderGaps = true;
43 SequenceGroup currentSequenceGroup = null;
45 SequenceGroup[] allGroups = null;
51 boolean monospacedFont;
53 boolean forOverview = false;
56 * Creates a new SequenceRenderer object
60 public SequenceRenderer(AlignViewport viewport)
71 public void prepare(Graphics g, boolean renderGaps)
74 fm = g.getFontMetrics();
76 // If EPS graphics, stringWidth will be a double, not an int
77 double dwidth = fm.getStringBounds("M", g).getWidth();
79 monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth() && av
80 .getCharWidth() == dwidth);
82 this.renderGaps = renderGaps;
85 protected Color getResidueBoxColour(SequenceI seq, int i)
87 // rate limiting step when rendering overview for lots of groups
88 allGroups = av.getAlignment().findAllGroups(seq);
90 if (inCurrentSequenceGroup(i))
92 if (currentSequenceGroup.getDisplayBoxes())
94 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq, i);
97 else if (av.getShowBoxes())
99 getBoxColour(av.getResidueShading(), seq, i);
106 * Get the residue colour at the given sequence position - as determined by
107 * the sequence group colour (if any), else the colour scheme, possibly
108 * overridden by a feature colour.
116 public Color getResidueColour(final SequenceI seq, int position,
117 FeatureColourFinder finder)
119 Color col = getResidueBoxColour(seq, position);
123 col = finder.findFeatureColour(col, seq, position);
138 void getBoxColour(ResidueShaderI shader, SequenceI seq, int i)
142 resBoxColour = shader.findColour(seq.getCharAt(i),
145 else if (forOverview && !Comparison.isGap(seq.getCharAt(i)))
147 resBoxColour = Color.lightGray;
151 resBoxColour = Color.white;
177 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
182 drawBoxes(seq, start, end, y1);
184 if (av.validCharWidth)
186 drawText(seq, start, end, y1);
208 public synchronized void drawBoxes(SequenceI seq, int start, int end,
213 return; // fix for racecondition
216 int length = seq.getLength();
219 int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(), avHeight = av
222 Color tempColour = null;
226 resBoxColour = Color.white;
230 if (inCurrentSequenceGroup(i))
232 if (currentSequenceGroup.getDisplayBoxes())
234 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq,
238 else if (av.getShowBoxes())
240 getBoxColour(av.getResidueShading(), seq, i);
244 if (resBoxColour != tempColour)
246 if (tempColour != null)
248 graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
252 graphics.setColor(resBoxColour);
256 tempColour = resBoxColour;
266 graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
288 public void drawText(SequenceI seq, int start, int end, int y1)
290 y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
294 if (end + 1 >= seq.getLength())
296 end = seq.getLength() - 1;
298 graphics.setColor(av.getTextColour());
300 if (monospacedFont && av.getShowText() && allGroups.length == 0
301 && !av.getColourText() && av.getThresholdTextColour() == 0)
303 if (av.isRenderGaps())
305 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
309 char gap = av.getGapCharacter();
310 graphics.drawString(seq.getSequenceAsString(start, end + 1)
311 .replace(gap, ' '), 0, y1);
316 boolean srep = av.isDisplayReferenceSeq();
317 boolean getboxColour = false;
318 boolean isarep = av.getAlignment().getSeqrep() == seq;
319 boolean isgrep = currentSequenceGroup != null ? currentSequenceGroup
320 .getSeqrep() == seq : false;
322 for (int i = start; i <= end; i++)
325 graphics.setColor(av.getTextColour());
326 getboxColour = false;
327 s = seq.getCharAt(i);
329 if (!renderGaps && jalview.util.Comparison.isGap(s))
334 if (inCurrentSequenceGroup(i))
336 if (!currentSequenceGroup.getDisplayText())
341 if (currentSequenceGroup.thresholdTextColour > 0
342 || currentSequenceGroup.getColourText())
345 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq,
348 if (currentSequenceGroup.getColourText())
350 graphics.setColor(resBoxColour.darker());
353 if (currentSequenceGroup.thresholdTextColour > 0)
355 if (resBoxColour.getRed() + resBoxColour.getBlue()
356 + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
358 graphics.setColor(currentSequenceGroup.textColour2);
364 graphics.setColor(currentSequenceGroup.textColour);
366 if (!isarep && !isgrep
367 && currentSequenceGroup.getShowNonconserved()) // todo
370 // todo - use sequence group consensus
371 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
378 if (!av.getShowText())
383 if (av.getColourText())
386 getBoxColour(av.getResidueShading(), seq, i);
388 if (av.getShowBoxes())
390 graphics.setColor(resBoxColour.darker());
394 graphics.setColor(resBoxColour);
398 if (av.getThresholdTextColour() > 0)
402 getBoxColour(av.getResidueShading(), seq, i);
405 if (resBoxColour.getRed() + resBoxColour.getBlue()
406 + resBoxColour.getGreen() < av.getThresholdTextColour())
408 graphics.setColor(av.getTextColour2());
411 if (!isarep && av.getShowUnconserved())
413 s = getDisplayChar(srep, i, s, '.', null);
419 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
420 graphics.drawString(String.valueOf(s),
421 charOffset + av.getCharWidth() * (i - start), y1);
428 * Returns 'conservedChar' to represent the given position if the sequence
429 * character at that position is equal to the consensus (ignoring case), else
430 * returns the sequence character
434 * @param sequenceChar
435 * @param conservedChar
438 private char getDisplayChar(final boolean usesrep, int position,
439 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
441 // TODO - use currentSequenceGroup rather than alignment
442 // currentSequenceGroup.getConsensus()
443 char conschar = (usesrep) ? (currentGroup == null
444 || position < currentGroup.getStartRes()
445 || position > currentGroup.getEndRes() ? av.getAlignment()
446 .getSeqrep().getCharAt(position)
447 : (currentGroup.getSeqrep() != null ? currentGroup.getSeqrep()
448 .getCharAt(position) : av.getAlignment().getSeqrep()
449 .getCharAt(position)))
450 : (currentGroup != null && currentGroup.getConsensus() != null
451 && position >= currentGroup.getStartRes()
452 && position <= currentGroup.getEndRes() && currentGroup
453 .getConsensus().annotations.length > position) ? currentGroup
454 .getConsensus().annotations[position].displayCharacter
456 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
458 if (!jalview.util.Comparison.isGap(conschar)
459 && (sequenceChar == conschar || sequenceChar + CHAR_TO_UPPER == conschar))
461 sequenceChar = conservedChar;
472 * @return DOCUMENT ME!
474 boolean inCurrentSequenceGroup(int res)
476 if (allGroups == null)
481 for (int i = 0; i < allGroups.length; i++)
483 if ((allGroups[i].getStartRes() <= res)
484 && (allGroups[i].getEndRes() >= res))
486 currentSequenceGroup = allGroups[i];
513 public void drawHighlightedText(SequenceI seq, int start, int end,
516 int pady = av.getCharHeight() / 5;
518 graphics.setColor(Color.BLACK);
519 graphics.fillRect(x1, y1, av.getCharWidth() * (end - start + 1),
521 graphics.setColor(Color.white);
525 // Need to find the sequence position here.
526 if (av.isValidCharWidth())
528 for (int i = start; i <= end; i++)
530 if (i < seq.getLength())
532 s = seq.getCharAt(i);
535 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
536 graphics.drawString(String.valueOf(s),
537 charOffset + x1 + (av.getCharWidth() * (i - start)),
538 (y1 + av.getCharHeight()) - pady);
543 public void drawCursor(SequenceI seq, int res, int x1, int y1)
545 int pady = av.getCharHeight() / 5;
547 graphics.setColor(Color.black);
548 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
550 if (av.isValidCharWidth())
552 graphics.setColor(Color.white);
554 char s = seq.getCharAt(res);
556 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
557 graphics.drawString(String.valueOf(s), charOffset + x1,
558 (y1 + av.getCharHeight()) - pady);