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 return resColourFinder.getResidueColour(av.getShowBoxes(),
99 av.getResidueShading(),
100 allGroups, seq, position,
126 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
131 drawBoxes(seq, start, end, y1);
133 if (av.isValidCharWidth())
135 drawText(seq, start, end, y1);
157 public synchronized void drawBoxes(SequenceI seq, int start, int end,
160 Color resBoxColour = Color.white;
164 return; // fix for racecondition
167 int length = seq.getLength();
170 int curWidth = av.getCharWidth(), avWidth = av.getCharWidth(),
171 avHeight = av.getCharHeight();
173 Color tempColour = null;
177 resBoxColour = Color.white;
181 SequenceGroup currentSequenceGroup = resColourFinder
182 .getCurrentSequenceGroup(
184 if (currentSequenceGroup != null)
186 if (currentSequenceGroup.getDisplayBoxes())
188 resBoxColour = resColourFinder.getBoxColour(
189 currentSequenceGroup.getGroupColourScheme(), seq,
193 else if (av.getShowBoxes())
195 resBoxColour = resColourFinder
196 .getBoxColour(av.getResidueShading(), seq, i);
200 if (resBoxColour != tempColour)
202 if (tempColour != null)
204 graphics.fillRect(avWidth * (curStart - start), y1, curWidth,
208 graphics.setColor(resBoxColour);
212 tempColour = resBoxColour;
222 graphics.fillRect(avWidth * (curStart - start), y1, curWidth, avHeight);
244 public void drawText(SequenceI seq, int start, int end, int y1)
246 y1 += av.getCharHeight() - av.getCharHeight() / 5; // height/5 replaces pady
250 if (end + 1 >= seq.getLength())
252 end = seq.getLength() - 1;
254 graphics.setColor(av.getTextColour());
256 if (monospacedFont && av.getShowText() && allGroups.length == 0
257 && !av.getColourText() && av.getThresholdTextColour() == 0)
259 if (av.isRenderGaps())
261 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
265 char gap = av.getGapCharacter();
267 seq.getSequenceAsString(start, end + 1).replace(gap, ' '),
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()
317 .getGreen() < currentSequenceGroup.thresholdTextColour)
319 graphics.setColor(currentSequenceGroup.textColour2);
325 graphics.setColor(currentSequenceGroup.textColour);
327 boolean isgrep = currentSequenceGroup != null
328 ? currentSequenceGroup.getSeqrep() == seq : false;
329 if (!isarep && !isgrep
330 && currentSequenceGroup.getShowNonconserved()) // todo
333 // todo - use sequence group consensus
334 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
341 if (!av.getShowText())
346 if (av.getColourText())
349 resBoxColour = resColourFinder
350 .getBoxColour(av.getResidueShading(), seq, i);
352 if (av.getShowBoxes())
354 graphics.setColor(resBoxColour.darker());
358 graphics.setColor(resBoxColour);
362 if (av.getThresholdTextColour() > 0)
366 resBoxColour = resColourFinder
367 .getBoxColour(av.getResidueShading(), seq, i);
370 if (resBoxColour.getRed() + resBoxColour.getBlue()
371 + resBoxColour.getGreen() < av.getThresholdTextColour())
373 graphics.setColor(av.getTextColour2());
376 if (!isarep && av.getShowUnconserved())
378 s = getDisplayChar(srep, i, s, '.', null);
384 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
385 graphics.drawString(String.valueOf(s),
386 charOffset + av.getCharWidth() * (i - start), y1);
393 * Returns 'conservedChar' to represent the given position if the sequence
394 * character at that position is equal to the consensus (ignoring case), else
395 * returns the sequence character
399 * @param sequenceChar
400 * @param conservedChar
403 private char getDisplayChar(final boolean usesrep, int position,
404 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
406 // TODO - use currentSequenceGroup rather than alignment
407 // currentSequenceGroup.getConsensus()
408 char conschar = (usesrep) ? (currentGroup == null
409 || position < currentGroup.getStartRes()
410 || position > currentGroup.getEndRes()
411 ? av.getAlignment().getSeqrep().getCharAt(position)
412 : (currentGroup.getSeqrep() != null
413 ? currentGroup.getSeqrep().getCharAt(position)
414 : av.getAlignment().getSeqrep()
415 .getCharAt(position)))
416 : (currentGroup != null && currentGroup.getConsensus() != null
417 && position >= currentGroup.getStartRes()
418 && position <= currentGroup.getEndRes()
420 .getConsensus().annotations.length > position)
422 .getConsensus().annotations[position].displayCharacter
424 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
426 if (!jalview.util.Comparison.isGap(conschar)
427 && (sequenceChar == conschar
428 || sequenceChar + CHAR_TO_UPPER == conschar))
430 sequenceChar = conservedChar;
453 public void drawHighlightedText(SequenceI seq, int start, int end, int x1,
456 int pady = av.getCharHeight() / 5;
458 graphics.setColor(Color.BLACK);
459 graphics.fillRect(x1, y1, av.getCharWidth() * (end - start + 1),
461 graphics.setColor(Color.white);
465 // Need to find the sequence position here.
466 if (av.isValidCharWidth())
468 for (int i = start; i <= end; i++)
470 if (i < seq.getLength())
472 s = seq.getCharAt(i);
475 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
476 graphics.drawString(String.valueOf(s),
477 charOffset + x1 + (av.getCharWidth() * (i - start)),
478 (y1 + av.getCharHeight()) - pady);
483 public void drawCursor(SequenceI seq, int res, int x1, int y1)
485 int pady = av.getCharHeight() / 5;
487 graphics.setColor(Color.black);
488 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
490 if (av.isValidCharWidth())
492 graphics.setColor(Color.white);
494 char s = seq.getCharAt(res);
496 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
497 graphics.drawString(String.valueOf(s), charOffset + x1,
498 (y1 + av.getCharHeight()) - pady);