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;
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[] allGroups = null;
49 boolean monospacedFont;
52 * Creates a new SequenceRenderer object
56 public SequenceRenderer(AlignViewportI viewport)
67 public void prepare(Graphics g, boolean renderGaps)
70 fm = g.getFontMetrics();
72 // If EPS graphics, stringWidth will be a double, not an int
73 double dwidth = fm.getStringBounds("M", g).getWidth();
75 monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth() && av
76 .getCharWidth() == dwidth);
78 this.renderGaps = renderGaps;
81 protected Color getResidueBoxColour(SequenceI seq, int i)
83 // rate limiting step when rendering overview for lots of groups
84 allGroups = av.getAlignment().findAllGroups(seq);
86 SequenceGroup currentSequenceGroup = inCurrentSequenceGroup(i);
87 if (currentSequenceGroup != null)
89 if (currentSequenceGroup.getDisplayBoxes())
91 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq, i);
94 else if (av.getShowBoxes())
96 getBoxColour(av.getResidueShading(), seq, i);
103 * Get the residue colour at the given sequence position - as determined by
104 * the sequence group colour (if any), else the colour scheme, possibly
105 * overridden by a feature colour.
113 public Color getResidueColour(final SequenceI seq, int position,
114 FeatureColourFinder finder)
116 Color col = getResidueBoxColour(seq, position);
120 col = finder.findFeatureColour(col, seq, position);
135 void getBoxColour(ResidueShaderI shader, SequenceI seq, int i)
137 if (shader.getColourScheme() != null)
139 resBoxColour = shader.findColour(seq.getCharAt(i), i, seq);
143 resBoxColour = Color.white;
169 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
174 drawBoxes(seq, start, end, y1);
176 if (av.isValidCharWidth())
178 drawText(seq, start, end, y1);
200 public synchronized void drawBoxes(SequenceI seq, int start, int end,
205 return; // fix for racecondition
208 int length = seq.getLength();
211 int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(), avHeight = av
214 Color tempColour = null;
218 resBoxColour = Color.white;
222 SequenceGroup currentSequenceGroup = inCurrentSequenceGroup(i);
223 if (currentSequenceGroup != null)
225 if (currentSequenceGroup.getDisplayBoxes())
227 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq,
231 else if (av.getShowBoxes())
233 getBoxColour(av.getResidueShading(), seq, i);
237 if (resBoxColour != tempColour)
239 if (tempColour != null)
241 graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
245 graphics.setColor(resBoxColour);
249 tempColour = resBoxColour;
259 graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
281 public void drawText(SequenceI seq, int start, int end, int y1)
283 y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
287 if (end + 1 >= seq.getLength())
289 end = seq.getLength() - 1;
291 graphics.setColor(av.getTextColour());
293 if (monospacedFont && av.getShowText() && allGroups.length == 0
294 && !av.getColourText() && av.getThresholdTextColour() == 0)
296 if (av.isRenderGaps())
298 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
302 char gap = av.getGapCharacter();
303 graphics.drawString(seq.getSequenceAsString(start, end + 1)
304 .replace(gap, ' '), 0, y1);
309 boolean srep = av.isDisplayReferenceSeq();
310 boolean getboxColour = false;
311 boolean isarep = av.getAlignment().getSeqrep() == seq;
313 for (int i = start; i <= end; i++)
316 graphics.setColor(av.getTextColour());
317 getboxColour = false;
318 s = seq.getCharAt(i);
320 if (!renderGaps && jalview.util.Comparison.isGap(s))
325 SequenceGroup currentSequenceGroup = inCurrentSequenceGroup(i);
326 if (currentSequenceGroup != null)
328 if (!currentSequenceGroup.getDisplayText())
333 if (currentSequenceGroup.thresholdTextColour > 0
334 || currentSequenceGroup.getColourText())
337 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq,
340 if (currentSequenceGroup.getColourText())
342 graphics.setColor(resBoxColour.darker());
345 if (currentSequenceGroup.thresholdTextColour > 0)
347 if (resBoxColour.getRed() + resBoxColour.getBlue()
348 + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
350 graphics.setColor(currentSequenceGroup.textColour2);
356 graphics.setColor(currentSequenceGroup.textColour);
358 boolean isgrep = currentSequenceGroup != null
359 ? currentSequenceGroup.getSeqrep() == seq : false;
360 if (!isarep && !isgrep
361 && currentSequenceGroup.getShowNonconserved()) // todo
364 // todo - use sequence group consensus
365 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
372 if (!av.getShowText())
377 if (av.getColourText())
380 getBoxColour(av.getResidueShading(), seq, i);
382 if (av.getShowBoxes())
384 graphics.setColor(resBoxColour.darker());
388 graphics.setColor(resBoxColour);
392 if (av.getThresholdTextColour() > 0)
396 getBoxColour(av.getResidueShading(), seq, i);
399 if (resBoxColour.getRed() + resBoxColour.getBlue()
400 + resBoxColour.getGreen() < av.getThresholdTextColour())
402 graphics.setColor(av.getTextColour2());
405 if (!isarep && av.getShowUnconserved())
407 s = getDisplayChar(srep, i, s, '.', null);
413 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
414 graphics.drawString(String.valueOf(s),
415 charOffset + av.getCharWidth() * (i - start), y1);
422 * Returns 'conservedChar' to represent the given position if the sequence
423 * character at that position is equal to the consensus (ignoring case), else
424 * returns the sequence character
428 * @param sequenceChar
429 * @param conservedChar
432 private char getDisplayChar(final boolean usesrep, int position,
433 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
435 // TODO - use currentSequenceGroup rather than alignment
436 // currentSequenceGroup.getConsensus()
437 char conschar = (usesrep) ? (currentGroup == null
438 || position < currentGroup.getStartRes()
439 || position > currentGroup.getEndRes() ? av.getAlignment()
440 .getSeqrep().getCharAt(position)
441 : (currentGroup.getSeqrep() != null ? currentGroup.getSeqrep()
442 .getCharAt(position) : av.getAlignment().getSeqrep()
443 .getCharAt(position)))
444 : (currentGroup != null && currentGroup.getConsensus() != null
445 && position >= currentGroup.getStartRes()
446 && position <= currentGroup.getEndRes() && currentGroup
447 .getConsensus().annotations.length > position) ? currentGroup
448 .getConsensus().annotations[position].displayCharacter
450 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
452 if (!jalview.util.Comparison.isGap(conschar)
453 && (sequenceChar == conschar || sequenceChar + CHAR_TO_UPPER == conschar))
455 sequenceChar = conservedChar;
466 * @return DOCUMENT ME!
468 SequenceGroup inCurrentSequenceGroup(int res)
470 if (allGroups == null)
475 for (int i = 0; i < allGroups.length; i++)
477 if ((allGroups[i].getStartRes() <= res)
478 && (allGroups[i].getEndRes() >= res))
480 return (allGroups[i]);
505 public void drawHighlightedText(SequenceI seq, int start, int end,
508 int pady = av.getCharHeight() / 5;
510 graphics.setColor(Color.BLACK);
511 graphics.fillRect(x1, y1, av.getCharWidth() * (end - start + 1),
513 graphics.setColor(Color.white);
517 // Need to find the sequence position here.
518 if (av.isValidCharWidth())
520 for (int i = start; i <= end; i++)
522 if (i < seq.getLength())
524 s = seq.getCharAt(i);
527 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
528 graphics.drawString(String.valueOf(s),
529 charOffset + x1 + (av.getCharWidth() * (i - start)),
530 (y1 + av.getCharHeight()) - pady);
535 public void drawCursor(SequenceI seq, int res, int x1, int y1)
537 int pady = av.getCharHeight() / 5;
539 graphics.setColor(Color.black);
540 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
542 if (av.isValidCharWidth())
544 graphics.setColor(Color.white);
546 char s = seq.getCharAt(res);
548 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
549 graphics.drawString(String.valueOf(s), charOffset + x1,
550 (y1 + av.getCharHeight()) - pady);