2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 import jalview.datamodel.AlignmentAnnotation;
22 import jalview.datamodel.SequenceGroup;
23 import jalview.datamodel.SequenceI;
24 import jalview.schemes.ColourSchemeI;
26 import java.awt.Color;
27 import java.awt.FontMetrics;
28 import java.awt.Graphics;
36 public class SequenceRenderer implements jalview.api.SequenceRenderer
42 boolean renderGaps = true;
44 SequenceGroup currentSequenceGroup = null;
46 SequenceGroup[] allGroups = null;
52 boolean monospacedFont;
54 boolean forOverview = false;
57 * Creates a new SequenceRenderer object.
62 public SequenceRenderer(AlignViewport av)
73 public void prepare(Graphics g, boolean renderGaps)
76 fm = g.getFontMetrics();
78 // If EPS graphics, stringWidth will be a double, not an int
79 double dwidth = fm.getStringBounds("M", g).getWidth();
81 monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth() && (float) av.charWidth == dwidth);
83 this.renderGaps = renderGaps;
86 public Color getResidueBoxColour(SequenceI seq, int i)
88 allGroups = av.getAlignment().findAllGroups(seq);
90 if (inCurrentSequenceGroup(i))
92 if (currentSequenceGroup.getDisplayBoxes())
94 getBoxColour(currentSequenceGroup.cs, seq, i);
97 else if (av.getShowBoxes())
99 getBoxColour(av.getGlobalColourScheme(), seq, i);
115 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
119 resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
122 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
124 resBoxColour = Color.lightGray;
128 resBoxColour = Color.white;
154 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
159 drawBoxes(seq, start, end, y1);
161 if (av.validCharWidth)
163 drawText(seq, start, end, y1);
185 public synchronized void drawBoxes(SequenceI seq, int start, int end,
189 return; // fix for racecondition
191 int length = seq.getLength();
194 int curWidth = av.charWidth;
196 Color tempColour = null;
200 resBoxColour = Color.white;
204 if (inCurrentSequenceGroup(i))
206 if (currentSequenceGroup.getDisplayBoxes())
208 getBoxColour(currentSequenceGroup.cs, seq, i);
211 else if (av.getShowBoxes())
213 getBoxColour(av.getGlobalColourScheme(), seq, i);
218 if (resBoxColour != tempColour)
220 if (tempColour != null)
222 graphics.fillRect(av.charWidth * (curStart - start), y1,
223 curWidth, av.charHeight);
226 graphics.setColor(resBoxColour);
229 curWidth = av.charWidth;
230 tempColour = resBoxColour;
234 curWidth += av.charWidth;
240 graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,
263 public void drawText(SequenceI seq, int start, int end, int y1)
265 y1 += av.charHeight - av.charHeight / 5; // height/5 replaces pady
269 if (end + 1 >= seq.getLength())
271 end = seq.getLength() - 1;
273 graphics.setColor(av.textColour);
275 if (monospacedFont && av.showText && allGroups.length == 0
276 && !av.getColourText() && av.thresholdTextColour == 0)
280 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
284 char gap = av.getGapCharacter();
285 graphics.drawString(seq.getSequenceAsString(start, end + 1)
286 .replace(gap, ' '), 0, y1);
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(av.getAlignmentConsensusAnnotation(), 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(av.getAlignmentConsensusAnnotation(), i, s,
386 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
387 graphics.drawString(String.valueOf(s), charOffset + av.charWidth
394 private char getDisplayChar(AlignmentAnnotation consensus, int position,
397 char conschar = consensus.annotations[position].displayCharacter
399 if (conschar != '-' && s == conschar)
412 * @return DOCUMENT ME!
414 boolean inCurrentSequenceGroup(int res)
416 if (allGroups == null)
421 for (int i = 0; i < allGroups.length; i++)
423 if ((allGroups[i].getStartRes() <= res)
424 && (allGroups[i].getEndRes() >= res))
426 currentSequenceGroup = allGroups[i];
453 public void drawHighlightedText(SequenceI seq, int start, int end,
456 int pady = av.charHeight / 5;
458 graphics.setColor(Color.BLACK);
459 graphics.fillRect(x1, y1, av.charWidth * (end - start + 1),
461 graphics.setColor(Color.white);
465 // Need to find the sequence position here.
466 if (av.validCharWidth)
468 for (int i = start; i <= end; i++)
470 if (i < seq.getLength())
472 s = seq.getCharAt(i);
475 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
476 graphics.drawString(String.valueOf(s), charOffset + x1
477 + (av.charWidth * (i - start)), (y1 + av.charHeight) - pady);
482 public void drawCursor(SequenceI seq, int res, int x1, int y1)
484 int pady = av.charHeight / 5;
486 graphics.setColor(Color.black);
487 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
489 if (av.validCharWidth)
491 graphics.setColor(Color.white);
493 char s = seq.getCharAt(res);
495 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
496 graphics.drawString(String.valueOf(s), charOffset + x1,
497 (y1 + av.charHeight) - pady);