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.ColourSchemeI;
28 import java.awt.Color;
29 import java.awt.FontMetrics;
30 import java.awt.Graphics;
32 public class SequenceRenderer implements jalview.api.SequenceRenderer
34 final static int CHAR_TO_UPPER = 'A' - 'a';
40 boolean renderGaps = true;
42 SequenceGroup currentSequenceGroup = null;
44 SequenceGroup[] allGroups = null;
50 boolean monospacedFont;
52 boolean forOverview = false;
55 * Creates a new SequenceRenderer object.
60 public SequenceRenderer(AlignViewport av)
71 public void prepare(Graphics g, boolean renderGaps)
74 fm = g.getFontMetrics();
76 // If EPS graphics, stringWidth will be a double, not an int
77 double dwidth = fm.getStringBounds("M", g).getWidth();
79 monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth() && av
80 .getCharWidth() == dwidth);
82 this.renderGaps = renderGaps;
86 public 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.cs, seq, i);
98 else if (av.getShowBoxes())
100 getBoxColour(av.getGlobalColourScheme(), 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,
120 // TODO replace 8 or so code duplications with calls to this method
121 // (refactored as needed)
122 Color col = getResidueBoxColour(seq, position);
126 col = fr.findFeatureColour(col, seq, position);
141 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
145 resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
148 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
150 resBoxColour = Color.lightGray;
154 resBoxColour = Color.white;
180 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
185 drawBoxes(seq, start, end, y1);
187 if (av.validCharWidth)
189 drawText(seq, start, end, y1);
211 public synchronized void drawBoxes(SequenceI seq, int start, int end,
216 return; // fix for racecondition
219 int length = seq.getLength();
222 int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(), avHeight = av
225 Color tempColour = null;
229 resBoxColour = Color.white;
233 if (inCurrentSequenceGroup(i))
235 if (currentSequenceGroup.getDisplayBoxes())
237 getBoxColour(currentSequenceGroup.cs, seq, i);
240 else if (av.getShowBoxes())
242 getBoxColour(av.getGlobalColourScheme(), seq, i);
247 if (resBoxColour != tempColour)
249 if (tempColour != null)
251 graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
255 graphics.setColor(resBoxColour);
259 tempColour = resBoxColour;
269 graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
291 public void drawText(SequenceI seq, int start, int end, int y1)
293 y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
297 if (end + 1 >= seq.getLength())
299 end = seq.getLength() - 1;
301 graphics.setColor(av.getTextColour());
303 if (monospacedFont && av.getShowText() && allGroups.length == 0
304 && !av.getColourText() && av.getThresholdTextColour() == 0)
306 if (av.isRenderGaps())
308 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
312 char gap = av.getGapCharacter();
313 graphics.drawString(seq.getSequenceAsString(start, end + 1)
314 .replace(gap, ' '), 0, y1);
319 boolean srep = av.isDisplayReferenceSeq();
320 boolean getboxColour = false;
321 boolean isarep = av.getAlignment().getSeqrep() == seq;
322 boolean isgrep = currentSequenceGroup != null ? currentSequenceGroup
323 .getSeqrep() == seq : false;
325 for (int i = start; i <= end; i++)
328 graphics.setColor(av.getTextColour());
329 getboxColour = false;
330 s = seq.getCharAt(i);
332 if (!renderGaps && jalview.util.Comparison.isGap(s))
337 if (inCurrentSequenceGroup(i))
339 if (!currentSequenceGroup.getDisplayText())
344 if (currentSequenceGroup.thresholdTextColour > 0
345 || currentSequenceGroup.getColourText())
348 getBoxColour(currentSequenceGroup.cs, seq, i);
350 if (currentSequenceGroup.getColourText())
352 graphics.setColor(resBoxColour.darker());
355 if (currentSequenceGroup.thresholdTextColour > 0)
357 if (resBoxColour.getRed() + resBoxColour.getBlue()
358 + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
360 graphics.setColor(currentSequenceGroup.textColour2);
366 graphics.setColor(currentSequenceGroup.textColour);
368 if (!isarep && !isgrep
369 && currentSequenceGroup.getShowNonconserved()) // todo
372 // todo - use sequence group consensus
373 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
380 if (!av.getShowText())
385 if (av.getColourText())
388 getBoxColour(av.getGlobalColourScheme(), seq, i);
390 if (av.getShowBoxes())
392 graphics.setColor(resBoxColour.darker());
396 graphics.setColor(resBoxColour);
400 if (av.getThresholdTextColour() > 0)
404 getBoxColour(av.getGlobalColourScheme(), seq, i);
407 if (resBoxColour.getRed() + resBoxColour.getBlue()
408 + resBoxColour.getGreen() < av.getThresholdTextColour())
410 graphics.setColor(av.getTextColour2());
413 if (!isarep && av.getShowUnconserved())
415 s = getDisplayChar(srep, i, s, '.', null);
421 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
422 graphics.drawString(String.valueOf(s),
423 charOffset + av.getCharWidth() * (i - start), y1);
430 * Returns 'conservedChar' to represent the given position if the sequence
431 * character at that position is equal to the consensus (ignoring case), else
432 * returns the sequence character
436 * @param sequenceChar
437 * @param conservedChar
440 private char getDisplayChar(final boolean usesrep, int position,
441 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
443 // TODO - use currentSequenceGroup rather than alignment
444 // currentSequenceGroup.getConsensus()
445 char conschar = (usesrep) ? (currentGroup == null
446 || position < currentGroup.getStartRes()
447 || position > currentGroup.getEndRes() ? av.getAlignment()
448 .getSeqrep().getCharAt(position)
449 : (currentGroup.getSeqrep() != null ? currentGroup.getSeqrep()
450 .getCharAt(position) : av.getAlignment().getSeqrep()
451 .getCharAt(position)))
452 : (currentGroup != null && currentGroup.getConsensus() != null
453 && position >= currentGroup.getStartRes()
454 && position <= currentGroup.getEndRes() && currentGroup
455 .getConsensus().annotations.length > position) ? currentGroup
456 .getConsensus().annotations[position].displayCharacter
458 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
460 if (!jalview.util.Comparison.isGap(conschar)
461 && (sequenceChar == conschar || sequenceChar + CHAR_TO_UPPER == conschar))
463 sequenceChar = conservedChar;
474 * @return DOCUMENT ME!
476 boolean inCurrentSequenceGroup(int res)
478 if (allGroups == null)
483 for (int i = 0; i < allGroups.length; i++)
485 if ((allGroups[i].getStartRes() <= res)
486 && (allGroups[i].getEndRes() >= res))
488 currentSequenceGroup = allGroups[i];
515 public void drawHighlightedText(SequenceI seq, int start, int end,
518 int pady = av.getCharHeight() / 5;
520 graphics.setColor(Color.BLACK);
521 graphics.fillRect(x1, y1, av.getCharWidth() * (end - start + 1),
523 graphics.setColor(Color.white);
527 // Need to find the sequence position here.
528 if (av.isValidCharWidth())
530 for (int i = start; i <= end; i++)
532 if (i < seq.getLength())
534 s = seq.getCharAt(i);
537 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
538 graphics.drawString(String.valueOf(s),
539 charOffset + x1 + (av.getCharWidth() * (i - start)),
540 (y1 + av.getCharHeight()) - pady);
545 public void drawCursor(SequenceI seq, int res, int x1, int y1)
547 int pady = av.getCharHeight() / 5;
549 graphics.setColor(Color.black);
550 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
552 if (av.isValidCharWidth())
554 graphics.setColor(Color.white);
556 char s = seq.getCharAt(res);
558 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
559 graphics.drawString(String.valueOf(s), charOffset + x1,
560 (y1 + av.getCharHeight()) - pady);