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;
38 public class SequenceRenderer implements jalview.api.SequenceRenderer
40 final static int CHAR_TO_UPPER = 'A' - 'a';
46 boolean renderGaps = true;
48 SequenceGroup currentSequenceGroup = null;
50 SequenceGroup[] allGroups = null;
56 boolean monospacedFont;
58 boolean forOverview = false;
61 * Creates a new SequenceRenderer object.
66 public SequenceRenderer(AlignViewport av)
77 public void prepare(Graphics g, boolean renderGaps)
80 fm = g.getFontMetrics();
82 // If EPS graphics, stringWidth will be a double, not an int
83 double dwidth = fm.getStringBounds("M", g).getWidth();
85 monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth() && av
86 .getCharWidth() == dwidth);
88 this.renderGaps = renderGaps;
92 public Color getResidueBoxColour(SequenceI seq, int i)
94 allGroups = av.getAlignment().findAllGroups(seq);
96 if (inCurrentSequenceGroup(i))
98 if (currentSequenceGroup.getDisplayBoxes())
100 getBoxColour(currentSequenceGroup.cs, seq, i);
103 else if (av.getShowBoxes())
105 getBoxColour(av.getGlobalColourScheme(), seq, i);
112 * Get the residue colour at the given sequence position - as determined by
113 * the sequence group colour (if any), else the colour scheme, possibly
114 * overridden by a feature colour.
122 public Color getResidueColour(final SequenceI seq, int position,
125 // TODO replace 8 or so code duplications with calls to this method
126 // (refactored as needed)
127 Color col = getResidueBoxColour(seq, position);
131 col = fr.findFeatureColour(col, seq, position);
146 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
150 resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
153 && !jalview.util.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.validCharWidth)
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.cs, seq, i);
245 else if (av.getShowBoxes())
247 getBoxColour(av.getGlobalColourScheme(), 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.cs, seq, i);
355 if (currentSequenceGroup.getColourText())
357 graphics.setColor(resBoxColour.darker());
360 if (currentSequenceGroup.thresholdTextColour > 0)
362 if (resBoxColour.getRed() + resBoxColour.getBlue()
363 + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
365 graphics.setColor(currentSequenceGroup.textColour2);
371 graphics.setColor(currentSequenceGroup.textColour);
373 if (!isarep && !isgrep
374 && currentSequenceGroup.getShowNonconserved()) // todo
377 // todo - use sequence group consensus
378 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
385 if (!av.getShowText())
390 if (av.getColourText())
393 getBoxColour(av.getGlobalColourScheme(), seq, i);
395 if (av.getShowBoxes())
397 graphics.setColor(resBoxColour.darker());
401 graphics.setColor(resBoxColour);
405 if (av.getThresholdTextColour() > 0)
409 getBoxColour(av.getGlobalColourScheme(), seq, i);
412 if (resBoxColour.getRed() + resBoxColour.getBlue()
413 + resBoxColour.getGreen() < av.getThresholdTextColour())
415 graphics.setColor(av.getTextColour2());
418 if (!isarep && av.getShowUnconserved())
420 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
426 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
427 graphics.drawString(String.valueOf(s),
428 charOffset + av.getCharWidth() * (i - start), y1);
435 * Returns 'conservedChar' to represent the given position if the sequence
436 * character at that position is equal to the consensus (ignoring case), else
437 * returns the sequence character
441 * @param sequenceChar
442 * @param conservedChar
445 private char getDisplayChar(final boolean usesrep, int position,
446 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
448 // TODO - use currentSequenceGroup rather than alignment
449 // currentSequenceGroup.getConsensus()
450 char conschar = (usesrep) ? (currentGroup == null ? av.getAlignment()
451 .getSeqrep().getCharAt(position)
452 : (currentGroup.getSeqrep() != null ? currentGroup.getSeqrep()
453 .getCharAt(position) : av.getAlignment().getSeqrep()
454 .getCharAt(position)))
455 : (currentGroup != null && currentGroup.getConsensus() != null) ? currentGroup
456 .getConsensus().annotations[position].displayCharacter
458 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
460 if (!jalview.util.Comparison.isGap(conschar)
461 && (sequenceChar == conschar || sequenceChar + CHAR_TO_UPPER == conschar))
463 sequenceChar = conservedChar;
474 * @return DOCUMENT ME!
476 boolean inCurrentSequenceGroup(int res)
478 if (allGroups == null)
483 for (int i = 0; i < allGroups.length; i++)
485 if ((allGroups[i].getStartRes() <= res)
486 && (allGroups[i].getEndRes() >= res))
488 currentSequenceGroup = allGroups[i];
515 public void drawHighlightedText(SequenceI seq, int start, int end,
518 int pady = av.getCharHeight() / 5;
520 graphics.setColor(Color.BLACK);
521 graphics.fillRect(x1, y1, av.getCharWidth() * (end - start + 1),
523 graphics.setColor(Color.white);
527 // Need to find the sequence position here.
528 if (av.isValidCharWidth())
530 for (int i = start; i <= end; i++)
532 if (i < seq.getLength())
534 s = seq.getCharAt(i);
537 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
538 graphics.drawString(String.valueOf(s),
539 charOffset + x1 + (av.getCharWidth() * (i - start)),
540 (y1 + av.getCharHeight()) - pady);
545 public void drawCursor(SequenceI seq, int res, int x1, int y1)
547 int pady = av.getCharHeight() / 5;
549 graphics.setColor(Color.black);
550 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
552 if (av.isValidCharWidth())
554 graphics.setColor(Color.white);
556 char s = seq.getCharAt(res);
558 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
559 graphics.drawString(String.valueOf(s), charOffset + x1,
560 (y1 + av.getCharHeight()) - pady);