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;
41 public class SequenceRenderer implements jalview.api.SequenceRenderer
43 final static int CHAR_TO_UPPER = 'A' - 'a';
49 boolean renderGaps = true;
51 SequenceGroup currentSequenceGroup = null;
53 SequenceGroup[] allGroups = null;
59 boolean monospacedFont;
61 boolean forOverview = false;
64 * Creates a new SequenceRenderer object.
69 public SequenceRenderer(AlignViewport av)
80 public void prepare(Graphics g, boolean renderGaps)
83 fm = g.getFontMetrics();
85 // If EPS graphics, stringWidth will be a double, not an int
86 double dwidth = fm.getStringBounds("M", g).getWidth();
88 monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth() && av
89 .getCharWidth() == dwidth);
91 this.renderGaps = renderGaps;
95 public ColorI getResidueBoxColour(SequenceI seq, int i)
97 allGroups = av.getAlignment().findAllGroups(seq);
99 if (inCurrentSequenceGroup(i))
101 if (currentSequenceGroup.getDisplayBoxes())
103 getBoxColour(currentSequenceGroup.cs, seq, i);
106 else if (av.getShowBoxes())
108 getBoxColour(av.getGlobalColourScheme(), seq, i);
111 return new Colour(resBoxColour);
115 * Get the residue colour at the given sequence position - as determined by
116 * the sequence group colour (if any), else the colour scheme, possibly
117 * overridden by a feature colour.
125 public ColorI getResidueColour(final SequenceI seq, int position,
128 // TODO replace 8 or so code duplications with calls to this method
129 // (refactored as needed)
130 ColorI col = getResidueBoxColour(seq, position);
134 col = fr.findFeatureColour(col, seq, position);
149 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
153 resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
156 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
158 resBoxColour = Color.lightGray;
162 resBoxColour = Color.white;
188 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
193 drawBoxes(seq, start, end, y1);
195 if (av.validCharWidth)
197 drawText(seq, start, end, y1);
219 public synchronized void drawBoxes(SequenceI seq, int start, int end,
224 return; // fix for racecondition
227 int length = seq.getLength();
230 int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(), avHeight = av
233 Color tempColour = null;
237 resBoxColour = Color.white;
241 if (inCurrentSequenceGroup(i))
243 if (currentSequenceGroup.getDisplayBoxes())
245 getBoxColour(currentSequenceGroup.cs, seq, i);
248 else if (av.getShowBoxes())
250 getBoxColour(av.getGlobalColourScheme(), seq, i);
255 if (resBoxColour != tempColour)
257 if (tempColour != null)
259 graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
263 graphics.setColor(resBoxColour);
267 tempColour = resBoxColour;
277 graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
299 public void drawText(SequenceI seq, int start, int end, int y1)
301 y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
305 if (end + 1 >= seq.getLength())
307 end = seq.getLength() - 1;
309 graphics.setColor(ColorUtils.getColor(av.getTextColour()));
311 if (monospacedFont && av.getShowText() && allGroups.length == 0
312 && !av.getColourText() && av.getThresholdTextColour() == 0)
314 if (av.isRenderGaps())
316 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
320 char gap = av.getGapCharacter();
321 graphics.drawString(seq.getSequenceAsString(start, end + 1)
322 .replace(gap, ' '), 0, y1);
327 boolean srep = av.isDisplayReferenceSeq();
328 boolean getboxColour = false;
329 boolean isarep = av.getAlignment().getSeqrep() == seq;
330 boolean isgrep = currentSequenceGroup != null ? currentSequenceGroup
331 .getSeqrep() == seq : false;
333 for (int i = start; i <= end; i++)
336 graphics.setColor(ColorUtils.getColor(av.getTextColour()));
337 getboxColour = false;
338 s = seq.getCharAt(i);
340 if (!renderGaps && jalview.util.Comparison.isGap(s))
345 if (inCurrentSequenceGroup(i))
347 if (!currentSequenceGroup.getDisplayText())
352 if (currentSequenceGroup.thresholdTextColour > 0
353 || currentSequenceGroup.getColourText())
356 getBoxColour(currentSequenceGroup.cs, seq, i);
358 if (currentSequenceGroup.getColourText())
360 graphics.setColor(resBoxColour.darker());
363 if (currentSequenceGroup.thresholdTextColour > 0)
365 if (resBoxColour.getRed() + resBoxColour.getBlue()
366 + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
368 graphics.setColor(ColorUtils.getColor(currentSequenceGroup.textColour2));
374 graphics.setColor(ColorUtils.getColor(currentSequenceGroup.textColour));
376 if (!isarep && !isgrep
377 && currentSequenceGroup.getShowNonconserved()) // todo
380 // todo - use sequence group consensus
381 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
388 if (!av.getShowText())
393 if (av.getColourText())
396 getBoxColour(av.getGlobalColourScheme(), seq, i);
398 if (av.getShowBoxes())
400 graphics.setColor(resBoxColour.darker());
404 graphics.setColor(resBoxColour);
408 if (av.getThresholdTextColour() > 0)
412 getBoxColour(av.getGlobalColourScheme(), seq, i);
415 if (resBoxColour.getRed() + resBoxColour.getBlue()
416 + resBoxColour.getGreen() < av.getThresholdTextColour())
418 graphics.setColor(ColorUtils.getColor(av.getTextColour2()));
421 if (!isarep && av.getShowUnconserved())
423 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
429 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
430 graphics.drawString(String.valueOf(s),
431 charOffset + av.getCharWidth() * (i - start), y1);
438 * Returns 'conservedChar' to represent the given position if the sequence
439 * character at that position is equal to the consensus (ignoring case), else
440 * returns the sequence character
444 * @param sequenceChar
445 * @param conservedChar
448 private char getDisplayChar(final boolean usesrep, int position,
449 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
451 // TODO - use currentSequenceGroup rather than alignment
452 // currentSequenceGroup.getConsensus()
453 char conschar = (usesrep) ? (currentGroup == null ? av.getAlignment()
454 .getSeqrep().getCharAt(position)
455 : (currentGroup.getSeqrep() != null ? currentGroup.getSeqrep()
456 .getCharAt(position) : av.getAlignment().getSeqrep()
457 .getCharAt(position)))
458 : (currentGroup != null && currentGroup.getConsensus() != null) ? 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);