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.AlignmentAnnotation;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.schemes.ColourSchemeI;
29 import java.awt.Color;
30 import java.awt.FontMetrics;
31 import java.awt.Graphics;
39 public class SequenceRenderer implements jalview.api.SequenceRenderer
45 boolean renderGaps = true;
47 SequenceGroup currentSequenceGroup = null;
49 SequenceGroup[] allGroups = null;
55 boolean monospacedFont;
57 boolean forOverview = false;
60 * Creates a new SequenceRenderer object.
65 public SequenceRenderer(AlignViewport av)
76 public void prepare(Graphics g, boolean renderGaps)
79 fm = g.getFontMetrics();
81 // If EPS graphics, stringWidth will be a double, not an int
82 double dwidth = fm.getStringBounds("M", g).getWidth();
84 monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth() && av
85 .getCharWidth() == dwidth);
87 this.renderGaps = renderGaps;
91 public Color getResidueBoxColour(SequenceI seq, int i)
93 allGroups = av.getAlignment().findAllGroups(seq);
95 if (inCurrentSequenceGroup(i))
97 if (currentSequenceGroup.getDisplayBoxes())
99 getBoxColour(currentSequenceGroup.cs, seq, i);
102 else if (av.getShowBoxes())
104 getBoxColour(av.getGlobalColourScheme(), seq, i);
111 * Get the residue colour at the given sequence position - as determined by
112 * the sequence group colour (if any), else the colour scheme, possibly
113 * overridden by a feature colour.
121 public Color getResidueColour(final SequenceI seq, int position,
124 // TODO replace 8 or so code duplications with calls to this method
125 // (refactored as needed)
126 Color col = getResidueBoxColour(seq, position);
130 col = fr.findFeatureColour(col, seq, position);
145 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
149 resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
152 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
154 resBoxColour = Color.lightGray;
158 resBoxColour = Color.white;
184 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
189 drawBoxes(seq, start, end, y1);
191 if (av.validCharWidth)
193 drawText(seq, start, end, y1);
215 public synchronized void drawBoxes(SequenceI seq, int start, int end,
220 return; // fix for racecondition
223 int length = seq.getLength();
226 int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(), avHeight = av
229 Color tempColour = null;
233 resBoxColour = Color.white;
237 if (inCurrentSequenceGroup(i))
239 if (currentSequenceGroup.getDisplayBoxes())
241 getBoxColour(currentSequenceGroup.cs, seq, i);
244 else if (av.getShowBoxes())
246 getBoxColour(av.getGlobalColourScheme(), seq, i);
251 if (resBoxColour != tempColour)
253 if (tempColour != null)
255 graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
259 graphics.setColor(resBoxColour);
263 tempColour = resBoxColour;
273 graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
295 public void drawText(SequenceI seq, int start, int end, int y1)
297 y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
301 if (end + 1 >= seq.getLength())
303 end = seq.getLength() - 1;
305 graphics.setColor(av.getTextColour());
307 if (monospacedFont && av.getShowText() && allGroups.length == 0
308 && !av.getColourText() && av.getThresholdTextColour() == 0)
310 if (av.isRenderGaps())
312 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
316 char gap = av.getGapCharacter();
317 graphics.drawString(seq.getSequenceAsString(start, end + 1)
318 .replace(gap, ' '), 0, y1);
323 boolean srep = av.isDisplayReferenceSeq();
324 boolean getboxColour = false;
325 for (int i = start; i <= end; i++)
327 graphics.setColor(av.getTextColour());
328 getboxColour = false;
329 s = seq.getCharAt(i);
330 if (!renderGaps && jalview.util.Comparison.isGap(s))
335 if (inCurrentSequenceGroup(i))
337 if (!currentSequenceGroup.getDisplayText())
342 if (currentSequenceGroup.thresholdTextColour > 0
343 || currentSequenceGroup.getColourText())
346 getBoxColour(currentSequenceGroup.cs, seq, i);
348 if (currentSequenceGroup.getColourText())
350 graphics.setColor(resBoxColour.darker());
353 if (currentSequenceGroup.thresholdTextColour > 0)
355 if (resBoxColour.getRed() + resBoxColour.getBlue()
356 + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
358 graphics.setColor(currentSequenceGroup.textColour2);
364 graphics.setColor(currentSequenceGroup.textColour);
366 if (currentSequenceGroup.getShowNonconserved()) // todo optimize
368 // todo - use sequence group consensus
369 s = getDisplayChar(srep, i, s,
377 if (!av.getShowText())
382 if (av.getColourText())
385 getBoxColour(av.getGlobalColourScheme(), seq, i);
387 if (av.getShowBoxes())
389 graphics.setColor(resBoxColour.darker());
393 graphics.setColor(resBoxColour);
397 if (av.getThresholdTextColour() > 0)
401 getBoxColour(av.getGlobalColourScheme(), seq, i);
404 if (resBoxColour.getRed() + resBoxColour.getBlue()
405 + resBoxColour.getGreen() < av.getThresholdTextColour())
407 graphics.setColor(av.getTextColour2());
410 if (av.getShowUnconserved())
412 s = getDisplayChar(srep, i, s,
419 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
420 graphics.drawString(String.valueOf(s),
421 charOffset + av.getCharWidth()
428 private char getDisplayChar(final boolean usesrep, int position,
431 // TODO - use currentSequenceGroup rather than alignemnt
432 // currentSequenceGroup.getConsensus()
433 char conschar = (usesrep) ? av.getAlignment().getSeqrep().getCharAt(position) : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
435 if (conschar != '-' && s == conschar)
448 * @return DOCUMENT ME!
450 boolean inCurrentSequenceGroup(int res)
452 if (allGroups == null)
457 for (int i = 0; i < allGroups.length; i++)
459 if ((allGroups[i].getStartRes() <= res)
460 && (allGroups[i].getEndRes() >= res))
462 currentSequenceGroup = allGroups[i];
489 public void drawHighlightedText(SequenceI seq, int start, int end,
492 int pady = av.getCharHeight() / 5;
494 graphics.setColor(Color.BLACK);
495 graphics.fillRect(x1, y1, av.getCharWidth() * (end - start + 1),
497 graphics.setColor(Color.white);
501 // Need to find the sequence position here.
502 if (av.validCharWidth)
504 for (int i = start; i <= end; i++)
506 if (i < seq.getLength())
508 s = seq.getCharAt(i);
511 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
512 graphics.drawString(String.valueOf(s),
513 charOffset + x1 + (av.getCharWidth() * (i - start)),
514 (y1 + av.getCharHeight()) - pady);
519 public void drawCursor(SequenceI seq, int res, int x1, int y1)
521 int pady = av.getCharHeight() / 5;
523 graphics.setColor(Color.black);
524 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
526 if (av.validCharWidth)
528 graphics.setColor(Color.white);
530 char s = seq.getCharAt(res);
532 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
533 graphics.drawString(String.valueOf(s), charOffset + x1,
534 (y1 + av.getCharHeight()) - pady);