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.CollectionColourSchemeI;
27 import jalview.util.Comparison;
29 import java.awt.Color;
30 import java.awt.FontMetrics;
31 import java.awt.Graphics;
33 public class SequenceRenderer implements jalview.api.SequenceRenderer
35 final static int CHAR_TO_UPPER = 'A' - 'a';
41 boolean renderGaps = true;
43 SequenceGroup currentSequenceGroup = null;
45 SequenceGroup[] allGroups = null;
51 boolean monospacedFont;
53 boolean forOverview = false;
56 * Creates a new SequenceRenderer object.
61 public SequenceRenderer(AlignViewport av)
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;
87 public Color getResidueBoxColour(SequenceI seq, int i)
89 // rate limiting step when rendering overview for lots of groups
90 allGroups = av.getAlignment().findAllGroups(seq);
92 if (inCurrentSequenceGroup(i))
94 if (currentSequenceGroup.getDisplayBoxes())
96 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq, i);
99 else if (av.getShowBoxes())
101 getBoxColour(av.getViewportColourScheme(), seq, i);
108 * Get the residue colour at the given sequence position - as determined by
109 * the sequence group colour (if any), else the colour scheme, possibly
110 * overridden by a feature colour.
118 public Color getResidueColour(final SequenceI seq, int position,
121 // TODO replace 8 or so code duplications with calls to this method
122 // (refactored as needed)
123 Color col = getResidueBoxColour(seq, position);
127 col = fr.findFeatureColour(col, seq, position);
135 * @param collectionColourSchemeI
142 void getBoxColour(CollectionColourSchemeI collectionColourSchemeI,
143 SequenceI seq, int i)
145 if (collectionColourSchemeI != null)
147 resBoxColour = collectionColourSchemeI.findColour(seq.getCharAt(i),
150 else if (forOverview && !Comparison.isGap(seq.getCharAt(i)))
152 resBoxColour = Color.lightGray;
156 resBoxColour = Color.white;
182 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
187 drawBoxes(seq, start, end, y1);
189 if (av.validCharWidth)
191 drawText(seq, start, end, y1);
213 public synchronized void drawBoxes(SequenceI seq, int start, int end,
218 return; // fix for racecondition
221 int length = seq.getLength();
224 int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(), avHeight = av
227 Color tempColour = null;
231 resBoxColour = Color.white;
235 if (inCurrentSequenceGroup(i))
237 if (currentSequenceGroup.getDisplayBoxes())
239 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq,
243 else if (av.getShowBoxes())
245 getBoxColour(av.getViewportColourScheme(), seq, i);
249 if (resBoxColour != tempColour)
251 if (tempColour != null)
253 graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
257 graphics.setColor(resBoxColour);
261 tempColour = resBoxColour;
271 graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
293 public void drawText(SequenceI seq, int start, int end, int y1)
295 y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
299 if (end + 1 >= seq.getLength())
301 end = seq.getLength() - 1;
303 graphics.setColor(av.getTextColour());
305 if (monospacedFont && av.getShowText() && allGroups.length == 0
306 && !av.getColourText() && av.getThresholdTextColour() == 0)
308 if (av.isRenderGaps())
310 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
314 char gap = av.getGapCharacter();
315 graphics.drawString(seq.getSequenceAsString(start, end + 1)
316 .replace(gap, ' '), 0, y1);
321 boolean srep = av.isDisplayReferenceSeq();
322 boolean getboxColour = false;
323 boolean isarep = av.getAlignment().getSeqrep() == seq;
324 boolean isgrep = currentSequenceGroup != null ? currentSequenceGroup
325 .getSeqrep() == seq : false;
327 for (int i = start; i <= end; i++)
330 graphics.setColor(av.getTextColour());
331 getboxColour = false;
332 s = seq.getCharAt(i);
334 if (!renderGaps && jalview.util.Comparison.isGap(s))
339 if (inCurrentSequenceGroup(i))
341 if (!currentSequenceGroup.getDisplayText())
346 if (currentSequenceGroup.thresholdTextColour > 0
347 || currentSequenceGroup.getColourText())
350 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq,
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(currentSequenceGroup.textColour2);
369 graphics.setColor(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.getViewportColourScheme(), seq, i);
393 if (av.getShowBoxes())
395 graphics.setColor(resBoxColour.darker());
399 graphics.setColor(resBoxColour);
403 if (av.getThresholdTextColour() > 0)
407 getBoxColour(av.getViewportColourScheme(), seq, i);
410 if (resBoxColour.getRed() + resBoxColour.getBlue()
411 + resBoxColour.getGreen() < av.getThresholdTextColour())
413 graphics.setColor(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);