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()
81 && av.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), i, seq);
145 else if (forOverview && !Comparison.isGap(seq.getCharAt(i)))
147 resBoxColour = Color.lightGray;
151 resBoxColour = Color.white;
177 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
182 drawBoxes(seq, start, end, y1);
184 if (av.isValidCharWidth())
186 drawText(seq, start, end, y1);
208 public synchronized void drawBoxes(SequenceI seq, int start, int end,
213 return; // fix for racecondition
216 int length = seq.getLength();
219 int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(),
220 avHeight = av.getCharHeight();
222 Color tempColour = null;
226 resBoxColour = Color.white;
230 if (inCurrentSequenceGroup(i))
232 if (currentSequenceGroup.getDisplayBoxes())
234 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq,
238 else if (av.getShowBoxes())
240 getBoxColour(av.getResidueShading(), seq, i);
244 if (resBoxColour != tempColour)
246 if (tempColour != null)
248 graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
252 graphics.setColor(resBoxColour);
256 tempColour = resBoxColour;
266 graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
288 public void drawText(SequenceI seq, int start, int end, int y1)
290 y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
294 if (end + 1 >= seq.getLength())
296 end = seq.getLength() - 1;
298 graphics.setColor(av.getTextColour());
300 if (monospacedFont && av.getShowText() && allGroups.length == 0
301 && !av.getColourText() && av.getThresholdTextColour() == 0)
303 if (av.isRenderGaps())
305 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
309 char gap = av.getGapCharacter();
311 seq.getSequenceAsString(start, end + 1).replace(gap, ' '),
317 boolean srep = av.isDisplayReferenceSeq();
318 boolean getboxColour = false;
319 boolean isarep = av.getAlignment().getSeqrep() == seq;
320 boolean isgrep = currentSequenceGroup != null
321 ? currentSequenceGroup.getSeqrep() == seq
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.getGroupColourScheme(), seq,
350 if (currentSequenceGroup.getColourText())
352 graphics.setColor(resBoxColour.darker());
355 if (currentSequenceGroup.thresholdTextColour > 0)
357 if (resBoxColour.getRed() + resBoxColour.getBlue()
359 .getGreen() < currentSequenceGroup.thresholdTextColour)
361 graphics.setColor(currentSequenceGroup.textColour2);
367 graphics.setColor(currentSequenceGroup.textColour);
369 if (!isarep && !isgrep
370 && currentSequenceGroup.getShowNonconserved()) // todo
373 // todo - use sequence group consensus
374 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
381 if (!av.getShowText())
386 if (av.getColourText())
389 getBoxColour(av.getResidueShading(), seq, i);
391 if (av.getShowBoxes())
393 graphics.setColor(resBoxColour.darker());
397 graphics.setColor(resBoxColour);
401 if (av.getThresholdTextColour() > 0)
405 getBoxColour(av.getResidueShading(), seq, i);
408 if (resBoxColour.getRed() + resBoxColour.getBlue()
409 + resBoxColour.getGreen() < av.getThresholdTextColour())
411 graphics.setColor(av.getTextColour2());
414 if (!isarep && av.getShowUnconserved())
416 s = getDisplayChar(srep, i, s, '.', null);
422 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
423 graphics.drawString(String.valueOf(s),
424 charOffset + av.getCharWidth() * (i - start), y1);
431 * Returns 'conservedChar' to represent the given position if the sequence
432 * character at that position is equal to the consensus (ignoring case), else
433 * returns the sequence character
437 * @param sequenceChar
438 * @param conservedChar
441 private char getDisplayChar(final boolean usesrep, int position,
442 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
444 // TODO - use currentSequenceGroup rather than alignment
445 // currentSequenceGroup.getConsensus()
446 char conschar = (usesrep) ? (currentGroup == null
447 || position < currentGroup.getStartRes()
448 || position > currentGroup.getEndRes()
449 ? av.getAlignment().getSeqrep().getCharAt(position)
450 : (currentGroup.getSeqrep() != null
451 ? currentGroup.getSeqrep().getCharAt(position)
452 : av.getAlignment().getSeqrep()
453 .getCharAt(position)))
454 : (currentGroup != null && currentGroup.getConsensus() != null
455 && position >= currentGroup.getStartRes()
456 && position <= currentGroup.getEndRes()
458 .getConsensus().annotations.length > position)
460 .getConsensus().annotations[position].displayCharacter
462 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
464 if (!jalview.util.Comparison.isGap(conschar)
465 && (sequenceChar == conschar
466 || sequenceChar + CHAR_TO_UPPER == conschar))
468 sequenceChar = conservedChar;
479 * @return DOCUMENT ME!
481 boolean inCurrentSequenceGroup(int res)
483 if (allGroups == null)
488 for (int i = 0; i < allGroups.length; i++)
490 if ((allGroups[i].getStartRes() <= res)
491 && (allGroups[i].getEndRes() >= res))
493 currentSequenceGroup = allGroups[i];
520 public void drawHighlightedText(SequenceI seq, int start, int end, int x1,
523 int pady = av.getCharHeight() / 5;
525 graphics.setColor(Color.BLACK);
526 graphics.fillRect(x1, y1, av.getCharWidth() * (end - start + 1),
528 graphics.setColor(Color.white);
532 // Need to find the sequence position here.
533 if (av.isValidCharWidth())
535 for (int i = start; i <= end; i++)
537 if (i < seq.getLength())
539 s = seq.getCharAt(i);
542 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
543 graphics.drawString(String.valueOf(s),
544 charOffset + x1 + (av.getCharWidth() * (i - start)),
545 (y1 + av.getCharHeight()) - pady);
550 public void drawCursor(SequenceI seq, int res, int x1, int y1)
552 int pady = av.getCharHeight() / 5;
554 graphics.setColor(Color.black);
555 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
557 if (av.isValidCharWidth())
559 graphics.setColor(Color.white);
561 char s = seq.getCharAt(res);
563 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
564 graphics.drawString(String.valueOf(s), charOffset + x1,
565 (y1 + av.getCharHeight()) - pady);