2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20 import jalview.datamodel.AlignmentAnnotation;
21 import jalview.datamodel.SequenceGroup;
22 import jalview.datamodel.SequenceI;
23 import jalview.schemes.ColourSchemeI;
25 import java.awt.Color;
26 import java.awt.FontMetrics;
27 import java.awt.Graphics;
35 public class SequenceRenderer implements jalview.api.SequenceRenderer
41 boolean renderGaps = true;
43 SequenceGroup currentSequenceGroup = null;
45 SequenceGroup[] allGroups = null;
51 boolean monospacedFont;
53 boolean forOverview = false;
56 * Creates a new SequenceRenderer object.
61 public SequenceRenderer(AlignViewport av)
72 public void prepare(Graphics g, boolean renderGaps)
75 fm = g.getFontMetrics();
77 // If EPS graphics, stringWidth will be a double, not an int
78 double dwidth = fm.getStringBounds("M", g).getWidth();
80 monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth() && (float) av.charWidth == dwidth);
82 this.renderGaps = renderGaps;
85 public Color getResidueBoxColour(SequenceI seq, int i)
87 allGroups = av.getAlignment().findAllGroups(seq);
89 if (inCurrentSequenceGroup(i))
91 if (currentSequenceGroup.getDisplayBoxes())
93 getBoxColour(currentSequenceGroup.cs, seq, i);
96 else if (av.getShowBoxes())
98 getBoxColour(av.getGlobalColourScheme(), seq, i);
114 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
118 resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
121 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
123 resBoxColour = Color.lightGray;
127 resBoxColour = Color.white;
153 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
158 drawBoxes(seq, start, end, y1);
160 if (av.validCharWidth)
162 drawText(seq, start, end, y1);
184 public synchronized void drawBoxes(SequenceI seq, int start, int end,
188 return; // fix for racecondition
190 int length = seq.getLength();
193 int curWidth = av.charWidth;
195 Color tempColour = null;
199 resBoxColour = Color.white;
203 if (inCurrentSequenceGroup(i))
205 if (currentSequenceGroup.getDisplayBoxes())
207 getBoxColour(currentSequenceGroup.cs, seq, i);
210 else if (av.getShowBoxes())
212 getBoxColour(av.getGlobalColourScheme(), seq, i);
217 if (resBoxColour != tempColour)
219 if (tempColour != null)
221 graphics.fillRect(av.charWidth * (curStart - start), y1,
222 curWidth, av.charHeight);
225 graphics.setColor(resBoxColour);
228 curWidth = av.charWidth;
229 tempColour = resBoxColour;
233 curWidth += av.charWidth;
239 graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,
262 public void drawText(SequenceI seq, int start, int end, int y1)
264 y1 += av.charHeight - av.charHeight / 5; // height/5 replaces pady
268 if (end + 1 >= seq.getLength())
270 end = seq.getLength() - 1;
272 graphics.setColor(av.textColour);
274 if (monospacedFont && av.showText && allGroups.length == 0
275 && !av.getColourText() && av.thresholdTextColour == 0)
279 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
283 char gap = av.getGapCharacter();
284 graphics.drawString(seq.getSequenceAsString(start, end + 1)
285 .replace(gap, ' '), 0, y1);
290 boolean getboxColour = false;
291 for (int i = start; i <= end; i++)
293 graphics.setColor(av.textColour);
294 getboxColour = false;
295 s = seq.getCharAt(i);
296 if (!renderGaps && jalview.util.Comparison.isGap(s))
301 if (inCurrentSequenceGroup(i))
303 if (!currentSequenceGroup.getDisplayText())
308 if (currentSequenceGroup.thresholdTextColour > 0
309 || currentSequenceGroup.getColourText())
312 getBoxColour(currentSequenceGroup.cs, seq, i);
314 if (currentSequenceGroup.getColourText())
316 graphics.setColor(resBoxColour.darker());
319 if (currentSequenceGroup.thresholdTextColour > 0)
321 if (resBoxColour.getRed() + resBoxColour.getBlue()
322 + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
324 graphics.setColor(currentSequenceGroup.textColour2);
330 graphics.setColor(currentSequenceGroup.textColour);
332 if (currentSequenceGroup.getShowNonconserved()) // todo optimize
334 // todo - use sequence group consensus
335 s = getDisplayChar(av.getAlignmentConsensusAnnotation(), i, s,
343 if (!av.getShowText())
348 if (av.getColourText())
351 getBoxColour(av.getGlobalColourScheme(), seq, i);
353 if (av.getShowBoxes())
355 graphics.setColor(resBoxColour.darker());
359 graphics.setColor(resBoxColour);
363 if (av.thresholdTextColour > 0)
367 getBoxColour(av.getGlobalColourScheme(), seq, i);
370 if (resBoxColour.getRed() + resBoxColour.getBlue()
371 + resBoxColour.getGreen() < av.thresholdTextColour)
373 graphics.setColor(av.textColour2);
376 if (av.getShowUnconserved())
378 s = getDisplayChar(av.getAlignmentConsensusAnnotation(), i, s,
385 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
386 graphics.drawString(String.valueOf(s), charOffset + av.charWidth
393 private char getDisplayChar(AlignmentAnnotation consensus, int position,
396 char conschar = consensus.annotations[position].displayCharacter
398 if (conschar != '-' && s == conschar)
411 * @return DOCUMENT ME!
413 boolean inCurrentSequenceGroup(int res)
415 if (allGroups == null)
420 for (int i = 0; i < allGroups.length; i++)
422 if ((allGroups[i].getStartRes() <= res)
423 && (allGroups[i].getEndRes() >= res))
425 currentSequenceGroup = allGroups[i];
452 public void drawHighlightedText(SequenceI seq, int start, int end,
455 int pady = av.charHeight / 5;
457 graphics.setColor(Color.BLACK);
458 graphics.fillRect(x1, y1, av.charWidth * (end - start + 1),
460 graphics.setColor(Color.white);
464 // Need to find the sequence position here.
465 if (av.validCharWidth)
467 for (int i = start; i <= end; i++)
469 if (i < seq.getLength())
471 s = seq.getCharAt(i);
474 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
475 graphics.drawString(String.valueOf(s), charOffset + x1
476 + (av.charWidth * (i - start)), (y1 + av.charHeight) - pady);
481 public void drawCursor(SequenceI seq, int res, int x1, int y1)
483 int pady = av.charHeight / 5;
485 graphics.setColor(Color.black);
486 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
488 if (av.validCharWidth)
490 graphics.setColor(Color.white);
492 char s = seq.getCharAt(res);
494 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
495 graphics.drawString(String.valueOf(s), charOffset + x1,
496 (y1 + av.charHeight) - pady);