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 srep = av.isDisplayReferenceSeq();
323 boolean getboxColour = false;
324 for (int i = start; i <= end; i++)
326 graphics.setColor(av.textColour);
327 getboxColour = false;
328 s = seq.getCharAt(i);
329 if (!renderGaps && jalview.util.Comparison.isGap(s))
334 if (inCurrentSequenceGroup(i))
336 if (!currentSequenceGroup.getDisplayText())
341 if (currentSequenceGroup.thresholdTextColour > 0
342 || currentSequenceGroup.getColourText())
345 getBoxColour(currentSequenceGroup.cs, seq, i);
347 if (currentSequenceGroup.getColourText())
349 graphics.setColor(resBoxColour.darker());
352 if (currentSequenceGroup.thresholdTextColour > 0)
354 if (resBoxColour.getRed() + resBoxColour.getBlue()
355 + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
357 graphics.setColor(currentSequenceGroup.textColour2);
363 graphics.setColor(currentSequenceGroup.textColour);
365 if (currentSequenceGroup.getShowNonconserved()) // todo optimize
367 // todo - use sequence group consensus
368 s = getDisplayChar(srep, i, s,
376 if (!av.getShowText())
381 if (av.getColourText())
384 getBoxColour(av.getGlobalColourScheme(), seq, i);
386 if (av.getShowBoxes())
388 graphics.setColor(resBoxColour.darker());
392 graphics.setColor(resBoxColour);
396 if (av.thresholdTextColour > 0)
400 getBoxColour(av.getGlobalColourScheme(), seq, i);
403 if (resBoxColour.getRed() + resBoxColour.getBlue()
404 + resBoxColour.getGreen() < av.thresholdTextColour)
406 graphics.setColor(av.textColour2);
409 if (av.getShowUnconserved())
411 s = getDisplayChar(srep, i, s,
418 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
419 graphics.drawString(String.valueOf(s), charOffset + av.charWidth
426 private char getDisplayChar(final boolean usesrep, int position,
429 // TODO - use currentSequenceGroup rather than alignemnt
430 // currentSequenceGroup.getConsensus()
431 char conschar = (usesrep) ? av.getAlignment().getSeqrep().getCharAt(position) : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
433 if (conschar != '-' && s == conschar)
446 * @return DOCUMENT ME!
448 boolean inCurrentSequenceGroup(int res)
450 if (allGroups == null)
455 for (int i = 0; i < allGroups.length; i++)
457 if ((allGroups[i].getStartRes() <= res)
458 && (allGroups[i].getEndRes() >= res))
460 currentSequenceGroup = allGroups[i];
487 public void drawHighlightedText(SequenceI seq, int start, int end,
490 int pady = av.charHeight / 5;
492 graphics.setColor(Color.BLACK);
493 graphics.fillRect(x1, y1, av.charWidth * (end - start + 1),
495 graphics.setColor(Color.white);
499 // Need to find the sequence position here.
500 if (av.validCharWidth)
502 for (int i = start; i <= end; i++)
504 if (i < seq.getLength())
506 s = seq.getCharAt(i);
509 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
510 graphics.drawString(String.valueOf(s), charOffset + x1
511 + (av.charWidth * (i - start)), (y1 + av.charHeight) - pady);
516 public void drawCursor(SequenceI seq, int res, int x1, int y1)
518 int pady = av.charHeight / 5;
520 graphics.setColor(Color.black);
521 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
523 if (av.validCharWidth)
525 graphics.setColor(Color.white);
527 char s = seq.getCharAt(res);
529 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
530 graphics.drawString(String.valueOf(s), charOffset + x1,
531 (y1 + av.charHeight) - pady);