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.AlignViewportI;
24 import jalview.datamodel.SequenceGroup;
25 import jalview.datamodel.SequenceI;
26 import jalview.renderer.ResidueShaderI;
27 import jalview.renderer.seqfeatures.FeatureColourFinder;
28 import jalview.util.Comparison;
30 import java.awt.Color;
31 import java.awt.FontMetrics;
32 import java.awt.Graphics;
34 public class SequenceRenderer implements jalview.api.SequenceRenderer
36 final static int CHAR_TO_UPPER = 'A' - 'a';
42 boolean renderGaps = true;
44 SequenceGroup currentSequenceGroup = null;
46 SequenceGroup[] allGroups = null;
52 boolean monospacedFont;
54 boolean forOverview = false;
57 * Creates a new SequenceRenderer object
61 public SequenceRenderer(AlignViewportI viewport)
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;
86 protected Color getResidueBoxColour(SequenceI seq, int i)
88 // rate limiting step when rendering overview for lots of groups
89 allGroups = av.getAlignment().findAllGroups(seq);
91 if (inCurrentSequenceGroup(i))
93 if (currentSequenceGroup.getDisplayBoxes())
95 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq, i);
98 else if (av.getShowBoxes())
100 getBoxColour(av.getResidueShading(), seq, i);
107 * Get the residue colour at the given sequence position - as determined by
108 * the sequence group colour (if any), else the colour scheme, possibly
109 * overridden by a feature colour.
117 public Color getResidueColour(final SequenceI seq, int position,
118 FeatureColourFinder finder)
120 Color col = getResidueBoxColour(seq, position);
124 col = finder.findFeatureColour(col, seq, position);
139 void getBoxColour(ResidueShaderI shader, SequenceI seq, int i)
141 if (shader.getColourScheme() != null)
143 resBoxColour = shader.findColour(seq.getCharAt(i),
146 else if (forOverview && !Comparison.isGap(seq.getCharAt(i)))
148 resBoxColour = Color.lightGray;
152 resBoxColour = Color.white;
178 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
183 drawBoxes(seq, start, end, y1);
185 if (av.isValidCharWidth())
187 drawText(seq, start, end, y1);
209 public synchronized void drawBoxes(SequenceI seq, int start, int end,
214 return; // fix for racecondition
217 int length = seq.getLength();
220 int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(), avHeight = av
223 Color tempColour = null;
227 resBoxColour = Color.white;
231 if (inCurrentSequenceGroup(i))
233 if (currentSequenceGroup.getDisplayBoxes())
235 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq,
239 else if (av.getShowBoxes())
241 getBoxColour(av.getResidueShading(), seq, i);
245 if (resBoxColour != tempColour)
247 if (tempColour != null)
249 graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
253 graphics.setColor(resBoxColour);
257 tempColour = resBoxColour;
267 graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
289 public void drawText(SequenceI seq, int start, int end, int y1)
291 y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
295 if (end + 1 >= seq.getLength())
297 end = seq.getLength() - 1;
299 graphics.setColor(av.getTextColour());
301 if (monospacedFont && av.getShowText() && allGroups.length == 0
302 && !av.getColourText() && av.getThresholdTextColour() == 0)
304 if (av.isRenderGaps())
306 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
310 char gap = av.getGapCharacter();
311 graphics.drawString(seq.getSequenceAsString(start, end + 1)
312 .replace(gap, ' '), 0, y1);
317 boolean srep = av.isDisplayReferenceSeq();
318 boolean getboxColour = false;
319 boolean isarep = av.getAlignment().getSeqrep() == seq;
320 boolean isgrep = currentSequenceGroup != null ? currentSequenceGroup
321 .getSeqrep() == seq : false;
323 for (int i = start; i <= end; i++)
326 graphics.setColor(av.getTextColour());
327 getboxColour = false;
328 s = seq.getCharAt(i);
330 if (!renderGaps && jalview.util.Comparison.isGap(s))
335 if (inCurrentSequenceGroup(i))
337 if (!currentSequenceGroup.getDisplayText())
342 if (currentSequenceGroup.thresholdTextColour > 0
343 || currentSequenceGroup.getColourText())
346 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq,
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.getResidueShading(), seq, i);
389 if (av.getShowBoxes())
391 graphics.setColor(resBoxColour.darker());
395 graphics.setColor(resBoxColour);
399 if (av.getThresholdTextColour() > 0)
403 getBoxColour(av.getResidueShading(), 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);