2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.AlignmentAnnotation;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.schemes.ColourSchemeI;
29 import java.awt.Color;
30 import java.awt.FontMetrics;
31 import java.awt.Graphics;
39 public class SequenceRenderer implements jalview.api.SequenceRenderer
45 boolean renderGaps = true;
47 SequenceGroup currentSequenceGroup = null;
49 SequenceGroup[] allGroups = null;
55 boolean monospacedFont;
57 boolean forOverview = false;
60 * Creates a new SequenceRenderer object.
65 public SequenceRenderer(AlignViewport av)
76 public void prepare(Graphics g, boolean renderGaps)
79 fm = g.getFontMetrics();
81 // If EPS graphics, stringWidth will be a double, not an int
82 double dwidth = fm.getStringBounds("M", g).getWidth();
84 monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth() && av.charWidth == dwidth);
86 this.renderGaps = renderGaps;
90 public Color getResidueBoxColour(SequenceI seq, int i)
92 allGroups = av.getAlignment().findAllGroups(seq);
94 if (inCurrentSequenceGroup(i))
96 if (currentSequenceGroup.getDisplayBoxes())
98 getBoxColour(currentSequenceGroup.cs, seq, i);
101 else if (av.getShowBoxes())
103 getBoxColour(av.getGlobalColourScheme(), seq, i);
110 * Get the residue colour at the given sequence position - as determined by
111 * the sequence group colour (if any), else the colour scheme, possibly
112 * overridden by a feature colour.
120 public Color getResidueColour(final SequenceI seq, int position,
123 // TODO replace 8 or so code duplications with calls to this method
124 // (refactored as needed)
125 Color col = getResidueBoxColour(seq, position);
129 col = fr.findFeatureColour(col, seq, position);
144 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
148 resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
151 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
153 resBoxColour = Color.lightGray;
157 resBoxColour = Color.white;
183 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
188 drawBoxes(seq, start, end, y1);
190 if (av.validCharWidth)
192 drawText(seq, start, end, y1);
214 public synchronized void drawBoxes(SequenceI seq, int start, int end,
219 return; // fix for racecondition
222 int length = seq.getLength();
225 int curWidth = av.charWidth;
227 Color tempColour = null;
231 resBoxColour = Color.white;
235 if (inCurrentSequenceGroup(i))
237 if (currentSequenceGroup.getDisplayBoxes())
239 getBoxColour(currentSequenceGroup.cs, seq, i);
242 else if (av.getShowBoxes())
244 getBoxColour(av.getGlobalColourScheme(), seq, i);
249 if (resBoxColour != tempColour)
251 if (tempColour != null)
253 graphics.fillRect(av.charWidth * (curStart - start), y1,
254 curWidth, av.charHeight);
257 graphics.setColor(resBoxColour);
260 curWidth = av.charWidth;
261 tempColour = resBoxColour;
265 curWidth += av.charWidth;
271 graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,
294 public void drawText(SequenceI seq, int start, int end, int y1)
296 y1 += av.charHeight - av.charHeight / 5; // height/5 replaces pady
300 if (end + 1 >= seq.getLength())
302 end = seq.getLength() - 1;
304 graphics.setColor(av.textColour);
306 if (monospacedFont && av.showText && allGroups.length == 0
307 && !av.getColourText() && av.thresholdTextColour == 0)
311 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
315 char gap = av.getGapCharacter();
316 graphics.drawString(seq.getSequenceAsString(start, end + 1)
317 .replace(gap, ' '), 0, y1);
322 boolean getboxColour = false;
323 for (int i = start; i <= end; i++)
325 graphics.setColor(av.textColour);
326 getboxColour = false;
327 s = seq.getCharAt(i);
328 if (!renderGaps && jalview.util.Comparison.isGap(s))
333 if (inCurrentSequenceGroup(i))
335 if (!currentSequenceGroup.getDisplayText())
340 if (currentSequenceGroup.thresholdTextColour > 0
341 || currentSequenceGroup.getColourText())
344 getBoxColour(currentSequenceGroup.cs, seq, i);
346 if (currentSequenceGroup.getColourText())
348 graphics.setColor(resBoxColour.darker());
351 if (currentSequenceGroup.thresholdTextColour > 0)
353 if (resBoxColour.getRed() + resBoxColour.getBlue()
354 + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
356 graphics.setColor(currentSequenceGroup.textColour2);
362 graphics.setColor(currentSequenceGroup.textColour);
364 if (currentSequenceGroup.getShowNonconserved()) // todo optimize
366 // todo - use sequence group consensus
367 s = getDisplayChar(av.getAlignmentConsensusAnnotation(), i, s,
375 if (!av.getShowText())
380 if (av.getColourText())
383 getBoxColour(av.getGlobalColourScheme(), seq, i);
385 if (av.getShowBoxes())
387 graphics.setColor(resBoxColour.darker());
391 graphics.setColor(resBoxColour);
395 if (av.thresholdTextColour > 0)
399 getBoxColour(av.getGlobalColourScheme(), seq, i);
402 if (resBoxColour.getRed() + resBoxColour.getBlue()
403 + resBoxColour.getGreen() < av.thresholdTextColour)
405 graphics.setColor(av.textColour2);
408 if (av.getShowUnconserved())
410 s = getDisplayChar(av.getAlignmentConsensusAnnotation(), i, s,
417 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
418 graphics.drawString(String.valueOf(s), charOffset + av.charWidth
425 private char getDisplayChar(AlignmentAnnotation consensus, int position,
428 char conschar = consensus.annotations[position].displayCharacter
430 if (conschar != '-' && s == conschar)
443 * @return DOCUMENT ME!
445 boolean inCurrentSequenceGroup(int res)
447 if (allGroups == null)
452 for (int i = 0; i < allGroups.length; i++)
454 if ((allGroups[i].getStartRes() <= res)
455 && (allGroups[i].getEndRes() >= res))
457 currentSequenceGroup = allGroups[i];
484 public void drawHighlightedText(SequenceI seq, int start, int end,
487 int pady = av.charHeight / 5;
489 graphics.setColor(Color.BLACK);
490 graphics.fillRect(x1, y1, av.charWidth * (end - start + 1),
492 graphics.setColor(Color.white);
496 // Need to find the sequence position here.
497 if (av.validCharWidth)
499 for (int i = start; i <= end; i++)
501 if (i < seq.getLength())
503 s = seq.getCharAt(i);
506 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
507 graphics.drawString(String.valueOf(s), charOffset + x1
508 + (av.charWidth * (i - start)), (y1 + av.charHeight) - pady);
513 public void drawCursor(SequenceI seq, int res, int x1, int y1)
515 int pady = av.charHeight / 5;
517 graphics.setColor(Color.black);
518 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
520 if (av.validCharWidth)
522 graphics.setColor(Color.white);
524 char s = seq.getCharAt(res);
526 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
527 graphics.drawString(String.valueOf(s), charOffset + x1,
528 (y1 + av.charHeight) - pady);