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.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() && av
84 .getCharWidth() == 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.getCharWidth(), avWidth = av.getCharWidth(), avHeight = av
228 Color tempColour = null;
232 resBoxColour = Color.white;
236 if (inCurrentSequenceGroup(i))
238 if (currentSequenceGroup.getDisplayBoxes())
240 getBoxColour(currentSequenceGroup.cs, seq, i);
243 else if (av.getShowBoxes())
245 getBoxColour(av.getGlobalColourScheme(), seq, i);
250 if (resBoxColour != tempColour)
252 if (tempColour != null)
254 graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
258 graphics.setColor(resBoxColour);
262 tempColour = resBoxColour;
272 graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
294 public void drawText(SequenceI seq, int start, int end, int y1)
296 y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
300 if (end + 1 >= seq.getLength())
302 end = seq.getLength() - 1;
304 graphics.setColor(av.getTextColour());
306 if (monospacedFont && av.getShowText() && allGroups.length == 0
307 && !av.getColourText() && av.getThresholdTextColour() == 0)
309 if (av.isRenderGaps())
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.getTextColour());
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.getThresholdTextColour() > 0)
400 getBoxColour(av.getGlobalColourScheme(), seq, i);
403 if (resBoxColour.getRed() + resBoxColour.getBlue()
404 + resBoxColour.getGreen() < av.getThresholdTextColour())
406 graphics.setColor(av.getTextColour2());
409 if (av.getShowUnconserved())
411 s = getDisplayChar(srep, i, s,
418 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
419 graphics.drawString(String.valueOf(s),
420 charOffset + av.getCharWidth()
427 private char getDisplayChar(final boolean usesrep, int position,
430 // TODO - use currentSequenceGroup rather than alignemnt
431 // currentSequenceGroup.getConsensus()
432 char conschar = (usesrep) ? av.getAlignment().getSeqrep().getCharAt(position) : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
434 if (conschar != '-' && s == conschar)
447 * @return DOCUMENT ME!
449 boolean inCurrentSequenceGroup(int res)
451 if (allGroups == null)
456 for (int i = 0; i < allGroups.length; i++)
458 if ((allGroups[i].getStartRes() <= res)
459 && (allGroups[i].getEndRes() >= res))
461 currentSequenceGroup = allGroups[i];
488 public void drawHighlightedText(SequenceI seq, int start, int end,
491 int pady = av.getCharHeight() / 5;
493 graphics.setColor(Color.BLACK);
494 graphics.fillRect(x1, y1, av.getCharWidth() * (end - start + 1),
496 graphics.setColor(Color.white);
500 // Need to find the sequence position here.
501 if (av.isValidCharWidth())
503 for (int i = start; i <= end; i++)
505 if (i < seq.getLength())
507 s = seq.getCharAt(i);
510 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
511 graphics.drawString(String.valueOf(s),
512 charOffset + x1 + (av.getCharWidth() * (i - start)),
513 (y1 + av.getCharHeight()) - pady);
518 public void drawCursor(SequenceI seq, int res, int x1, int y1)
520 int pady = av.getCharHeight() / 5;
522 graphics.setColor(Color.black);
523 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
525 if (av.isValidCharWidth())
527 graphics.setColor(Color.white);
529 char s = seq.getCharAt(res);
531 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
532 graphics.drawString(String.valueOf(s), charOffset + x1,
533 (y1 + av.getCharHeight()) - pady);