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 srep = av.isDisplayReferenceSeq();
291 boolean getboxColour = false;
292 for (int i = start; i <= end; i++)
294 graphics.setColor(av.textColour);
295 getboxColour = false;
296 s = seq.getCharAt(i);
297 if (!renderGaps && jalview.util.Comparison.isGap(s))
302 if (inCurrentSequenceGroup(i))
304 if (!currentSequenceGroup.getDisplayText())
309 if (currentSequenceGroup.thresholdTextColour > 0
310 || currentSequenceGroup.getColourText())
313 getBoxColour(currentSequenceGroup.cs, seq, i);
315 if (currentSequenceGroup.getColourText())
317 graphics.setColor(resBoxColour.darker());
320 if (currentSequenceGroup.thresholdTextColour > 0)
322 if (resBoxColour.getRed() + resBoxColour.getBlue()
323 + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
325 graphics.setColor(currentSequenceGroup.textColour2);
331 graphics.setColor(currentSequenceGroup.textColour);
333 if (currentSequenceGroup.getShowNonconserved()) // todo optimize
335 // todo - use sequence group consensus
336 s = getDisplayChar(srep, i, s,
344 if (!av.getShowText())
349 if (av.getColourText())
352 getBoxColour(av.getGlobalColourScheme(), seq, i);
354 if (av.getShowBoxes())
356 graphics.setColor(resBoxColour.darker());
360 graphics.setColor(resBoxColour);
364 if (av.thresholdTextColour > 0)
368 getBoxColour(av.getGlobalColourScheme(), seq, i);
371 if (resBoxColour.getRed() + resBoxColour.getBlue()
372 + resBoxColour.getGreen() < av.thresholdTextColour)
374 graphics.setColor(av.textColour2);
377 if (av.getShowUnconserved())
379 s = getDisplayChar(srep, i, s,
386 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
387 graphics.drawString(String.valueOf(s), charOffset + av.charWidth
394 private char getDisplayChar(final boolean usesrep, int position,
397 // TODO - use currentSequenceGroup rather than alignemnt
398 // currentSequenceGroup.getConsensus()
399 char conschar = (usesrep) ? av.getAlignment().getSeqrep().getCharAt(position) : av.getAlignmentConsensusAnnotation().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);