2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.AlignViewportI;
24 import jalview.datamodel.SequenceGroup;
25 import jalview.datamodel.SequenceI;
26 import jalview.renderer.ResidueColourFinder;
27 import jalview.renderer.seqfeatures.FeatureColourFinder;
29 import java.awt.Color;
30 import java.awt.FontMetrics;
31 import java.awt.Graphics;
33 public class SequenceRenderer implements jalview.api.SequenceRenderer
35 final static int CHAR_TO_UPPER = 'A' - 'a';
41 boolean renderGaps = true;
43 SequenceGroup[] allGroups = null;
45 // Color resBoxColour;
49 boolean monospacedFont;
51 ResidueColourFinder resColourFinder;
54 * Creates a new SequenceRenderer object
58 public SequenceRenderer(AlignViewportI viewport)
61 resColourFinder = new ResidueColourFinder();
70 public void prepare(Graphics g, boolean renderGaps)
73 fm = g.getFontMetrics();
75 // If EPS graphics, stringWidth will be a double, not an int
76 double dwidth = fm.getStringBounds("M", g).getWidth();
78 monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth()
79 && av.getCharWidth() == dwidth);
81 this.renderGaps = renderGaps;
85 * Get the residue colour at the given sequence position - as determined by
86 * the sequence group colour (if any), else the colour scheme, possibly
87 * overridden by a feature colour.
95 public Color getResidueColour(final SequenceI seq, int position,
96 FeatureColourFinder finder)
98 allGroups = av.getAlignment().findAllGroups(seq);
99 return resColourFinder.getResidueColour(av.getShowBoxes(),
100 av.getResidueShading(),
101 allGroups, seq, position,
127 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
132 drawBoxes(seq, start, end, y1);
134 if (av.isValidCharWidth())
136 drawText(seq, start, end, y1);
158 public synchronized void drawBoxes(SequenceI seq, int start, int end,
161 Color resBoxColour = Color.white;
165 return; // fix for racecondition
168 int length = seq.getLength();
171 int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(),
172 avHeight = av.getCharHeight();
174 Color tempColour = null;
178 resBoxColour = Color.white;
182 SequenceGroup currentSequenceGroup = resColourFinder
183 .getCurrentSequenceGroup(
185 if (currentSequenceGroup != null)
187 if (currentSequenceGroup.getDisplayBoxes())
189 resBoxColour = resColourFinder.getBoxColour(
190 currentSequenceGroup.getGroupColourScheme(), seq,
194 else if (av.getShowBoxes())
196 resBoxColour = resColourFinder
197 .getBoxColour(av.getResidueShading(), seq, i);
201 if (resBoxColour != tempColour)
203 if (tempColour != null)
205 graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
209 graphics.setColor(resBoxColour);
213 tempColour = resBoxColour;
223 graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
245 public void drawText(SequenceI seq, int start, int end, int y1)
247 y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
251 if (end + 1 >= seq.getLength())
253 end = seq.getLength() - 1;
255 graphics.setColor(av.getTextColour());
257 if (monospacedFont && av.getShowText() && allGroups.length == 0
258 && !av.getColourText() && av.getThresholdTextColour() == 0)
260 if (av.isRenderGaps())
262 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
266 char gap = av.getGapCharacter();
268 seq.getSequenceAsString(start, end + 1).replace(gap, ' '),
274 boolean srep = av.isDisplayReferenceSeq();
275 boolean getboxColour = false;
276 boolean isarep = av.getAlignment().getSeqrep() == seq;
277 Color resBoxColour = Color.white;
279 for (int i = start; i <= end; i++)
282 graphics.setColor(av.getTextColour());
283 getboxColour = false;
284 s = seq.getCharAt(i);
286 if (!renderGaps && jalview.util.Comparison.isGap(s))
291 SequenceGroup currentSequenceGroup = resColourFinder
292 .getCurrentSequenceGroup(
294 if (currentSequenceGroup != null)
296 if (!currentSequenceGroup.getDisplayText())
301 if (currentSequenceGroup.thresholdTextColour > 0
302 || currentSequenceGroup.getColourText())
305 resBoxColour = resColourFinder.getBoxColour(
306 currentSequenceGroup.getGroupColourScheme(), seq,
309 if (currentSequenceGroup.getColourText())
311 graphics.setColor(resBoxColour.darker());
314 if (currentSequenceGroup.thresholdTextColour > 0)
316 if (resBoxColour.getRed() + resBoxColour.getBlue()
318 .getGreen() < currentSequenceGroup.thresholdTextColour)
320 graphics.setColor(currentSequenceGroup.textColour2);
326 graphics.setColor(currentSequenceGroup.textColour);
328 boolean isgrep = currentSequenceGroup != null
329 ? currentSequenceGroup.getSeqrep() == seq : false;
330 if (!isarep && !isgrep
331 && currentSequenceGroup.getShowNonconserved()) // todo
334 // todo - use sequence group consensus
335 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
342 if (!av.getShowText())
347 if (av.getColourText())
350 resBoxColour = resColourFinder
351 .getBoxColour(av.getResidueShading(), seq, i);
353 if (av.getShowBoxes())
355 graphics.setColor(resBoxColour.darker());
359 graphics.setColor(resBoxColour);
363 if (av.getThresholdTextColour() > 0)
367 resBoxColour = resColourFinder
368 .getBoxColour(av.getResidueShading(), seq, i);
371 if (resBoxColour.getRed() + resBoxColour.getBlue()
372 + resBoxColour.getGreen() < av.getThresholdTextColour())
374 graphics.setColor(av.getTextColour2());
377 if (!isarep && av.getShowUnconserved())
379 s = getDisplayChar(srep, i, s, '.', null);
385 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
386 graphics.drawString(String.valueOf(s),
387 charOffset + av.getCharWidth() * (i - start), y1);
394 * Returns 'conservedChar' to represent the given position if the sequence
395 * character at that position is equal to the consensus (ignoring case), else
396 * returns the sequence character
400 * @param sequenceChar
401 * @param conservedChar
404 private char getDisplayChar(final boolean usesrep, int position,
405 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
407 // TODO - use currentSequenceGroup rather than alignment
408 // currentSequenceGroup.getConsensus()
409 char conschar = (usesrep) ? (currentGroup == null
410 || position < currentGroup.getStartRes()
411 || position > currentGroup.getEndRes()
412 ? av.getAlignment().getSeqrep().getCharAt(position)
413 : (currentGroup.getSeqrep() != null
414 ? currentGroup.getSeqrep().getCharAt(position)
415 : av.getAlignment().getSeqrep()
416 .getCharAt(position)))
417 : (currentGroup != null && currentGroup.getConsensus() != null
418 && position >= currentGroup.getStartRes()
419 && position <= currentGroup.getEndRes()
421 .getConsensus().annotations.length > position)
423 .getConsensus().annotations[position].displayCharacter
425 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
427 if (!jalview.util.Comparison.isGap(conschar)
428 && (sequenceChar == conschar
429 || sequenceChar + CHAR_TO_UPPER == conschar))
431 sequenceChar = conservedChar;
454 public void drawHighlightedText(SequenceI seq, int start, int end, int x1,
457 int pady = av.getCharHeight() / 5;
459 graphics.setColor(Color.BLACK);
460 graphics.fillRect(x1, y1, av.getCharWidth() * (end - start + 1),
462 graphics.setColor(Color.white);
466 // Need to find the sequence position here.
467 if (av.isValidCharWidth())
469 for (int i = start; i <= end; i++)
471 if (i < seq.getLength())
473 s = seq.getCharAt(i);
476 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
477 graphics.drawString(String.valueOf(s),
478 charOffset + x1 + (av.getCharWidth() * (i - start)),
479 (y1 + av.getCharHeight()) - pady);
485 * Draw a sequence canvas cursor
488 * graphics context to draw on
490 * character to draw at cursor
492 * x position of cursor in graphics context
494 * y position of cursor in graphics context
496 public void drawCursor(Graphics g, char s, int x1, int y1)
498 int pady = av.getCharHeight() / 5;
500 g.setColor(Color.black);
501 g.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
503 if (av.isValidCharWidth())
505 g.setColor(Color.white);
506 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
507 g.drawString(String.valueOf(s), charOffset + x1,
508 (y1 + av.getCharHeight()) - pady);