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.schemes.ColourSchemeI;
28 import java.awt.Color;
29 import java.awt.FontMetrics;
30 import java.awt.Graphics;
32 public class SequenceRenderer implements jalview.api.SequenceRenderer
34 final static int CHAR_TO_UPPER = 'A' - 'a';
40 boolean renderGaps = true;
42 SequenceGroup currentSequenceGroup = null;
44 SequenceGroup[] allGroups = null;
50 boolean monospacedFont;
52 boolean forOverview = false;
55 * Creates a new SequenceRenderer object.
60 public SequenceRenderer(AlignViewport av)
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;
86 public Color getResidueBoxColour(SequenceI seq, int i)
88 allGroups = av.getAlignment().findAllGroups(seq);
90 if (inCurrentSequenceGroup(i))
92 if (currentSequenceGroup.getDisplayBoxes())
94 getBoxColour(currentSequenceGroup.cs, seq, i);
97 else if (av.getShowBoxes())
99 getBoxColour(av.getGlobalColourScheme(), 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,
119 // TODO replace 8 or so code duplications with calls to this method
120 // (refactored as needed)
121 Color col = getResidueBoxColour(seq, position);
125 col = fr.findFeatureColour(col, seq, position);
140 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
144 resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
147 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
149 resBoxColour = Color.lightGray;
153 resBoxColour = Color.white;
179 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
184 drawBoxes(seq, start, end, y1);
186 if (av.validCharWidth)
188 drawText(seq, start, end, y1);
210 public synchronized void drawBoxes(SequenceI seq, int start, int end,
215 return; // fix for racecondition
218 int length = seq.getLength();
221 int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(), avHeight = av
224 Color tempColour = null;
228 resBoxColour = Color.white;
232 if (inCurrentSequenceGroup(i))
234 if (currentSequenceGroup.getDisplayBoxes())
236 getBoxColour(currentSequenceGroup.cs, seq, i);
239 else if (av.getShowBoxes())
241 getBoxColour(av.getGlobalColourScheme(), seq, i);
246 if (resBoxColour != tempColour)
248 if (tempColour != null)
250 graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
254 graphics.setColor(resBoxColour);
258 tempColour = resBoxColour;
268 graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
290 public void drawText(SequenceI seq, int start, int end, int y1)
292 y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
296 if (end + 1 >= seq.getLength())
298 end = seq.getLength() - 1;
300 graphics.setColor(av.getTextColour());
302 if (monospacedFont && av.getShowText() && allGroups.length == 0
303 && !av.getColourText() && av.getThresholdTextColour() == 0)
305 if (av.isRenderGaps())
307 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
311 char gap = av.getGapCharacter();
312 graphics.drawString(seq.getSequenceAsString(start, end + 1)
313 .replace(gap, ' '), 0, y1);
318 boolean srep = av.isDisplayReferenceSeq();
319 boolean getboxColour = false;
320 boolean isarep = av.getAlignment().getSeqrep() == seq;
321 boolean isgrep = currentSequenceGroup != null ? currentSequenceGroup
322 .getSeqrep() == seq : false;
324 for (int i = start; i <= end; i++)
327 graphics.setColor(av.getTextColour());
328 getboxColour = false;
329 s = seq.getCharAt(i);
331 if (!renderGaps && jalview.util.Comparison.isGap(s))
336 if (inCurrentSequenceGroup(i))
338 if (!currentSequenceGroup.getDisplayText())
343 if (currentSequenceGroup.thresholdTextColour > 0
344 || currentSequenceGroup.getColourText())
347 getBoxColour(currentSequenceGroup.cs, seq, i);
349 if (currentSequenceGroup.getColourText())
351 graphics.setColor(resBoxColour.darker());
354 if (currentSequenceGroup.thresholdTextColour > 0)
356 if (resBoxColour.getRed() + resBoxColour.getBlue()
357 + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
359 graphics.setColor(currentSequenceGroup.textColour2);
365 graphics.setColor(currentSequenceGroup.textColour);
367 if (!isarep && !isgrep
368 && currentSequenceGroup.getShowNonconserved()) // todo
371 // todo - use sequence group consensus
372 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
379 if (!av.getShowText())
384 if (av.getColourText())
387 getBoxColour(av.getGlobalColourScheme(), seq, i);
389 if (av.getShowBoxes())
391 graphics.setColor(resBoxColour.darker());
395 graphics.setColor(resBoxColour);
399 if (av.getThresholdTextColour() > 0)
403 getBoxColour(av.getGlobalColourScheme(), seq, i);
406 if (resBoxColour.getRed() + resBoxColour.getBlue()
407 + resBoxColour.getGreen() < av.getThresholdTextColour())
409 graphics.setColor(av.getTextColour2());
412 if (!isarep && av.getShowUnconserved())
414 s = getDisplayChar(srep, i, s, '.', null);
420 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
421 graphics.drawString(String.valueOf(s),
422 charOffset + av.getCharWidth() * (i - start), y1);
429 * Returns 'conservedChar' to represent the given position if the sequence
430 * character at that position is equal to the consensus (ignoring case), else
431 * returns the sequence character
435 * @param sequenceChar
436 * @param conservedChar
439 private char getDisplayChar(final boolean usesrep, int position,
440 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
442 // TODO - use currentSequenceGroup rather than alignment
443 // currentSequenceGroup.getConsensus()
444 char conschar = (usesrep) ? (currentGroup == null
445 || position < currentGroup.getStartRes()
446 || position > currentGroup.getEndRes() ? av.getAlignment()
447 .getSeqrep().getCharAt(position)
448 : (currentGroup.getSeqrep() != null ? currentGroup.getSeqrep()
449 .getCharAt(position) : av.getAlignment().getSeqrep()
450 .getCharAt(position)))
451 : (currentGroup != null && currentGroup.getConsensus() != null
452 && position >= currentGroup.getStartRes()
453 && position <= currentGroup.getEndRes() && currentGroup
454 .getConsensus().annotations.length > position) ? currentGroup
455 .getConsensus().annotations[position].displayCharacter
457 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
459 if (!jalview.util.Comparison.isGap(conschar)
460 && (sequenceChar == conschar || sequenceChar + CHAR_TO_UPPER == conschar))
462 sequenceChar = conservedChar;
473 * @return DOCUMENT ME!
475 boolean inCurrentSequenceGroup(int res)
477 if (allGroups == null)
482 for (int i = 0; i < allGroups.length; i++)
484 if ((allGroups[i].getStartRes() <= res)
485 && (allGroups[i].getEndRes() >= res))
487 currentSequenceGroup = allGroups[i];
514 public void drawHighlightedText(SequenceI seq, int start, int end,
517 int pady = av.getCharHeight() / 5;
519 graphics.setColor(Color.BLACK);
520 graphics.fillRect(x1, y1, av.getCharWidth() * (end - start + 1),
522 graphics.setColor(Color.white);
526 // Need to find the sequence position here.
527 if (av.isValidCharWidth())
529 for (int i = start; i <= end; i++)
531 if (i < seq.getLength())
533 s = seq.getCharAt(i);
536 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
537 graphics.drawString(String.valueOf(s),
538 charOffset + x1 + (av.getCharWidth() * (i - start)),
539 (y1 + av.getCharHeight()) - pady);
544 public void drawCursor(SequenceI seq, int res, int x1, int y1)
546 int pady = av.getCharHeight() / 5;
548 graphics.setColor(Color.black);
549 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
551 if (av.isValidCharWidth())
553 graphics.setColor(Color.white);
555 char s = seq.getCharAt(res);
557 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
558 graphics.drawString(String.valueOf(s), charOffset + x1,
559 (y1 + av.getCharHeight()) - pady);