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.datamodel.AlignmentAnnotation;
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;
38 public class SequenceRenderer implements jalview.api.SequenceRenderer
44 boolean renderGaps = true;
46 SequenceGroup currentSequenceGroup = null;
48 SequenceGroup[] allGroups = null;
54 boolean monospacedFont;
56 boolean forOverview = false;
59 * Creates a new SequenceRenderer object.
64 public SequenceRenderer(AlignViewport av)
75 public void prepare(Graphics g, boolean renderGaps)
78 fm = g.getFontMetrics();
80 // If EPS graphics, stringWidth will be a double, not an int
81 double dwidth = fm.getStringBounds("M", g).getWidth();
83 monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth() && (float) av.charWidth == dwidth);
85 this.renderGaps = renderGaps;
88 public Color getResidueBoxColour(SequenceI seq, int i)
90 allGroups = av.getAlignment().findAllGroups(seq);
92 if (inCurrentSequenceGroup(i))
94 if (currentSequenceGroup.getDisplayBoxes())
96 getBoxColour(currentSequenceGroup.cs, seq, i);
99 else if (av.getShowBoxes())
101 getBoxColour(av.getGlobalColourScheme(), seq, i);
117 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
121 resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
124 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
126 resBoxColour = Color.lightGray;
130 resBoxColour = Color.white;
156 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
161 drawBoxes(seq, start, end, y1);
163 if (av.validCharWidth)
165 drawText(seq, start, end, y1);
187 public synchronized void drawBoxes(SequenceI seq, int start, int end,
191 return; // fix for racecondition
193 int length = seq.getLength();
196 int curWidth = av.charWidth;
198 Color tempColour = null;
202 resBoxColour = Color.white;
206 if (inCurrentSequenceGroup(i))
208 if (currentSequenceGroup.getDisplayBoxes())
210 getBoxColour(currentSequenceGroup.cs, seq, i);
213 else if (av.getShowBoxes())
215 getBoxColour(av.getGlobalColourScheme(), seq, i);
220 if (resBoxColour != tempColour)
222 if (tempColour != null)
224 graphics.fillRect(av.charWidth * (curStart - start), y1,
225 curWidth, av.charHeight);
228 graphics.setColor(resBoxColour);
231 curWidth = av.charWidth;
232 tempColour = resBoxColour;
236 curWidth += av.charWidth;
242 graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,
265 public void drawText(SequenceI seq, int start, int end, int y1)
267 y1 += av.charHeight - av.charHeight / 5; // height/5 replaces pady
271 if (end + 1 >= seq.getLength())
273 end = seq.getLength() - 1;
275 graphics.setColor(av.textColour);
277 if (monospacedFont && av.showText && allGroups.length == 0
278 && !av.getColourText() && av.thresholdTextColour == 0)
282 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
286 char gap = av.getGapCharacter();
287 graphics.drawString(seq.getSequenceAsString(start, end + 1)
288 .replace(gap, ' '), 0, y1);
293 boolean getboxColour = false;
294 for (int i = start; i <= end; i++)
296 graphics.setColor(av.textColour);
297 getboxColour = false;
298 s = seq.getCharAt(i);
299 if (!renderGaps && jalview.util.Comparison.isGap(s))
304 if (inCurrentSequenceGroup(i))
306 if (!currentSequenceGroup.getDisplayText())
311 if (currentSequenceGroup.thresholdTextColour > 0
312 || currentSequenceGroup.getColourText())
315 getBoxColour(currentSequenceGroup.cs, seq, i);
317 if (currentSequenceGroup.getColourText())
319 graphics.setColor(resBoxColour.darker());
322 if (currentSequenceGroup.thresholdTextColour > 0)
324 if (resBoxColour.getRed() + resBoxColour.getBlue()
325 + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
327 graphics.setColor(currentSequenceGroup.textColour2);
333 graphics.setColor(currentSequenceGroup.textColour);
335 if (currentSequenceGroup.getShowNonconserved()) // todo optimize
337 // todo - use sequence group consensus
338 s = getDisplayChar(av.getAlignmentConsensusAnnotation(), i, s,
346 if (!av.getShowText())
351 if (av.getColourText())
354 getBoxColour(av.getGlobalColourScheme(), seq, i);
356 if (av.getShowBoxes())
358 graphics.setColor(resBoxColour.darker());
362 graphics.setColor(resBoxColour);
366 if (av.thresholdTextColour > 0)
370 getBoxColour(av.getGlobalColourScheme(), seq, i);
373 if (resBoxColour.getRed() + resBoxColour.getBlue()
374 + resBoxColour.getGreen() < av.thresholdTextColour)
376 graphics.setColor(av.textColour2);
379 if (av.getShowUnconserved())
381 s = getDisplayChar(av.getAlignmentConsensusAnnotation(), i, s,
388 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
389 graphics.drawString(String.valueOf(s), charOffset + av.charWidth
396 private char getDisplayChar(AlignmentAnnotation consensus, int position,
399 char conschar = consensus.annotations[position].displayCharacter
401 if (conschar != '-' && s == conschar)
414 * @return DOCUMENT ME!
416 boolean inCurrentSequenceGroup(int res)
418 if (allGroups == null)
423 for (int i = 0; i < allGroups.length; i++)
425 if ((allGroups[i].getStartRes() <= res)
426 && (allGroups[i].getEndRes() >= res))
428 currentSequenceGroup = allGroups[i];
455 public void drawHighlightedText(SequenceI seq, int start, int end,
458 int pady = av.charHeight / 5;
460 graphics.setColor(Color.BLACK);
461 graphics.fillRect(x1, y1, av.charWidth * (end - start + 1),
463 graphics.setColor(Color.white);
467 // Need to find the sequence position here.
468 if (av.validCharWidth)
470 for (int i = start; i <= end; i++)
472 if (i < seq.getLength())
474 s = seq.getCharAt(i);
477 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
478 graphics.drawString(String.valueOf(s), charOffset + x1
479 + (av.charWidth * (i - start)), (y1 + av.charHeight) - pady);
484 public void drawCursor(SequenceI seq, int res, int x1, int y1)
486 int pady = av.charHeight / 5;
488 graphics.setColor(Color.black);
489 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
491 if (av.validCharWidth)
493 graphics.setColor(Color.white);
495 char s = seq.getCharAt(res);
497 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
498 graphics.drawString(String.valueOf(s), charOffset + x1,
499 (y1 + av.charHeight) - pady);