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.FeatureRenderer;
24 import jalview.datamodel.SequenceGroup;
25 import jalview.datamodel.SequenceI;
26 import jalview.renderer.ResidueShaderI;
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.
61 public SequenceRenderer(AlignViewport av)
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;
87 public Color getResidueBoxColour(SequenceI seq, int i)
89 // rate limiting step when rendering overview for lots of groups
90 allGroups = av.getAlignment().findAllGroups(seq);
92 if (inCurrentSequenceGroup(i))
94 if (currentSequenceGroup.getDisplayBoxes())
96 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq, i);
99 else if (av.getShowBoxes())
101 getBoxColour(av.getResidueShading(), seq, i);
108 * Get the residue colour at the given sequence position - as determined by
109 * the sequence group colour (if any), else the colour scheme, possibly
110 * overridden by a feature colour.
118 public Color getResidueColour(final SequenceI seq, int position,
121 // TODO replace 8 or so code duplications with calls to this method
122 // (refactored as needed)
123 Color col = getResidueBoxColour(seq, position);
127 col = fr.findFeatureColour(col, seq, position);
142 void getBoxColour(ResidueShaderI shader, SequenceI seq, int i)
146 resBoxColour = shader.findColour(seq.getCharAt(i),
149 else if (forOverview && !Comparison.isGap(seq.getCharAt(i)))
151 resBoxColour = Color.lightGray;
155 resBoxColour = Color.white;
181 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
186 drawBoxes(seq, start, end, y1);
188 if (av.validCharWidth)
190 drawText(seq, start, end, y1);
212 public synchronized void drawBoxes(SequenceI seq, int start, int end,
217 return; // fix for racecondition
220 int length = seq.getLength();
223 int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(), avHeight = av
226 Color tempColour = null;
230 resBoxColour = Color.white;
234 if (inCurrentSequenceGroup(i))
236 if (currentSequenceGroup.getDisplayBoxes())
238 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq,
242 else if (av.getShowBoxes())
244 getBoxColour(av.getResidueShading(), seq, i);
248 if (resBoxColour != tempColour)
250 if (tempColour != null)
252 graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
256 graphics.setColor(resBoxColour);
260 tempColour = resBoxColour;
270 graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
292 public void drawText(SequenceI seq, int start, int end, int y1)
294 y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
298 if (end + 1 >= seq.getLength())
300 end = seq.getLength() - 1;
302 graphics.setColor(av.getTextColour());
304 if (monospacedFont && av.getShowText() && allGroups.length == 0
305 && !av.getColourText() && av.getThresholdTextColour() == 0)
307 if (av.isRenderGaps())
309 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
313 char gap = av.getGapCharacter();
314 graphics.drawString(seq.getSequenceAsString(start, end + 1)
315 .replace(gap, ' '), 0, y1);
320 boolean srep = av.isDisplayReferenceSeq();
321 boolean getboxColour = false;
322 boolean isarep = av.getAlignment().getSeqrep() == seq;
323 boolean isgrep = currentSequenceGroup != null ? currentSequenceGroup
324 .getSeqrep() == seq : false;
326 for (int i = start; i <= end; i++)
329 graphics.setColor(av.getTextColour());
330 getboxColour = false;
331 s = seq.getCharAt(i);
333 if (!renderGaps && jalview.util.Comparison.isGap(s))
338 if (inCurrentSequenceGroup(i))
340 if (!currentSequenceGroup.getDisplayText())
345 if (currentSequenceGroup.thresholdTextColour > 0
346 || currentSequenceGroup.getColourText())
349 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq,
352 if (currentSequenceGroup.getColourText())
354 graphics.setColor(resBoxColour.darker());
357 if (currentSequenceGroup.thresholdTextColour > 0)
359 if (resBoxColour.getRed() + resBoxColour.getBlue()
360 + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
362 graphics.setColor(currentSequenceGroup.textColour2);
368 graphics.setColor(currentSequenceGroup.textColour);
370 if (!isarep && !isgrep
371 && currentSequenceGroup.getShowNonconserved()) // todo
374 // todo - use sequence group consensus
375 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
382 if (!av.getShowText())
387 if (av.getColourText())
390 getBoxColour(av.getResidueShading(), seq, i);
392 if (av.getShowBoxes())
394 graphics.setColor(resBoxColour.darker());
398 graphics.setColor(resBoxColour);
402 if (av.getThresholdTextColour() > 0)
406 getBoxColour(av.getResidueShading(), seq, i);
409 if (resBoxColour.getRed() + resBoxColour.getBlue()
410 + resBoxColour.getGreen() < av.getThresholdTextColour())
412 graphics.setColor(av.getTextColour2());
415 if (!isarep && av.getShowUnconserved())
417 s = getDisplayChar(srep, i, s, '.', null);
423 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
424 graphics.drawString(String.valueOf(s),
425 charOffset + av.getCharWidth() * (i - start), y1);
432 * Returns 'conservedChar' to represent the given position if the sequence
433 * character at that position is equal to the consensus (ignoring case), else
434 * returns the sequence character
438 * @param sequenceChar
439 * @param conservedChar
442 private char getDisplayChar(final boolean usesrep, int position,
443 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
445 // TODO - use currentSequenceGroup rather than alignment
446 // currentSequenceGroup.getConsensus()
447 char conschar = (usesrep) ? (currentGroup == null
448 || position < currentGroup.getStartRes()
449 || position > currentGroup.getEndRes() ? av.getAlignment()
450 .getSeqrep().getCharAt(position)
451 : (currentGroup.getSeqrep() != null ? currentGroup.getSeqrep()
452 .getCharAt(position) : av.getAlignment().getSeqrep()
453 .getCharAt(position)))
454 : (currentGroup != null && currentGroup.getConsensus() != null
455 && position >= currentGroup.getStartRes()
456 && position <= currentGroup.getEndRes() && currentGroup
457 .getConsensus().annotations.length > position) ? currentGroup
458 .getConsensus().annotations[position].displayCharacter
460 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
462 if (!jalview.util.Comparison.isGap(conschar)
463 && (sequenceChar == conschar || sequenceChar + CHAR_TO_UPPER == conschar))
465 sequenceChar = conservedChar;
476 * @return DOCUMENT ME!
478 boolean inCurrentSequenceGroup(int res)
480 if (allGroups == null)
485 for (int i = 0; i < allGroups.length; i++)
487 if ((allGroups[i].getStartRes() <= res)
488 && (allGroups[i].getEndRes() >= res))
490 currentSequenceGroup = allGroups[i];
517 public void drawHighlightedText(SequenceI seq, int start, int end,
520 int pady = av.getCharHeight() / 5;
522 graphics.setColor(Color.BLACK);
523 graphics.fillRect(x1, y1, av.getCharWidth() * (end - start + 1),
525 graphics.setColor(Color.white);
529 // Need to find the sequence position here.
530 if (av.isValidCharWidth())
532 for (int i = start; i <= end; i++)
534 if (i < seq.getLength())
536 s = seq.getCharAt(i);
539 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
540 graphics.drawString(String.valueOf(s),
541 charOffset + x1 + (av.getCharWidth() * (i - start)),
542 (y1 + av.getCharHeight()) - pady);
547 public void drawCursor(SequenceI seq, int res, int x1, int y1)
549 int pady = av.getCharHeight() / 5;
551 graphics.setColor(Color.black);
552 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
554 if (av.isValidCharWidth())
556 graphics.setColor(Color.white);
558 char s = seq.getCharAt(res);
560 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
561 graphics.drawString(String.valueOf(s), charOffset + x1,
562 (y1 + av.getCharHeight()) - pady);