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.ColorI;
24 import jalview.api.FeatureRenderer;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.schemes.Colour;
28 import jalview.schemes.ColourSchemeI;
29 import jalview.util.ColorUtils;
31 import java.awt.Color;
32 import java.awt.FontMetrics;
33 import java.awt.Graphics;
35 public class SequenceRenderer implements jalview.api.SequenceRenderer
37 final static int CHAR_TO_UPPER = 'A' - 'a';
43 boolean renderGaps = true;
45 SequenceGroup currentSequenceGroup = null;
47 SequenceGroup[] allGroups = null;
53 boolean monospacedFont;
55 boolean forOverview = false;
58 * Creates a new SequenceRenderer object.
63 public SequenceRenderer(AlignViewport av)
74 public void prepare(Graphics g, boolean renderGaps)
77 fm = g.getFontMetrics();
79 // If EPS graphics, stringWidth will be a double, not an int
80 double dwidth = fm.getStringBounds("M", g).getWidth();
82 monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth() && av
83 .getCharWidth() == dwidth);
85 this.renderGaps = renderGaps;
89 public ColorI getResidueBoxColour(SequenceI seq, int i)
91 // rate limiting step when rendering overview for lots of groups
92 allGroups = av.getAlignment().findAllGroups(seq);
94 if (inCurrentSequenceGroup(i))
96 if (currentSequenceGroup.getDisplayBoxes())
98 getBoxColour(currentSequenceGroup.cs, seq, i);
101 else if (av.getShowBoxes())
103 getBoxColour(av.getGlobalColourScheme(), seq, i);
106 return new Colour(resBoxColour);
110 * Get the residue colour at the given sequence position - as determined by
111 * the sequence group colour (if any), else the colour scheme, possibly
112 * overridden by a feature colour.
120 public ColorI getResidueColour(final SequenceI seq, int position,
123 // TODO replace 8 or so code duplications with calls to this method
124 // (refactored as needed)
125 ColorI col = getResidueBoxColour(seq, position);
129 col = fr.findFeatureColour(col, seq, position);
144 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
148 resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
151 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
153 resBoxColour = Color.lightGray;
157 resBoxColour = Color.white;
183 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
188 drawBoxes(seq, start, end, y1);
190 if (av.validCharWidth)
192 drawText(seq, start, end, y1);
214 public synchronized void drawBoxes(SequenceI seq, int start, int end,
219 return; // fix for racecondition
222 int length = seq.getLength();
225 int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(), avHeight = av
228 Color tempColour = null;
232 resBoxColour = Color.white;
236 if (inCurrentSequenceGroup(i))
238 if (currentSequenceGroup.getDisplayBoxes())
240 getBoxColour(currentSequenceGroup.cs, seq, i);
243 else if (av.getShowBoxes())
245 getBoxColour(av.getGlobalColourScheme(), seq, i);
250 if (resBoxColour != tempColour)
252 if (tempColour != null)
254 graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
258 graphics.setColor(resBoxColour);
262 tempColour = resBoxColour;
272 graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
294 public void drawText(SequenceI seq, int start, int end, int y1)
296 y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
300 if (end + 1 >= seq.getLength())
302 end = seq.getLength() - 1;
304 graphics.setColor(ColorUtils.getColor(av.getTextColour()));
306 if (monospacedFont && av.getShowText() && allGroups.length == 0
307 && !av.getColourText() && av.getThresholdTextColour() == 0)
309 if (av.isRenderGaps())
311 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
315 char gap = av.getGapCharacter();
316 graphics.drawString(seq.getSequenceAsString(start, end + 1)
317 .replace(gap, ' '), 0, y1);
322 boolean srep = av.isDisplayReferenceSeq();
323 boolean getboxColour = false;
324 boolean isarep = av.getAlignment().getSeqrep() == seq;
325 boolean isgrep = currentSequenceGroup != null ? currentSequenceGroup
326 .getSeqrep() == seq : false;
328 for (int i = start; i <= end; i++)
331 graphics.setColor(ColorUtils.getColor(av.getTextColour()));
332 getboxColour = false;
333 s = seq.getCharAt(i);
335 if (!renderGaps && jalview.util.Comparison.isGap(s))
340 if (inCurrentSequenceGroup(i))
342 if (!currentSequenceGroup.getDisplayText())
347 if (currentSequenceGroup.thresholdTextColour > 0
348 || currentSequenceGroup.getColourText())
351 getBoxColour(currentSequenceGroup.cs, seq, i);
353 if (currentSequenceGroup.getColourText())
355 graphics.setColor(resBoxColour.darker());
358 if (currentSequenceGroup.thresholdTextColour > 0)
360 if (resBoxColour.getRed() + resBoxColour.getBlue()
361 + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
363 graphics.setColor(ColorUtils.getColor(currentSequenceGroup.textColour2));
369 graphics.setColor(ColorUtils.getColor(currentSequenceGroup.textColour));
371 if (!isarep && !isgrep
372 && currentSequenceGroup.getShowNonconserved()) // todo
375 // todo - use sequence group consensus
376 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
383 if (!av.getShowText())
388 if (av.getColourText())
391 getBoxColour(av.getGlobalColourScheme(), seq, i);
393 if (av.getShowBoxes())
395 graphics.setColor(resBoxColour.darker());
399 graphics.setColor(resBoxColour);
403 if (av.getThresholdTextColour() > 0)
407 getBoxColour(av.getGlobalColourScheme(), seq, i);
410 if (resBoxColour.getRed() + resBoxColour.getBlue()
411 + resBoxColour.getGreen() < av.getThresholdTextColour())
413 graphics.setColor(ColorUtils.getColor(av.getTextColour2()));
416 if (!isarep && av.getShowUnconserved())
418 s = getDisplayChar(srep, i, s, '.', null);
424 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
425 graphics.drawString(String.valueOf(s),
426 charOffset + av.getCharWidth() * (i - start), y1);
433 * Returns 'conservedChar' to represent the given position if the sequence
434 * character at that position is equal to the consensus (ignoring case), else
435 * returns the sequence character
439 * @param sequenceChar
440 * @param conservedChar
443 private char getDisplayChar(final boolean usesrep, int position,
444 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
446 // TODO - use currentSequenceGroup rather than alignment
447 // currentSequenceGroup.getConsensus()
448 char conschar = (usesrep) ? (currentGroup == null
449 || position < currentGroup.getStartRes()
450 || position > currentGroup.getEndRes() ? 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
456 && position >= currentGroup.getStartRes()
457 && position <= currentGroup.getEndRes() && currentGroup
458 .getConsensus().annotations.length > position) ? currentGroup
459 .getConsensus().annotations[position].displayCharacter
461 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
463 if (!jalview.util.Comparison.isGap(conschar)
464 && (sequenceChar == conschar || sequenceChar + CHAR_TO_UPPER == conschar))
466 sequenceChar = conservedChar;
477 * @return DOCUMENT ME!
479 boolean inCurrentSequenceGroup(int res)
481 if (allGroups == null)
486 for (int i = 0; i < allGroups.length; i++)
488 if ((allGroups[i].getStartRes() <= res)
489 && (allGroups[i].getEndRes() >= res))
491 currentSequenceGroup = allGroups[i];
518 public void drawHighlightedText(SequenceI seq, int start, int end,
521 int pady = av.getCharHeight() / 5;
523 graphics.setColor(Color.BLACK);
524 graphics.fillRect(x1, y1, av.getCharWidth() * (end - start + 1),
526 graphics.setColor(Color.white);
530 // Need to find the sequence position here.
531 if (av.isValidCharWidth())
533 for (int i = start; i <= end; i++)
535 if (i < seq.getLength())
537 s = seq.getCharAt(i);
540 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
541 graphics.drawString(String.valueOf(s),
542 charOffset + x1 + (av.getCharWidth() * (i - start)),
543 (y1 + av.getCharHeight()) - pady);
548 public void drawCursor(SequenceI seq, int res, int x1, int y1)
550 int pady = av.getCharHeight() / 5;
552 graphics.setColor(Color.black);
553 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
555 if (av.isValidCharWidth())
557 graphics.setColor(Color.white);
559 char s = seq.getCharAt(res);
561 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
562 graphics.drawString(String.valueOf(s), charOffset + x1,
563 (y1 + av.getCharHeight()) - pady);