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() && av
79 .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 return resColourFinder.getResidueColour(av.getResidueShading(),
99 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(), avHeight = av
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();
267 graphics.drawString(seq.getSequenceAsString(start, end + 1)
268 .replace(gap, ' '), 0, y1);
273 boolean srep = av.isDisplayReferenceSeq();
274 boolean getboxColour = false;
275 boolean isarep = av.getAlignment().getSeqrep() == seq;
276 Color resBoxColour = Color.white;
278 for (int i = start; i <= end; i++)
281 graphics.setColor(av.getTextColour());
282 getboxColour = false;
283 s = seq.getCharAt(i);
285 if (!renderGaps && jalview.util.Comparison.isGap(s))
290 SequenceGroup currentSequenceGroup = resColourFinder
291 .getCurrentSequenceGroup(
293 if (currentSequenceGroup != null)
295 if (!currentSequenceGroup.getDisplayText())
300 if (currentSequenceGroup.thresholdTextColour > 0
301 || currentSequenceGroup.getColourText())
304 resBoxColour = resColourFinder.getBoxColour(
305 currentSequenceGroup.getGroupColourScheme(), seq,
308 if (currentSequenceGroup.getColourText())
310 graphics.setColor(resBoxColour.darker());
313 if (currentSequenceGroup.thresholdTextColour > 0)
315 if (resBoxColour.getRed() + resBoxColour.getBlue()
316 + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
318 graphics.setColor(currentSequenceGroup.textColour2);
324 graphics.setColor(currentSequenceGroup.textColour);
326 boolean isgrep = currentSequenceGroup != null
327 ? currentSequenceGroup.getSeqrep() == seq : false;
328 if (!isarep && !isgrep
329 && currentSequenceGroup.getShowNonconserved()) // todo
332 // todo - use sequence group consensus
333 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
340 if (!av.getShowText())
345 if (av.getColourText())
348 resBoxColour = resColourFinder
349 .getBoxColour(av.getResidueShading(), seq, i);
351 if (av.getShowBoxes())
353 graphics.setColor(resBoxColour.darker());
357 graphics.setColor(resBoxColour);
361 if (av.getThresholdTextColour() > 0)
365 resBoxColour = resColourFinder
366 .getBoxColour(av.getResidueShading(), seq, i);
369 if (resBoxColour.getRed() + resBoxColour.getBlue()
370 + resBoxColour.getGreen() < av.getThresholdTextColour())
372 graphics.setColor(av.getTextColour2());
375 if (!isarep && av.getShowUnconserved())
377 s = getDisplayChar(srep, i, s, '.', null);
383 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
384 graphics.drawString(String.valueOf(s),
385 charOffset + av.getCharWidth() * (i - start), y1);
392 * Returns 'conservedChar' to represent the given position if the sequence
393 * character at that position is equal to the consensus (ignoring case), else
394 * returns the sequence character
398 * @param sequenceChar
399 * @param conservedChar
402 private char getDisplayChar(final boolean usesrep, int position,
403 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
405 // TODO - use currentSequenceGroup rather than alignment
406 // currentSequenceGroup.getConsensus()
407 char conschar = (usesrep) ? (currentGroup == null
408 || position < currentGroup.getStartRes()
409 || position > currentGroup.getEndRes() ? av.getAlignment()
410 .getSeqrep().getCharAt(position)
411 : (currentGroup.getSeqrep() != null ? currentGroup.getSeqrep()
412 .getCharAt(position) : av.getAlignment().getSeqrep()
413 .getCharAt(position)))
414 : (currentGroup != null && currentGroup.getConsensus() != null
415 && position >= currentGroup.getStartRes()
416 && position <= currentGroup.getEndRes() && currentGroup
417 .getConsensus().annotations.length > position) ? currentGroup
418 .getConsensus().annotations[position].displayCharacter
420 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
422 if (!jalview.util.Comparison.isGap(conschar)
423 && (sequenceChar == conschar || sequenceChar + CHAR_TO_UPPER == conschar))
425 sequenceChar = conservedChar;
448 public void drawHighlightedText(SequenceI seq, int start, int end,
451 int pady = av.getCharHeight() / 5;
453 graphics.setColor(Color.BLACK);
454 graphics.fillRect(x1, y1, av.getCharWidth() * (end - start + 1),
456 graphics.setColor(Color.white);
460 // Need to find the sequence position here.
461 if (av.isValidCharWidth())
463 for (int i = start; i <= end; i++)
465 if (i < seq.getLength())
467 s = seq.getCharAt(i);
470 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
471 graphics.drawString(String.valueOf(s),
472 charOffset + x1 + (av.getCharWidth() * (i - start)),
473 (y1 + av.getCharHeight()) - pady);
478 public void drawCursor(SequenceI seq, int res, int x1, int y1)
480 int pady = av.getCharHeight() / 5;
482 graphics.setColor(Color.black);
483 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
485 if (av.isValidCharWidth())
487 graphics.setColor(Color.white);
489 char s = seq.getCharAt(res);
491 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
492 graphics.drawString(String.valueOf(s), charOffset + x1,
493 (y1 + av.getCharHeight()) - pady);