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 srep = av.isDisplayReferenceSeq();
292 boolean getboxColour = false;
293 for (int i = start; i <= end; i++)
295 graphics.setColor(av.textColour);
296 getboxColour = false;
297 s = seq.getCharAt(i);
298 if (!renderGaps && jalview.util.Comparison.isGap(s))
303 if (inCurrentSequenceGroup(i))
305 if (!currentSequenceGroup.getDisplayText())
310 if (currentSequenceGroup.thresholdTextColour > 0
311 || currentSequenceGroup.getColourText())
314 getBoxColour(currentSequenceGroup.cs, seq, i);
316 if (currentSequenceGroup.getColourText())
318 graphics.setColor(resBoxColour.darker());
321 if (currentSequenceGroup.thresholdTextColour > 0)
323 if (resBoxColour.getRed() + resBoxColour.getBlue()
324 + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
326 graphics.setColor(currentSequenceGroup.textColour2);
332 graphics.setColor(currentSequenceGroup.textColour);
334 if (currentSequenceGroup.getShowNonconserved()) // todo optimize
336 // todo - use sequence group consensus
337 s = getDisplayChar(srep, i, s,
345 if (!av.getShowText())
350 if (av.getColourText())
353 getBoxColour(av.getGlobalColourScheme(), seq, i);
355 if (av.getShowBoxes())
357 graphics.setColor(resBoxColour.darker());
361 graphics.setColor(resBoxColour);
365 if (av.thresholdTextColour > 0)
369 getBoxColour(av.getGlobalColourScheme(), seq, i);
372 if (resBoxColour.getRed() + resBoxColour.getBlue()
373 + resBoxColour.getGreen() < av.thresholdTextColour)
375 graphics.setColor(av.textColour2);
378 if (av.getShowUnconserved())
380 s = getDisplayChar(srep, i, s,
387 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
388 graphics.drawString(String.valueOf(s), charOffset + av.charWidth
395 private char getDisplayChar(final boolean usesrep, int position,
398 // TODO - use currentSequenceGroup rather than alignemnt
399 // currentSequenceGroup.getConsensus()
400 char conschar = (usesrep) ? av.getAlignment().getSeqrep().getCharAt(position) : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
402 if (conschar != '-' && s == conschar)
415 * @return DOCUMENT ME!
417 boolean inCurrentSequenceGroup(int res)
419 if (allGroups == null)
424 for (int i = 0; i < allGroups.length; i++)
426 if ((allGroups[i].getStartRes() <= res)
427 && (allGroups[i].getEndRes() >= res))
429 currentSequenceGroup = allGroups[i];
456 public void drawHighlightedText(SequenceI seq, int start, int end,
459 int pady = av.charHeight / 5;
461 graphics.setColor(Color.BLACK);
462 graphics.fillRect(x1, y1, av.charWidth * (end - start + 1),
464 graphics.setColor(Color.white);
468 // Need to find the sequence position here.
469 if (av.validCharWidth)
471 for (int i = start; i <= end; i++)
473 if (i < seq.getLength())
475 s = seq.getCharAt(i);
478 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
479 graphics.drawString(String.valueOf(s), charOffset + x1
480 + (av.charWidth * (i - start)), (y1 + av.charHeight) - pady);
485 public void drawCursor(SequenceI seq, int res, int x1, int y1)
487 int pady = av.charHeight / 5;
489 graphics.setColor(Color.black);
490 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
492 if (av.validCharWidth)
494 graphics.setColor(Color.white);
496 char s = seq.getCharAt(res);
498 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
499 graphics.drawString(String.valueOf(s), charOffset + x1,
500 (y1 + av.charHeight) - pady);