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 for (int i = start; i <= end; i++)
328 graphics.setColor(av.getTextColour());
329 getboxColour = false;
330 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 (currentSequenceGroup.getShowNonconserved()) // todo optimize
369 // todo - use sequence group consensus
370 s = getDisplayChar(srep, i, s,
378 if (!av.getShowText())
383 if (av.getColourText())
386 getBoxColour(av.getGlobalColourScheme(), seq, i);
388 if (av.getShowBoxes())
390 graphics.setColor(resBoxColour.darker());
394 graphics.setColor(resBoxColour);
398 if (av.getThresholdTextColour() > 0)
402 getBoxColour(av.getGlobalColourScheme(), seq, i);
405 if (resBoxColour.getRed() + resBoxColour.getBlue()
406 + resBoxColour.getGreen() < av.getThresholdTextColour())
408 graphics.setColor(av.getTextColour2());
411 if (av.getShowUnconserved())
413 s = getDisplayChar(srep, i, s,
420 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
421 graphics.drawString(String.valueOf(s),
422 charOffset + av.getCharWidth()
430 * Returns 'conservedChar' to represent the given position if the sequence
431 * character at that position is equal to the consensus (ignoring case), else
432 * returns the sequence character
436 * @param sequenceChar
437 * @param conservedChar
440 private char getDisplayChar(final boolean usesrep, int position,
441 char sequenceChar, char conservedChar)
443 // TODO - use currentSequenceGroup rather than alignment
444 // currentSequenceGroup.getConsensus()
445 char conschar = (usesrep) ? av.getAlignment().getSeqrep().getCharAt(position) : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
448 && (sequenceChar == conschar || sequenceChar + CHAR_TO_UPPER == conschar))
450 sequenceChar = conservedChar;
461 * @return DOCUMENT ME!
463 boolean inCurrentSequenceGroup(int res)
465 if (allGroups == null)
470 for (int i = 0; i < allGroups.length; i++)
472 if ((allGroups[i].getStartRes() <= res)
473 && (allGroups[i].getEndRes() >= res))
475 currentSequenceGroup = allGroups[i];
502 public void drawHighlightedText(SequenceI seq, int start, int end,
505 int pady = av.getCharHeight() / 5;
507 graphics.setColor(Color.BLACK);
508 graphics.fillRect(x1, y1, av.getCharWidth() * (end - start + 1),
510 graphics.setColor(Color.white);
514 // Need to find the sequence position here.
515 if (av.isValidCharWidth())
517 for (int i = start; i <= end; i++)
519 if (i < seq.getLength())
521 s = seq.getCharAt(i);
524 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
525 graphics.drawString(String.valueOf(s),
526 charOffset + x1 + (av.getCharWidth() * (i - start)),
527 (y1 + av.getCharHeight()) - pady);
532 public void drawCursor(SequenceI seq, int res, int x1, int y1)
534 int pady = av.getCharHeight() / 5;
536 graphics.setColor(Color.black);
537 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
539 if (av.isValidCharWidth())
541 graphics.setColor(Color.white);
543 char s = seq.getCharAt(res);
545 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
546 graphics.drawString(String.valueOf(s), charOffset + x1,
547 (y1 + av.getCharHeight()) - pady);