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;
27 import jalview.util.Comparison;
29 import java.awt.Color;
30 import java.awt.FontMetrics;
31 import java.awt.Graphics;
39 public class SequenceRenderer implements jalview.api.SequenceRenderer
41 final static int CHAR_TO_UPPER = 'A' - 'a';
47 boolean renderGaps = true;
49 SequenceGroup currentSequenceGroup = null;
51 SequenceGroup[] allGroups = null;
57 boolean monospacedFont;
59 boolean forOverview = false;
62 * Creates a new SequenceRenderer object.
67 public SequenceRenderer(AlignViewport av)
78 public void prepare(Graphics g, boolean renderGaps)
81 fm = g.getFontMetrics();
83 // If EPS graphics, stringWidth will be a double, not an int
84 double dwidth = fm.getStringBounds("M", g).getWidth();
86 monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth() && av
87 .getCharWidth() == dwidth);
89 this.renderGaps = renderGaps;
93 public Color getResidueBoxColour(SequenceI seq, int i)
95 allGroups = av.getAlignment().findAllGroups(seq);
97 if (inCurrentSequenceGroup(i))
99 if (currentSequenceGroup.getDisplayBoxes())
101 getBoxColour(currentSequenceGroup.cs, seq, i);
104 else if (av.getShowBoxes())
106 getBoxColour(av.getGlobalColourScheme(), seq, i);
113 * Get the residue colour at the given sequence position - as determined by
114 * the sequence group colour (if any), else the colour scheme, possibly
115 * overridden by a feature colour.
123 public Color getResidueColour(final SequenceI seq, int position,
126 // TODO replace 8 or so code duplications with calls to this method
127 // (refactored as needed)
128 Color col = getResidueBoxColour(seq, position);
132 col = fr.findFeatureColour(col, seq, position);
147 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
151 resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
154 && !Comparison.isGap(seq.getCharAt(i)))
156 resBoxColour = Color.lightGray;
160 resBoxColour = Color.white;
186 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
191 drawBoxes(seq, start, end, y1);
193 if (av.validCharWidth)
195 drawText(seq, start, end, y1);
217 public synchronized void drawBoxes(SequenceI seq, int start, int end,
222 return; // fix for racecondition
225 int length = seq.getLength();
228 int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(), avHeight = av
231 Color tempColour = null;
235 resBoxColour = Color.white;
239 if (inCurrentSequenceGroup(i))
241 if (currentSequenceGroup.getDisplayBoxes())
243 getBoxColour(currentSequenceGroup.cs, seq, i);
246 else if (av.getShowBoxes())
248 getBoxColour(av.getGlobalColourScheme(), seq, i);
253 if (resBoxColour != tempColour)
255 if (tempColour != null)
257 graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
261 graphics.setColor(resBoxColour);
265 tempColour = resBoxColour;
275 graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
297 public void drawText(SequenceI seq, int start, int end, int y1)
299 y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
303 if (end + 1 >= seq.getLength())
305 end = seq.getLength() - 1;
307 graphics.setColor(av.getTextColour());
309 if (monospacedFont && av.getShowText() && allGroups.length == 0
310 && !av.getColourText() && av.getThresholdTextColour() == 0)
312 if (av.isRenderGaps())
314 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
318 char gap = av.getGapCharacter();
319 graphics.drawString(seq.getSequenceAsString(start, end + 1)
320 .replace(gap, ' '), 0, y1);
325 boolean srep = av.isDisplayReferenceSeq();
326 boolean getboxColour = false;
327 boolean isarep = av.getAlignment().getSeqrep() == seq;
328 boolean isgrep = currentSequenceGroup != null ? currentSequenceGroup
329 .getSeqrep() == seq : false;
331 for (int i = start; i <= end; i++)
334 graphics.setColor(av.getTextColour());
335 getboxColour = false;
336 s = seq.getCharAt(i);
338 if (!renderGaps && Comparison.isGap(s))
343 if (inCurrentSequenceGroup(i))
345 if (!currentSequenceGroup.getDisplayText())
350 if (currentSequenceGroup.thresholdTextColour > 0
351 || currentSequenceGroup.getColourText())
354 getBoxColour(currentSequenceGroup.cs, seq, i);
356 if (currentSequenceGroup.getColourText())
358 graphics.setColor(resBoxColour.darker());
361 if (currentSequenceGroup.thresholdTextColour > 0)
363 if (resBoxColour.getRed() + resBoxColour.getBlue()
364 + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
366 graphics.setColor(currentSequenceGroup.textColour2);
372 graphics.setColor(currentSequenceGroup.textColour);
374 if (!isarep && !isgrep
375 && currentSequenceGroup.getShowNonconserved()) // todo
378 // todo - use sequence group consensus
379 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
386 if (!av.getShowText())
391 if (av.getColourText())
394 getBoxColour(av.getGlobalColourScheme(), seq, i);
396 if (av.getShowBoxes())
398 graphics.setColor(resBoxColour.darker());
402 graphics.setColor(resBoxColour);
406 if (av.getThresholdTextColour() > 0)
410 getBoxColour(av.getGlobalColourScheme(), seq, i);
413 if (resBoxColour.getRed() + resBoxColour.getBlue()
414 + resBoxColour.getGreen() < av.getThresholdTextColour())
416 graphics.setColor(av.getTextColour2());
419 if (!isarep && av.getShowUnconserved())
421 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
427 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
428 graphics.drawString(String.valueOf(s),
429 charOffset + av.getCharWidth()
437 * Returns 'conservedChar' to represent the given position if the sequence
438 * character at that position is equal to the consensus (ignoring case), else
439 * returns the sequence character
443 * @param sequenceChar
444 * @param conservedChar
447 private char getDisplayChar(final boolean usesrep, int position,
448 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
450 // TODO - use currentSequenceGroup rather than alignment
451 // currentSequenceGroup.getConsensus()
452 char conschar = (usesrep) ? (currentGroup == null ? av.getAlignment()
453 .getSeqrep().getCharAt(position)
454 : (currentGroup.getSeqrep() != null ? currentGroup.getSeqrep()
455 .getCharAt(position) : av.getAlignment().getSeqrep()
456 .getCharAt(position)))
457 : (currentGroup != null && currentGroup.getConsensus() != null) ? currentGroup
458 .getConsensus().annotations[position].displayCharacter
460 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
463 && (sequenceChar == conschar || sequenceChar + CHAR_TO_UPPER == conschar))
465 sequenceChar = conservedChar;
476 * @return DOCUMENT ME!
478 boolean inCurrentSequenceGroup(int res)
480 if (allGroups == null)
485 for (int i = 0; i < allGroups.length; i++)
487 if ((allGroups[i].getStartRes() <= res)
488 && (allGroups[i].getEndRes() >= res))
490 currentSequenceGroup = allGroups[i];
517 public void drawHighlightedText(SequenceI seq, int start, int end,
520 int pady = av.getCharHeight() / 5;
522 graphics.setColor(Color.BLACK);
523 graphics.fillRect(x1, y1, av.getCharWidth() * (end - start + 1),
525 graphics.setColor(Color.white);
529 // Need to find the sequence position here.
530 if (av.isValidCharWidth())
532 for (int i = start; i <= end; i++)
534 if (i < seq.getLength())
536 s = seq.getCharAt(i);
539 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
540 graphics.drawString(String.valueOf(s),
541 charOffset + x1 + (av.getCharWidth() * (i - start)),
542 (y1 + av.getCharHeight()) - pady);
547 public void drawCursor(SequenceI seq, int res, int x1, int y1)
549 int pady = av.getCharHeight() / 5;
551 graphics.setColor(Color.black);
552 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
554 if (av.isValidCharWidth())
556 graphics.setColor(Color.white);
558 char s = seq.getCharAt(res);
560 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
561 graphics.drawString(String.valueOf(s), charOffset + x1,
562 (y1 + av.getCharHeight()) - pady);